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}`, ) }) })