fix(client): read walls snapshot with the frame scene (#87)

The snapshot is {t:'walls', scene, walls:[{id, a, b, ...}]} per DESIGN 6.1:
scene lives on the frame, entries carry only the wall record. The client
required scene on each entry and dropped every snapshot wall, so a GM
reload/reconnect showed canvas.dataset.walls==='0' while the server still
blocked player sight.

Pass the enclosing frame's scene into wallViewFromFrame and keep an entry's
own scene as the tolerant fallback. The server already matched DESIGN, so
no wall wire shape changed.

Tests: client reducer from a real snapshot frame, WS pin that a snapshot
entry equals a wall delta record, and e2e reload plus server-restart.
The old e2e missed it because every wall test built walls in-session.
This commit is contained in:
2026-11-21 23:44:37 +01:00
parent 7379d450d5
commit b938e6f256
4 changed files with 144 additions and 8 deletions
+58 -1
View File
@@ -12,7 +12,7 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { createPlayer, namePlayer, sleep } from '../../tools/lib.mjs'
import { openAs } from '../../tools/withserver.mjs'
import { openAs, startServer, stopServer } from '../../tools/withserver.mjs'
import { writeTileGrid } from '../../tools/tilefixture.mjs'
import {
launchBrowser,
@@ -290,6 +290,63 @@ test('GM wall editor: presets, label, door, secret/locked, advanced, undo, drag
})
})
test('GM walls survive a reload and a server restart: drawn, selectable, door toggles',
{ timeout: 240000 }, async () => {
await withStack(async ({ origin, gmKey, stateDir, server }) => {
const { gm } = await worldWithPlayer(origin, gmKey)
const browser = await launchBrowser()
let restarted = null
try {
const { page, errors } = await gmPage(browser, origin, gmKey)
await drawPreset(page, 'wall', [2112, 1920], [2112, 2144])
await nextWall(gm, message => message.a[0] === 2112 && message.b[1] === 2144)
await drawPreset(page, 'door', [1920, 1920], [1920, 2144])
const door = await nextWall(gm, message => message.a[0] === 1920 && message.door === true)
await waitWalls(page, 2)
async function doorStillWorks(client) {
await waitWalls(page, 2)
await clickWorld(page, [1920, 2025])
await waitForText(page, '#wall-kind', /^door$/)
await page.click('#wall-open')
await nextWall(client, message => message.id === door.id && message.open === true)
await page.click('#wall-open')
await nextWall(client, message => message.id === door.id && message.open === false)
}
// Reload is the regression: the GM reconnects and must get the `walls` snapshot back in a
// shape the client accepts (DESIGN 6.1: scene on the frame, entries without it).
await page.reload()
await waitForVisible(page, '#stage canvas')
await waitForVisible(page, '#gm-walls')
await doorStillWorks(gm)
// Restart variant: same state dir, same port, nginx untouched. The client reconnects by
// itself and the same two walls must come back and stay editable.
restarted = await startServer({
stateDir,
port: server.port,
publicOrigin: server.publicOrigin ?? origin,
})
const gmAfter = await openAs(origin, gmKey)
await gmAfter.ws.waitText('hello')
await gmAfter.ws.waitText('walls')
try {
await waitWalls(page, 2, 60000)
await doorStillWorks(gmAfter)
} finally {
gmAfter.ws.close()
}
assert.deepEqual(errors, [])
await page.context().close()
} finally {
if (restarted) await stopServer(restarted)
await browser.close()
gm.ws.close()
}
})
})
async function fogState(page) {
return page.evaluate(() => {
const canvas = document.querySelector('#stage canvas')
+16 -6
View File
@@ -86,13 +86,22 @@ function flagObject(value: unknown): Record<string, unknown> {
}
/**
* One GM `wall` frame (the same shape inside `walls`) as the editor stores it, or null when the
* frame cannot be that shape. `kind` is the server's derived label; an absent one is derived from
* the switches so an older-shaped frame still shows a label.
* One GM `wall` record as the editor stores it, or null when it cannot be that shape. `kind` is the
* server's derived label; an absent one is derived from the switches so an older-shaped frame still
* shows a label.
*
* DESIGN 6.1 fixes where `scene` lives: a `wall` delta carries it on the frame beside the record,
* while a `walls` entry carries the wall fields only (`{t:"walls", scene, walls:[{id, a, b, ...}]}`).
* `sceneHint` is the enclosing frame's `scene`, so a snapshot entry resolves it the same way a delta
* does. An entry that still carries its own `scene` wins, so older/foreign snapshots stay readable.
*/
export function wallViewFromFrame(frame: Record<string, unknown>): WallView | null {
export function wallViewFromFrame(
frame: Record<string, unknown>,
sceneHint?: unknown,
): WallView | null {
const id = frame['id']
const scene = frame['scene']
const ownScene = frame['scene']
const scene = ownScene === undefined ? sceneHint : ownScene
const a = frame['a']
const b = frame['b']
const thickness = frame['thickness']
@@ -178,11 +187,12 @@ export const slice: Slice<WallsState> = {
if (frame.t === 'walls') {
const list = frame['walls']
if (!Array.isArray(list)) return
const frameScene = frame['scene']
const byId: Record<number, WallView> = {}
const order: number[] = []
for (const entry of list) {
if (!isRecord(entry)) continue
const wall = wallViewFromFrame(entry)
const wall = wallViewFromFrame(entry, frameScene)
if (!wall) continue
byId[wall.id] = wall
order.push(wall.id)
+32 -1
View File
@@ -63,6 +63,12 @@ const wallBody = (id: number, overrides: Record<string, unknown> = {}): Record<s
...overrides,
})
// DESIGN 6.1: a `walls` snapshot entry carries the wall fields only; `scene` is on the frame.
const snapBody = (id: number, overrides: Record<string, unknown> = {}): Record<string, unknown> => {
const { scene: _scene, ...entry } = wallBody(id, overrides)
return entry
}
const run = (...events: ReducerEvent[]): RootState =>
events.reduce((state, event) => reduce(state, event), initialState())
@@ -76,7 +82,7 @@ describe('wall slice (DESIGN 6.1, GM only)', () => {
const state = run(
{ type: 'connecting' },
hello(1),
frame({ t: 'walls', n: 2, scene: 1, walls: [wallBody(1), wallBody(2, { a: [100, 200], b: [100, 400] })] }),
frame({ t: 'walls', n: 2, scene: 1, walls: [snapBody(1), snapBody(2, { a: [100, 200], b: [100, 400] })] }),
)
expect(wallsOf(state).map(wall => wall.id)).toEqual([1, 2])
expect(wallOf(state, 1)?.kind).toBe('wall')
@@ -89,6 +95,31 @@ describe('wall slice (DESIGN 6.1, GM only)', () => {
expect(wallsOf(gone).map(wall => wall.id)).toEqual([1])
})
it('loads a real `walls` snapshot: scene on the frame, entries without scene (6.1)', () => {
// The server's snapshot frame is `{t:'walls', scene, walls:[{id, a, b, ...}]}`; a delta is
// `{t:'wall', scene, id, a, b, ...}`. This is the reload regression: the reducer must read the
// frame's scene for every entry, not require `scene` on the entry itself.
const snapshot = frame({
t: 'walls',
n: 2,
scene: 7,
walls: [snapBody(1), snapBody(2, { a: [100, 200], b: [100, 400] })],
})
const state = run({ type: 'connecting' }, hello(1), snapshot)
expect(wallsOf(state).map(wall => wall.id)).toEqual([1, 2])
expect(wallOf(state, 1)?.scene).toBe(7)
expect(wallOf(state, 2)?.scene).toBe(7)
// An entry that still carries its own scene (an older/foreign snapshot) is tolerated.
const explicit = run(
{ type: 'connecting' },
hello(1),
frame({ t: 'walls', n: 2, scene: 7, walls: [wallBody(3, { scene: 3 })] }),
)
expect(wallOf(explicit, 3)?.scene).toBe(3)
expect(wallOf(explicit, 3)).not.toBeNull()
})
it('keeps selection while the wall exists and clears it on wall_gone or a snapshot without it', () => {
const state = run(
{ type: 'connecting' },
+38
View File
@@ -111,6 +111,44 @@ test('wall.add: the typed record, raw-switch defaults and the derived kind', asy
})
})
test('walls snapshot entries have the same wall-record shape as a wall delta (6.1)', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const plain = await addWall(gm, { a: [10, 10], b: [400, 10], thickness: 8,
position: 'right', height: 2.5, label: 'railing',
flags: { side: 'north' } })
const door = await addWall(gm, { a: [10, 300], b: [400, 300], door: true })
assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: door.id,
open: true })).ok, true)
const deltas = new Map()
deltas.set(plain.id, await wallEffect(gm.ws, m => m.t === 'wall' && m.id === plain.id))
deltas.set(door.id, await wallEffect(gm.ws, m => m.t === 'wall' && m.id === door.id &&
m.open === true))
// A second GM connection is the reload/reconnect: the server sends the `walls` snapshot.
const reload = await openAs(s.origin, gmKey)
await reload.ws.waitText('hello')
const snap = await reload.ws.waitText('walls')
assert.equal(snap.scene, gm.scene, 'the frame carries the scene')
assert.equal(typeof snap.n, 'number', 'the frame carries its own n')
assert.deepEqual(snap.walls.map(w => w.id), [plain.id, door.id])
const wallKeys = [...WALL_FIELDS].sort()
for (const entry of snap.walls) {
// DESIGN 6.1: `scene` sits on the `walls` frame; the entry is exactly the wall record a
// `wall` delta carries beside its frame-level `t`/`scene`/`n`.
assert.ok(!('scene' in entry), `a walls entry carries scene: ${JSON.stringify(entry)}`)
assert.deepEqual(Object.keys(entry).sort(), wallKeys)
const delta = deltas.get(entry.id)
const record = Object.fromEntries(
Object.entries(delta).filter(([key]) => !['t', 'scene', 'n'].includes(key)),
)
assert.deepEqual(entry, record, `snapshot and delta disagree for wall ${entry.id}`)
}
reload.ws.close(); gm.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)