Files
FFF_vtt_frozen/client/test/walls.test.ts
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

303 lines
12 KiB
TypeScript

// T10 (#73): the wall slice and the wall editor's pure rules are the reducer seam. Server GM frames
// in, the wall view out; presets expand to the 5.8 table; `kind` is derived in the fixed order; the
// one-way arrow points at the blind side; a payload is preset XOR raw, and `open` never rides in a
// `wall.update`. No DOM, no Pixi, no socket.
import { describe, expect, it } from 'vitest'
import { initialState, reduce } from '../src/reducer.ts'
import type { ReducerEvent, RootState } from '../src/reducer.ts'
import { parseServerFrame } from '../src/protocol.ts'
import type { Vec2 } from '../src/protocol.ts'
import { selectedWallId, wallOf, wallsOf } from '../src/slices/walls.ts'
import type { SightDir, WallPreset, WallSwitches } from '../src/wallEditor.ts'
import {
PRESETS,
arrowVector,
buildAddFields,
buildDoorFields,
buildUpdateFields,
deriveKind,
flipSightDir,
presetSwitches,
snapPoint,
undoIntent,
validateLabel,
validateSwitches,
} from '../src/wallEditor.ts'
const frame = (body: Record<string, unknown>): ReducerEvent => {
const parsed = parseServerFrame(JSON.stringify(body))
if (!parsed) throw new Error(`bad test frame ${JSON.stringify(body)}`)
return { type: 'frame', frame: parsed }
}
const hello = (n: number, role: 'gm' | 'player' = 'gm'): ReducerEvent =>
frame({
t: 'hello',
n,
proto: 2,
role,
me: { id: role === 'gm' ? 1 : 2, name: role === 'gm' ? 'GM' : 'Ann' },
...(role === 'player' ? {} : { scene: { id: 1, name: 'S', w: 4096, h: 4096, tile_size: 32, cell: 32, grid_size: 32, vision_radius_default: 300, token_radius_default: 20, art_version: 1 } }),
})
const wallBody = (id: number, overrides: Record<string, unknown> = {}): Record<string, unknown> => ({
id,
scene: 1,
a: [100, 100],
b: [300, 100],
thickness: 0,
position: 'center',
height: null,
kind: 'wall',
blocks_sight: true,
blocks_movement: true,
sight_dir: 'both',
player_marker: false,
door: false,
open: false,
secret: false,
locked: false,
label: null,
flags: {},
...overrides,
})
const run = (...events: ReducerEvent[]): RootState =>
events.reduce((state, event) => reduce(state, event), initialState())
const switches = (overrides: Partial<WallSwitches> = {}): WallSwitches => ({
...presetSwitches('wall'),
...overrides,
})
describe('wall slice (DESIGN 6.1, GM only)', () => {
it('loads the walls snapshot, upserts a wall and removes one', () => {
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] })] }),
)
expect(wallsOf(state).map(wall => wall.id)).toEqual([1, 2])
expect(wallOf(state, 1)?.kind).toBe('wall')
const moved = reduce(state, frame({ t: 'wall', n: 3, ...wallBody(1, { kind: 'door', door: true, a: [120, 100] }) }))
expect(wallsOf(moved)).toHaveLength(2)
expect(wallOf(moved, 1)).toMatchObject({ kind: 'door', a: [120, 100] })
const gone = reduce(moved, frame({ t: 'wall_gone', n: 4, scene: 1, id: 2 }))
expect(wallsOf(gone).map(wall => wall.id)).toEqual([1])
})
it('keeps selection while the wall exists and clears it on wall_gone or a snapshot without it', () => {
const state = run(
{ type: 'connecting' },
hello(1),
frame({ t: 'wall', n: 2, ...wallBody(7) }),
{ type: 'wall-select', id: 7 },
)
expect(selectedWallId(state)).toBe(7)
expect(selectedWallId(reduce(state, frame({ t: 'wall_gone', n: 3, id: 7 })))).toBeNull()
expect(
selectedWallId(reduce(state, frame({ t: 'walls', n: 4, scene: 1, walls: [] }))),
).toBeNull()
})
it('resets on hello and ignores wall frames on a player-shaped session (3.1)', () => {
const gm = run({ type: 'connecting' }, hello(1), frame({ t: 'wall', n: 2, ...wallBody(1) }))
expect(wallsOf(reduce(gm, hello(3)))).toEqual([])
const player = run({ type: 'connecting' }, hello(1, 'player'), frame({ t: 'wall', n: 2, ...wallBody(1) }))
expect(wallsOf(player)).toEqual([])
})
it('derives `kind` when the server frame omits it', () => {
const state = run(
{ type: 'connecting' },
hello(1),
frame({
t: 'wall',
n: 2,
...wallBody(1, { kind: undefined, blocks_sight: false, blocks_movement: true, door: true }),
}),
)
expect(wallOf(state, 1)?.kind).toBe('window')
})
})
describe('presets, derived kind and the arrow (5.8)', () => {
it('expands the four presets exactly as the 5.8 table says', () => {
expect(PRESETS).toEqual(['wall', 'door', 'window', 'one_way'])
expect(presetSwitches('wall')).toEqual({
blocksSight: true,
blocksMovement: true,
sightDir: 'both',
playerMarker: false,
door: false,
open: false,
secret: false,
locked: false,
})
expect(presetSwitches('door')).toMatchObject({ door: true, open: false, playerMarker: false })
expect(presetSwitches('window')).toMatchObject({
blocksSight: false,
blocksMovement: true,
door: true,
playerMarker: true,
})
expect(presetSwitches('one_way')).toMatchObject({ sightDir: 'left', blocksSight: true, door: false })
})
it('derives kind in the fixed order: window, one_way, door, wall, else custom', () => {
expect(deriveKind(switches({ blocksSight: false }))).toBe('window')
expect(deriveKind(switches({ sightDir: 'left' }))).toBe('one_way')
expect(deriveKind(switches({ door: true }))).toBe('door')
expect(deriveKind(switches())).toBe('wall')
expect(deriveKind(switches({ blocksMovement: false }))).toBe('custom')
// A one-way door is labelled one_way, and secret/locked never change the label.
expect(deriveKind(switches({ door: true, sightDir: 'right' }))).toBe('one_way')
expect(deriveKind(switches({ door: true, secret: true, locked: true }))).toBe('door')
})
it('points the arrow at the blind side named by sight_dir, and flips left/right only', () => {
const a: Vec2 = [0, 0]
const b: Vec2 = [100, 0]
// a -> b runs +x; left is (-dy, dx) = +y, so sight_dir:"left" means the +y side is blind.
expect(arrowVector(a, b, 'left')).toEqual([0, 1])
expect(arrowVector(a, b, 'right')).toEqual([0, -1])
expect(arrowVector(a, b, 'both')).toBeNull()
expect(flipSightDir('left')).toBe('right')
expect(flipSightDir('right')).toBe('left')
expect(flipSightDir('both')).toBe('both')
})
})
describe('snap while drawing (story 45)', () => {
it('prefers an endpoint inside the threshold, else rounds to the grid, and does nothing when off', () => {
const endpoints = [([100, 100] as Vec2), ([300, 200] as Vec2)]
expect(snapPoint([105, 97], { enabled: true, gridSize: 32, endpoints, threshold: 12 })).toEqual([100, 100])
expect(snapPoint([143, 154], { enabled: true, gridSize: 32, endpoints, threshold: 12 })).toEqual([128, 160])
expect(snapPoint([143, 154], { enabled: false, gridSize: 32, endpoints, threshold: 12 })).toEqual([143, 154])
// An endpoint outside the threshold loses to the grid.
expect(snapPoint([130, 130], { enabled: true, gridSize: 32, endpoints, threshold: 12 })).toEqual([128, 128])
})
})
describe('payload rules (6.1): preset XOR raw, and open is a door intent', () => {
it('sends only `preset` beside a preset, never a raw switch such as open:false', () => {
const fields = buildAddFields(1, [0, 0], [100, 0], { preset: 'door', switches: presetSwitches('door'), label: null })
expect(fields).toEqual({ scene: 1, a: [0, 0], b: [100, 0], preset: 'door' })
for (const key of ['open', 'blocks_sight', 'blocks_movement', 'sight_dir', 'player_marker', 'door', 'secret', 'locked']) {
expect(fields).not.toHaveProperty(key)
}
})
it('sends every raw switch on a raw add and a label only when given', () => {
const fields = buildAddFields(1, [0, 0], [100, 0], {
preset: null,
switches: switches({ sightDir: 'left' }),
label: 'railing',
})
expect(fields).toMatchObject({
blocks_sight: true,
blocks_movement: true,
sight_dir: 'left',
player_marker: false,
door: false,
open: false,
secret: false,
locked: false,
label: 'railing',
})
})
it('never puts `open` in a wall.update patch, preset or raw', () => {
const raw = buildUpdateFields(9, { preset: null, switches: switches({ open: true }), label: null })
expect(raw).toEqual({
id: 9,
blocks_sight: true,
blocks_movement: true,
sight_dir: 'both',
player_marker: false,
door: false,
secret: false,
locked: false,
label: null,
})
expect(raw).not.toHaveProperty('open')
const preset = buildUpdateFields(9, { preset: 'window', switches: presetSwitches('window') })
expect(preset).toEqual({ id: 9, preset: 'window' })
expect(buildDoorFields(1, 9, true)).toEqual({ scene: 1, id: 9, open: true })
})
it('builds the inverse of each edit for single-step undo', () => {
expect(undoIntent(1, { kind: 'remove', id: 4 })).toEqual({
t: 'wall.remove',
fields: { scene: 1, id: 4 },
})
expect(undoIntent(1, { kind: 'door', id: 4, open: true })).toEqual({
t: 'door.set',
fields: { scene: 1, id: 4, open: true },
})
const update = undoIntent(1, {
kind: 'update',
id: 4,
switches: switches({ sightDir: 'right' }),
label: 'x',
flags: { a: 1 },
})
expect(update?.t).toBe('wall.update')
expect(update?.fields).toMatchObject({ id: 4, sight_dir: 'right', label: 'x', flags: { a: 1 } })
expect(update?.fields).not.toHaveProperty('open')
const restore = undoIntent(1, {
kind: 'restore',
wall: {
a: [0, 0],
b: [100, 0],
thickness: 0,
position: 'center',
height: null,
switches: switches({ open: true, door: true }),
label: 'door',
flags: {},
},
})
expect(restore?.t).toBe('wall.add')
expect(restore?.fields).toMatchObject({ door: true, open: true, label: 'door', height: null })
})
})
describe('local validation mirrors the server rules (5.8, 6.1)', () => {
it('rejects a sight direction with nothing to block', () => {
expect(validateSwitches(switches({ blocksSight: false, sightDir: 'left' }))).toContain('sight direction')
expect(validateSwitches(switches({ blocksSight: false, sightDir: 'both' }))).toBeNull()
})
it('rejects door settings without a door and impossible secret/locked doors', () => {
expect(validateSwitches(switches({ locked: true }))).toContain('need a door')
expect(validateSwitches(switches({ door: true, secret: true, open: true }))).toContain('always closed')
expect(validateSwitches(switches({ door: true, secret: true, sightDir: 'left' }))).toContain('two-way')
expect(validateSwitches(switches({ door: true, secret: true, blocksMovement: false }))).toContain('opaque')
expect(validateSwitches(switches({ door: true, open: true, locked: true }))).toContain('locked')
expect(validateSwitches(switches({ door: true, secret: true, locked: true }))).toBeNull()
expect(validateSwitches(switches({ door: true, open: true }))).toBeNull()
})
it('accepts 1..64 trimmed code points with no control characters', () => {
expect(validateLabel(' railing ')).toBeNull()
expect(validateLabel('')).toBeNull()
expect(validateLabel('x'.repeat(65))).toContain('64')
expect(validateLabel('a\nb')).toContain('control')
expect(validateLabel('😀'.repeat(64))).toBeNull()
})
it('treats a preset and raw switches as one draft mode', () => {
// The editor's preset draft never carries a raw switch; the type is the guard.
const preset: WallPreset = 'wall'
const fields = buildAddFields(1, [0, 0], [1, 1], { preset, switches: switches(), label: null })
expect(fields).toHaveProperty('preset', 'wall')
expect(fields).not.toHaveProperty('open')
const dir: SightDir = 'left'
expect(validateSwitches(switches({ sightDir: dir }))).toBeNull()
})
})