Map gate (a): measure the fastest of 4 fresh page loads after a warm-up (min-of-N, TESTING 5.3) against 2x the 1 s DESIGN budget (the documented saturated-host tolerance), and print every sample; a planted 50 ms/tile decode delay fails it. Wall editor: an ack dispatch re-renders with the previous server record, and the panel repopulated every control on every render, so a just-toggled checkbox was reverted until the wall frame landed (Playwright saw 'did not change its state'). Repopulate only when the record changed (src/panels/wallSync.ts, unit-tested) and restore the controls when the server refuses an edit.
329 lines
13 KiB
TypeScript
329 lines
13 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, wallViewFromFrame, wallsOf } from '../src/slices/walls.ts'
|
|
import { shouldPopulate, wallFieldsSignature } from '../src/panels/wallSync.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()
|
|
})
|
|
})
|
|
|
|
describe('panel synchronisation (#76): an ack render never undoes a toggle', () => {
|
|
const view = (id = 1, overrides: Record<string, unknown> = {}) => {
|
|
const wall = wallViewFromFrame(wallBody(id, overrides))
|
|
if (!wall) throw new Error('bad wall body')
|
|
return wall
|
|
}
|
|
|
|
it('populates a newly selected wall, then leaves an identical record alone', () => {
|
|
const wall = view()
|
|
expect(shouldPopulate(null, wall)).toBe(true)
|
|
const populated = { id: wall.id, signature: wallFieldsSignature(wall) }
|
|
// The editor's own ack dispatch re-renders with the previous record (same fields, usually the
|
|
// same object): repopulating here is what reverted the GM's just-toggled checkbox.
|
|
expect(shouldPopulate(populated, view())).toBe(false)
|
|
})
|
|
|
|
it('repopulates when the server record or the selection changes', () => {
|
|
const wall = view()
|
|
const populated = { id: wall.id, signature: wallFieldsSignature(wall) }
|
|
expect(shouldPopulate(populated, view(1, { locked: true, door: true, kind: 'door' }))).toBe(true)
|
|
expect(shouldPopulate(populated, view(1, { label: 'railing' }))).toBe(true)
|
|
expect(shouldPopulate(populated, view(2))).toBe(true)
|
|
})
|
|
})
|