|
| 1 | +import { existsSync, readdirSync, readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { appendToDebugLogFile, debugLogFilepath } from "../../utils/log-file"; |
| 5 | +import { runInTempDir } from "../helpers/run-in-tmp"; |
| 6 | + |
| 7 | +describe("appendToDebugLogFile", () => { |
| 8 | + runInTempDir(); |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + vi.stubEnv("WRANGLER_LOG_PATH", "logs"); |
| 12 | + }); |
| 13 | + |
| 14 | + function getLogFileContent(): string { |
| 15 | + if (existsSync(debugLogFilepath)) { |
| 16 | + return readFileSync(debugLogFilepath, "utf8"); |
| 17 | + } |
| 18 | + |
| 19 | + if (existsSync("logs")) { |
| 20 | + const logFiles = readdirSync("logs"); |
| 21 | + expect(logFiles.length).toBeGreaterThan(0); |
| 22 | + |
| 23 | + const logFilePath = join("logs", logFiles[0]); |
| 24 | + return readFileSync(logFilePath, "utf8"); |
| 25 | + } |
| 26 | + |
| 27 | + throw new Error( |
| 28 | + `No log files found. debugLogFilepath: ${debugLogFilepath}, logs dir exists: ${existsSync("logs")}` |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + it("should strip ANSI escape codes from error messages", async () => { |
| 33 | + const messageWithAnsi = "\u001b[31mError: Something went wrong\u001b[0m"; |
| 34 | + const expectedCleanMessage = "Error: Something went wrong"; |
| 35 | + |
| 36 | + await appendToDebugLogFile("error", messageWithAnsi); |
| 37 | + |
| 38 | + const logContent = getLogFileContent(); |
| 39 | + expect(logContent).toContain(expectedCleanMessage); |
| 40 | + expect(logContent).not.toContain("\u001b[31m"); |
| 41 | + expect(logContent).not.toContain("\u001b[0m"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should strip complex ANSI escape sequences", async () => { |
| 45 | + const messageWithComplexAnsi = |
| 46 | + "\u001b[1;32mSuccess:\u001b[0m \u001b[33mWarning text\u001b[0m"; |
| 47 | + const expectedCleanMessage = "Success: Warning text"; |
| 48 | + |
| 49 | + await appendToDebugLogFile("log", messageWithComplexAnsi); |
| 50 | + |
| 51 | + const logContent = getLogFileContent(); |
| 52 | + expect(logContent).toContain(expectedCleanMessage); |
| 53 | + expect(logContent).not.toContain("\u001b[1;32m"); |
| 54 | + expect(logContent).not.toContain("\u001b[33m"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should preserve plain messages without ANSI codes", async () => { |
| 58 | + const plainMessage = "This is a plain log message"; |
| 59 | + |
| 60 | + await appendToDebugLogFile("info", plainMessage); |
| 61 | + |
| 62 | + const logContent = getLogFileContent(); |
| 63 | + expect(logContent).toContain(plainMessage); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should handle multiline messages with ANSI codes", async () => { |
| 67 | + const multilineMessageWithAnsi = |
| 68 | + "\u001b[31mLine 1 with color\u001b[0m\nLine 2 plain\n\u001b[32mLine 3 with different color\u001b[0m"; |
| 69 | + const expectedCleanMessage = |
| 70 | + "Line 1 with color\nLine 2 plain\nLine 3 with different color"; |
| 71 | + |
| 72 | + await appendToDebugLogFile("warn", multilineMessageWithAnsi); |
| 73 | + |
| 74 | + const logContent = getLogFileContent(); |
| 75 | + expect(logContent).toContain(expectedCleanMessage); |
| 76 | + expect(logContent).not.toContain("\u001b[31m"); |
| 77 | + expect(logContent).not.toContain("\u001b[32m"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should maintain timestamp and log level formatting", async () => { |
| 81 | + const message = "\u001b[31mTest message\u001b[0m"; |
| 82 | + |
| 83 | + await appendToDebugLogFile("error", message); |
| 84 | + |
| 85 | + const logContent = getLogFileContent(); |
| 86 | + expect(logContent).toMatch( |
| 87 | + /--- \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z error/ |
| 88 | + ); |
| 89 | + expect(logContent).toContain("Test message"); |
| 90 | + expect(logContent).toContain("---"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should handle empty messages", async () => { |
| 94 | + await appendToDebugLogFile("debug", ""); |
| 95 | + |
| 96 | + const logContent = getLogFileContent(); |
| 97 | + expect(logContent).toMatch( |
| 98 | + /--- \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z debug/ |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle messages with only ANSI codes", async () => { |
| 103 | + const onlyAnsiMessage = "\u001b[31m\u001b[0m"; |
| 104 | + |
| 105 | + await appendToDebugLogFile("log", onlyAnsiMessage); |
| 106 | + |
| 107 | + const logContent = getLogFileContent(); |
| 108 | + expect(logContent).not.toContain("\u001b[31m"); |
| 109 | + expect(logContent).not.toContain("\u001b[0m"); |
| 110 | + expect(logContent).toMatch( |
| 111 | + /--- \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z log/ |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments