feat: client shell, sign-in and protocol client (#67)
New `client/` package (Vite + TypeScript + PixiJS v8, plain DOM overlay): the pure reducer seam (no Pixi import), an append-only slice registry build-time glob, the `proto` 2 WS client with envelope/ack/closing mapping, the sign-in door (`POST /join`, one "Key not recognised." for wrong and unknown), the DOM shell/HUD and a minimal Pixi stage created only after a live `hello`. `hello` resets every slice so a reconnect cannot show stale state; `proto-mismatch` shows reload; `revoked` returns to the sign-in page and stops. The browser e2e moves into the client package (`client/e2e/`, owning playwright-core) and runs the real Vite build through the TLS/HTTP2 test nginx: join -> canvas -> hello/roster for GM and player, no data before join, identical wrong/unknown key refusal, proto mismatch, revoked. `devbox run client-test` adds the Vitest seam; `devbox run e2e` builds the client first. The #66 stub dist stays local to `tools/vhost.test.mjs`, so `devbox run test` still needs no npm install. Refs #67
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { SLICES, initialState, reduce, rosterOf } from '../src/reducer.ts'
|
||||
import type { ReducerEvent, RootState } from '../src/reducer.ts'
|
||||
import { parseServerFrame } from '../src/protocol.ts'
|
||||
import type { RosterEntry } from '../src/protocol.ts'
|
||||
|
||||
const scene = (name: string) => ({
|
||||
id: 1,
|
||||
name,
|
||||
w: 4096,
|
||||
h: 4096,
|
||||
tile_size: 32,
|
||||
cell: 32,
|
||||
grid_size: 64,
|
||||
vision_radius_default: 320,
|
||||
token_radius_default: 20,
|
||||
art_version: 1,
|
||||
})
|
||||
|
||||
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, extra: Record<string, unknown> = {}): ReducerEvent =>
|
||||
frame({
|
||||
t: 'hello',
|
||||
n,
|
||||
proto: 2,
|
||||
role: 'player',
|
||||
me: { id: 2, name: 'Ann' },
|
||||
scene: scene('Cellar'),
|
||||
...extra,
|
||||
})
|
||||
|
||||
const roster = (n: number, players: RosterEntry[]): ReducerEvent =>
|
||||
frame({ t: 'roster', n, players })
|
||||
|
||||
const run = (...events: ReducerEvent[]): RootState =>
|
||||
events.reduce((state, event) => reduce(state, event), initialState())
|
||||
|
||||
describe('hello resets the whole view (6.1/6.3)', () => {
|
||||
it('clears every slice and replaces session fields on a second hello', () => {
|
||||
const first = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
roster(2, [
|
||||
{ id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true },
|
||||
{ id: 1, name: 'GM', role: 'gm', colour: '#0f0', connected: true },
|
||||
]),
|
||||
)
|
||||
expect(first.connection.phase).toBe('live')
|
||||
expect(first.session.role).toBe('player')
|
||||
expect(first.session.me).toEqual({ id: 2, name: 'Ann' })
|
||||
expect(first.session.scene?.name).toBe('Cellar')
|
||||
expect(rosterOf(first)).toHaveLength(2)
|
||||
|
||||
// Reconnect: the server sends hello first, then the resync. Nothing from the old connection
|
||||
// may survive, otherwise a token that left the view would stay on screen.
|
||||
const second = reduce(first, hello(9, { role: 'gm', me: { id: 1, name: 'GM' }, scene: scene('Attic') }))
|
||||
expect(rosterOf(second)).toEqual([])
|
||||
expect(second.session.role).toBe('gm')
|
||||
expect(second.session.scene?.name).toBe('Attic')
|
||||
expect(second.connection.phase).toBe('live')
|
||||
expect(second.connection.frames).toBe(3)
|
||||
expect(second.connection.lastN).toBe(9)
|
||||
})
|
||||
|
||||
it('stays on the other slices while a hello resets them', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
roster(2, [{ id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true }]),
|
||||
hello(3),
|
||||
)
|
||||
expect(Object.keys(state.slices).sort()).toEqual(SLICES.map(slice => slice.name).sort())
|
||||
expect(rosterOf(state)).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('no data before join', () => {
|
||||
it('connecting clears session and slices, so a stranger is shown nothing', () => {
|
||||
const live = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
roster(2, [{ id: 1, name: 'GM', role: 'gm', colour: '#0f0', connected: true }]),
|
||||
)
|
||||
const cleared = reduce(live, { type: 'connecting' })
|
||||
expect(cleared.connection.phase).toBe('connecting')
|
||||
expect(cleared.session).toEqual({ role: null, me: null, proto: null, scene: null })
|
||||
expect(rosterOf(cleared)).toEqual([])
|
||||
})
|
||||
|
||||
it('a refused upgrade (close before hello) lands on sign-in', () => {
|
||||
const state = run({ type: 'connecting' }, { type: 'close', reason: null, code: 1006 })
|
||||
expect(state.connection.phase).toBe('signin')
|
||||
expect(state.session.me).toBeNull()
|
||||
})
|
||||
|
||||
it('a failed key shows a sign-in error and no session', () => {
|
||||
const state = run({ type: 'connecting' }, { type: 'signin-failed', message: 'Key not recognised.' })
|
||||
expect(state.connection.phase).toBe('signin')
|
||||
expect(state.connection.error).toBe('Key not recognised.')
|
||||
expect(state.session.me).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('connection outcomes (6.5)', () => {
|
||||
it('proto-mismatch asks for a reload', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
frame({ t: 'closing', n: 2, reason: 'proto-mismatch', code: 4003 }),
|
||||
{ type: 'close', reason: 'proto-mismatch', code: 4003 },
|
||||
)
|
||||
expect(state.connection.phase).toBe('reload')
|
||||
})
|
||||
|
||||
it('revoked returns to sign-in and clears the view', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
roster(2, [{ id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true }]),
|
||||
frame({ t: 'closing', n: 3, reason: 'revoked', code: 4001 }),
|
||||
{ type: 'close', reason: 'revoked', code: 4001 },
|
||||
)
|
||||
expect(state.connection.phase).toBe('signin')
|
||||
expect(state.connection.notice).toMatch(/session has ended/i)
|
||||
expect(state.session.me).toBeNull()
|
||||
expect(rosterOf(state)).toEqual([])
|
||||
})
|
||||
|
||||
it('evicted goes offline but keeps the tab usable', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
frame({ t: 'closing', n: 2, reason: 'evicted', code: 4002 }),
|
||||
)
|
||||
expect(state.connection.phase).toBe('offline')
|
||||
})
|
||||
|
||||
it('a transport close while live is offline, not a silent sign-out', () => {
|
||||
const state = run({ type: 'connecting' }, hello(1), { type: 'close', reason: null, code: 1006 })
|
||||
expect(state.connection.phase).toBe('offline')
|
||||
})
|
||||
})
|
||||
|
||||
describe('frames', () => {
|
||||
it('ignores an unknown server message type (6.2) without touching the view', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
frame({ t: 'future_frame', n: 2, payload: 42 }),
|
||||
)
|
||||
expect(state.connection.phase).toBe('live')
|
||||
expect(state.connection.lastN).toBe(2)
|
||||
})
|
||||
|
||||
it('a roster frame replaces the list instead of merging', () => {
|
||||
const a: RosterEntry = { id: 1, name: 'GM', role: 'gm', colour: '#0f0', connected: true }
|
||||
const b: RosterEntry = { id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true }
|
||||
const state = run({ type: 'connecting' }, hello(1), roster(2, [a, b]), roster(3, [b]))
|
||||
expect(rosterOf(state)).toEqual([b])
|
||||
})
|
||||
|
||||
it('keeps the session intact when a non-hello frame arrives', () => {
|
||||
const state = run({ type: 'connecting' }, hello(1), frame({ t: 'error', n: 2, code: 'malformed' }))
|
||||
expect(state.session.me).toEqual({ id: 2, name: 'Ann' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('registry', () => {
|
||||
it('registers the roster slice without an import list', () => {
|
||||
expect(SLICES.map(slice => slice.name)).toContain('roster')
|
||||
})
|
||||
|
||||
it('rotates every server frame through the slice too', () => {
|
||||
const state = run({ type: 'connecting' }, hello(1))
|
||||
const withRoster = reduce(state, roster(2, [{ id: 1, name: 'GM', role: 'gm', colour: '#0f0', connected: true }]))
|
||||
expect(rosterOf(withRoster)).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user