|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Unit tests for the leveled logger (`scripts/logger.mjs`). |
| 4 | + * Run: `node --test tools/test-logger.mjs` |
| 5 | + */ |
| 6 | + |
| 7 | +import test from 'node:test'; |
| 8 | +import assert from 'node:assert/strict'; |
| 9 | + |
| 10 | +const { log, setLogLevel, getLogLevelName, getLogBuffer, clearLogBuffer, exportLogText, LOG_LEVELS } = |
| 11 | + await import('../scripts/logger.mjs'); |
| 12 | + |
| 13 | +function withConsoleSpy(fn) { |
| 14 | + const calls = { error: 0, warn: 0, info: 0, debug: 0, log: 0 }; |
| 15 | + const orig = { error: console.error, warn: console.warn, info: console.info, debug: console.debug, log: console.log }; |
| 16 | + console.error = () => { calls.error++; }; |
| 17 | + console.warn = () => { calls.warn++; }; |
| 18 | + console.info = () => { calls.info++; }; |
| 19 | + console.debug = () => { calls.debug++; }; |
| 20 | + console.log = () => { calls.log++; }; |
| 21 | + try { fn(calls); } finally { Object.assign(console, orig); } |
| 22 | +} |
| 23 | + |
| 24 | +test('level set/get by name and number; unknown name ignored', () => { |
| 25 | + setLogLevel('warn'); |
| 26 | + assert.equal(getLogLevelName(), 'warn'); |
| 27 | + setLogLevel(LOG_LEVELS.debug); |
| 28 | + assert.equal(getLogLevelName(), 'debug'); |
| 29 | + setLogLevel('bogus'); |
| 30 | + assert.equal(getLogLevelName(), 'debug'); // unchanged |
| 31 | +}); |
| 32 | + |
| 33 | +test('ring buffer ALWAYS captures, regardless of console level', () => { |
| 34 | + clearLogBuffer(); |
| 35 | + setLogLevel('error'); // only errors print… |
| 36 | + withConsoleSpy(() => { |
| 37 | + log.debug('a debug line'); |
| 38 | + log.error('an error line'); |
| 39 | + }); |
| 40 | + const buf = getLogBuffer(); |
| 41 | + assert.equal(buf.length, 2); // both captured |
| 42 | + assert.deepEqual(buf.map((e) => e.level), ['debug', 'error']); |
| 43 | + assert.match(buf[0].msg, /a debug line/); |
| 44 | +}); |
| 45 | + |
| 46 | +test('console output is gated by the active level', () => { |
| 47 | + clearLogBuffer(); |
| 48 | + setLogLevel('warn'); |
| 49 | + withConsoleSpy((calls) => { |
| 50 | + log.error('e'); |
| 51 | + log.warn('w'); |
| 52 | + log.info('i'); |
| 53 | + log.debug('d'); |
| 54 | + assert.equal(calls.error, 1); |
| 55 | + assert.equal(calls.warn, 1); |
| 56 | + assert.equal(calls.info, 0); // below threshold → not printed |
| 57 | + assert.equal(calls.debug, 0); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +test('silent suppresses all console output but still captures', () => { |
| 62 | + clearLogBuffer(); |
| 63 | + setLogLevel('silent'); |
| 64 | + withConsoleSpy((calls) => { |
| 65 | + log.error('still recorded'); |
| 66 | + assert.equal(calls.error, 0); |
| 67 | + }); |
| 68 | + assert.equal(getLogBuffer().length, 1); |
| 69 | +}); |
| 70 | + |
| 71 | +test('objects/errors are stringified; exportLogText renders lines', () => { |
| 72 | + clearLogBuffer(); |
| 73 | + setLogLevel('trace'); |
| 74 | + withConsoleSpy(() => { |
| 75 | + log.info('obj', { a: 1 }); |
| 76 | + log.error(new Error('boom')); |
| 77 | + }); |
| 78 | + const txt = exportLogText(); |
| 79 | + assert.match(txt, /INFO obj \{"a":1\}/); |
| 80 | + assert.match(txt, /ERROR boom/); |
| 81 | +}); |
| 82 | + |
| 83 | +test('ring buffer is capped (does not grow unbounded)', () => { |
| 84 | + clearLogBuffer(); |
| 85 | + setLogLevel('silent'); |
| 86 | + for (let i = 0; i < 600; i++) log.info('x' + i); |
| 87 | + assert.ok(getLogBuffer().length <= 500); |
| 88 | +}); |
0 commit comments