// 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, waitForLog } 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('presets expand server-side to switches only; preset xor raw switches (#55)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) // The four 5.8 presets: each sets only switches, and the derived kind follows the expansion. const cases = [ ['wall', { blocks_sight: true, blocks_movement: true, sight_dir: 'both', player_marker: false, door: false, open: false, secret: false, locked: false }, 'wall'], ['door', { blocks_sight: true, blocks_movement: true, sight_dir: 'both', player_marker: false, door: true, open: false, secret: false, locked: false }, 'door'], ['window', { blocks_sight: false, blocks_movement: true, sight_dir: 'both', player_marker: true, door: true, open: false, secret: false, locked: false }, 'window'], ['one_way', { blocks_sight: true, blocks_movement: true, sight_dir: 'left', player_marker: false, door: false, open: false, secret: false, locked: false }, 'one_way'], ] for (const [preset, switches, kind] of cases) { const ack = await addWall(gm, { a: [10, 10], b: [300, 10], preset }) assert.equal(ack.ok, true, preset) const wall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === ack.id) for (const [key, value] of Object.entries(switches)) { assert.equal(wall[key], value, `${preset}: ${key}`) } assert.equal(wall.kind, kind, preset) } // a preset never implies or resets thickness, position or height const thick = await addWall(gm, { a: [10, 20], b: [300, 20], preset: 'door', thickness: 20, position: 'left', height: 3.5, label: 'west door' }) assert.equal(thick.ok, true) const thickWall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === thick.id) assert.equal(thickWall.thickness, 20) assert.equal(thickWall.position, 'left') assert.equal(thickWall.height, 3.5) assert.equal(thickWall.label, 'west door') // preset or raw switches, never both (open counts as a raw switch) const both = [ { preset: 'door', blocks_sight: false }, { preset: 'wall', player_marker: true }, { preset: 'door', open: true }, { preset: 'door', open: false }, { preset: 'window', sight_dir: 'left' }, ] for (const fields of both) { const ack = await addWall(gm, { a: [1, 1], b: [2, 2], ...fields }) assert.equal(ack.ok, false, JSON.stringify(fields)) assert.equal(ack.code, 'invalid', JSON.stringify(fields)) } assert.equal((await addWall(gm, { a: [1, 1], b: [2, 2], preset: 'terrain' })).code, 'invalid', 'an unknown preset name is invalid') // a raw create starts from the `wall` preset's switch defaults const raw = await addWall(gm, { a: [10, 40], b: [300, 40], blocks_sight: false }) const rawWall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === raw.id) assert.equal(rawWall.kind, 'window') assert.equal(rawWall.door, false) assert.equal(rawWall.player_marker, false, 'only the window preset turns the marker on') assert.equal(rawWall.sight_dir, 'both') assert.equal(rawWall.blocks_movement, true) gm.ws.close() }) }) test('wall.update presets replace the whole switch set and raw patches only named switches (#55)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [10, 10], b: [300, 10], thickness: 16, position: 'right', height: 2 }) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id) // a raw patch names one switch and leaves the rest alone assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, blocks_sight: false })).ok, true) const rawPatched = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.blocks_sight === false) assert.equal(rawPatched.blocks_movement, true) assert.equal(rawPatched.door, false) assert.equal(rawPatched.kind, 'window') // a preset replaces the whole switch set but keeps geometry assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, preset: 'door' })).ok, true) const asDoor = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.door === true) assert.equal(asDoor.blocks_sight, true) assert.equal(asDoor.blocks_movement, true) assert.equal(asDoor.player_marker, false) assert.equal(asDoor.open, false) assert.equal(asDoor.kind, 'door') assert.equal(asDoor.thickness, 16, 'a preset never resets geometry') assert.equal(asDoor.position, 'right') assert.equal(asDoor.height, 2) // the window preset turns its marker on and is a closed opening window assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, preset: 'window' })).ok, true) const asWindow = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.blocks_sight === false && m.player_marker === true) assert.equal(asWindow.door, true) assert.equal(asWindow.player_marker, true) assert.equal(asWindow.kind, 'window') // preset xor raw switches in an update too for (const fields of [{ preset: 'wall', door: true }, { preset: 'one_way', blocks_sight: false }, { preset: 'door', open: true }, { preset: 'door', open: false }]) { const ack = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, ...fields }) assert.equal(ack.ok, false, JSON.stringify(fields)) assert.equal(ack.code, 'invalid', JSON.stringify(fields)) } // a fieldless update is invalid whatever the id, before the lookup (6.1, 6.5 steps 7 then 8) assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: 99 })).code, 'invalid') gm.ws.close() }) }) test('window: the opening and fixed forms author, echo and derive kind "window" (#57)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [10, 10], b: [300, 10], preset: 'window' }) const opening = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id) assert.equal(opening.kind, 'window') assert.equal(opening.blocks_sight, false) assert.equal(opening.blocks_movement, true) assert.equal(opening.door, true, 'the preset form can be opened') assert.equal(opening.open, false) assert.equal(opening.player_marker, true, 'the window preset carries the marker') // The fixed form is the same switches with the door switch off; it still derives `window`. assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, door: false })).ok, true) const fixed = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.door === false) assert.equal(fixed.kind, 'window') assert.equal(fixed.blocks_sight, false) assert.equal(fixed.blocks_movement, true) assert.equal(fixed.open, false) assert.equal(fixed.player_marker, true, 'turning the door off keeps the marker') // A fixed window has no open state, so `door.set` refuses both directions. for (const open of [true, false]) { const ack = await gm.ws.request({ t: 'door.set', scene: gm.scene, id, open }) assert.equal(ack.ok, false, String(open)) assert.equal(ack.code, 'invalid') } // The obvious conversion, `door:true`, yields a closed opening window. `open` in a wall update // is an unknown field and is ignored (it belongs to door.set); the door patch still applies. assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, door: true, open: true })).ok, true) const reopened = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.door === true) assert.equal(reopened.kind, 'window') assert.equal(reopened.open, false, 'open is not a wall-update patch field') // An update naming only `open` has no patch field at all and is invalid. assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, open: true })).code, 'invalid') gm.ws.close() }) }) test('switch-set invariants: door-only settings need a door, and open excludes secret/locked (#55)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const invalid = [ { open: true }, // open without door { secret: true }, // secret without door { locked: true }, // locked without door { open: true, secret: true }, { open: true, locked: true, door: true }, // an open door is never locked { door: true, secret: true, open: true }, { door: true, secret: true, sight_dir: 'left' }, // a secret door is two-way { door: true, secret: true, blocks_sight: false }, // a secret door is opaque { door: true, secret: true, blocks_movement: false }, { door: true, locked: true, open: true }, ] for (const fields of invalid) { const ack = await addWall(gm, { a: [10, 10], b: [20, 20], ...fields }) assert.equal(ack.ok, false, JSON.stringify(fields)) assert.equal(ack.code, 'invalid', JSON.stringify(fields)) } // valid door settings: a closed secret+locked door is allowed; so is a locked window const secret = await addWall(gm, { a: [40, 40], b: [80, 40], door: true, secret: true, locked: true }) assert.equal(secret.ok, true) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === secret.id) const windowLocked = await addWall(gm, { a: [40, 60], b: [80, 60], preset: 'window', locked: true }) assert.equal(windowLocked.ok, false, 'preset xor raw: locked alongside window is refused') const windowLockedRaw = await addWall(gm, { a: [40, 60], b: [80, 60], blocks_sight: false, door: true, locked: true }) assert.equal(windowLockedRaw.ok, true) const lockedWindow = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === windowLockedRaw.id) assert.equal(lockedWindow.kind, 'window') assert.equal(lockedWindow.locked, true) // raw switches on a non-door are refused, not silently dropped const plain = await addWall(gm, { a: [100, 100], b: [200, 100] }) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === plain.id) for (const fields of [{ secret: true }, { locked: true }]) { const ack = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: plain.id, ...fields }) assert.equal(ack.ok, false, JSON.stringify(fields)) assert.equal(ack.code, 'invalid', JSON.stringify(fields)) } // clearing door closes and de-secrets; naming a true door-only switch with it is invalid assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: secret.id, door: false })).ok, true) const cleared = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === secret.id && m.door === false) assert.equal(cleared.open, false) assert.equal(cleared.secret, false) assert.equal(cleared.locked, false) assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: secret.id, door: true, secret: true, blocks_sight: false })).code, 'invalid') gm.ws.close() }) }) test('wall label: set, change, clear and the 6.5 text rule (#55)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const { id } = await addWall(gm, { a: [10, 10], b: [200, 10], label: 'railing' }) const added = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id) assert.equal(added.label, 'railing') assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, label: "the king's window" })).ok, true) const changed = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.label === "the king's window") assert.equal(changed.label, "the king's window") // one code point and 64 code points are legal; unicode counts code points, not bytes const one = await addWall(gm, { a: [10, 20], b: [200, 20], label: 'x' }) assert.equal(one.ok, true) const exactly64 = await addWall(gm, { a: [10, 30], b: [200, 30], label: 'é'.repeat(64) }) assert.equal(exactly64.ok, true) await wallEffect(gm.ws, m => m.t === 'wall' && m.id === exactly64.id) const invalid = [ '', // 0 code points 'x'.repeat(65), // 65 code points ' railing', // untrimmed 'railing ', // untrimmed 'rail\ting', // control character 'rail\u007fing', // DEL is a control character ' ', // whitespace is not a label 42, // not a string true, ] for (const label of invalid) { const ack = await addWall(gm, { a: [10, 40], b: [200, 40], label }) assert.equal(ack.ok, false, JSON.stringify(label)) assert.equal(ack.code, 'invalid', JSON.stringify(label)) const upd = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, label }) assert.equal(upd.ok, false, JSON.stringify(label)) assert.equal(upd.code, 'invalid', JSON.stringify(label)) } // a refused update left the stored label alone const stillChanged = gm.ws.messages.filter(m => m.t === 'wall' && m.id === id).pop() assert.equal(stillChanged.label, "the king's window") // null clears it, and the clear survives a fresh snapshot assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, label: null })).ok, true) const cleared = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.label === null) assert.equal(cleared.label, null) const gm2 = await openAs(s.origin, gmKey) await gm2.ws.waitText('hello') const snap = await gm2.ws.waitText('walls') assert.equal(snap.walls.find(w => w.id === id).label, null) gm.ws.close(); gm2.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 }) } }) // #35: wall `flags` is opaque but bounded: 8 KB serialized, 64 keys per object, depth 8. function objectWithKeys(n) { const o = {} for (let i = 0; i < n; i++) o[`k${i}`] = i return o } function nestedObjects(depth) { let o = {} const root = o for (let i = 1; i < depth; i++) { o.a = {}; o = o.a } return root } const atByteCap = () => ({ blob: 'x'.repeat(8192 - 11) }) const overByteCap = () => ({ blob: 'x'.repeat(8192 - 10) }) test('wall.flags: size, key-count and depth caps, boundaries included (#35)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) // Exactly 8 KB is accepted and echoed; one byte over is `invalid` and stores nothing. const ok = await addWall(gm, { a: [10, 10], b: [100, 10], flags: atByteCap() }) assert.equal(ok.ok, true, JSON.stringify(ok)) const frame = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === ok.id) assert.equal(JSON.stringify(frame.flags).length, 8192) const before = gm.ws.messages.filter(m => m.t === 'wall').length const over = await addWall(gm, { a: [10, 20], b: [100, 20], flags: overByteCap() }) assert.equal(over.code, 'invalid') await sleep(80) assert.equal(gm.ws.messages.filter(m => m.t === 'wall').length, before, 'a refused add emits no wall frame') const refusedLog = await waitForLog(s, /opaque_refused field=flags limit=size/) assert.match(refusedLog, /opaque_refused field=flags limit=size/) assert.equal(refusedLog.includes('xxxxxxxx'), false, 'the refused value is never logged') // The refused add consumes no wall id: the next wall follows the accepted one densely. const next = await addWall(gm, { a: [10, 30], b: [100, 30] }) assert.equal(next.id, ok.id + 1) // 64 keys per object accepted, 65 refused; the cap is per object, not per value. assert.equal((await addWall(gm, { a: [5, 5], b: [60, 5], flags: objectWithKeys(64) })).ok, true) assert.equal((await addWall(gm, { a: [5, 15], b: [60, 15], flags: objectWithKeys(65) })).code, 'invalid') const nestedBase = objectWithKeys(63) nestedBase.inner = objectWithKeys(64) assert.equal((await addWall(gm, { a: [5, 25], b: [60, 25], flags: nestedBase })).ok, true) nestedBase.inner = objectWithKeys(65) assert.equal((await addWall(gm, { a: [5, 35], b: [60, 35], flags: nestedBase })).code, 'invalid') // Depth 8 accepted, 9 refused. assert.equal((await addWall(gm, { a: [5, 45], b: [60, 45], flags: nestedObjects(8) })).ok, true) assert.equal((await addWall(gm, { a: [5, 55], b: [60, 55], flags: nestedObjects(9) })).code, 'invalid') // A refused update leaves the previous flags intact. const keep = await addWall(gm, { a: [200, 200], b: [300, 200], flags: { note: 'keep' } }) const bad = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: keep.id, flags: overByteCap() }) assert.equal(bad.code, 'invalid') const good = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: keep.id, thickness: 5 }) assert.equal(good.ok, true) const after = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === keep.id) assert.deepEqual(after.flags, { note: 'keep' }) gm.ws.close() }) })