Files
FFF_vtt_frozen/client/test/cast.test.ts
T
SirStone 0323fbd2ae feat: GM characters, reveals, who-sees-it and seen_by (#75)
Server: GM token frames gain the additive, GM-only seen_by:[player ids],
computed from each player's existing sight (PlayerVision.visibleTo) with
no extra polygon pass. Leakcheck BannedAlways bans seen_by; a seenBy
fault build is caught as exactly WALLDATA. proto stays 2 (6.2 additive).

Client: new cast slice parses character/character_gone/encountered frames
and seen_by; gmcast panel adds character create/rename/assign/remove,
reveal to one/all, encountered forget (grey entries only) and the
who-sees-it list (0/100 in M4). Mounted beside the #74 admin panel.

Docs: DESIGN 5.9/6.1/6.2/7.4 record the round-1 shape and the no-bump
call. Gates: devbox run test, client-test, e2e green; bench recorded.
2026-11-21 01:04:10 +01:00

149 lines
4.4 KiB
TypeScript

// T12 (#75) reducer seam: the GM cast slice consumes `character`, `encountered` and the GM-only
// `seen_by` field of `token` frames. Pure, no DOM and 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 { castCharacters, encounteredOf, seenByOf } from '../src/slices/cast.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): ReducerEvent =>
frame({
t: 'hello',
n,
proto: 2,
role: 'gm',
me: { id: 1, name: 'GM' },
})
const character = (id: string, patch: Record<string, unknown> = {}): ReducerEvent =>
frame({
t: 'character',
n: 2,
id,
owner: 2,
descriptive_name: 'a cloaked figure',
actual_name: 'Lord Vex',
reveal_all: false,
revealed: [],
props: { public: {}, owner: {}, gm: {} },
...patch,
})
const encountered = (player: number, patch: Record<string, unknown> = {}): ReducerEvent =>
frame({ t: 'encountered', n: 3, player, full: false, set: [], del: [], ...patch })
const token = (id: string, seenBy: number[]): ReducerEvent =>
frame({
t: 'token',
n: 4,
id,
scene: 1,
name: 'a cloaked figure',
pos: [400, 400],
radius: 20,
mine: false,
statuses: [],
props: {},
seen_by: seenBy,
})
const run = (...events: ReducerEvent[]): RootState =>
events.reduce((state, event) => reduce(state, event), initialState())
describe('GM cast slice', () => {
it('upserts character frames, keeps the reveal list and replaces on a later frame', () => {
const first = run({ type: 'connecting' }, hello(1), character('c1'))
expect(castCharacters(first)).toEqual([
expect.objectContaining({
id: 'c1',
owner: 2,
descriptiveName: 'a cloaked figure',
actualName: 'Lord Vex',
revealAll: false,
revealed: [],
}),
])
const changed = reduce(
first,
character('c1', { owner: 3, actual_name: 'The Duke', reveal_all: true, revealed: [2, 3] }),
)
expect(castCharacters(changed)).toEqual([
expect.objectContaining({
id: 'c1',
owner: 3,
actualName: 'The Duke',
revealAll: true,
revealed: [2, 3],
}),
])
})
it('removes a character on character_gone and ignores a malformed record', () => {
const state = run(
{ type: 'connecting' },
hello(1),
character('c1'),
frame({ t: 'character', n: 3, id: 7 }),
frame({ t: 'character_gone', n: 4, id: 'c1' }),
)
expect(castCharacters(state)).toEqual([])
})
it('keeps one encountered list per player: full replaces, a diff upserts, del removes', () => {
const full = run(
{ type: 'connecting' },
hello(1),
encountered(2, {
full: true,
set: [
{ id: 't1', name: 'Rat', grey: false },
{ id: 't2', name: 'Bat', grey: true },
],
}),
)
expect(encounteredOf(full, 2)).toEqual([
{ id: 't1', name: 'Rat', grey: false },
{ id: 't2', name: 'Bat', grey: true },
])
const diff = reduce(
full,
encountered(2, { set: [{ id: 't2', name: 'Giant Bat', grey: false }], del: ['t1'] }),
)
expect(encounteredOf(diff, 2)).toEqual([{ id: 't2', name: 'Giant Bat', grey: false }])
expect(encounteredOf(diff, 3)).toEqual([])
})
it('records seen_by from GM token frames and drops it on token_gone', () => {
const state = run(
{ type: 'connecting' },
hello(1),
token('t1', [2, 3]),
token('t2', []),
)
expect(seenByOf(state, 't1')).toEqual([2, 3])
expect(seenByOf(state, 't2')).toEqual([])
expect(seenByOf(reduce(state, frame({ t: 'token_gone', n: 5, id: 't1' })), 't1')).toBeNull()
})
it('hello resets every list and the seen_by map with every other slice', () => {
const live = run(
{ type: 'connecting' },
hello(1),
character('c1'),
encountered(2, { full: true, set: [{ id: 't1', name: 'Rat', grey: false }] }),
token('t1', [2]),
)
const reset = reduce(live, hello(9))
expect(castCharacters(reset)).toEqual([])
expect(encounteredOf(reset, 2)).toEqual([])
expect(seenByOf(reset, 't1')).toBeNull()
})
})