// 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): 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 = {}): 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 = {}): 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() }) })