tools: wall harness, and the walls frame in the existing groups (#19)

tools/walls.test.mjs: GM snapshot, CRUD acks and effects, ids monotonic and
never reused, the 6.1 validation table, door toggling, "players get no wall
data" (no walls/wall/wall_gone type and no wall coordinate in any player
frame) and a restart that leaves walls, door state and the id counter
identical. protocol.test.mjs/smoke.test.mjs follow the new frame and schema:
hello = 1, walls = 2, the first ack = 3; user_version 3 with the seeded scene.
This commit is contained in:
2026-11-15 08:49:24 +01:00
parent a8f508ef2e
commit d72fc6ed20
3 changed files with 351 additions and 12 deletions
+12 -8
View File
@@ -27,19 +27,22 @@ test('hello is the first frame; n counts server frames per connection, never glo
assert.equal(hello.proto, 1) // M3 ships proto 1 (DESIGN 6.2)
assert.equal(hello.role, 'gm')
assert.deepEqual(hello.me, { id: 1, name: 'GM' })
// the GM wall snapshot follows hello on the same connection (slice 4, DESIGN 6.1 `walls`)
assert.equal((await gm.ws.waitText('walls')).n, 2)
const first = await gm.ws.request({ t: 'conn.ping', ts: 42 })
assert.equal(first.ok, true)
assert.equal(first.ts, 42) // the ack echoes ts
assert.equal(first.n, 2)
assert.equal((await gm.ws.request({ t: 'conn.ping', ts: 43 })).n, 3)
assert.equal(first.n, 3)
assert.equal((await gm.ws.request({ t: 'conn.ping', ts: 43 })).n, 4)
// a second connection of the same player counts on its own (H25: no global counter)
const other = await openWs(s.origin, gm.cookie)
assert.equal((await other.waitText('hello')).n, 1)
assert.equal((await other.request({ t: 'conn.ping', ts: 7 })).n, 2)
assert.equal((await other.waitText('walls')).n, 2)
assert.equal((await other.request({ t: 'conn.ping', ts: 7 })).n, 3)
// ... and the first connection's sequence did not move
assert.equal((await gm.ws.request({ t: 'conn.ping', ts: 44 })).n, 4)
assert.equal((await gm.ws.request({ t: 'conn.ping', ts: 44 })).n, 5)
for (const m of gm.ws.messages) assert.equal(typeof m.n, 'number', JSON.stringify(m))
})
})
@@ -59,7 +62,7 @@ test('every server frame carries a catalogue type (nothing outside the wire cata
await gm.ws.sendText('{')
await sleep(100)
const seen = new Set(gm.ws.messages.map(m => m.t))
assert.deepEqual([...seen].sort(), ['ack', 'error', 'hello'])
assert.deepEqual([...seen].sort(), ['ack', 'error', 'hello', 'walls'])
for (const m of gm.ws.messages) assert.ok(SERVER_TYPES.has(m.t), `off-catalogue type ${m.t}`)
assert.ok(!seen.has('event'), '`event` is reserved and not built (DESIGN 9)')
gm.ws.close()
@@ -95,7 +98,8 @@ test('an unknown type is refused, unknown fields are ignored, and every intent g
assert.equal((await gm.ws.request({ t: '' })).code, 'unknown-type')
assert.equal((await gm.ws.request({ t: 'TOKEN.MOVE', id: 'x' })).code, 'unknown-type')
// a catalogue intent whose slice has not landed still answers exactly once
assert.equal((await gm.ws.request({ t: 'wall.add', scene: 's' })).code, 'unknown-type')
assert.equal((await gm.ws.request({ t: 'token.create', scene: 1 })).code, 'unknown-type')
assert.equal((await gm.ws.request({ t: 'scene.set', scene: 1 })).code, 'unknown-type')
// the reserved FFF hook is refused for everyone and is never ok
const act = await gm.ws.request({ t: 'act.do', actor: 1, action: 'x', args: {} })
assert.equal(act.ok, false)
@@ -277,12 +281,12 @@ test('the server pings the connection and answers a client Ping with a Pong', as
const pong = await raw.waitFor(f => f.opcode === opcodes.pong && f.payload.toString() === 'hello')
assert.ok(pong)
// control frames are not counted by `n`: the next app frame is n = 2
// control frames are not counted by `n`: hello = 1, walls = 2, this ack = 3
raw.sendText({ t: 'conn.ping', reqId: 900, ts: 5 })
const ack = await raw.waitText('ack')
assert.equal(ack.reqId, 900)
assert.equal(ack.ok, true)
assert.equal(ack.n, 2)
assert.equal(ack.n, 3)
raw.close()
})
})
+9 -4
View File
@@ -49,16 +49,21 @@ test('a fresh DB is created and migrated; re-opening applies nothing', async ()
const first = await startServer({ stateDir })
const firstLog = first.log()
await stopServer(first)
assert.match(firstLog, /db_ready .*schema_version=2 applied=2/, firstLog)
assert.match(firstLog, /db_ready .*schema_version=3 applied=3/, firstLog)
assert.ok(fs.existsSync(first.dbPath), 'state.db was not created')
const db = new DatabaseSync(first.dbPath)
try {
assert.equal(Number(db.prepare('PRAGMA user_version').get().user_version), 2)
assert.equal(Number(db.prepare('PRAGMA user_version').get().user_version), 3)
const rows = db.prepare('SELECT version, name FROM schema_migration ORDER BY version').all()
assert.deepEqual(rows.map(r => [Number(r.version), r.name]), [[1, 'meta'], [2, 'auth']])
assert.deepEqual(rows.map(r => [Number(r.version), r.name]),
[[1, 'meta'], [2, 'auth'], [3, 'world']])
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM meta').get().n), 0)
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM player').get().n), 0)
// step 3 seeds the single v1 scene (Q3) with no walls and a counter at 1
assert.deepEqual(db.prepare('SELECT id, next_wall_id FROM scene').all()
.map(r => [Number(r.id), Number(r.next_wall_id)]), [[1, 1]])
assert.equal(Number(db.prepare('SELECT count(*) AS n FROM wall').get().n), 0)
assert.equal(db.prepare('PRAGMA journal_mode').get().journal_mode, 'wal')
} finally {
db.close()
@@ -67,7 +72,7 @@ test('a fresh DB is created and migrated; re-opening applies nothing', async ()
const second = await startServer({ stateDir })
const secondLog = second.log().slice(firstLog.length)
await stopServer(second)
assert.match(secondLog, /db_ready .*schema_version=2 applied=0/, secondLog)
assert.match(secondLog, /db_ready .*schema_version=3 applied=0/, secondLog)
assert.doesNotMatch(secondLog, /applied=[1-9]/)
} finally {
fs.rmSync(stateDir, { recursive: true, force: true })
+330
View File
@@ -0,0 +1,330 @@
// 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 })
}
})