// M3 slice 4 (#19): walls and doors. Black-box through the real binary (withserver): the GM // `walls` snapshot, wall.add/wall.update/wall.remove and door.set with their 6.5 acks and GM-only // effects, the per-scene never-reused wall ids, the 6.1 validation table, "no wall data reaches a // player" and a restart that leaves walls (and door state) identical (DESIGN 3.1, 6.1, 8, 10.1). 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 { createPlayer, sleep } from './lib.mjs' const stripN = msg => { const { n, ...rest } = msg; return rest } // The GM's first connection: hello, then the wall snapshot (DESIGN 6.1 `walls`, 6.3). 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, scene: walls.scene, walls } } async function addWall(gm, fields) { const ack = await gm.ws.request({ t: 'wall.add', scene: gm.scene, ...fields }) return ack } // Waits for the `wall`/`wall_gone` effect frame matching pred (the fan-out is queued after the ack). async function wallEffect(ws, pred, timeoutMs = 5000) { return await ws.waitFor(m => (m.t === 'wall' || m.t === 'wall_gone') && pred(m), timeoutMs) } test('wall.add: one ack with the id, then the wall to the GM with the 6.1 shape', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) assert.equal(gm.walls.t, 'walls') assert.equal(typeof gm.scene, 'number') assert.deepEqual(gm.walls.walls, [], 'a fresh scene has no walls') const ack = await addWall(gm, { a: [100, 200.5], b: [400, 200.5], kind: 'wall' }) assert.equal(ack.ok, true) assert.equal(ack.id, 1) assert.equal(ack.code, undefined, 'an ok ack carries the result fields only') assert.equal(typeof ack.n, 'number') const wall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === 1) assert.equal(wall.scene, gm.scene) assert.deepEqual(wall.a, [100, 200.5]) assert.deepEqual(wall.b, [400, 200.5]) assert.equal(wall.kind, 'wall') assert.equal(wall.open, false) // omitted open = closed assert.deepEqual(wall.flags, {}) // omitted flags = {} assert.equal(typeof wall.n, 'number') assert.ok(gm.ws.messages.indexOf(ack) < gm.ws.messages.indexOf(wall), 'the ack is enqueued before the effect (DESIGN 4.2)') // the flags object round-trips untouched; unknown intent fields are ignored (6.2) const withFlags = await addWall(gm, { a: [10, 10], b: [10, 300], kind: 'door', open: true, flags: { locked: false, note: 'side' }, junk: { ignored: true } }) assert.equal(withFlags.ok, true) assert.equal(withFlags.id, 2) const door = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === 2) assert.equal(door.kind, 'door') assert.equal(door.open, true) assert.deepEqual(door.flags, { locked: false, note: 'side' }) // a second GM connection gets the snapshot of both walls, in id order const gm2 = await openAs(s.origin, gmKey) await gm2.ws.waitText('hello') const snap = await gm2.ws.waitText('walls') assert.deepEqual(snap.walls.map(w => w.id), [1, 2]) assert.equal(snap.walls[1].open, true) gm.ws.close(); gm2.ws.close() }) }) test('wall ids: monotonic per scene and never reused after removal', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) for (let i = 0; i < 3; i++) { assert.equal((await addWall(gm, { a: [i * 10, 0], b: [i * 10 + 5, 0], kind: 'wall' })).id, i + 1) } assert.equal((await gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: 2 })).ok, true) await wallEffect(gm.ws, m => m.t === 'wall_gone' && m.id === 2) // the surviving ids do not renumber, and the next add does not reuse 2 const next = await addWall(gm, { a: [0, 50], b: [50, 50], kind: 'wall' }) assert.equal(next.id, 4) const gm2 = await openAs(s.origin, gmKey) await gm2.ws.waitText('hello') const snap = await gm2.ws.waitText('walls') assert.deepEqual(snap.walls.map(w => w.id), [1, 3, 4]) // remove the newest and add again: still no reuse assert.equal((await gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: 4 })).ok, true) assert.equal((await addWall(gm, { a: [0, 60], b: [50, 60], kind: 'wall' })).id, 5) gm.ws.close(); gm2.ws.close() }) }) test('wall.update: patches a, b, kind and flags; the id and open state stay', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [100, 100], b: [200, 100], kind: 'door' }) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id) assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id, open: true })).ok, true) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.open === true) const ack = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, a: [100, 100], b: [300, 400], flags: { kind: 'thin' } }) assert.equal(ack.ok, true) const updated = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.b[1] === 400) assert.deepEqual(updated.a, [100, 100]) assert.deepEqual(updated.b, [300, 400]) assert.deepEqual(updated.flags, { kind: 'thin' }) assert.equal(updated.open, true, 'wall.update does not touch the open state') assert.equal(updated.kind, 'door') // a partial patch keeps the other endpoint assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, a: [0, 0] })).ok, true) const partial = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.a[0] === 0) assert.deepEqual(partial.b, [300, 400]) // door -> wall closes it (a wall is never open) assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, kind: 'wall' })).ok, true) const asWall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.kind === 'wall') assert.equal(asWall.open, false) assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id, open: true })).code, 'invalid', 'the row is a plain wall now') gm.ws.close() }) }) test('wall.remove and door.set: acks, GM effects, and the codes of 6.5', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id: wallId } = await addWall(gm, { a: [0, 0], b: [100, 0], kind: 'wall' }) const { id: doorId } = await addWall(gm, { a: [0, 100], b: [100, 100], kind: 'door' }) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === doorId) // door.set toggles only doors assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: doorId, open: true })).ok, true) const opened = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === doorId && m.open === true) assert.equal(opened.kind, 'door') assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: doorId, open: false })).ok, true) const closed = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === doorId && m.open === false) assert.equal(closed.open, false) // wall.remove: ack {}, wall_gone to the GM, then not-found (no existence oracle for a GM either) const removed = await gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: doorId }) assert.equal(removed.ok, true) const gone = await wallEffect(gm.ws, m => m.t === 'wall_gone' && m.id === doorId) assert.equal(gone.scene, gm.scene) assert.equal((await gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: doorId })).code, 'not-found') // the door is gone for a fresh GM connection too const gm2 = await openAs(s.origin, gmKey) await gm2.ws.waitText('hello') const snap = await gm2.ws.waitText('walls') assert.deepEqual(snap.walls.map(w => w.id), [wallId]) // door.set on a plain wall, an unknown id, and a missing open flag assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: wallId, open: true })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: 99, open: true })).code, 'not-found') assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: wallId })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: wallId, open: 'yes' })).code, 'invalid') gm.ws.close(); gm2.ws.close() }) }) test('field validation: invalid geometry and shapes are refused, nothing is applied', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const scene = gm.scene const invalid = [ { a: [10, 10], b: [10, 10], kind: 'wall' }, // a == b { a: [10, 10], kind: 'wall' }, // no b { b: [10, 10], kind: 'wall' }, // no a { a: [10, 10], b: [20, 20] }, // no kind { a: [10, 10], b: [20, 20], kind: 'window' }, // kind outside wall|door { a: [10, 10], b: [20, 20], kind: 'WALL' }, { a: [-1, 10], b: [20, 20], kind: 'wall' }, // out of bounds { a: [10, 10], b: [4097, 20], kind: 'wall' }, { a: [10], b: [20, 20], kind: 'wall' }, // not a point { a: [10, 10, 10], b: [20, 20], kind: 'wall' }, { a: ['10', 10], b: [20, 20], kind: 'wall' }, { a: [10, 10], b: [20, 20], kind: 'wall', open: 'yes' }, // open is not a boolean { a: [10, 10], b: [20, 20], kind: 'wall', open: true }, // a wall is never open { a: [10, 10], b: [20, 20], kind: 'door', flags: [1, 2] }, // flags is not an object { a: [10, 10], b: [20, 20], kind: 'door', flags: 7 }, ] for (const fields of invalid) { const ack = await gm.ws.request({ t: 'wall.add', scene, ...fields }) assert.equal(ack.ok, false, JSON.stringify(fields)) assert.equal(ack.code, 'invalid', JSON.stringify(fields)) assert.equal(ack.id, undefined, 'a refusal carries no state') } // scene: a wrong id is not-found, a wrongly typed one is invalid assert.equal((await gm.ws.request({ t: 'wall.add', scene: '1', a: [0, 0], b: [1, 1], kind: 'wall' })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'wall.add', scene: scene + 1, a: [0, 0], b: [1, 1], kind: 'wall' })).code, 'not-found') // fields are checked before the lookup (6.5 steps 7 then 8): no patch field is `invalid`, // whatever the id assert.equal((await gm.ws.request({ t: 'wall.update', scene, id: 1 })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'wall.update', scene, id: 99, a: [0, 0] })).code, 'not-found') // nothing above was applied const gm2 = await openAs(s.origin, gmKey) await gm2.ws.waitText('hello') assert.deepEqual((await gm2.ws.waitText('walls')).walls, []) gm.ws.close(); gm2.ws.close() }) }) test('wall.update refuses an empty patch, bad fields and bad geometry', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [100, 100], b: [200, 100], kind: 'wall' }) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id) const cases = [ [{}, 'invalid'], // no updatable field at all [{ id: 'x' }, 'invalid'], // id is not an integer [{ a: [1] }, 'invalid'], [{ kind: 'window' }, 'invalid'], [{ flags: 'no' }, 'invalid'], [{ open: true }, 'invalid'], // `open` alone is not an update [{ a: [5, 5], b: [5, 5] }, 'invalid'], // degenerate [{ b: [9999, 5] }, 'invalid'], // out of bounds [{ id: 42, a: [0, 0] }, 'not-found'], ] for (const [fields, code] of cases) { const ack = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: id, ...fields }) assert.equal(ack.ok, false, JSON.stringify(fields)) assert.equal(ack.code, code, JSON.stringify(fields)) } gm.ws.close() }) }) test('players get no wall data at all, and every wall intent is refused to them', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const player = await createPlayer(gm.ws) const p = await openAs(s.origin, player.key) await p.ws.waitText('hello') assert.equal(p.ws.messages.some(m => m.t === 'walls'), false, 'a player connection gets no wall snapshot') // a marker unique in the whole session: no number of the GM's walls may appear in a player frame const marker = [1234.5, 321.5] const added = await addWall(gm, { a: marker, b: [2345.5, 321.5], kind: 'door' }) assert.equal(added.ok, true) await gm.ws.request({ t: 'door.set', scene: gm.scene, id: added.id, open: true }) await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: added.id, b: [3456.5, 321.5] }) await gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: added.id }) await sleep(150) const types = new Set(p.ws.messages.map(m => m.t)) for (const t of ['walls', 'wall', 'wall_gone']) { assert.equal(types.has(t), false, `a player received a ${t} frame`) } for (const m of p.ws.messages) { assert.ok(!JSON.stringify(m).includes('1234.5'), `wall coordinates leaked: ${JSON.stringify(m)}`) assert.ok(!JSON.stringify(m).includes('321.5'), `wall coordinates leaked: ${JSON.stringify(m)}`) } // DESIGN 6.1 roles / 6.5 step 5-6: an unnamed non-GM connection is refused (the named-player // role gate is the same `GmOnlyIntents` check, pinned by tests/tprotocol.nim until slice 8 // brings naming). for (const intent of [{ t: 'wall.add', scene: gm.scene, a: [0, 0], b: [1, 1], kind: 'wall' }, { t: 'wall.update', scene: gm.scene, id: 1, a: [0, 0] }, { t: 'wall.remove', scene: gm.scene, id: 1 }, { t: 'door.set', scene: gm.scene, id: 1, open: true }]) { const ack = await p.ws.request(intent) assert.equal(ack.ok, false, intent.t) assert.equal(ack.code, 'not-allowed', intent.t) } p.ws.close(); gm.ws.close() }) }) test('restart: walls, door state and the id counter are identical afterwards', async () => { const stateDir = makeStateDir() try { const gmKey = gmKeyOf(stateDir) let s = await startServer({ stateDir }) let scene try { const gm = await gmConnect(s.origin, gmKey) scene = gm.scene const w1 = await addWall(gm, { a: [0, 0], b: [4096, 0], kind: 'wall', flags: { side: 'north' } }) const w2 = await addWall(gm, { a: [100, 100], b: [400, 100], kind: 'door' }) const w3 = await addWall(gm, { a: [0, 500], b: [0, 900], kind: 'wall' }) assert.deepEqual([w1.id, w2.id, w3.id], [1, 2, 3]) assert.equal((await gm.ws.request({ t: 'door.set', scene, id: w2.id, open: true })).ok, true) assert.equal((await gm.ws.request({ t: 'wall.remove', scene, id: w3.id })).ok, true) await gm.ws.request({ t: 'wall.update', scene, id: w1.id, b: [4000, 0] }) await sleep(100) gm.ws.close() } finally { await stopServer(s) } s = await startServer({ stateDir }) try { const gm = await gmConnect(s.origin, gmKey) assert.equal(gm.scene, scene) const { n, ...snapshot } = gm.walls assert.deepEqual(snapshot, { t: 'walls', scene, walls: [ { id: 1, a: [0, 0], b: [4000, 0], kind: 'wall', open: false, flags: { side: 'north' } }, { id: 2, a: [100, 100], b: [400, 100], kind: 'door', open: true, flags: {} }, ], }) // the counter survived the removal of wall 3: the next id is 4, never 3 assert.equal((await addWall(gm, { a: [10, 10], b: [20, 20], kind: 'wall' })).id, 4) gm.ws.close() } finally { await stopServer(s) } } finally { fs.rmSync(stateDir, { recursive: true, force: true }) } })