// M3 slice 4 (#19) walls and doors, rewritten for the proto-2 typed wall record (#54). 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 typed geometry and raw switches, the // derived shape label, the per-scene never-reused wall ids, "no wall data reaches a player" and a // restart that leaves walls (and door state) identical (DESIGN 3.1, 5.8, 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' // 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) } // The full proto-2 GM frame every wall must carry (5.8 + 6.1). const WALL_FIELDS = ['id', 'a', 'b', 'thickness', 'position', 'height', 'kind', 'blocks_sight', 'blocks_movement', 'sight_dir', 'player_marker', 'door', 'open', 'secret', 'locked', 'label', 'flags'] function assertTypedWall(wall) { // A `wall` frame also carries `t`/`scene`; a `walls` entry only the wall fields plus `n` on send. for (const key of WALL_FIELDS) assert.ok(key in wall, `wall frame is missing ${key}`) } test('wall.add: the typed record, raw-switch defaults and the derived kind', 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] }) assert.equal(ack.ok, true) assert.equal(ack.id, 1) assert.equal(ack.code, undefined, 'an ok ack carries the result fields only') const wall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === 1) assertTypedWall(wall) assert.equal(wall.scene, gm.scene) assert.deepEqual(wall.a, [100, 200.5]) assert.deepEqual(wall.b, [400, 200.5]) assert.equal(wall.thickness, 0) assert.equal(wall.position, 'center') assert.equal(wall.height, null) assert.equal(wall.kind, 'wall') assert.equal(wall.blocks_sight, true) assert.equal(wall.blocks_movement, true) assert.equal(wall.sight_dir, 'both') assert.equal(wall.player_marker, false) assert.equal(wall.door, false) assert.equal(wall.open, false) assert.equal(wall.secret, false) assert.equal(wall.locked, false) assert.equal(wall.label, null) assert.deepEqual(wall.flags, {}) assert.ok(gm.ws.messages.indexOf(ack) < gm.ws.messages.indexOf(wall), 'the ack is enqueued before the effect (DESIGN 4.2)') // `kind` is output-only: an input kind is an unknown field and ignored (#54, 6.2) const ignored = await addWall(gm, { a: [10, 10], b: [10, 300], kind: 'door', flags: { note: 'side' }, junk: { ignored: true } }) assert.equal(ignored.ok, true) const plain = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === ignored.id) assert.equal(plain.kind, 'wall') assert.equal(plain.door, false, 'an input kind never creates a door') assert.deepEqual(plain.flags, { note: 'side' }) // geometry is accepted, stored and echoed; the switch set derives the shape label in 5.8 order const shapes = [ [{ blocks_sight: false }, 'window'], [{ sight_dir: 'left' }, 'one_way'], [{ door: true }, 'door'], [{ door: true, blocks_sight: false }, 'window'], // a one-way check first: see-through wall [{ blocks_movement: false }, 'custom'], [{ secret: true, door: true }, 'door'], // secret never changes the label ] for (const [switches, kind] of shapes) { const a = await addWall(gm, { a: [5, 5], b: [50, 50], thickness: 12, position: 'left', height: 2.5, label: 'railing', ...switches }) assert.equal(a.ok, true, JSON.stringify(switches)) const frame = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === a.id) assert.equal(frame.kind, kind, JSON.stringify(switches)) assert.equal(frame.thickness, 12) assert.equal(frame.position, 'left') assert.equal(frame.height, 2.5) assert.equal(frame.label, 'railing') } // a second GM connection gets the snapshot of every wall, 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, 3, 4, 5, 6, 7, 8]) for (const w of snap.walls) assertTypedWall(w) 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] })).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] }) 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] })).id, 5) gm.ws.close(); gm2.ws.close() }) }) test('wall.update: patches only what it names; open and kind are unknown fields', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [100, 100], b: [200, 100], door: true }) 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], thickness: 30, position: 'right', height: 4, label: 'deep', 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.equal(updated.thickness, 30) assert.equal(updated.position, 'right') assert.equal(updated.height, 4) assert.equal(updated.label, 'deep') assert.deepEqual(updated.flags, { kind: 'thin' }) assert.equal(updated.open, true, 'wall.update does not touch the open state') assert.equal(updated.kind, 'door') // `open` in an update is an unknown field: it neither opens nor closes the door const framesBefore = gm.ws.messages.filter(m => m.t === 'wall' && m.id === id).length assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, open: false })).code, 'invalid', 'an update naming only the ignored open has no patch field') await sleep(50) assert.equal(gm.ws.messages.filter(m => m.t === 'wall' && m.id === id).length, framesBefore, 'the ignored open produced no frame') // a partial patch keeps the other fields; an explicit null clears height and label 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]) assert.equal(partial.thickness, 30) assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, height: null, label: null })).ok, true) const cleared = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.height === null) assert.equal(cleared.label, null) // clearing the door switch closes and de-secrets it (a non-door stores no door settings) assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, door: false })).ok, true) const asWall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.door === false) assert.equal(asWall.open, false) assert.equal(asWall.secret, false) assert.equal(asWall.locked, false) assert.equal(asWall.kind, 'wall') 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.update {door:true} on a see-through fixed window yields a closed opening window', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [100, 100], b: [200, 100], blocks_sight: false, blocks_movement: true }) const fixed = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id) assert.equal(fixed.kind, 'window') assert.equal(fixed.door, false) assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, door: true })).ok, true) const opening = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.door === true) assert.equal(opening.kind, 'window') assert.equal(opening.open, false, 'the new door is closed, never silently open') assert.equal(opening.blocks_sight, false) assert.equal(opening.blocks_movement, true) 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] }) const { id: doorId } = await addWall(gm, { a: [0, 100], b: [100, 100], door: true }) 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 bad open flags 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: geometry, switch shapes and impossible rows are refused', 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] }, // a == b { a: [10, 10] }, // no b { b: [10, 10] }, // no a { a: [-1, 10], b: [20, 20] }, // out of bounds { a: [10, 10], b: [4097, 20] }, { a: [10], b: [20, 20] }, // not a point { a: [10, 10, 10], b: [20, 20] }, { a: ['10', 10], b: [20, 20] }, { a: [10, 10], b: [20, 20], thickness: -1 }, // ranges (5.10, 5.7) { a: [10, 10], b: [20, 20], thickness: 257 }, { a: [10, 10], b: [20, 20], thickness: 'thick' }, { a: [10, 10], b: [20, 20], position: 'middle' }, { a: [10, 10], b: [20, 20], height: 0 }, { a: [10, 10], b: [20, 20], height: 101 }, { a: [10, 10], b: [20, 20], height: 'tall' }, { a: [10, 10], b: [20, 20], blocks_sight: 'yes' }, // switches are booleans { a: [10, 10], b: [20, 20], open: 'yes' }, // open is a boolean on a create { a: [10, 10], b: [20, 20], sight_dir: 'up' }, { a: [10, 10], b: [20, 20], sight_dir: 'left', blocks_sight: false }, // direction needs sight { a: [10, 10], b: [20, 20], door: true, secret: true, blocks_sight: false }, { a: [10, 10], b: [20, 20], flags: [1, 2] }, // flags is not an object { a: [10, 10], b: [20, 20], flags: 7 }, { a: [10, 10], b: [20, 20], label: 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] })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'wall.add', scene: scene + 1, a: [0, 0], b: [1, 1] })).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, ignored-only fields and bad values', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [100, 100], b: [200, 100] }) 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'], // output-only, so no patch field [{ open: true }, 'invalid'], // door.set owns open, so no patch field [{ flags: 'no' }, 'invalid'], [{ thickness: -1 }, 'invalid'], [{ position: 'up' }, 'invalid'], [{ height: -2 }, 'invalid'], [{ blocks_movement: 0 }, 'invalid'], [{ a: [5, 5], b: [5, 5] }, 'invalid'], // degenerate [{ b: [9999, 5] }, 'invalid'], // out of bounds [{ door: true, secret: true, blocks_sight: false }, 'invalid'], [{ 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], door: true, height: 3.25 }) 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`) } // no wall field name may ride a player frame (DESIGN 3.1, 5.8; the leak checker bans them all) const banned = ['thickness', 'position', 'height', 'blocks_sight', 'blocks_movement', 'sight_dir', 'player_marker', 'door', 'secret', 'locked', 'label'] for (const m of p.ws.messages) { assert.ok(!JSON.stringify(m).includes('1234.5'), `wall coordinates leaked: ${JSON.stringify(m)}`) for (const key of banned) { const deepest = (node) => { if (node === null || typeof node !== 'object') return false if (Array.isArray(node)) return node.some(deepest) return Object.keys(node).some(k => k === key || deepest(node[k])) } assert.equal(deepest(m), false, `wall field ${key} leaked: ${JSON.stringify(m)}`) } } // DESIGN 6.5 step 6: every wall intent is G-only, whatever the wall's settings for (const intent of [{ t: 'wall.add', scene: gm.scene, a: [0, 0], b: [1, 1] }, { 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: the typed 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], flags: { side: 'north' }, thickness: 20, position: 'left', height: 3 }) const w2 = await addWall(gm, { a: [100, 100], b: [400, 100], door: true }) const w3 = await addWall(gm, { a: [0, 500], b: [0, 900], blocks_sight: false }) 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], label: 'north' }) 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], thickness: 20, position: 'left', height: 3, kind: 'wall', blocks_sight: true, blocks_movement: true, sight_dir: 'both', player_marker: false, door: false, open: false, secret: false, locked: false, label: 'north', flags: { side: 'north' } }, { id: 2, a: [100, 100], b: [400, 100], thickness: 0, position: 'center', height: null, kind: 'door', blocks_sight: true, blocks_movement: true, sight_dir: 'both', player_marker: false, door: true, open: true, secret: false, locked: false, label: null, 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] })).id, 4) gm.ws.close() } finally { await stopServer(s) } } finally { fs.rmSync(stateDir, { recursive: true, force: true }) } })