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
148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { ProtocolClient } from '../src/ws.ts'
|
|
import type { ClientEvent, SocketLike } from '../src/ws.ts'
|
|
|
|
class FakeSocket implements SocketLike {
|
|
onopen: (() => void) | null = null
|
|
onmessage: ((event: { data: unknown }) => void) | null = null
|
|
onclose: ((event: { code: number; reason: string }) => void) | null = null
|
|
onerror: ((event: unknown) => void) | null = null
|
|
sent: string[] = []
|
|
closed = false
|
|
|
|
send(data: string): void {
|
|
if (this.closed) throw new Error('socket is closed')
|
|
this.sent.push(data)
|
|
}
|
|
|
|
close(): void {
|
|
this.closed = true
|
|
}
|
|
|
|
open(): void {
|
|
this.onopen?.()
|
|
}
|
|
|
|
receive(body: unknown): void {
|
|
this.onmessage?.({ data: typeof body === 'string' ? body : JSON.stringify(body) })
|
|
}
|
|
|
|
serverClose(code = 1005, reason = ''): void {
|
|
this.closed = true
|
|
this.onclose?.({ code, reason })
|
|
}
|
|
}
|
|
|
|
function connect(): {
|
|
client: ProtocolClient
|
|
socket: FakeSocket
|
|
events: ClientEvent[]
|
|
} {
|
|
const socket = new FakeSocket()
|
|
const events: ClientEvent[] = []
|
|
const client = new ProtocolClient({
|
|
url: 'wss://test/ws?proto=2',
|
|
createSocket: () => socket,
|
|
onEvent: event => events.push(event),
|
|
})
|
|
client.connect()
|
|
socket.open()
|
|
return { client, socket, events }
|
|
}
|
|
|
|
describe('protocol client', () => {
|
|
it('parses text frames and ignores binary or unreadable ones', () => {
|
|
const { socket, events } = connect()
|
|
socket.receive({ t: 'hello', n: 1, proto: 2, role: 'player', me: { id: 1, name: null } })
|
|
socket.receive('not json')
|
|
socket.onmessage?.({ data: new Uint8Array([1, 2]) })
|
|
expect(events.filter(event => event.type === 'frame')).toHaveLength(1)
|
|
})
|
|
|
|
it('correlates one ack per reqId and ignores an unknown ack', async () => {
|
|
const { client, socket } = connect()
|
|
const first = client.send('conn.ping', { ts: 11 })
|
|
const second = client.send('conn.ping', { ts: 12 })
|
|
expect(client.pendingCount).toBe(2)
|
|
const sent = socket.sent.map(raw => JSON.parse(raw) as { t: string; reqId: number; ts: number })
|
|
expect(sent.map(intent => intent.t)).toEqual(['conn.ping', 'conn.ping'])
|
|
expect(sent[0]?.reqId).toBe(1)
|
|
expect(sent[1]?.reqId).toBe(2)
|
|
|
|
socket.receive({ t: 'ack', n: 1, reqId: 2, ok: true, ts: 12 })
|
|
socket.receive({ t: 'ack', n: 2, reqId: 99, ok: true })
|
|
expect(client.pendingCount).toBe(1)
|
|
await expect(second).resolves.toMatchObject({ reqId: 2, ok: true })
|
|
|
|
socket.receive({ t: 'ack', n: 3, reqId: 1, ok: false, code: 'invalid' })
|
|
await expect(first).resolves.toMatchObject({ reqId: 1, ok: false, code: 'invalid' })
|
|
expect(client.pendingCount).toBe(0)
|
|
})
|
|
|
|
it('keeps pending requests across a hello on a live connection (6.3)', async () => {
|
|
const { client, socket } = connect()
|
|
const pending = client.send('player.setname', { name: 'Ann' })
|
|
socket.receive({ t: 'hello', n: 1, proto: 2, role: 'player', me: { id: 1, name: 'Ann' } })
|
|
expect(client.pendingCount).toBe(1)
|
|
socket.receive({ t: 'ack', n: 2, reqId: 1, ok: true })
|
|
await expect(pending).resolves.toMatchObject({ ok: true })
|
|
})
|
|
|
|
it('drops pending requests with the connection (6.3)', async () => {
|
|
const { client, socket } = connect()
|
|
const pending = client.send('conn.ping', { ts: 1 })
|
|
socket.serverClose(1006)
|
|
expect(client.pendingCount).toBe(0)
|
|
await expect(pending).rejects.toThrow(/closed/)
|
|
})
|
|
|
|
it('reads a server close reason from the last closing frame (6.5, FIXED #18)', () => {
|
|
const { socket, events } = connect()
|
|
socket.receive({ t: 'closing', n: 2, reason: 'revoked', code: 4001 })
|
|
socket.serverClose(1005)
|
|
expect(events.at(-1)).toEqual({ type: 'close', reason: 'revoked', code: 4001 })
|
|
})
|
|
|
|
it('treats a close without a closing frame as a transport failure', () => {
|
|
const { socket, events } = connect()
|
|
socket.serverClose(1006)
|
|
expect(events.at(-1)).toEqual({ type: 'close', reason: null, code: 1006 })
|
|
})
|
|
|
|
it('does not reconnect to a terminal outcome (single connect)', () => {
|
|
const socket = new FakeSocket()
|
|
const sockets: FakeSocket[] = [socket]
|
|
const client = new ProtocolClient({
|
|
url: 'wss://test/ws?proto=2',
|
|
createSocket: () => socket,
|
|
onEvent: () => {},
|
|
})
|
|
client.connect()
|
|
socket.open()
|
|
socket.receive({ t: 'closing', n: 1, reason: 'proto-mismatch', code: 4003 })
|
|
socket.serverClose(1005)
|
|
client.connect() // a no-op once closed
|
|
expect(sockets).toHaveLength(1)
|
|
expect(client.state).toBe('closed')
|
|
})
|
|
|
|
it('ignores a frame that arrives after the client closed the socket', () => {
|
|
const { client, socket, events } = connect()
|
|
client.close()
|
|
const before = events.length
|
|
socket.receive({ t: 'hello', n: 1, proto: 2, role: 'player', me: { id: 1, name: null } })
|
|
expect(events).toHaveLength(before)
|
|
})
|
|
|
|
it('refuses to send before the socket opens', async () => {
|
|
const socket = new FakeSocket()
|
|
const client = new ProtocolClient({
|
|
url: 'wss://test/ws?proto=2',
|
|
createSocket: () => socket,
|
|
onEvent: () => {},
|
|
})
|
|
client.connect()
|
|
await expect(client.send('conn.ping', { ts: 1 })).rejects.toThrow(/not connected/)
|
|
})
|
|
})
|