|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 5 | +import { createLogger } from "../../src/backend/util/file-logger.js"; |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + vi.restoreAllMocks(); |
| 9 | +}); |
| 10 | + |
| 11 | +describe("file logger", () => { |
| 12 | + test("is quiet by default except for errors", () => { |
| 13 | + const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 14 | + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 15 | + const logger = createLogger(undefined); |
| 16 | + |
| 17 | + logger.log("debug line"); |
| 18 | + logger.error("boom"); |
| 19 | + |
| 20 | + expect(logSpy).not.toHaveBeenCalled(); |
| 21 | + expect(errorSpy).toHaveBeenCalledWith("boom"); |
| 22 | + }); |
| 23 | + |
| 24 | + test("writes log and error entries to file when debug path is provided", () => { |
| 25 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "tmux-mobile-logger-")); |
| 26 | + const logPath = path.join(tmpDir, "debug.log"); |
| 27 | + const logger = createLogger(logPath); |
| 28 | + |
| 29 | + logger.log("hello", { a: 1 }); |
| 30 | + logger.error(new Error("oops")); |
| 31 | + |
| 32 | + const content = fs.readFileSync(logPath, "utf8"); |
| 33 | + expect(content).toContain("[INFO] hello {\"a\":1}"); |
| 34 | + expect(content).toContain("[ERROR] Error: oops"); |
| 35 | + |
| 36 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 37 | + }); |
| 38 | +}); |
0 commit comments