T5 of the M4 client (spec #63). All of it at the existing seams: the pure reducer slice, the WS client and the browser e2e. - First sign-in: a live player with `me.name === null` gets a name prompt instead of the HUD and no Pixi stage (the server sends no scene until named). `player.setname` is acked before the second `hello` (#33); `name-taken`, `invalid` and `not-allowed` become visible text and the prompt stays put. Acceptance is followed by `hello` + the 6.3 resync, so the prompt is keyed on state and is asked once. - Badge (`#badge[data-state]`): connected / reconnecting / offline. Offline is driven by `navigator.onLine` (a phone with no network) and by terminal close outcomes; reconnecting keeps the stale map on screen. - Reconnect: a transport close after a live connection retries with 1/2/4/8/10 s base delays +/-20% jitter, then the cap. `revoked` and `proto-mismatch` stay terminal; pending `reqId`s die with the socket; the browser `online` event skips the rest of the wait. A drop does not clear the view: `hello` resets every slice and the resync rebuilds tokens, fog, explored, markers and lists. - Two devices on one player key both stay live and receive the same frames. Proof: new unit tests for the schedule/retry machine (`test/ws.test.ts`) and the badge/reconnect/name transitions (`test/reducer.test.ts`); new `client/e2e/session.test.mjs` runs the real server and TLS/HTTP2 vhost, kills and restarts the server under a live page, drops the browser network with `context.setOffline`, and asserts the full resync frames (roster, encountered, vision, explored, marker, token) plus two pages on one key. Refs #68
306 lines
12 KiB
TypeScript
306 lines
12 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { SLICES, connectionBadge, 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 reconnecting, not a silent sign-out', () => {
|
|
const state = run({ type: 'connecting' }, hello(1), { type: 'close', reason: null, code: 1006 })
|
|
expect(state.connection.phase).toBe('reconnecting')
|
|
expect(connectionBadge(state)).toBe('reconnecting')
|
|
})
|
|
})
|
|
|
|
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)
|
|
})
|
|
})
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// T5 (#68): the connection slice — badge, backoff display, name prompt errors, reconnect reset.
|
|
|
|
const named = (): RootState =>
|
|
run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
roster(2, [{ id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true }]),
|
|
)
|
|
|
|
describe('connection badge (story 7)', () => {
|
|
it('is connected while live, hidden at the sign-in door and on boot', () => {
|
|
expect(connectionBadge(named())).toBe('connected')
|
|
expect(connectionBadge(initialState())).toBeNull()
|
|
expect(connectionBadge(run({ type: 'connecting' }))).toBeNull()
|
|
expect(
|
|
connectionBadge(run({ type: 'connecting' }, { type: 'signin-failed', message: 'Key not recognised.' })),
|
|
).toBeNull()
|
|
})
|
|
|
|
it('is reconnecting after a transport close and keeps showing the last view', () => {
|
|
const state = reduce(named(), { type: 'close', reason: null, code: 1006 })
|
|
expect(state.connection.phase).toBe('reconnecting')
|
|
expect(connectionBadge(state)).toBe('reconnecting')
|
|
expect(state.connection.notice).toMatch(/reconnect/i)
|
|
// The map is stale but stays on screen until the resync replaces it (no blank flash).
|
|
expect(state.session.me).toEqual({ id: 2, name: 'Ann' })
|
|
expect(rosterOf(state)).toHaveLength(1)
|
|
})
|
|
|
|
it('records the attempt and delay the socket scheduled', () => {
|
|
const state = reduce(named(), { type: 'reconnecting', attempt: 3, delay: 4400 })
|
|
expect(state.connection.phase).toBe('reconnecting')
|
|
expect(state.connection.attempt).toBe(3)
|
|
expect(state.connection.retryDelayMs).toBe(4400)
|
|
expect(state.connection.notice).toMatch(/reconnect/i)
|
|
})
|
|
|
|
it('is offline when the browser reports no network, and connected again when it returns', () => {
|
|
const offline = reduce(named(), { type: 'network', online: false })
|
|
expect(connectionBadge(offline)).toBe('offline')
|
|
expect(offline.session.me).toEqual({ id: 2, name: 'Ann' })
|
|
const online = reduce(offline, { type: 'network', online: true })
|
|
expect(connectionBadge(online)).toBe('connected')
|
|
})
|
|
|
|
it('is offline for a terminal outcome (evicted) even while a retry was in flight', () => {
|
|
const reconnecting = reduce(named(), { type: 'close', reason: null, code: 1006 })
|
|
const state = reduce(reconnecting, { type: 'close', reason: 'evicted', code: 4002 })
|
|
expect(state.connection.phase).toBe('offline')
|
|
expect(connectionBadge(state)).toBe('offline')
|
|
})
|
|
|
|
it('a reconnect keeps the view until hello, then reset+resync rebuilds it', () => {
|
|
const reconnecting = run(
|
|
{ type: 'connecting' },
|
|
hello(1),
|
|
roster(2, [{ id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true }]),
|
|
{ type: 'close', reason: null, code: 1006 },
|
|
{ type: 'reconnecting', attempt: 2, delay: 2200 },
|
|
)
|
|
expect(connectionBadge(reconnecting)).toBe('reconnecting')
|
|
expect(rosterOf(reconnecting)).toHaveLength(1)
|
|
|
|
const back = reduce(reconnecting, { type: 'open' })
|
|
expect(back.connection.frames).toBe(0)
|
|
expect(rosterOf(back)).toHaveLength(1)
|
|
|
|
const resynced = reduce(back, hello(1))
|
|
expect(resynced.connection.phase).toBe('live')
|
|
expect(resynced.connection.attempt).toBe(0)
|
|
expect(resynced.connection.retryDelayMs).toBeNull()
|
|
expect(connectionBadge(resynced)).toBe('connected')
|
|
// `hello` cleared the old view; the resync frames after it refill it.
|
|
expect(rosterOf(resynced)).toEqual([])
|
|
const filled = reduce(
|
|
resynced,
|
|
roster(2, [
|
|
{ id: 2, name: 'Ann', role: 'player', colour: '#f00', connected: true },
|
|
{ id: 1, name: 'GM', role: 'gm', colour: '#0f0', connected: true },
|
|
]),
|
|
)
|
|
expect(rosterOf(filled)).toHaveLength(2)
|
|
})
|
|
})
|
|
|
|
describe('name prompt (stories 5, 6)', () => {
|
|
const unnamed = (): RootState =>
|
|
run({ type: 'connecting' }, frame({ t: 'hello', n: 1, proto: 2, role: 'player', me: { id: 5, name: null } }))
|
|
|
|
it('an unnamed player is live with a null name and no scene', () => {
|
|
const state = unnamed()
|
|
expect(state.connection.phase).toBe('live')
|
|
expect(state.session.me).toEqual({ id: 5, name: null })
|
|
expect(state.session.scene).toBeNull()
|
|
expect(state.connection.nameError).toBeNull()
|
|
})
|
|
|
|
it('a taken name keeps the connection live and carries the visible reason', () => {
|
|
const state = reduce(unnamed(), { type: 'name-failed', message: 'That name is taken.' })
|
|
expect(state.connection.phase).toBe('live')
|
|
expect(state.connection.nameError).toBe('That name is taken.')
|
|
expect(state.session.me?.name).toBeNull()
|
|
})
|
|
|
|
it('the second hello (me.name set) clears the name error and starts the resync', () => {
|
|
const failed = reduce(unnamed(), { type: 'name-failed', message: 'That name is taken.' })
|
|
const state = reduce(failed, hello(2, { me: { id: 5, name: 'Bob' }, scene: scene('Cellar') }))
|
|
expect(state.connection.nameError).toBeNull()
|
|
expect(state.session.me).toEqual({ id: 5, name: 'Bob' })
|
|
expect(state.session.scene?.name).toBe('Cellar')
|
|
})
|
|
|
|
it('signing out or reconnecting clears a stale name error', () => {
|
|
const failed = reduce(unnamed(), { type: 'name-failed', message: 'Enter a name.' })
|
|
expect(reduce(failed, { type: 'connecting' }).connection.nameError).toBeNull()
|
|
expect(reduce(failed, { type: 'signed-out' }).connection.nameError).toBeNull()
|
|
})
|
|
})
|