Files
FFF_vtt_frozen/tools/scene.test.mjs
T
SirStone 100df38230 test: wait for the async log line instead of racing it
Under the fan-out a test that greps server.log right after an ack can lose:
the logger writes the line after the ack. waitForLog polls to a deadline and
the assertions are unchanged (the line must still appear; absence checks still
run on the same snapshot). Fixes the one observed assertion failure
(fault.test.mjs's intent_failed line) and the same latent race in the
opaque_refused and startup-line checks.
2026-11-21 21:42:34 +01:00

276 lines
14 KiB
JavaScript

// M3 slice 10 (#25): `scene.set`, the last catalogue intent (DESIGN 6.1). One GM edit of the
// settable scene fields is a hello-equivalent reset of every connection: `hello` (new scene block)
// then the full 6.3 resync, including the unchanged tokens the client just reset. A
// `token_radius_default` change resizes the tokens that use the default through the 5.6 rules
// (grow pushes or squeezes; a token with its own radius does not move).
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, waitForLog } 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 }
}
async function namedPlayer(origin, gm, name) {
const player = await createPlayer(gm.ws)
await namePlayer(origin, player.key, name)
return player
}
// A frame that arrives after `mark` (the message index captured before the mutation).
function after(client, mark, t) {
return client.waitFor((m, i) => i >= mark && m.t === t)
}
test('scene.set edits the scene and resets every connection with hello plus a full resync', async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
const alice = await namedPlayer(s.origin, gm, 'Alice')
const tok = 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 } })
assert.equal(tok.ok, true, JSON.stringify(tok))
const a = await openAs(s.origin, alice.key)
await a.ws.waitText('hello')
await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id)
const gmMark = gm.ws.messages.length
const aMark = a.ws.messages.length
const ack = await gm.ws.request({ t: 'scene.set', scene: gm.sceneId, name: 'Cave',
grid_size: 64, vision_radius_default: 500,
token_radius_default: 25, extra: { mood: 'dark' } })
assert.equal(ack.ok, true, JSON.stringify(ack))
// The GM: a new hello with the edited scene block (and the GM-only `extra`), then walls and the
// unchanged token again (the hello reset the server's diff base too).
const gmHello = await after(gm.ws, gmMark, 'hello')
assert.equal(gmHello.scene.name, 'Cave')
assert.equal(gmHello.scene.grid_size, 64)
assert.equal(gmHello.scene.vision_radius_default, 500)
assert.equal(gmHello.scene.token_radius_default, 25)
assert.equal(gmHello.scene.tile_size, 32, 'tile_size is not settable on the wire')
assert.deepEqual(gmHello.extra, { mood: 'dark' })
await after(gm.ws, gmMark, 'walls')
await after(gm.ws, gmMark, 'roster')
const gmTok = await after(gm.ws, gmMark, 'token')
assert.equal(gmTok.id, tok.id)
// The player: the same hello (no `extra` for a player) and the token resent after the reset.
const aHello = await after(a.ws, aMark, 'hello')
assert.equal(aHello.scene.name, 'Cave')
assert.equal(aHello.scene.token_radius_default, 25)
assert.equal('extra' in aHello, false, '`extra` is GM-only (6.1 hello)')
const aTok = await after(a.ws, aMark, 'token')
assert.equal(aTok.id, tok.id)
assert.equal(aTok.radius, 25, 'a default-radius token follows the scene default')
// A patch that changes no token at all still re-sends it: `hello` is a client reset, so the
// server's diff base resets with it (a resync that skipped unchanged tokens would empty the
// client).
const mark2 = gm.ws.messages.length
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
name: 'Cave II' })).ok, true)
await after(gm.ws, mark2, 'hello')
assert.equal((await after(gm.ws, mark2, 'token')).id, tok.id)
a.ws.close(); gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
test('scene.set validation: not-found, invalid shapes, one field required, unknown fields ignored',
async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
const bad = [
[{ scene: 99, name: 'x' }, 'not-found'],
[{ scene: '1', name: 'x' }, 'invalid'],
[{ scene: 1 }, 'invalid'], // no patch field
[{ scene: 1, w: 8000 }, 'invalid'], // not settable, so no field at all
[{ scene: 1, tile_size: 256 }, 'invalid'],
[{ scene: 1, art_version: 9 }, 'invalid'],
[{ scene: 1, name: '' }, 'invalid'],
[{ scene: 1, name: ' padded' }, 'invalid'],
[{ scene: 1, name: 'x'.repeat(65) }, 'invalid'],
[{ scene: 1, name: null }, 'invalid'],
[{ scene: 1, grid_size: -1 }, 'invalid'],
[{ scene: 1, grid_size: '60' }, 'invalid'],
[{ scene: 1, vision_radius_default: 0 }, 'invalid'],
[{ scene: 1, token_radius_default: 0 }, 'invalid'],
[{ scene: 1, token_radius_default: -10 }, 'invalid'],
[{ scene: 1, extra: [] }, 'invalid'],
[{ scene: 1, extra: null }, 'invalid'],
]
for (const [payload, code] of bad) {
const reply = await gm.ws.request({ t: 'scene.set', ...payload })
assert.equal(reply.ok, false, `${JSON.stringify(payload)} must be refused`)
assert.equal(reply.code, code, JSON.stringify(payload))
}
// An unknown field next to a real one is ignored (6.2), not a second error.
const mark = gm.ws.messages.length
const ok = await gm.ws.request({ t: 'scene.set', scene: 1, name: 'Cave', w: 9999,
tile_size: 256, art_version: 7 })
assert.equal(ok.ok, true, JSON.stringify(ok))
const hello = await after(gm.ws, mark, 'hello')
assert.equal(hello.scene.name, 'Cave')
assert.equal(hello.scene.w, 4096)
assert.equal(hello.scene.tile_size, 32)
assert.equal(hello.scene.art_version, 1)
// A plain player may not edit the scene (6.1, 6.5 step 6).
const alice = await namedPlayer(s.origin, gm, 'Alice')
const a = await openAs(s.origin, alice.key)
await a.ws.waitText('hello')
assert.equal((await a.ws.request({ t: 'scene.set', scene: 1, name: 'nope' })).code,
'not-allowed')
a.ws.close(); gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
test('token_radius_default: a grow pushes default-radius tokens, an own radius never moves',
async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
// A wall 40 px above the token: clear at radius 20, overlapping at 50.
assert.equal((await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [0, 440],
b: [1000, 440] })).ok, true)
const def = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [400, 400], descriptive_name: 'a goblin',
actual_name: 'Grib' })
assert.equal(def.ok, true, JSON.stringify(def))
const own = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [2000, 2000], descriptive_name: 'a big troll',
actual_name: 'Grum', radius: 30 })
assert.equal(own.ok, true, JSON.stringify(own))
const ownBefore = await gm.ws.waitFor(m => m.t === 'token' && m.id === own.id)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
token_radius_default: 50 })).ok, true)
const pushed = await gm.ws.waitFor(m => m.t === 'token' && m.id === def.id && m.radius === 50)
// 5.6 grow push: away from the wall by exactly the distance that ends at r + eps.
assert.ok(pushed.pos[1] < 400, `expected a push up, got ${JSON.stringify(pushed.pos)}`)
assert.ok(Math.abs(440 - pushed.pos[1] - 50) < 1,
`the circle ends clear of the wall: ${JSON.stringify(pushed.pos)}`)
assert.deepEqual(pushed.statuses, [], 'a successful push leaves no status')
await sleep(50)
const ownAfter = gm.ws.messages.filter(m => m.t === 'token' && m.id === own.id).pop()
assert.equal(ownAfter.radius, 30, 'a token with its own radius is untouched')
assert.deepEqual(ownAfter.pos, ownBefore.pos)
gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
test('token_radius_default: a grow with no room on either side squeezes', async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
// A corridor 60 px wide: a radius-20 token fits centred, a radius-40 token cannot fit at all.
assert.equal((await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [1000, 900],
b: [1000, 1100] })).ok, true)
assert.equal((await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [1060, 900],
b: [1060, 1100] })).ok, true)
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [1030, 1000], descriptive_name: 'a rat',
actual_name: 'Squeak' })
assert.equal(tok.ok, true, JSON.stringify(tok))
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
token_radius_default: 40 })).ok, true)
const frame = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.radius === 40)
assert.deepEqual(frame.pos, [1030, 1000], 'no valid side: the token stays where it was')
assert.deepEqual(frame.statuses, [{ status: 'squeezed', source: 'walls' }])
gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
// #35: scene `extra` uses the same opaque caps as wall `flags` (8 KB, 64 keys per object, depth 8).
function extraObjectWithKeys(n) {
const o = {}
for (let i = 0; i < n; i++) o[`k${i}`] = i
return o
}
function extraNested(depth) {
let o = {}
const root = o
for (let i = 1; i < depth; i++) { o.a = {}; o = o.a }
return root
}
test('scene.extra: size, key-count and depth caps, boundaries included (#35)', async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
const atCap = { blob: 'x'.repeat(8192 - 11) }
const mark = gm.ws.messages.length
const ack = await gm.ws.request({ t: 'scene.set', scene: gm.sceneId, extra: atCap })
assert.equal(ack.ok, true, JSON.stringify(ack))
const hello = await after(gm.ws, mark, 'hello')
assert.equal(JSON.stringify(hello.extra).length, 8192, 'exactly 8 KB is echoed')
// One byte over, with another declared field: `invalid`, and none of the patch is applied.
const refused = await gm.ws.request({ t: 'scene.set', scene: gm.sceneId, name: 'Nope',
extra: { blob: 'x'.repeat(8192 - 10) } })
assert.equal(refused.code, 'invalid')
const mark2 = gm.ws.messages.length
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
grid_size: 60 })).ok, true)
const hello2 = await after(gm.ws, mark2, 'hello')
assert.equal(hello2.scene.name, 'Scene', 'the refused patch applied no other field')
assert.equal(hello2.scene.grid_size, 60)
assert.match(await waitForLog(s, /opaque_refused field=extra limit=size/),
/opaque_refused field=extra limit=size/)
// Key count: 64 accepted, 65 refused; per object, so a 64-key nested object is fine.
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraObjectWithKeys(64) })).ok, true)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraObjectWithKeys(65) })).code, 'invalid')
const nested = extraObjectWithKeys(63)
nested.inner = extraObjectWithKeys(64)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: nested })).ok, true)
nested.inner = extraObjectWithKeys(65)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: nested })).code, 'invalid')
// Depth: 8 accepted, 9 refused.
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraNested(8) })).ok, true)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraNested(9) })).code, 'invalid')
gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})