feat(client): GM roster, player keys and token panel (#74)
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
// T11 (#74) reducer seam: the GM admin slice consumes `token`/`token_gone` frames and owns the
|
||||
// token selection that #71/#72 will read. 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 type { AdminToken } from '../src/slices/admin.ts'
|
||||
import { adminTokens, selectedTokenId } from '../src/slices/admin.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' },
|
||||
scene: {
|
||||
id: 1,
|
||||
name: 'Cellar',
|
||||
w: 4096,
|
||||
h: 4096,
|
||||
tile_size: 32,
|
||||
cell: 32,
|
||||
grid_size: 64,
|
||||
vision_radius_default: 320,
|
||||
token_radius_default: 20,
|
||||
art_version: 1,
|
||||
},
|
||||
})
|
||||
|
||||
const gmToken = (id: string, patch: Record<string, unknown> = {}): ReducerEvent =>
|
||||
frame({
|
||||
t: 'token',
|
||||
n: 3,
|
||||
id,
|
||||
scene: 1,
|
||||
name: 'Rat',
|
||||
pos: [400, 400],
|
||||
radius: 20,
|
||||
mine: false,
|
||||
statuses: [{ status: 'stuck', source: 'walls' }],
|
||||
props: {},
|
||||
kind: 'npc',
|
||||
controller: null,
|
||||
gm_only: false,
|
||||
flags: {},
|
||||
vision_radius: null,
|
||||
radius_own: null,
|
||||
character_id: null,
|
||||
descriptive_name: 'Rat',
|
||||
actual_name: 'Giant Rat',
|
||||
reveal_all: false,
|
||||
revealed: [],
|
||||
...patch,
|
||||
})
|
||||
|
||||
const run = (...events: ReducerEvent[]): RootState =>
|
||||
events.reduce((state, event) => reduce(state, event), initialState())
|
||||
|
||||
const tokenOf = (state: RootState, id: string): AdminToken | undefined =>
|
||||
adminTokens(state).find(token => token.id === id)
|
||||
|
||||
describe('GM token records', () => {
|
||||
it('upserts the GM fields of a `token` frame and replaces on a later frame', () => {
|
||||
const first = run({ type: 'connecting' }, hello(1), gmToken('abc'))
|
||||
expect(adminTokens(first)).toHaveLength(1)
|
||||
expect(tokenOf(first, 'abc')).toMatchObject({
|
||||
id: 'abc',
|
||||
scene: 1,
|
||||
name: 'Rat',
|
||||
pos: [400, 400],
|
||||
radius: 20,
|
||||
kind: 'npc',
|
||||
controller: null,
|
||||
gmOnly: false,
|
||||
statuses: [{ status: 'stuck', source: 'walls' }],
|
||||
descriptiveName: 'Rat',
|
||||
actualName: 'Giant Rat',
|
||||
})
|
||||
|
||||
const changed = reduce(
|
||||
first,
|
||||
gmToken('abc', {
|
||||
name: 'Big Rat',
|
||||
pos: [420, 400],
|
||||
radius: 30,
|
||||
gm_only: true,
|
||||
controller: 2,
|
||||
statuses: [{ status: 'squeezed', source: 'GM' }],
|
||||
}),
|
||||
)
|
||||
expect(adminTokens(changed)).toHaveLength(1)
|
||||
expect(tokenOf(changed, 'abc')).toMatchObject({
|
||||
name: 'Big Rat',
|
||||
pos: [420, 400],
|
||||
radius: 30,
|
||||
gmOnly: true,
|
||||
controller: 2,
|
||||
statuses: [{ status: 'squeezed', source: 'GM' }],
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores a malformed token frame instead of inventing a row', () => {
|
||||
const state = run({ type: 'connecting' }, hello(1), frame({ t: 'token', n: 2, id: 7 }))
|
||||
expect(adminTokens(state)).toEqual([])
|
||||
})
|
||||
|
||||
it('removes the row on token_gone and clears a selection that pointed at it', () => {
|
||||
const selected = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
gmToken('abc'),
|
||||
{ type: 'admin-select', token: 'abc' },
|
||||
frame({ t: 'token_gone', n: 4, id: 'abc' }),
|
||||
)
|
||||
expect(adminTokens(selected)).toEqual([])
|
||||
expect(selectedTokenId(selected)).toBeNull()
|
||||
})
|
||||
|
||||
it('hello resets the token list and the selection with every other slice', () => {
|
||||
const live = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
gmToken('abc'),
|
||||
{ type: 'admin-select', token: 'abc' },
|
||||
)
|
||||
expect(selectedTokenId(live)).toBe('abc')
|
||||
const reset = reduce(live, hello(9))
|
||||
expect(adminTokens(reset)).toEqual([])
|
||||
expect(selectedTokenId(reset)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('token selection (#71/#72 hook)', () => {
|
||||
it('is slice state, changed only by the admin-select event', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
gmToken('abc'),
|
||||
gmToken('def'),
|
||||
{ type: 'admin-select', token: 'def' },
|
||||
)
|
||||
expect(selectedTokenId(state)).toBe('def')
|
||||
expect(adminTokens(state).map(token => token.id)).toEqual(['abc', 'def'])
|
||||
expect(selectedTokenId(reduce(state, { type: 'admin-select', token: null }))).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user