|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { CloudflareRealtimeTransportLayer } from '../src/CloudflareRealtimeTransport'; |
| 3 | + |
| 4 | +class FakeWorkersWebSocket { |
| 5 | + url: string; |
| 6 | + listeners: Record<string, ((ev: any) => void)[]> = {}; |
| 7 | + accepted = false; |
| 8 | + constructor(url: string) { |
| 9 | + this.url = url; |
| 10 | + } |
| 11 | + addEventListener(type: string, listener: (ev: any) => void) { |
| 12 | + this.listeners[type] = this.listeners[type] || []; |
| 13 | + this.listeners[type].push(listener); |
| 14 | + } |
| 15 | + accept() { |
| 16 | + this.accepted = true; |
| 17 | + } |
| 18 | + send(_data: any) {} |
| 19 | + close() { |
| 20 | + this.emit('close', {}); |
| 21 | + } |
| 22 | + emit(type: string, ev: any) { |
| 23 | + (this.listeners[type] || []).forEach((fn) => fn(ev)); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +describe('CloudflareRealtimeTransportLayer', () => { |
| 28 | + let savedFetch: any; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + savedFetch = (globalThis as any).fetch; |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + (globalThis as any).fetch = savedFetch; |
| 36 | + }); |
| 37 | + |
| 38 | + it('connects via fetch upgrade and emits connection changes', async () => { |
| 39 | + const fakeSocket = new FakeWorkersWebSocket('ws://example'); |
| 40 | + const fetchSpy = vi.fn().mockResolvedValue({ |
| 41 | + webSocket: fakeSocket, |
| 42 | + status: 101, |
| 43 | + text: vi.fn().mockResolvedValue(''), |
| 44 | + }); |
| 45 | + (globalThis as any).fetch = fetchSpy; |
| 46 | + |
| 47 | + const transport = new CloudflareRealtimeTransportLayer({ |
| 48 | + url: 'wss://api.openai.com/v1/realtime?model=foo', |
| 49 | + }); |
| 50 | + |
| 51 | + const statuses: string[] = []; |
| 52 | + transport.on('connection_change', (s) => statuses.push(s)); |
| 53 | + |
| 54 | + await transport.connect({ apiKey: 'ek_test', model: 'foo' }); |
| 55 | + |
| 56 | + expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 57 | + // wss -> https |
| 58 | + expect(fetchSpy.mock.calls[0][0]).toBe( |
| 59 | + 'https://api.openai.com/v1/realtime?model=foo', |
| 60 | + ); |
| 61 | + const init = fetchSpy.mock.calls[0][1]; |
| 62 | + expect(init.method).toBe('GET'); |
| 63 | + expect(init.headers['Authorization']).toBe('Bearer ek_test'); |
| 64 | + expect(init.headers['Upgrade']).toBe('websocket'); |
| 65 | + expect(init.headers['Connection']).toBe('Upgrade'); |
| 66 | + expect(init.headers['Sec-WebSocket-Protocol']).toBe('realtime'); |
| 67 | + |
| 68 | + // connected without relying on 'open' listener. |
| 69 | + expect(statuses).toEqual(['connecting', 'connected']); |
| 70 | + }); |
| 71 | + |
| 72 | + it('propagates fetch-upgrade failures with detailed error', async () => { |
| 73 | + const fetchSpy = vi.fn().mockResolvedValue({ |
| 74 | + status: 400, |
| 75 | + text: vi.fn().mockResolvedValue('No upgrade'), |
| 76 | + }); |
| 77 | + (globalThis as any).fetch = fetchSpy; |
| 78 | + |
| 79 | + const transport = new CloudflareRealtimeTransportLayer({ |
| 80 | + url: 'wss://api.openai.com/v1/realtime?model=bar', |
| 81 | + }); |
| 82 | + |
| 83 | + await expect( |
| 84 | + transport.connect({ apiKey: 'ek_x', model: 'bar' }), |
| 85 | + ).rejects.toThrow('Failed to upgrade websocket: 400 No upgrade'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments