diff --git a/prototype-server-vision/tools/restart.mjs b/prototype-server-vision/tools/restart.mjs new file mode 100644 index 0000000..3aad9df --- /dev/null +++ b/prototype-server-vision/tools/restart.mjs @@ -0,0 +1,85 @@ +// Restart durability: mutate, stop the server (SIGTERM, then SIGKILL), restart on the same DB, compare everything. +// Run via `NOSERVE=1 tools/withserver.sh node tools/restart.mjs` (withserver only builds run/test/server). Talks to the backend directly. +import fs from 'node:fs' +import { spawn } from 'node:child_process' +process.env.PORT = process.env.BACKEND_PORT // lib.mjs: origin = backend port (no nginx needed for ws/join) +const RUN = process.env.RUN_DIR +const sleep = ms => new Promise(r => setTimeout(r, ms)) +let fails = 0 +const check = (c, m) => { console.log(c ? 'PASS' : 'FAIL', m); if (!c) fails++ } + +let proc +const start = async () => { + fs.writeFileSync(`${RUN}/server.log`, '', { flag: 'a' }) + proc = spawn(`./${RUN}/server`, { env: process.env, stdio: ['ignore', fs.openSync(`${RUN}/server.log`, 'a'), 'inherit'] }) + for (let i = 0; i < 100 && !fs.readFileSync(`${RUN}/server.log`, 'utf8').includes('listening'); i++) await sleep(100) + await sleep(200) +} +const stop = sig => new Promise(res => { proc.once('exit', res); proc.kill(sig) }) +const invites = () => fs.readFileSync(`${RUN}/invites.txt`, 'utf8') + +// reduce a connection's messages to comparable state (gseq is in-memory only, so it is not compared) +async function view(role, keep) { + const st = { toks: {}, doors: {}, explored: new Set(), poly: '[]', hello: null } + const c = await connect(role, m => { + switch (m.t) { + case 'hello': st.hello = JSON.stringify({ ...m, gseq: 0 }); break + case 'walls': for (const w of m.walls) if (w.door) st.doors[w.id] = w.open; break + case 'door': st.doors[m.id] = m.open; break + case 'vision': st.poly = JSON.stringify(m.polys); break + case 'explored': for (const x of m.cells) st.explored.add(x + ''); break + case 'token': st.toks[m.id] = m.pos + ''; break + case 'remove': delete st.toks[m.id]; break + } + }) + if (!keep) c.ws.close() + return { st, c } +} +const norm = st => JSON.stringify({ ...st, explored: [...st.explored].sort(), toks: Object.entries(st.toks).sort(), doors: Object.entries(st.doors).sort() }) +const snap = async () => { const o = {}; for (const r of ['gm', 'A', 'B']) o[r] = norm((await view(r)).st); return o } + +let req = 0 +async function mutate(steps) { + const C = {}, acks = new Map() + for (const r of ['gm', 'A', 'B']) C[r] = await connect(r, m => { if (m.t === 'moveAck') acks.get(m.reqId)?.(m) }) + for (const [who, id, pos] of steps) { + if (who === 'door') { C.gm.ws.send(JSON.stringify({ t: 'door', id, open: pos })); await sleep(100); continue } + const r = ++req + const p = new Promise(res => acks.set(r, res)); C[who].ws.send(JSON.stringify({ t: 'move', reqId: r, id, pos })) + check((await p).ok, `move ${id} -> ${pos}`) + } + await sleep(200) + const live = {} // the long-lived clients' own accumulated view is not tracked; fresh views are the reference + for (const r of ['gm', 'A', 'B']) C[r].ws.close() + return live +} + +async function cycle(label, steps, sig) { + await mutate(steps) + const before = await snap(), inv = invites() + await stop(sig) + await start() + const after = await snap() + check(invites() === inv, `${label}: invite secrets identical`) + for (const r of ['gm', 'A', 'B']) check(before[r] === after[r], `${label}: ${r} state identical after ${sig} restart`) + return after +} + +await start() +const { connect } = await import('./lib.mjs') // lib reads invites.txt at import, so only after the first start +const s0 = await snap() +const a = JSON.parse(s0.A), g = JSON.parse(s0.gm) +const sA = await cycle('cycle1', [['A', 'A', [400, 800]], ['A', 'A', [800, 800]], ['A', 'A', [800, 640]], ['A', 'A', [1000, 640]], ['A', 'A', [1510, 640]], + ['B', 'B', [700, 2300]], ['gm', 'M1', [900, 640]], ['gm', 'M2', [2200, 300]], ['door', 10, true], ['door', 27, false], + ['A', 'A', [1700, 640]]], 'SIGTERM') +const a1 = JSON.parse(sA.A), g1 = JSON.parse(sA.gm) +check(a1.explored.length > a.explored.length, `A explored new area (${a.explored.length} -> ${a1.explored.length})`) +check(g1.toks.find(t => t[0] === 'A')[1] === '1700,640' && g1.toks.find(t => t[0] === 'B')[1] === '700,2300' && g1.toks.find(t => t[0] === 'M2')[1] === '2200,300', 'cycle1: token positions are the moved ones') +check(g1.doors.find(d => d[0] === '10')[1] === true && g1.doors.find(d => d[0] === '27')[1] === false, 'cycle1: door states are the toggled ones') +check(JSON.stringify(g.doors) !== JSON.stringify(g1.doors), 'cycle1: doors differ from initial (test is not vacuous)') +const sB = await cycle('cycle2', [['A', 'A', [1900, 640]], ['B', 'B', [1000, 2300]], ['gm', 'M1', [1200, 640]], ['door', 10, false], ['door', 27, true]], 'SIGKILL') +check(JSON.parse(sB.A).explored.length > a1.explored.length, 'cycle2: A explored more before kill -9') +check(JSON.parse(sB.B).explored.length > JSON.parse(sA.B).explored.length, 'cycle2: B explored more before kill -9') +await stop('SIGKILL') +console.log(fails ? `restart: FAILED ${fails}` : 'restart: PASS') +process.exit(fails ? 1 : 0)