import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { GameConnection } from '$lib/protocol/connection'; import { FakeSocket, SocketSequence } from './fake-socket'; function welcome(overrides: Record = {}) { return { t: 'welcome', v: 1, session: 's1', role: 'spectator', index: 0, description: null, event: null, state: { currentList: [] }, seats: {}, ...overrides }; } describe('GameConnection', () => { let sockets: SocketSequence; let conn: GameConnection; beforeEach(() => { vi.useFakeTimers(); sockets = new SocketSequence(); conn = new GameConnection({ transport: sockets.factory, url: 'ws://test/ws', reconnectBaseMs: 100, reconnectMaxMs: 400, pingIntervalMs: 1_000 }); }); afterEach(() => { vi.useRealTimers(); }); it('handshakes with hello and records the session from welcome', () => { const statuses: string[] = []; conn.onStatus = (s) => statuses.push(s); conn.connect(); sockets.current.open(); expect(sockets.current.lastJson()).toEqual({ t: 'hello', v: 1 }); expect(conn.status).toBe('open'); let welcomed: unknown = null; conn.onMessage = (m) => (welcomed = m); sockets.current.serverSend(welcome({ session: 'abc' })); expect(conn.session).toBe('abc'); expect(welcomed).toMatchObject({ t: 'welcome' }); expect(statuses).toContain('open'); }); it('forwards update, seats, and error to their handlers', () => { const messages: string[] = []; let errorMessage: string | null = null; conn.onMessage = (m) => messages.push(m.t); conn.onError = (e) => (errorMessage = e.message); conn.connect(); sockets.current.open(); sockets.current.serverSend(welcome()); sockets.current.serverSend({ t: 'update', index: 1, description: 'd', event: { type: 'none' }, state: {} }); sockets.current.serverSend({ t: 'seats', seats: { a: null } }); sockets.current.serverSend({ t: 'error', code: 'rejected', message: 'nope' }); sockets.current.serverSend({ t: 'pong' }); expect(messages).toEqual([ 'welcome', 'update', 'seats', 'error', 'pong' ]); expect(errorMessage).toBe('nope'); }); it('sends outbound commands, claims, undo and redo', () => { conn.connect(); sockets.current.open(); sockets.current.serverSend(welcome()); conn.sendCmd('draw'); expect(sockets.current.lastJson()).toEqual({ t: 'cmd', name: 'draw', args: {} }); conn.sendCmd('addCharacter', { id: 'Berserker', edition: 'x' }); expect(sockets.current.lastJson()).toEqual({ t: 'cmd', name: 'addCharacter', args: { id: 'Berserker', edition: 'x' } }); conn.claim('Berserker'); expect(sockets.current.lastJson()).toEqual({ t: 'claim', characterId: 'Berserker' }); conn.undo(); expect(sockets.current.lastJson()).toEqual({ t: 'undo' }); conn.redo(); expect(sockets.current.lastJson()).toEqual({ t: 'redo' }); }); it('pings on the keepalive interval while open', () => { conn.connect(); sockets.current.open(); sockets.current.serverSend(welcome()); // clear the hello frame so we can see pings const before = sockets.current.sent.length; vi.advanceTimersByTime(1_000); expect(sockets.current.sent.slice(before)).toEqual([ JSON.stringify({ t: 'ping' }) ]); vi.advanceTimersByTime(2_000); expect(sockets.current.sent.slice(before)).toHaveLength(3); }); it('reconnects, resyncs via a fresh hello, and backs off across churn', () => { conn.connect(); const first = sockets.current; first.open(); first.serverSend(welcome({ session: 'abc' })); expect(conn.session).toBe('abc'); // Server drops the connection → first retry after the base delay. first.serverClose(); expect(conn.status).toBe('reconnecting'); vi.advanceTimersByTime(99); expect(sockets.sockets.length).toBe(1); vi.advanceTimersByTime(1); expect(sockets.sockets.length).toBe(2); // The reconnect re-sends hello (the resync). const second = sockets.current; second.open(); expect(second.lastJson()).toEqual({ t: 'hello', v: 1 }); expect(conn.status).toBe('open'); // A failed second attempt (closes without a welcome) doubles the backoff. second.serverClose(); vi.advanceTimersByTime(199); expect(sockets.sockets.length).toBe(2); vi.advanceTimersByTime(1); expect(sockets.sockets.length).toBe(3); expect(conn.status).toBe('reconnecting'); // A real welcome resets the backoff to the base. const third = sockets.current; third.open(); third.serverSend(welcome({ session: 'abc' })); third.serverClose(); vi.advanceTimersByTime(99); expect(sockets.sockets.length).toBe(3); vi.advanceTimersByTime(1); expect(sockets.sockets.length).toBe(4); }); it('resync() re-requests a full welcome', () => { conn.connect(); sockets.current.open(); sockets.current.serverSend(welcome()); conn.resync(); expect(sockets.current.lastJson()).toEqual({ t: 'hello', v: 1 }); }); it('does not reconnect after an intentional disconnect', () => { conn.connect(); sockets.current.open(); sockets.current.serverSend(welcome()); conn.disconnect(); expect(conn.status).toBe('closed'); vi.advanceTimersByTime(10_000); expect(sockets.sockets.length).toBe(1); }); it('ignores non-JSON frames', () => { conn.connect(); sockets.current.open(); sockets.current.serverSend('not json'); expect(conn.session).toBeNull(); }); });