|
| 1 | +// deno-lint-ignore-file no-import-prefix |
| 2 | + |
| 3 | +import { assertEquals, assertRejects } from "jsr:@std/assert@^1.0.0"; |
| 4 | +import { getOcvLogger, initLogger } from "./logger.ts"; |
| 5 | + |
| 6 | +Deno.test("getOcvLogger throws before initLogger is called", () => { |
| 7 | + assertEquals( |
| 8 | + typeof getOcvLogger, |
| 9 | + "function", |
| 10 | + "getOcvLogger should be an exported function", |
| 11 | + ); |
| 12 | + |
| 13 | + // getOcvLogger() uses rootLogger which is null before init |
| 14 | + // It throws an Error with a specific message |
| 15 | + try { |
| 16 | + getOcvLogger(); |
| 17 | + assertEquals(true, false, "Expected getOcvLogger() to throw"); |
| 18 | + } catch (e) { |
| 19 | + assertEquals( |
| 20 | + e instanceof Error, |
| 21 | + true, |
| 22 | + "Expected an Error to be thrown", |
| 23 | + ); |
| 24 | + assertEquals( |
| 25 | + (e as Error).message, |
| 26 | + "Logger not initialized. Call initLogger() first.", |
| 27 | + ); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +Deno.test({ |
| 32 | + name: "suppresses LogTape meta logger info message", |
| 33 | + async fn() { |
| 34 | + const tempDir = await Deno.makeTempDir({ prefix: "ocv-test-" }); |
| 35 | + |
| 36 | + // Capture stdout by replacing Deno.stdout's writable stream. |
| 37 | + // LogTape's getConsoleSink() writes to Deno.stdout.writable. |
| 38 | + const captured: Uint8Array[] = []; |
| 39 | + const originalStdout = Deno.stdout; |
| 40 | + |
| 41 | + const capturingWritable = new WritableStream<Uint8Array>({ |
| 42 | + write(chunk) { |
| 43 | + captured.push(chunk); |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + Object.defineProperty(Deno, "stdout", { |
| 48 | + value: { writable: capturingWritable }, |
| 49 | + configurable: true, |
| 50 | + writable: true, |
| 51 | + }); |
| 52 | + |
| 53 | + try { |
| 54 | + await initLogger(tempDir); |
| 55 | + |
| 56 | + // Decode all captured chunks into a single string |
| 57 | + const output = captured.map((c) => new TextDecoder().decode(c)).join(""); |
| 58 | + const lower = output.toLowerCase(); |
| 59 | + |
| 60 | + assertEquals( |
| 61 | + lower.includes("meta"), |
| 62 | + false, |
| 63 | + "Expected no meta logger info message in stdout — category ['logtape', 'meta'] set to lowestLevel 'warning' with no sinks", |
| 64 | + ); |
| 65 | + } finally { |
| 66 | + // Restore original stdout |
| 67 | + Object.defineProperty(Deno, "stdout", { |
| 68 | + value: originalStdout, |
| 69 | + configurable: true, |
| 70 | + writable: true, |
| 71 | + }); |
| 72 | + |
| 73 | + // Clean up temp directory |
| 74 | + try { |
| 75 | + await Deno.remove(tempDir, { recursive: true }); |
| 76 | + } catch { |
| 77 | + // Ignore cleanup errors — leaked temp dirs are acceptable in test failure |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | +}); |
| 82 | + |
| 83 | +Deno.test("getOcvLogger returns a Logger after initLogger", async () => { |
| 84 | + const tempDir = await Deno.makeTempDir({ prefix: "ocv-test-" }); |
| 85 | + |
| 86 | + try { |
| 87 | + await initLogger(tempDir); |
| 88 | + const logger = getOcvLogger(); |
| 89 | + |
| 90 | + // Logger should have the standard LogTape methods |
| 91 | + assertEquals(typeof logger, "object", "Logger should be an object"); |
| 92 | + assertEquals( |
| 93 | + typeof logger.info, |
| 94 | + "function", |
| 95 | + "Logger.info should be a function", |
| 96 | + ); |
| 97 | + assertEquals( |
| 98 | + typeof logger.warn, |
| 99 | + "function", |
| 100 | + "Logger.warn should be a function", |
| 101 | + ); |
| 102 | + assertEquals( |
| 103 | + typeof logger.error, |
| 104 | + "function", |
| 105 | + "Logger.error should be a function", |
| 106 | + ); |
| 107 | + assertEquals( |
| 108 | + typeof logger.debug, |
| 109 | + "function", |
| 110 | + "Logger.debug should be a function", |
| 111 | + ); |
| 112 | + } finally { |
| 113 | + try { |
| 114 | + await Deno.remove(tempDir, { recursive: true }); |
| 115 | + } catch { |
| 116 | + // Ignore cleanup errors |
| 117 | + } |
| 118 | + } |
| 119 | +}); |
0 commit comments