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
+20 -9
View File
@@ -15,16 +15,25 @@ function nimblePkgs() {
}
let builtAt = null
const builtCache = new Map()
export function buildServer() {
if (builtAt) return builtAt
fs.mkdirSync(path.dirname(SERVER_BIN), { recursive: true })
// Build the real binary from the working tree. `defines` adds `-d:` flags (a leak-fault build,
// DESIGN 10.3); each variant lands in its own file so the clean build is never overwritten.
export function buildServer(opts = {}) {
const defines = opts.defines ?? []
const key = defines.join(',')
const out = opts.out ?? (defines.length === 0
? SERVER_BIN
: path.join(projectRoot, 'run', 'test', `server-${defines.join('-').replace(/[^A-Za-z0-9-]/g, '')}`))
if (builtCache.has(key)) return builtCache.get(key)
fs.mkdirSync(path.dirname(out), { recursive: true })
const args = ['c', '--hints:off', '--mm:orc', `--nimblePath:${nimblePkgs()}`,
`-o:${SERVER_BIN}`, path.join('server', 'main.nim')]
...defines.map(d => `-d:${d}`), `-o:${out}`, path.join('server', 'main.nim')]
const r = spawnSync('nim', args, { cwd: projectRoot, encoding: 'utf8' })
if (r.status !== 0) throw new Error(`nim c server/main.nim failed:\n${r.stdout}\n${r.stderr}`)
builtAt = SERVER_BIN
return builtAt
if (r.status !== 0) throw new Error(`nim c server/main.nim ${defines.join(' ')} failed:\n${r.stdout}\n${r.stderr}`)
builtAt = out
builtCache.set(key, out)
return out
}
export function makeStateDir(prefix = 'fffvtt-') {
@@ -52,11 +61,12 @@ async function waitForHealth(handle) {
}
// Starts the server and waits for /healthz. Resolves to a handle; stopServer() tears it down.
// `defines` selects a build variant (e.g. a -d:leakfault build), `env` extra environment for it.
export async function startServer(opts = {}) {
const ownStateDir = opts.stateDir === undefined
const stateDir = opts.stateDir ?? makeStateDir()
const args = opts.args ?? []
const bin = buildServer()
const bin = buildServer({ defines: opts.defines })
const port = opts.port ?? await freePort()
refuseLivePort(port)
fs.mkdirSync(stateDir, { recursive: true })
@@ -64,7 +74,8 @@ export async function startServer(opts = {}) {
const logFd = fs.openSync(logPath, 'a')
const child = spawn(bin, [`--port=${port}`, `--state-dir=${stateDir}`,
`--public-origin=${baseUrl(port)}`, ...args],
{ cwd: projectRoot, stdio: ['ignore', logFd, logFd] })
{ cwd: projectRoot, stdio: ['ignore', logFd, logFd],
env: { ...process.env, ...(opts.env ?? {}) } })
const handle = {
port, origin: baseUrl(port), stateDir, dbPath: path.join(stateDir, 'state.db'),
logPath, child, log: () => fs.readFileSync(logPath, 'utf8'),