// M3 slice 10 (#25): the "restart" acceptance group (DESIGN 10.1, 13 M3 group 7). // // A restart must bring back *identical state*, not just the single feature of one slice. This builds // a world that exercises every CRUD path M3 has (walls and an open door, tokens with sizes, props in // all three buckets and statuses, characters with props, a character reveal, a token reveal-all, a // sighting in the encountered list, explored cells, players and their names), then compares what a // fresh GM connection and a fresh player connection receive as their full 6.3 resync before and // after the restart. SIGTERM and SIGKILL are both checked: every acked commit is durable (4.2). 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' async function gmConnect(origin, gmKey) { const conn = await openAs(origin, gmKey) await conn.ws.waitText('hello') const walls = await conn.ws.waitText('walls') return { ...conn, sceneId: walls.scene } } // Build the world on a running server. Every mutation is acked before the next one, so a kill after // this returns cannot lose an acked commit. async function buildWorld(origin, gmKey) { const gm = await gmConnect(origin, gmKey) const saved = {} try { // Players: a GM plus one named player with a key (its own CRUD path is key.* + player.setname). const alice = await createPlayer(gm.ws) await namePlayer(origin, alice.key, 'Alice') saved.alice = alice // A second, still unnamed player: the GM roster carries it (`name:null`), players never see it. await createPlayer(gm.ws) // A wall and a door; the door is opened (door.set is its own write path). const wall = await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [700, 0], b: [700, 900], kind: 'wall' }) const door = await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [700, 1000], b: [700, 1400], kind: 'door' }) assert.equal(wall.ok, true, JSON.stringify(wall)) assert.equal(door.ok, true, JSON.stringify(door)) assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.sceneId, id: door.id, open: true })).ok, true) // The player's own token: vision, explored cells and the encountered list come from it. const pc = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [400, 400], descriptive_name: 'a wanderer', actual_name: 'Wanderer', controller: alice.id, flags: { vision: true }, radius: 35, props: { public: { aura: 'cold' }, owner: { hp: 5 }, gm: { secret: 1 } } }) assert.equal(pc.ok, true, JSON.stringify(pc)) // A monster right next to it: Alice meets it (one encountered entry, grey after it moves away). const monster = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc', pos: [500, 400], descriptive_name: 'a goblin', actual_name: 'Grib' }) assert.equal(monster.ok, true, JSON.stringify(monster)) // A character with props, and a token that figures it (linked tokens carry no names). const character = await gm.ws.request({ t: 'character.create', descriptive_name: 'a cloaked figure', actual_name: 'Lady Vex', owner: alice.id, props: { public: { rank: 'noble' }, owner: { hp: 9 }, gm: { plot: 'ally' } } }) assert.equal(character.ok, true, JSON.stringify(character)) const linked = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [560, 400], character_id: character.id, controller: alice.id }) assert.equal(linked.ok, true, JSON.stringify(linked)) // Props: a set and an unset on the plain token, a set on the character. assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: pc.id, bucket: 'public', key: 'light', value: 30 })).ok, true) assert.equal((await gm.ws.request({ t: 'prop.unset', target: 'token', id: pc.id, bucket: 'owner', key: 'hp' })).ok, true) assert.equal((await gm.ws.request({ t: 'prop.set', target: 'character', id: character.id, bucket: 'public', key: 'banner', value: 'red' })).ok, true) // Status (GM source) and a resize: two more persisted tables/columns. assert.equal((await gm.ws.request({ t: 'status.set', id: monster.id, status: 'stuck' })).ok, true) assert.equal((await gm.ws.request({ t: 'token.resize', id: monster.id, radius: 28 })).ok, true) // A reveal for one player, reveal-all for a plain token, one encountered entry removed later. assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'character', id: character.id, player: alice.id })).ok, true) assert.equal((await gm.ws.request({ t: 'name.reveal_all', subject: 'token', id: pc.id })).ok, true) saved.ids = { pc: pc.id, monster: monster.id, character: character.id, linked: linked.id, wall: wall.id, door: door.id } gm.ws.close() } finally { try { gm.ws.close() } catch {} } return saved } function count(client, t) { return client.messages.filter(m => m && m.t === t).length } async function waitResync(client, expected) { // The 6.3 sequence is ordered but arrives asynchronously; wait until every expected part is there. const deadline = Date.now() + 5000 for (;;) { const ok = count(client, 'hello') >= 1 && count(client, 'roster') >= 1 && count(client, 'token') >= expected.tokens && count(client, 'character') >= expected.characters && (expected.gm ? count(client, 'walls') >= 1 : count(client, 'encountered') >= 1 && count(client, 'vision') >= 1 && count(client, 'explored') >= 1) if (ok) return if (Date.now() > deadline) { throw new Error(`resync incomplete: ${JSON.stringify(client.messages.map(m => m.t))}`) } await sleep(5) } } const strip = m => { const { n, ...rest } = m; return rest } const by = key => (a, b) => String(a[key]).localeCompare(String(b[key])) // One connection's whole resync, normalized: `n` is per-connection and therefore not state. async function snapshot(origin, key, expected, isGm) { const c = await openAs(origin, key) try { await waitResync(c.ws, { ...expected, gm: isGm }) const pick = t => c.ws.messages.filter(m => m && m.t === t).map(strip) const snap = { hello: pick('hello')[0], roster: pick('roster').pop(), tokens: pick('token') } snap.tokens.sort(by('id')) snap.roster.players.sort(by('id')) if (isGm) { const walls = pick('walls').pop() snap.walls = walls snap.walls.walls.sort(by('id')) snap.characters = pick('character').sort(by('id')) snap.encountered = pick('encountered').map(e => { e.set.sort(by('id')) return e }).sort(by('player')) } else { snap.characters = pick('character').sort(by('id')) snap.encountered = pick('encountered').pop() snap.encountered.set.sort(by('id')) snap.vision = pick('vision').pop() snap.vision.polys.sort(by('id')) // explored RLE rows are already ordered by cy; keep the payload as the view, not a set. snap.explored = pick('explored').pop() } return snap } finally { c.ws.close() await c.ws.closed } } // The full before/after comparison: one fresh GM connection and one fresh Alice connection. async function compareAcross(origin, gmKey, aliceKey, ids, expected) { const beforeGm = await snapshot(origin, gmKey, expected, true) const beforeAlice = await snapshot(origin, aliceKey, expected, false) // The CRUD inventory is not empty: a restart test over an empty world would prove nothing. assert.ok(beforeGm.walls.walls.length >= 2, 'the wall and the door are there') assert.equal(beforeGm.walls.walls.find(w => w.id === ids.door).open, true, 'the door is open') assert.ok(beforeGm.tokens.length >= 3, 'plain, monster, player and linked tokens are there') assert.ok(beforeGm.characters.length >= 1, 'the character is there') assert.equal(beforeGm.characters.find(c => c.id === ids.character).props.public.banner, 'red') assert.equal(beforeGm.tokens.find(t => t.id === ids.monster).radius, 28, 'the resize survived') assert.ok(beforeGm.tokens.find(t => t.id === ids.monster).statuses .some(s => s.status === 'stuck' && s.source === 'GM'), 'the GM status survived') assert.equal(beforeAlice.encountered.set.find(e => e.id === ids.monster)?.name, 'a goblin') assert.ok(beforeAlice.vision.polys.length >= 1, 'the player has sight') assert.ok(beforeAlice.explored.rle.length >= 1, 'the player explored something') return { beforeGm, beforeAlice } } async function restartAndCompare(stateDir, gmKey, aliceKey, ids, expected, hard) { let s = await startServer({ stateDir }) const before = await compareAcross(s.origin, gmKey, aliceKey, ids, expected) if (hard) { // Every ack is sent after COMMIT (4.2), so a hard kill cannot lose an acked mutation. s.child.kill('SIGKILL') const deadline = Date.now() + 5000 while (s.child.signalCode === null && s.child.exitCode === null && Date.now() < deadline) { await sleep(20) } } await stopServer(s) s = await startServer({ stateDir }) try { const afterGm = await snapshot(s.origin, gmKey, expected, true) const afterAlice = await snapshot(s.origin, aliceKey, expected, false) assert.deepEqual(afterGm, before.beforeGm, 'the GM resync after the restart is identical') assert.deepEqual(afterAlice, before.beforeAlice, 'the player resync after the restart is identical') } finally { await stopServer(s) } } const EXPECTED = { tokens: 3, characters: 1 } test('restart: full state, all CRUD, before and after SIGTERM', async () => { const stateDir = makeStateDir() const gmKey = gmKeyOf(stateDir) let s = await startServer({ stateDir }) let saved try { saved = await buildWorld(s.origin, gmKey) } finally { await stopServer(s) } try { await restartAndCompare(stateDir, gmKey, saved.alice.key, saved.ids, EXPECTED, false) } finally { fs.rmSync(stateDir, { recursive: true, force: true }) } }) test('restart: full state, all CRUD, before and after SIGKILL', async () => { const stateDir = makeStateDir() const gmKey = gmKeyOf(stateDir) let s = await startServer({ stateDir }) let saved try { saved = await buildWorld(s.origin, gmKey) } finally { await stopServer(s) } try { await restartAndCompare(stateDir, gmKey, saved.alice.key, saved.ids, EXPECTED, true) } finally { fs.rmSync(stateDir, { recursive: true, force: true }) } })