Files
SirStone b938e6f256 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.
2026-11-21 23:44:37 +01:00

481 lines
20 KiB
JavaScript

// T10 (#73, TESTING 10.3): the GM wall editor against the real server through the TLS/HTTP2 nginx.
// The browser's real mouse draws walls; the assertions read the real GM `wall`/`wall_gone` frames,
// the player's real fog pixels and a second real player WebSocket for the leak scan. No fixture, no
// mocked protocol.
//
// Two tests:
// 1. every round-1 field round-trips (four presets, label, marker switch, open/close, secret,
// locked hint + one click, advanced raw switches with the invalid combination shown, delete +
// undo, and token drag still beats draw mode);
// 2. the player's fog changes: a drawn wall blocks, the one-way flip re-blocks the floor, opening
// a door reveals it again, and a marked window sends its marker pixel.
import test from 'node:test'
import assert from 'node:assert/strict'
import { createPlayer, namePlayer, sleep } from '../../tools/lib.mjs'
import { openAs, startServer, stopServer } from '../../tools/withserver.mjs'
import { writeTileGrid } from '../../tools/tilefixture.mjs'
import {
launchBrowser,
newPage,
signIn,
waitForText,
waitForVisible,
withStack,
} from './stack.mjs'
import { luminance, probe } from './pixels.mjs'
const CENTRE = [2048, 2048]
const SCENE = { w: 4096, h: 4096, tile: 32, cell: 32 }
function tileColour(worldX, worldY) {
const x = Math.floor(worldX / SCENE.tile)
const y = Math.floor(worldY / SCENE.tile)
return { r: (x * 37 + y * 11) & 0xff, g: (x * 17 + y * 53) & 0xff, b: (x * 97 + y * 5) & 0xff }
}
const cellCentre = (cx, cy) => [cx * SCENE.cell + 16, cy * SCENE.cell + 16]
// Cell centres chosen off the drawn wall lines (DESIGN 5.8: a wall blocks the ray crossing it).
const PROBES = {
right: cellCentre(67, 64), // [2160,2064]: blocked by the vertical wall at x=2112
left: cellCentre(58, 64), // [1872,2064]: blocked by the closed door at x=1920
below: cellCentre(64, 71), // [2064,2288]: blocked by the one-way wall once its blind side is above
window: [2032, 1920], // the marked window's drawn midpoint
}
function near(actual, expected, tolerance = 12) {
return (
Math.abs(actual.r - expected.r) <= tolerance &&
Math.abs(actual.g - expected.g) <= tolerance &&
Math.abs(actual.b - expected.b) <= tolerance
)
}
async function gmWorld(origin, gmKey) {
const gm = await openAs(origin, gmKey)
const hello = await gm.ws.waitText('hello')
const walls = await gm.ws.waitText('walls')
return { ...gm, hello, scene: hello.scene ?? walls.scene }
}
/** A named player with one sighted token: the wall editor's effects have something to change. */
async function worldWithPlayer(origin, gmKey, { tiles = false, stateDir } = {}) {
const gm = await gmWorld(origin, gmKey)
const scene = gm.scene
if (tiles) {
writeTileGrid(stateDir, {
scene: scene.id,
artVersion: scene.art_version,
size: scene.tile_size,
x0: 40,
x1: 87,
y0: 40,
y1: 87,
})
}
const player = await createPlayer(gm.ws)
await namePlayer(origin, player.key, 'Ann')
const token = await gm.ws.request({
t: 'token.create',
scene: scene.id,
kind: 'pc',
pos: CENTRE,
descriptive_name: 'Ann',
actual_name: 'Ann',
controller: player.id,
flags: { vision: true },
vision_radius: 300,
radius: 20,
})
assert.equal(token.ok, true, JSON.stringify(token))
return { gm, player, scene, token: token.id }
}
async function viewOf(page) {
return page.evaluate(() => {
const canvas = document.querySelector('#stage canvas')
if (!canvas) throw new Error('no canvas')
const rect = canvas.getBoundingClientRect()
const [x, y] = (canvas.dataset.camera ?? '').split(',').map(Number)
return { left: rect.left, top: rect.top, x, y, zoom: Number(canvas.dataset.zoom ?? '1') }
})
}
async function screenOf(page, world) {
const view = await viewOf(page)
return {
x: view.left + world[0] * view.zoom + view.x,
y: view.top + world[1] * view.zoom + view.y,
}
}
async function clickWorld(page, world) {
const point = await screenOf(page, world)
await page.mouse.click(point.x, point.y)
}
async function drawPreset(page, preset, a, b) {
await page.click(`#wall-preset-${preset}`)
await clickWorld(page, a)
await clickWorld(page, b)
}
async function waitWalls(page, count, timeout = 20000) {
await page.waitForFunction(
expected => {
const canvas = document.querySelector('#stage canvas')
return canvas !== null && Number(canvas.dataset.walls ?? '-1') === expected
},
count,
{ timeout },
)
}
async function gmPage(browser, origin, gmKey) {
const { page } = await newPage(browser)
const errors = []
page.on('pageerror', error => errors.push(error.message))
await page.goto(`${origin}/`)
await waitForVisible(page, '#signin')
await signIn(page, gmKey)
await waitForVisible(page, '#stage canvas')
await waitForVisible(page, '#gm-walls')
await waitWalls(page, 0)
return { page, errors }
}
/** A GM-frame wait that returns the wall message, so a test can read the server's record. */
function nextWall(gm, predicate, timeout = 10000) {
return gm.ws.waitFor(message => message.t === 'wall' && predicate(message), timeout)
}
test('GM wall editor: presets, label, door, secret/locked, advanced, undo, drag precedence', { timeout: 240000 }, async () => {
await withStack(async ({ origin, gmKey }) => {
const { gm, player } = await worldWithPlayer(origin, gmKey)
// A second real player connection: the leak scan's independent witness.
const witness = await openAs(origin, player.key)
await witness.ws.waitText('hello')
const browser = await launchBrowser()
try {
const { page, errors } = await gmPage(browser, origin, gmKey)
// --- wall preset + label -------------------------------------------------------------
await drawPreset(page, 'wall', [2112, 1920], [2112, 2144])
const wall = await nextWall(gm, message => message.a[0] === 2112 && message.b[1] === 2144)
assert.equal(wall.kind, 'wall')
await waitWalls(page, 1)
await waitForVisible(page, '#wall-selected')
await page.fill('#wall-label', 'railing')
await page.click('#wall-label-apply')
await nextWall(gm, message => message.id === wall.id && message.label === 'railing')
assert.equal((await page.textContent('#wall-kind'))?.trim(), 'wall')
// --- door: open/close, locked hint and one-click unlock ------------------------------
await drawPreset(page, 'door', [1920, 1920], [1920, 2144])
const door = await nextWall(gm, message => message.a[0] === 1920 && message.door === true)
assert.equal(door.kind, 'door')
await waitWalls(page, 2)
await page.check('#wall-locked')
await nextWall(gm, message => message.id === door.id && message.locked === true)
await page.click('#wall-open')
await waitForVisible(page, '#wall-hint-locked')
assert.match((await page.textContent('#wall-hint-locked')) ?? '', /locked: unlock first/)
assert.equal(door.open, false)
await page.click('#wall-unlock')
await nextWall(gm, message => message.id === door.id && message.locked === false)
await page.click('#wall-open')
await nextWall(gm, message => message.id === door.id && message.open === true)
await page.click('#wall-open')
await nextWall(gm, message => message.id === door.id && message.open === false)
// Secret is a door checkbox that round-trips; it never reaches the player (leak scan below).
await page.check('#wall-secret')
await nextWall(gm, message => message.id === door.id && message.secret === true)
await page.uncheck('#wall-secret')
await nextWall(gm, message => message.id === door.id && message.secret === false)
// --- window and one-way presets ------------------------------------------------------
await drawPreset(page, 'window', [1984, 1920], [2080, 1920])
const win = await nextWall(gm, message => message.a[1] === 1920 && message.a[0] === 1984)
assert.equal(win.kind, 'window')
assert.equal(win.player_marker, true)
await waitWalls(page, 3)
// The marker switch is authorable on any wall; the window preset turns it on.
assert.equal(await page.isChecked('#wall-player-marker'), true)
await page.uncheck('#wall-player-marker')
await nextWall(gm, message => message.id === win.id && message.player_marker === false)
await page.check('#wall-player-marker')
await nextWall(gm, message => message.id === win.id && message.player_marker === true)
// Round 1 has no thickness, height or position-flip control (ADR-0012).
assert.equal(await page.locator('#wall-thickness').count(), 0)
assert.equal(await page.locator('#wall-height').count(), 0)
await drawPreset(page, 'one_way', [1792, 2208], [2176, 2208])
const oneWay = await nextWall(gm, message => message.a[1] === 2208)
assert.equal(oneWay.kind, 'one_way')
assert.equal(oneWay.sight_dir, 'left')
await waitWalls(page, 4)
// --- advanced switches: an impossible raw combination shows the reason --------------
await page.click('#wall-advanced-toggle')
await page.uncheck('#wall-blocks-sight')
await page.selectOption('#wall-sight-dir', 'left')
await page.click('#wall-adv-apply')
await waitForVisible(page, '#wall-error')
assert.match((await page.textContent('#wall-error')) ?? '', /sight direction/)
// The server still holds the valid one-way wall; the client did not send an invalid patch.
assert.equal(oneWay.blocks_sight, true)
// --- delete then single-step undo ----------------------------------------------------
await page.click('#wall-delete')
await gm.ws.waitFor(message => message.t === 'wall_gone' && message.id === oneWay.id)
await waitWalls(page, 3)
await page.click('#wall-undo')
const restored = await nextWall(gm, message => message.a[1] === 2208 && message.id !== oneWay.id)
assert.equal(restored.kind, 'one_way')
assert.deepEqual([restored.a, restored.b], [oneWay.a, oneWay.b])
await waitWalls(page, 4)
// --- input precedence: a token drag wins over an armed draw --------------------------
await page.click('#wall-preset-wall')
const start = await screenOf(page, CENTRE)
const end = await screenOf(page, [2000, 2112])
await page.mouse.move(start.x, start.y)
await page.mouse.down()
for (let step = 1; step <= 6; step += 1) {
await page.mouse.move(
start.x + ((end.x - start.x) * step) / 6,
start.y + ((end.y - start.y) * step) / 6,
)
await sleep(60)
}
await page.mouse.up()
const moved = await gm.ws.waitFor(
message =>
message.t === 'token' &&
Math.abs(message.pos[0] - 2000) <= 3 &&
Math.abs(message.pos[1] - 2112) <= 3,
10000,
)
assert.deepEqual(moved.pos, [2000, 2112])
// Drawing mode never saw the gesture: no fifth wall and the draw is still armed.
await sleep(300)
assert.equal(await page.getAttribute('#stage canvas', 'data-walls'), '4')
assert.equal(await page.getAttribute('#stage canvas', 'data-wall-draw'), 'first')
await page.keyboard.press('Escape')
// --- player traffic: no wall data beyond the marker (3.1) ---------------------------
const bad = witness.ws.messages.filter(message => {
if (['wall', 'walls', 'wall_gone'].includes(message.t)) return true
if (message.t === 'marker') return false
const banned = [
'blocks_sight',
'blocks_movement',
'sight_dir',
'player_marker',
'thickness',
'position',
'secret',
'locked',
'label',
]
return banned.some(key => Object.prototype.hasOwnProperty.call(message, key))
})
assert.deepEqual(bad, [], `player frames carried wall data: ${JSON.stringify(bad.slice(0, 2))}`)
assert.deepEqual(errors, [])
await page.context().close()
} finally {
await browser.close()
witness.ws.close()
gm.ws.close()
}
})
})
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')
if (!canvas) return null
const data = canvas.dataset
return {
tiles: data.tiles ?? '',
markers: (data.fogMarkers ?? '').split(' ').filter(Boolean),
cells: Number(data.fogCells ?? '-1'),
visionBuilds: Number(data.fogVisionBuilds ?? '-1'),
}
})
}
async function waitFog(page, predicate, timeout = 30000) {
const deadline = Date.now() + timeout
let last = null
while (Date.now() < deadline) {
last = await fogState(page)
if (last !== null && predicate(last)) return last
await sleep(50)
}
throw new Error(`fog never reached the expected state: ${JSON.stringify(last)}`)
}
async function probeWorld(page, world) {
const point = await screenOf(page, world)
return probe(page, point.x, point.y)
}
async function waitProbe(page, world, predicate, timeout = 20000) {
const deadline = Date.now() + timeout
let last = null
while (Date.now() < deadline) {
last = await probeWorld(page, world)
if (predicate(last)) return last
await sleep(100)
}
throw new Error(`pixel at ${world.join(',')} never matched: ${JSON.stringify(last)}`)
}
test('GM walls change the player fog: block, one-way flip, door open/close, marker pixel', { timeout: 240000 }, async () => {
await withStack(async ({ origin, gmKey, stateDir }) => {
const { gm, player } = await worldWithPlayer(origin, gmKey, { tiles: true, stateDir })
const witness = await openAs(origin, player.key)
await witness.ws.waitText('hello')
const browser = await launchBrowser()
try {
const playerView = await browser.newContext({
ignoreHTTPSErrors: true,
viewport: { width: 900, height: 900 },
})
const page = await playerView.newPage()
const errors = []
page.on('pageerror', error => errors.push(error.message))
await page.goto(`${origin}/`)
await waitForVisible(page, '#signin')
await signIn(page, player.key)
await waitForVisible(page, '#stage canvas')
await page.waitForFunction(
() => document.querySelector('#stage canvas')?.dataset.tiles === 'ready',
null,
{ timeout: 30000 },
)
await waitFog(page, state => state.cells > 100)
const rightMap = tileColour(...PROBES.right)
const leftMap = tileColour(...PROBES.left)
const belowMap = tileColour(...PROBES.below)
assert.ok(near(await probeWorld(page, PROBES.right), rightMap), 'right floor starts visible')
assert.ok(near(await probeWorld(page, PROBES.left), leftMap), 'left floor starts visible')
assert.ok(near(await probeWorld(page, PROBES.below), belowMap), 'below floor starts visible')
const gmView = await gmPage(browser, origin, gmKey)
// A drawn solid wall blocks the right floor: visible -> explored-dim without a reload.
await drawPreset(gmView.page, 'wall', [2112, 1920], [2112, 2144])
await nextWall(gm, message => message.a[0] === 2112)
const dimRight = await waitProbe(page, PROBES.right, colour => luminance(colour) < luminance(rightMap) * 0.6)
assert.ok(luminance(dimRight) < luminance(rightMap) * 0.6, 'the wall dimmed the right floor')
// A closed door blocks the left floor.
await drawPreset(gmView.page, 'door', [1920, 1920], [1920, 2144])
const door = await nextWall(gm, message => message.a[0] === 1920 && message.door === true)
const dimLeft = await waitProbe(page, PROBES.left, colour => luminance(colour) < luminance(leftMap) * 0.6)
assert.ok(luminance(dimLeft) < luminance(leftMap) * 0.6, 'the door dimmed the left floor')
// One-way preset: sight_dir "left" names the blind side (below). The player is above, so the
// wall is initially transparent to sight; flipping it to "right" makes the player's side
// blind and dims the floor below.
await drawPreset(gmView.page, 'one_way', [1792, 2208], [2176, 2208])
const oneWay = await nextWall(gm, message => message.a[1] === 2208)
assert.equal(oneWay.sight_dir, 'left')
assert.ok(near(await probeWorld(page, PROBES.below), belowMap), 'the seeing side sees across')
await waitForVisible(gmView.page, '#wall-selected')
await waitForText(gmView.page, '#wall-kind', /^one_way$/)
await gmView.page.click('#wall-flip')
await nextWall(gm, message => message.id === oneWay.id && message.sight_dir === 'right')
const dimBelow = await waitProbe(page, PROBES.below, colour => luminance(colour) < luminance(belowMap) * 0.6)
assert.ok(luminance(dimBelow) < luminance(belowMap) * 0.6, 'the flip moved the blind side to the player')
// Opening the door reveals the left floor; closing it dims it again.
await clickWorld(gmView.page, [1920, 2025])
await waitForText(gmView.page, '#wall-kind', /^door$/)
await gmView.page.click('#wall-open')
await nextWall(gm, message => message.id === door.id && message.open === true)
await waitProbe(page, PROBES.left, colour => near(colour, leftMap))
await gmView.page.click('#wall-open')
await nextWall(gm, message => message.id === door.id && message.open === false)
await waitProbe(page, PROBES.left, colour => luminance(colour) < luminance(leftMap) * 0.6)
// A marked window sends its marker; the player draws it at the wall's drawn midpoint.
await drawPreset(gmView.page, 'window', [1984, 1920], [2080, 1920])
const win = await nextWall(gm, message => message.a[1] === 1920 && message.a[0] === 1984)
assert.equal(win.player_marker, true)
await waitFog(page, state => state.markers.length === 1)
const marker = await probeWorld(page, PROBES.window)
assert.ok(near(marker, { r: 255, g: 204, b: 51 }, 26), `the window marker pixel: ${JSON.stringify(marker)}`)
// The player never received the editor's wall records, even for the secret/locked states.
assert.equal(witness.ws.messages.some(message => ['wall', 'walls', 'wall_gone'].includes(message.t)), false)
assert.deepEqual(errors, [])
await playerView.close()
await gmView.page.context().close()
} finally {
await browser.close()
witness.ws.close()
gm.ws.close()
}
})
})