feat(client): name prompt, connection badge, reconnect and resync (#68)
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
This commit is contained in:
+124
-3
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { SLICES, initialState, reduce, rosterOf } from '../src/reducer.ts'
|
||||
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'
|
||||
@@ -140,9 +140,10 @@ describe('connection outcomes (6.5)', () => {
|
||||
expect(state.connection.phase).toBe('offline')
|
||||
})
|
||||
|
||||
it('a transport close while live is offline, not a silent sign-out', () => {
|
||||
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('offline')
|
||||
expect(state.connection.phase).toBe('reconnecting')
|
||||
expect(connectionBadge(state)).toBe('reconnecting')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -182,3 +183,123 @@ describe('registry', () => {
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// 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()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user