tools: leak driver, checker and the fault controls (#20)

tools/leak.mjs builds a 27-group scene over the real wire (movement, a closed door,
create/remove, gm_only, a controller hand-over, another player walking in and out, a
wall added and removed, a door push) and records every frame each socket saw, one
group per mutation. tools/leakcheck.nim replays it: the GM socket is ground truth,
the subject's polygons and explored cells are recomputed from geometry.nim, and the
explored set is also probed with a clear-segment sampler. Subjects A and B both pass
with zero failures; the transcripts and logs land in results/.

tools/fault.test.mjs runs the same scenario against a -d:leakfault build for each
fault and asserts the EXACT class set (HIDDEN_TOKEN, GMONLY_TOKEN, WALLDATA,
WITHHELD, OTHER_PLAYER, NO_REMOVE, EXPLORED), plus that the shipped binary carries no
LEAK_FAULT string.

withserver can build a variant with extra -d: flags and pass an env to it; lib.mjs
can name a player out of band, the fixture the player-side tests need until slice 8
brings player.setname (the unnamed gate itself is pinned in tools/tokens.test.mjs).
This commit is contained in:
2026-11-15 09:35:24 +01:00
parent 6fcf7f8271
commit 959412ff09
6 changed files with 861 additions and 9 deletions
+32
View File
@@ -6,6 +6,7 @@ import crypto from 'node:crypto'
import fs from 'node:fs'
import net from 'node:net'
import path from 'node:path'
import { DatabaseSync } from 'node:sqlite'
import { fileURLToPath } from 'node:url'
export const sleep = ms => new Promise(r => setTimeout(r, ms))
@@ -408,3 +409,34 @@ export async function collectAcks(client, ids, timeoutMs = 5000) {
await sleep(5)
}
}
// ---------------------------------------------------------------------------------------------
// Slice 5 (#20) fixtures: naming a player out of band.
//
// DESIGN 3.2 gives a player their display name at first sign-in, through `player.setname` - and
// `player.setname` is slice 8 (#23). Until then every non-GM connection is *unnamed*, and 6.1/6.5
// refuse an unnamed player everything but `conn.ping`: no scene block, no resync, no intents. The
// player-side acceptance of this slice (leak test A and B, WS probing) therefore cannot be reached
// through the product's own surface yet, so the harness writes the name into the DB before the
// connection is opened. That is a fixture, not a product path: the unnamed-player gate itself is
// pinned by tools/tokens.test.mjs (hello without a scene block, `not-allowed` for every intent).
export async function namePlayer(stateDir, playerId, name) {
const dbPath = path.join(stateDir, 'state.db')
let lastError
for (let attempt = 0; attempt < 40; attempt++) {
const db = new DatabaseSync(dbPath)
try {
db.exec('PRAGMA busy_timeout=5000')
db.prepare('UPDATE player SET name = ? WHERE id = ?').run(name, playerId)
const row = db.prepare('SELECT name FROM player WHERE id = ?').get(playerId)
if (!row || row.name !== name) throw new Error(`naming player ${playerId} failed`)
return
} catch (e) {
lastError = e
} finally {
try { db.close() } catch {}
}
await sleep(25) // SQLITE_BUSY while the server holds the write lock: back off and retry
}
throw lastError
}