diff --git a/tools/fault.test.mjs b/tools/fault.test.mjs index 11ee7de..d78deec 100644 --- a/tools/fault.test.mjs +++ b/tools/fault.test.mjs @@ -14,7 +14,7 @@ import fs from 'node:fs' import { DatabaseSync } from 'node:sqlite' import path from 'node:path' import { runLeak, runChecker } from './leak.mjs' -import { buildServer, gmKeyOf, makeStateDir, signIn, startServer, stopServer } from './withserver.mjs' +import { buildServer, gmKeyOf, makeStateDir, signIn, startServer, stopServer, waitForLog } from './withserver.mjs' import { openWs } from './lib.mjs' import { projectRoot } from './lib.mjs' @@ -144,8 +144,9 @@ test('a CatchableError inside a locked mutation: one internal ack, then the serv assert.deepEqual({ ...db.prepare('SELECT x, y FROM entity WHERE id = ?').get(tok.id) }, { x: 100, y: 100 }) db.close() - // One log event names the intent and the error class, never the message or payload. - const log = s.log() + // One log event names the intent and the error class, never the message or payload. The + // logger writes after the ack, so wait for the line instead of racing it. + const log = await waitForLog(s, /ERROR intent_failed intent=token\.move reqId=\d+ class=ValueError message=unhandled/) assert.match(log, /ERROR intent_failed intent=token\.move reqId=\d+ class=ValueError message=unhandled/) assert.equal(log.includes('injected fault'), false, 'the exception message is not logged') diff --git a/tools/logs.test.mjs b/tools/logs.test.mjs index 35fa471..e8c952a 100644 --- a/tools/logs.test.mjs +++ b/tools/logs.test.mjs @@ -15,7 +15,7 @@ import crypto from 'node:crypto' import fs from 'node:fs' import { createPlayer, join, openWs, rawWs, sleep } from './lib.mjs' import { gmKeyOf, makeStateDir, openAs, parseGmKey, runGmKey, startServer, - stopServer } from './withserver.mjs' + stopServer, waitForLog } from './withserver.mjs' const sha256 = s => crypto.createHash('sha256').update(s).digest('hex') @@ -134,8 +134,7 @@ test('logs: the negative control finds a raw key when the server leaks one', asy env: { LEAK_FAULT: 'keylog' } }) try { assert.equal((await join(s.origin, gmKey)).status, 303) - await sleep(100) - const log = s.log() + const log = await waitForLog(s, gmKey) assert.ok(log.includes(gmKey), `the leaked key must be visible to the grep; log:\n${log}`) } finally { await stopServer(s) diff --git a/tools/persistence.test.mjs b/tools/persistence.test.mjs index c33e37c..9de0428 100644 --- a/tools/persistence.test.mjs +++ b/tools/persistence.test.mjs @@ -8,7 +8,7 @@ import assert from 'node:assert/strict' import fs from 'node:fs' import { DatabaseSync } from 'node:sqlite' import { createPlayer, namePlayer, sleep } from './lib.mjs' -import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs' +import { gmKeyOf, makeStateDir, openAs, startServer, stopServer, waitForLog } from './withserver.mjs' import { V7_DDL, V7_WALLS } from './schema7fixture.mjs' const CELL = 32 @@ -175,7 +175,8 @@ test('explored is strictly per player, before and after a restart', async () => assert.deepEqual(sorted(after.alice), sorted(before.alice)) assert.deepEqual(sorted(after.bob), sorted(before.bob)) assert.ok(!after.alice.has(cellOf(3100, 3800)), 'still strictly per player after the restart') - assert.match(s.log(), /explored_loaded players=2 chunks=[1-9]/, s.log()) + const log2 = await waitForLog(s, /explored_loaded players=2 chunks=[1-9]/) + assert.match(log2, /explored_loaded players=2 chunks=[1-9]/, log2) } finally { await stopServer(s) fs.rmSync(stateDir, { recursive: true, force: true }) @@ -209,7 +210,8 @@ test('restart: every player\'s explored area is identical before and after (SIGT // The start is more than one vision radius from the last position, so these cells cannot come // from recomputing sight: they are the loaded chunks. assert.ok(after.has(cellOf(200, 200))) - assert.match(s.log(), /explored_loaded players=1 chunks=[1-9]/, s.log()) + const log1 = await waitForLog(s, /explored_loaded players=1 chunks=[1-9]/) + assert.match(log1, /explored_loaded players=1 chunks=[1-9]/, log1) } finally { await stopServer(s) fs.rmSync(stateDir, { recursive: true, force: true }) @@ -411,7 +413,8 @@ test('migration: a schema-4 DB (fixture) migrates under the real binary to the l } const s = await startServer({ stateDir }) try { - assert.match(s.log(), /db_ready .*schema_version=8 applied=4/, s.log()) + const boot4 = await waitForLog(s, /db_ready .*schema_version=8 applied=4/) + assert.match(boot4, /db_ready .*schema_version=8 applied=4/, boot4) // The old data is served unchanged through the product surface. const alice = { id: 2, key: 'ALIC-ALIC-ALIC-ALIC-ALIC' } const conn = await openAs(s.origin, alice.key) @@ -483,7 +486,8 @@ test('migration: a schema-7 wall/token/character fixture maps to typed storage a let before let s = await startServer({ stateDir }) try { - assert.match(s.log(), /db_ready .*schema_version=8 applied=1/, s.log()) + const boot1 = await waitForLog(s, /db_ready .*schema_version=8 applied=1/) + assert.match(boot1, /db_ready .*schema_version=8 applied=1/, boot1) const db = new DatabaseSync(dbPath) try { const cols = db.prepare("SELECT name FROM pragma_table_info('wall')").all().map(r => r.name) @@ -528,7 +532,8 @@ test('migration: a schema-7 wall/token/character fixture maps to typed storage a // Second start: the migration runner writes nothing and the data is byte-identical. s = await startServer({ stateDir }) try { - assert.match(s.log(), /db_ready .*schema_version=8 applied=0/, s.log()) + const boot0 = await waitForLog(s, /db_ready .*schema_version=8 applied=0/) + assert.match(boot0, /db_ready .*schema_version=8 applied=0/, boot0) const db = new DatabaseSync(dbPath) try { assert.deepEqual(readWallRows(db), before.walls) diff --git a/tools/scene.test.mjs b/tools/scene.test.mjs index 13212ba..541bf77 100644 --- a/tools/scene.test.mjs +++ b/tools/scene.test.mjs @@ -7,7 +7,7 @@ import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import { createPlayer, namePlayer, sleep } from './lib.mjs' -import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs' +import { gmKeyOf, makeStateDir, openAs, startServer, stopServer, waitForLog } from './withserver.mjs' async function gmConnect(origin, gmKey) { const conn = await openAs(origin, gmKey) @@ -246,7 +246,8 @@ test('scene.extra: size, key-count and depth caps, boundaries included (#35)', a const hello2 = await after(gm.ws, mark2, 'hello') assert.equal(hello2.scene.name, 'Scene', 'the refused patch applied no other field') assert.equal(hello2.scene.grid_size, 60) - assert.match(s.log(), /opaque_refused field=extra limit=size/) + assert.match(await waitForLog(s, /opaque_refused field=extra limit=size/), + /opaque_refused field=extra limit=size/) // Key count: 64 accepted, 65 refused; per object, so a 64-key nested object is fine. assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId, diff --git a/tools/smoke.test.mjs b/tools/smoke.test.mjs index 4360411..929246c 100644 --- a/tools/smoke.test.mjs +++ b/tools/smoke.test.mjs @@ -6,7 +6,7 @@ import fs from 'node:fs' import { spawnSync } from 'node:child_process' import { DatabaseSync } from 'node:sqlite' import { listeningSockets } from './lib.mjs' -import { buildServer, makeStateDir, startServer, stopServer, withServer } from './withserver.mjs' +import { buildServer, makeStateDir, startServer, stopServer, withServer, waitForLog } from './withserver.mjs' test('withserver brings the server up and /healthz answers 200 JSON', async () => { await withServer(async s => { @@ -17,7 +17,8 @@ test('withserver brings the server up and /healthz answers 200 JSON', async () = assert.equal((await fetch(`${s.origin}/nope`)).status, 404) assert.equal((await fetch(`${s.origin}/healthz`, { method: 'POST' })).status, 405) // flags reached the config and the start line: the harness origin and the fixed loopback bind - assert.match(s.log(), new RegExp(`server_start origin=${s.origin} .* bind=127\\.0\\.0\\.1`)) + const startLog = await waitForLog(s, new RegExp(`server_start origin=${s.origin} .* bind=127\\.0\\.0\\.1`)) + assert.match(startLog, new RegExp(`server_start origin=${s.origin} .* bind=127\\.0\\.0\\.1`)) }) }) diff --git a/tools/walls.test.mjs b/tools/walls.test.mjs index 9c55901..3f7cc74 100644 --- a/tools/walls.test.mjs +++ b/tools/walls.test.mjs @@ -6,7 +6,8 @@ import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' -import { gmKeyOf, makeStateDir, startServer, stopServer, withGmServer, openAs } from './withserver.mjs' +import { gmKeyOf, makeStateDir, startServer, stopServer, withGmServer, openAs, + waitForLog } from './withserver.mjs' import { createPlayer, sleep } from './lib.mjs' // The GM's first connection: hello, then the wall snapshot (DESIGN 6.1 `walls`, 6.3). @@ -753,8 +754,9 @@ test('wall.flags: size, key-count and depth caps, boundaries included (#35)', as await sleep(80) assert.equal(gm.ws.messages.filter(m => m.t === 'wall').length, before, 'a refused add emits no wall frame') - assert.match(s.log(), /opaque_refused field=flags limit=size/) - assert.equal(s.log().includes('xxxxxxxx'), false, 'the refused value is never logged') + const refusedLog = await waitForLog(s, /opaque_refused field=flags limit=size/) + assert.match(refusedLog, /opaque_refused field=flags limit=size/) + assert.equal(refusedLog.includes('xxxxxxxx'), false, 'the refused value is never logged') // The refused add consumes no wall id: the next wall follows the accepted one densely. const next = await addWall(gm, { a: [10, 30], b: [100, 30] }) assert.equal(next.id, ok.id + 1) diff --git a/tools/withserver.mjs b/tools/withserver.mjs index 95fef60..f326579 100644 --- a/tools/withserver.mjs +++ b/tools/withserver.mjs @@ -98,6 +98,22 @@ export async function startServer(opts = {}) { return handle } +// Wait for a line the server writes asynchronously (the log is not flushed before the ack that +// triggered it). A test that greps the log right after an ack is otherwise racing the logger: under +// the parallel fan-out that race is lost. Returns the log when the needle matches, or the last read +// at the deadline so the test's own assertion reports the content it saw. +export async function waitForLog(handle, needle, timeoutMs = 15000) { + const matches = log => typeof needle === 'function' ? needle(log) + : needle instanceof RegExp ? needle.test(log) : log.includes(needle) + const deadline = Date.now() + timeoutMs + let log = handle.log() + while (!matches(log) && Date.now() < deadline) { + await sleep(25) + log = handle.log() + } + return log +} + export async function stopServer(handle, logFd = handle.logFd) { // A process killed by a signal keeps exitCode null and sets signalCode, so test both. if (!hasExited(handle.child)) {