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:
2026-11-20 23:57:16 +01:00
parent 97f618c5ad
commit 3c0258ea31
37 changed files with 3439 additions and 178 deletions
+137
View File
@@ -0,0 +1,137 @@
import { describe, expect, it } from 'vitest'
import {
CLOSE_REASONS,
PROTO,
closeOutcome,
encodeIntent,
isAck,
isClosing,
isHello,
parseServerFrame,
wsUrl,
} from '../src/protocol.ts'
const 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,
}
describe('server envelope', () => {
it('parses a JSON object with a string t and an integer n >= 1', () => {
const frame = parseServerFrame(JSON.stringify({ t: 'roster', n: 3, players: [] }))
expect(frame).toEqual({ t: 'roster', n: 3, players: [] })
})
it('ignores unknown frame types without dropping them (6.2: additive only)', () => {
const frame = parseServerFrame(JSON.stringify({ t: 'future_frame', n: 2, x: 1 }))
expect(frame?.t).toBe('future_frame')
})
it.each([
['binary', new Uint8Array([1, 2, 3])],
['non-JSON', '{not json'],
['array', '[]'],
['null', 'null'],
['no t', JSON.stringify({ n: 1 })],
['empty t', JSON.stringify({ t: '', n: 1 })],
['no n', JSON.stringify({ t: 'hello' })],
['n = 0', JSON.stringify({ t: 'hello', n: 0 })],
['fractional n', JSON.stringify({ t: 'hello', n: 1.5 })],
['string n', JSON.stringify({ t: 'hello', n: '1' })],
])('rejects %s', (_name, raw) => {
expect(parseServerFrame(raw)).toBeNull()
})
})
describe('hello', () => {
it('accepts the DESIGN 6.1 shape and carries the scene block', () => {
const frame = parseServerFrame(
JSON.stringify({ t: 'hello', n: 1, proto: 2, role: 'player', me: { id: 4, name: 'Ann' }, scene }),
)
expect(frame && isHello(frame)).toBe(true)
if (frame && isHello(frame)) {
expect(frame.proto).toBe(2)
expect(frame.scene?.w).toBe(4096)
}
})
it('accepts the unnamed-player minimum (proto, role, me only)', () => {
const frame = parseServerFrame(
JSON.stringify({ t: 'hello', n: 1, proto: 2, role: 'player', me: { id: 4, name: null } }),
)
expect(frame && isHello(frame)).toBe(true)
})
it('rejects a wrong role or proto', () => {
const badRole = parseServerFrame(
JSON.stringify({ t: 'hello', n: 1, proto: 2, role: 'admin', me: { id: 1, name: null } }),
)
const badProto = parseServerFrame(
JSON.stringify({ t: 'hello', n: 1, proto: '2', role: 'gm', me: { id: 1, name: null } }),
)
expect(badRole && isHello(badRole)).toBe(false)
expect(badProto && isHello(badProto)).toBe(false)
})
})
describe('ack', () => {
it('is the envelope plus reqId and ok', () => {
const frame = parseServerFrame(JSON.stringify({ t: 'ack', n: 2, reqId: 7, ok: true, pos: [1, 2] }))
expect(frame && isAck(frame)).toBe(true)
if (frame && isAck(frame)) expect(frame.pos).toEqual([1, 2])
})
it('is not an ack without an integer reqId or a boolean ok', () => {
for (const body of [
{ t: 'ack', n: 1, ok: true },
{ t: 'ack', n: 1, reqId: '7', ok: true },
{ t: 'ack', n: 1, reqId: 7, ok: 'yes' },
]) {
const frame = parseServerFrame(JSON.stringify(body))
expect(frame && isAck(frame)).toBe(false)
}
})
})
describe('closing and outcomes', () => {
it('accepts every static reason of DESIGN 6.5', () => {
for (const reason of CLOSE_REASONS) {
const frame = parseServerFrame(
JSON.stringify({ t: 'closing', n: 9, reason, code: 4001 }),
)
expect(frame && isClosing(frame)).toBe(true)
}
})
it('maps reasons to the shell outcome', () => {
expect(closeOutcome('proto-mismatch')).toBe('reload')
expect(closeOutcome('revoked')).toBe('signin')
expect(closeOutcome('evicted')).toBe('offline')
expect(closeOutcome('malformed-flood')).toBe('offline')
expect(closeOutcome(null)).toBe('offline')
})
})
describe('intents', () => {
it('always carry t and reqId and nothing secret', () => {
const raw = encodeIntent('conn.ping', 3, { ts: 99 })
expect(JSON.parse(raw)).toEqual({ t: 'conn.ping', reqId: 3, ts: 99 })
})
it('builds the /ws URL with the required proto parameter', () => {
expect(wsUrl({ protocol: 'https:', host: 'table.example' })).toBe(
`wss://table.example/ws?proto=${PROTO}`,
)
expect(wsUrl({ protocol: 'http:', host: '127.0.0.1:8080' })).toBe(
`ws://127.0.0.1:8080/ws?proto=${PROTO}`,
)
})
})