|
| 1 | +/** |
| 2 | + * Tests for installFileLogger — tees process.stdout/stderr writes into a |
| 3 | + * daily log file under a configurable directory. |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, it, expect, afterEach } from "vitest"; |
| 7 | +import { mkdtempSync, readFileSync, rmSync, statSync } from "node:fs"; |
| 8 | +import { tmpdir } from "node:os"; |
| 9 | +import { join } from "node:path"; |
| 10 | + |
| 11 | +import { installFileLogger } from "@src/utils/log-file.js"; |
| 12 | + |
| 13 | +describe("installFileLogger", () => { |
| 14 | + const cleanups: Array<() => void> = []; |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + while (cleanups.length > 0) { |
| 18 | + const fn = cleanups.pop(); |
| 19 | + try { |
| 20 | + fn?.(); |
| 21 | + } catch { |
| 22 | + // swallow — cleanup is best-effort |
| 23 | + } |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it("tees process.stdout writes into the target file", () => { |
| 28 | + const dir = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 29 | + cleanups.push(() => rmSync(dir, { recursive: true, force: true })); |
| 30 | + |
| 31 | + const handle = installFileLogger({ dir, filename: "test.log" }); |
| 32 | + cleanups.push(() => handle.uninstall()); |
| 33 | + |
| 34 | + process.stdout.write("hello stdout\n"); |
| 35 | + |
| 36 | + expect(handle.path).toBe(join(dir, "test.log")); |
| 37 | + expect(readFileSync(handle.path, "utf8")).toContain("hello stdout"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("tees process.stderr writes into the target file", () => { |
| 41 | + const dir = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 42 | + cleanups.push(() => rmSync(dir, { recursive: true, force: true })); |
| 43 | + |
| 44 | + const handle = installFileLogger({ dir, filename: "test.log" }); |
| 45 | + cleanups.push(() => handle.uninstall()); |
| 46 | + |
| 47 | + process.stderr.write("boom stderr\n"); |
| 48 | + |
| 49 | + expect(readFileSync(handle.path, "utf8")).toContain("boom stderr"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("creates nested target directory if it does not exist", () => { |
| 53 | + const base = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 54 | + cleanups.push(() => rmSync(base, { recursive: true, force: true })); |
| 55 | + const dir = join(base, "nested", "logs"); |
| 56 | + |
| 57 | + const handle = installFileLogger({ dir, filename: "test.log" }); |
| 58 | + cleanups.push(() => handle.uninstall()); |
| 59 | + |
| 60 | + process.stdout.write("nested\n"); |
| 61 | + |
| 62 | + expect(statSync(handle.path).isFile()).toBe(true); |
| 63 | + expect(readFileSync(handle.path, "utf8")).toContain("nested"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("uninstall restores original write functions and stops teeing", () => { |
| 67 | + const dir = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 68 | + cleanups.push(() => rmSync(dir, { recursive: true, force: true })); |
| 69 | + |
| 70 | + const originalStdoutWrite = process.stdout.write; |
| 71 | + const handle = installFileLogger({ dir, filename: "test.log" }); |
| 72 | + expect(process.stdout.write).not.toBe(originalStdoutWrite); |
| 73 | + |
| 74 | + process.stdout.write("before\n"); |
| 75 | + handle.uninstall(); |
| 76 | + expect(process.stdout.write).toBe(originalStdoutWrite); |
| 77 | + |
| 78 | + process.stdout.write("after\n"); |
| 79 | + |
| 80 | + const contents = readFileSync(handle.path, "utf8"); |
| 81 | + expect(contents).toContain("before"); |
| 82 | + expect(contents).not.toContain("after"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("defaults filename to dev-YYYY-MM-DD.log", () => { |
| 86 | + const dir = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 87 | + cleanups.push(() => rmSync(dir, { recursive: true, force: true })); |
| 88 | + |
| 89 | + const handle = installFileLogger({ dir }); |
| 90 | + cleanups.push(() => handle.uninstall()); |
| 91 | + |
| 92 | + expect(handle.path).toMatch(/dev-\d{4}-\d{2}-\d{2}\.log$/); |
| 93 | + }); |
| 94 | + |
| 95 | + it("preserves the boolean return value from the underlying write", () => { |
| 96 | + const dir = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 97 | + cleanups.push(() => rmSync(dir, { recursive: true, force: true })); |
| 98 | + |
| 99 | + const handle = installFileLogger({ dir, filename: "test.log" }); |
| 100 | + cleanups.push(() => handle.uninstall()); |
| 101 | + |
| 102 | + const result = process.stdout.write("payload\n"); |
| 103 | + expect(typeof result).toBe("boolean"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("appends to an existing file instead of truncating", () => { |
| 107 | + const dir = mkdtempSync(join(tmpdir(), "codex-proxy-log-")); |
| 108 | + cleanups.push(() => rmSync(dir, { recursive: true, force: true })); |
| 109 | + |
| 110 | + const first = installFileLogger({ dir, filename: "test.log" }); |
| 111 | + process.stdout.write("first run\n"); |
| 112 | + first.uninstall(); |
| 113 | + |
| 114 | + const second = installFileLogger({ dir, filename: "test.log" }); |
| 115 | + cleanups.push(() => second.uninstall()); |
| 116 | + process.stdout.write("second run\n"); |
| 117 | + |
| 118 | + const contents = readFileSync(second.path, "utf8"); |
| 119 | + expect(contents).toContain("first run"); |
| 120 | + expect(contents).toContain("second run"); |
| 121 | + }); |
| 122 | +}); |
0 commit comments