Files
FFF_vtt_frozen/client/e2e/walls.test.mjs
T
SirStone 91a51c706d feat(client): GM wall editor, round-1 fields (#73)
Draw, select, edit and delete walls through the GM panel: the four 5.8
presets, raw switches (preset XOR raw, open excluded from wall.update), a
private label, the one-way arrow + flip (sight_dir names the blind side),
the player-marker switch, door open/close through door.set, secret/locked
with the locked hint and one-click unlock, delete and single-step undo.
No thickness, height or position-flip control (ADR-0012).

The pure rules and payload builders live in src/wallEditor.ts; the GM wall
slice (src/slices/walls.ts) owns the server records, selection and undo;
the render module draws on the gm layer and defines the input precedence:
token drag (#72) > armed wall draw > camera pan (#69), with a click (no
movement) selecting the nearest wall.

Shared core edits: client/src/reducer.ts gains the wall-select and
wall-record-undo events (the only new core events); style.css and the
client README document the panel and rules. Unit tests cover the slice,
presets, derived kind, arrow side, snapping, payload XOR, door intent and
undo builders; the e2e draws every preset, edits the fields, flips the
one-way wall, opens/closes a door, deletes and undoes, proves a token drag
still wins over draw mode, and probes the player's fog pixels, with the
player WS leaking no wall data.

Ref #73
2026-11-21 11:26:45 +01:00

424 lines
18 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 } 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()
}
})
})
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()
}
})
})