- slices/tokens.ts: token upsert, token_gone, hello reset; status names only. - slices/characters.ts + slices/encountered.ts: own characters, encountered list with grey entries; the GM's per-player copies are ignored. - render/tokenBoard.ts (pure): event -> entered/updated/left patches, so one changed token costs one container; seeds from the reducer on mount. - render/modules/tokens.ts: pooled world-coordinate nodes on the tokens layer, one shared unit-circle context per kind, BitmapText from one pre-installed BitmapFont assigned only on a name change, stuck/squeezed badges, canvas dataset instrumentation for the "only changed entities" criterion. - panels/lists.ts + render/modules/lists.ts: #lists under #panels, textContent only, greyed encountered rows; mounted by a render module so no shared bootstrap file changed.
143 lines
4.8 KiB
TypeScript
143 lines
4.8 KiB
TypeScript
// T8 (#71): the token slice is the reducer seam for stories 28-30 and 34. Server frames in, the
|
|
// token view out; `token` upserts one token, `token_gone` removes it, `hello` resets the view.
|
|
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 { tokenOf, tokensOf } from '../src/slices/tokens.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: 'player', me: { id: 2, name: 'Ann' } })
|
|
|
|
/** A player-shaped token frame: `statuses` are names only, no `source` (6.1 leak note 6). */
|
|
const playerToken = (n: number, id: string, overrides: Record<string, unknown> = {}): ReducerEvent =>
|
|
frame({
|
|
t: 'token',
|
|
n,
|
|
id,
|
|
scene: 1,
|
|
name: 'Rat',
|
|
pos: [400, 400],
|
|
radius: 20,
|
|
mine: false,
|
|
statuses: [],
|
|
props: { public: {}, cprops: { public: {} } },
|
|
...overrides,
|
|
})
|
|
|
|
const run = (...events: ReducerEvent[]): RootState =>
|
|
events.reduce((state, event) => reduce(state, event), initialState())
|
|
|
|
describe('token enters, updates and leaves (stories 28, 29)', () => {
|
|
it('enters on a token frame and replaces the whole view on a later one', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
playerToken(2, 'aaa'),
|
|
playerToken(3, 'bbb'),
|
|
)
|
|
expect(tokensOf(state).map(token => token.id)).toEqual(['aaa', 'bbb'])
|
|
|
|
// 6.1: every `token` message is the whole view of one token; a later one replaces it in place.
|
|
const moved = reduce(state, playerToken(4, 'aaa', { pos: [420, 400], radius: 26, name: 'Giant Rat' }))
|
|
expect(tokensOf(moved)).toHaveLength(2)
|
|
expect(tokensOf(moved).map(token => token.id)).toEqual(['aaa', 'bbb'])
|
|
expect(tokenOf(moved, 'aaa')).toMatchObject({ name: 'Giant Rat', pos: [420, 400], radius: 26 })
|
|
expect(tokenOf(moved, 'bbb')).toMatchObject({ name: 'Rat', pos: [400, 400] })
|
|
})
|
|
|
|
it('removes a token on token_gone, and a later frame re-enters it', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
playerToken(2, 'aaa'),
|
|
playerToken(3, 'bbb'),
|
|
frame({ t: 'token_gone', n: 4, id: 'aaa' }),
|
|
)
|
|
expect(tokensOf(state).map(token => token.id)).toEqual(['bbb'])
|
|
expect(tokenOf(state, 'aaa')).toBeNull()
|
|
|
|
const back = reduce(state, playerToken(5, 'aaa'))
|
|
expect(tokensOf(back).map(token => token.id)).toEqual(['bbb', 'aaa'])
|
|
})
|
|
|
|
it('clears every token on hello: a token that left the view must not survive a reconnect', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
playerToken(2, 'aaa'),
|
|
playerToken(3, 'bbb'),
|
|
hello(4),
|
|
)
|
|
expect(tokensOf(state)).toEqual([])
|
|
})
|
|
|
|
it('keeps the status names and drops the GM-only source (6.1 leak note 6)', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
playerToken(2, 'aaa', { statuses: [{ status: 'stuck', source: 'walls' }] }),
|
|
)
|
|
expect(tokenOf(state, 'aaa')?.statuses).toEqual(['stuck'])
|
|
})
|
|
|
|
it('keeps the GM-only fields when the server sends them (story 34)', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
playerToken(2, 'aaa', {
|
|
statuses: [{ status: 'stuck', source: 'walls' }, { status: 'squeezed', source: 'GM' }],
|
|
gm_only: true,
|
|
kind: 'npc',
|
|
controller: null,
|
|
character_id: 'char1',
|
|
flags: {},
|
|
vision_radius: 320,
|
|
radius_own: null,
|
|
}),
|
|
)
|
|
const token = tokenOf(state, 'aaa')
|
|
expect(token).toMatchObject({
|
|
gmOnly: true,
|
|
kind: 'npc',
|
|
controller: null,
|
|
characterId: 'char1',
|
|
statuses: ['stuck', 'squeezed'],
|
|
})
|
|
})
|
|
|
|
it('marks the tokens the recipient controls (mine)', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
playerToken(2, 'aaa', { mine: true }),
|
|
playerToken(3, 'bbb'),
|
|
)
|
|
expect(tokensOf(state).filter(token => token.mine).map(token => token.id)).toEqual(['aaa'])
|
|
})
|
|
|
|
it('ignores a malformed token frame instead of inventing a token (6.2)', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
frame({ t: 'token', n: 2, id: 'aaa', name: 'Rat' }),
|
|
)
|
|
expect(tokensOf(state)).toEqual([])
|
|
})
|
|
|
|
it('does not let a token_gone for an unknown id create anything', () => {
|
|
const state = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
frame({ t: 'token_gone', n: 2, id: 'nope' }),
|
|
)
|
|
expect(tokensOf(state)).toEqual([])
|
|
})
|
|
})
|