// T8 (#71): the lists' reducer seam. Story 31 (my characters with my own values) and story 32 (an // encountered list with greyed entries). `character`/`character_gone` and `encountered` in, the // two lists out; `hello` resets both with every other slice. 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 { charactersOf, ownCharactersOf } from '../src/slices/characters.ts' import { encounteredOf } from '../src/slices/encountered.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, role: 'gm' | 'player' = 'player', id = 2): ReducerEvent => frame({ t: 'hello', n, proto: 2, role, me: { id, name: role === 'gm' ? 'GM' : 'Ann' } }) /** The owner's view has no `owner`/`reveal_all`/`revealed` field (6.1). */ const ownerCharacter = (n: number, id: string, overrides: Record = {}): ReducerEvent => frame({ t: 'character', n, id, descriptive_name: 'Ann', actual_name: 'Ann of the Vale', props: { public: {}, owner: { hp: 12 } }, ...overrides, }) const gmCharacter = (n: number, id: string, owner: number, overrides: Record = {}): ReducerEvent => frame({ t: 'character', n, id, owner, descriptive_name: 'Ann', actual_name: 'Ann of the Vale', reveal_all: false, revealed: [], props: { public: {}, owner: { hp: 12 }, gm: { secret: true } }, ...overrides, }) const encountered = (n: number, body: Record): ReducerEvent => frame({ t: 'encountered', n, ...body }) const run = (...events: ReducerEvent[]): RootState => events.reduce((state, event) => reduce(state, event), initialState()) describe('my characters (story 31)', () => { it('lists the characters the server sent the owner, with their own values', () => { const state = run({ type: 'connecting' }, hello(1), ownerCharacter(2, 'char1')) expect(charactersOf(state)).toHaveLength(1) expect(ownCharactersOf(state)).toEqual([ expect.objectContaining({ id: 'char1', descriptiveName: 'Ann', actualName: 'Ann of the Vale', props: { public: {}, owner: { hp: 12 } }, }), ]) }) it('replaces a character on a later frame and drops it on character_gone', () => { const state = run( { type: 'connecting' }, hello(1), ownerCharacter(2, 'char1'), ownerCharacter(3, 'char1', { descriptive_name: 'Ann the Bold' }), ) expect(ownCharactersOf(state)).toEqual([expect.objectContaining({ descriptiveName: 'Ann the Bold' })]) const gone = reduce(state, frame({ t: 'character_gone', n: 4, id: 'char1' })) expect(ownCharactersOf(gone)).toEqual([]) expect(charactersOf(gone)).toEqual([]) }) it('shows a GM only the characters that GM owns, never the whole cast', () => { const state = run( { type: 'connecting' }, hello(1, 'gm', 1), gmCharacter(2, 'char1', 2), gmCharacter(3, 'char2', 1), ) expect(ownCharactersOf(state).map(character => character.id)).toEqual(['char2']) }) it('clears the list on hello', () => { const state = run({ type: 'connecting' }, hello(1), ownerCharacter(2, 'char1'), hello(3)) expect(ownCharactersOf(state)).toEqual([]) }) }) describe('encountered list (story 32)', () => { it('replaces the whole list on full:true and keeps the grey flag', () => { const state = run( { type: 'connecting' }, hello(1), encountered(2, { full: true, set: [ { id: 'aaa', name: 'Rat', grey: false }, { id: 'bbb', name: 'Bat', grey: true }, ], }), ) expect(encounteredOf(state)).toEqual([ { id: 'aaa', name: 'Rat', grey: false }, { id: 'bbb', name: 'Bat', grey: true }, ]) }) it('applies a diff: set upserts and del removes, without touching anything else', () => { const state = run( { type: 'connecting' }, hello(1), encountered(2, { full: true, set: [ { id: 'aaa', name: 'Rat', grey: false }, { id: 'bbb', name: 'Bat', grey: false }, ], }), encountered(3, { full: false, set: [{ id: 'aaa', name: 'Rat', grey: true }], del: ['bbb'] }), ) expect(encounteredOf(state)).toEqual([{ id: 'aaa', name: 'Rat', grey: true }]) }) it('ignores the GM copy, which is one message per player and not the GM own list', () => { const state = run( { type: 'connecting' }, hello(1, 'gm', 1), encountered(2, { full: true, set: [{ id: 'aaa', name: 'Rat', grey: false }], player: 2 }), ) expect(encounteredOf(state)).toEqual([]) }) it('clears on hello', () => { const state = run( { type: 'connecting' }, hello(1), encountered(2, { full: true, set: [{ id: 'aaa', name: 'Rat', grey: false }] }), hello(3), ) expect(encounteredOf(state)).toEqual([]) }) })