From bc2e37bf2f13979c5daf712ea081b896b52a5add Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 18 Sep 2026 00:35:16 +0000 Subject: [PATCH] Add diagnostics buffer tests --- tests/diagnostics.test.mjs | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/diagnostics.test.mjs diff --git a/tests/diagnostics.test.mjs b/tests/diagnostics.test.mjs new file mode 100644 index 0000000..a019522 --- /dev/null +++ b/tests/diagnostics.test.mjs @@ -0,0 +1,49 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import { + airshipLog, + clearAirshipLogs, + getAirshipLogs, +} from '../src/diagnostics.js'; + +test('diagnostics records structured entries and returns copies', () => { + clearAirshipLogs(); + + const originalLog = console.log; + console.log = () => {}; + try { + airshipLog('test:event', { value: 42 }); + } finally { + console.log = originalLog; + } + + const logs = getAirshipLogs(); + assert.equal(logs.length, 1); + assert.equal(logs[0].event, 'test:event'); + assert.equal(logs[0].value, 42); + assert.equal(logs[0].level, 'info'); + assert.match(logs[0].ts, /^\d{4}-\d{2}-\d{2}T/); + + logs[0].value = 99; + assert.equal(getAirshipLogs()[0].value, 42); +}); + +test('diagnostics ring buffer is capped at 300 entries', () => { + clearAirshipLogs(); + + const originalLog = console.log; + console.log = () => {}; + try { + for (let i = 0; i < 305; i += 1) { + airshipLog('test:ring', { i }); + } + } finally { + console.log = originalLog; + } + + const logs = getAirshipLogs(); + assert.equal(logs.length, 300); + assert.equal(logs[0].i, 5); + assert.equal(logs.at(-1).i, 304); +});