A minimal fake WS that records outbound frames and routes event listeners.
| 10 | |
| 11 | /** A minimal fake WS that records outbound frames and routes event listeners. */ |
| 12 | class FakeWebSocket implements WebSocketLike { |
| 13 | readonly sent: string[] = []; |
| 14 | closed: { code: number | undefined; reason: string | undefined } | null = null; |
| 15 | private readonly listeners = new Map<string, Array<(ev: any) => void>>(); |
| 16 | sendThrows = false; |
| 17 | closeThrows = false; |
| 18 | |
| 19 | send(data: string): void { |
| 20 | if (this.sendThrows) throw new Error('boom'); |
| 21 | this.sent.push(data); |
| 22 | } |
| 23 | |
| 24 | close(code?: number, reason?: string): void { |
| 25 | if (this.closeThrows) throw new Error('close-boom'); |
| 26 | this.closed = { code, reason }; |
| 27 | } |
| 28 | |
| 29 | addEventListener( |
| 30 | type: 'message' | 'close' | 'error', |
| 31 | listener: (ev: any) => void, |
| 32 | ): void { |
| 33 | const bucket = this.listeners.get(type) ?? []; |
| 34 | bucket.push(listener); |
| 35 | this.listeners.set(type, bucket); |
| 36 | } |
| 37 | |
| 38 | /** Drive inbound events from the test side. */ |
| 39 | emit(type: 'message' | 'close' | 'error', ev: any): void { |
| 40 | const bucket = this.listeners.get(type) ?? []; |
| 41 | for (const l of bucket) l(ev); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** A fake timer harness — caller can advance ticks deterministically. */ |
| 46 | function makeFakeTimerHarness(): { |
nothing calls this directly
no outgoing calls
no test coverage detected