|
1 | | -import { EventEmitter } from "node:events"; |
2 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 2 |
|
4 | | -const sentMessages: Array<Record<string, unknown>> = []; |
5 | | -const lifecycleEvents: string[] = []; |
6 | | -const wsUrls: string[] = []; |
7 | | - |
8 | | -class MockWebSocket extends EventEmitter { |
9 | | - static readonly OPEN = 1; |
10 | | - static readonly CLOSED = 3; |
11 | | - |
12 | | - readyState = 0; |
13 | | - |
14 | | - constructor(url: string, _opts?: Record<string, unknown>) { |
15 | | - super(); |
16 | | - wsUrls.push(url); |
17 | | - |
18 | | - setTimeout(() => { |
19 | | - this.readyState = MockWebSocket.OPEN; |
20 | | - this.emit("open"); |
21 | | - }, 0); |
22 | | - } |
23 | | - |
24 | | - send(payload: string): void { |
25 | | - const parsed = JSON.parse(payload) as Record<string, unknown>; |
26 | | - sentMessages.push(parsed); |
27 | | - |
28 | | - // Respond to EOS (empty text) with isFinal |
29 | | - if (parsed.text === "") { |
30 | | - lifecycleEvents.push("eos"); |
31 | | - setTimeout(() => { |
32 | | - this.emit("message", Buffer.from(JSON.stringify({ isFinal: true }))); |
33 | | - }, 0); |
34 | | - return; |
35 | | - } |
| 3 | +/** |
| 4 | + * `vi.mock` factories are hoisted above the module body, and the `ws` mock |
| 5 | + * is pulled in while the runtime's python-websocket-bridge is evaluated — |
| 6 | + * before this file's own declarations initialize. So the mock and the state |
| 7 | + * it records live in `vi.hoisted`, which runs first by construction. |
| 8 | + */ |
| 9 | +const { sentMessages, lifecycleEvents, wsUrls, MockWebSocket } = |
| 10 | + await vi.hoisted(async () => { |
| 11 | + const { EventEmitter } = await import("node:events"); |
| 12 | + |
| 13 | + const sentMessages: Array<Record<string, unknown>> = []; |
| 14 | + const lifecycleEvents: string[] = []; |
| 15 | + const wsUrls: string[] = []; |
| 16 | + |
| 17 | + class MockWebSocket extends EventEmitter { |
| 18 | + static readonly OPEN = 1; |
| 19 | + static readonly CLOSED = 3; |
| 20 | + |
| 21 | + readyState = 0; |
| 22 | + |
| 23 | + constructor(url: string, _opts?: Record<string, unknown>) { |
| 24 | + super(); |
| 25 | + wsUrls.push(url); |
| 26 | + |
| 27 | + setTimeout(() => { |
| 28 | + this.readyState = MockWebSocket.OPEN; |
| 29 | + this.emit("open"); |
| 30 | + }, 0); |
| 31 | + } |
| 32 | + |
| 33 | + send(payload: string): void { |
| 34 | + const parsed = JSON.parse(payload) as Record<string, unknown>; |
| 35 | + sentMessages.push(parsed); |
36 | 36 |
|
37 | | - // Respond to non-init text with an audio chunk |
38 | | - const text = parsed.text as string | undefined; |
39 | | - if (text && text.trim() && text !== " ") { |
40 | | - setTimeout(() => { |
41 | | - this.emit( |
42 | | - "message", |
43 | | - Buffer.from( |
44 | | - JSON.stringify({ |
45 | | - audio: "ZmFrZS1hdWRpbw==", // base64: "fake-audio" |
46 | | - isFinal: false |
47 | | - }) |
48 | | - ) |
49 | | - ); |
50 | | - }, 0); |
| 37 | + // Respond to EOS (empty text) with isFinal |
| 38 | + if (parsed.text === "") { |
| 39 | + lifecycleEvents.push("eos"); |
| 40 | + setTimeout(() => { |
| 41 | + this.emit( |
| 42 | + "message", |
| 43 | + Buffer.from(JSON.stringify({ isFinal: true })) |
| 44 | + ); |
| 45 | + }, 0); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Respond to non-init text with an audio chunk |
| 50 | + const text = parsed.text as string | undefined; |
| 51 | + if (text && text.trim() && text !== " ") { |
| 52 | + setTimeout(() => { |
| 53 | + this.emit( |
| 54 | + "message", |
| 55 | + Buffer.from( |
| 56 | + JSON.stringify({ |
| 57 | + audio: "ZmFrZS1hdWRpbw==", // base64: "fake-audio" |
| 58 | + isFinal: false |
| 59 | + }) |
| 60 | + ) |
| 61 | + ); |
| 62 | + }, 0); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + close(): void { |
| 67 | + lifecycleEvents.push("close"); |
| 68 | + this.readyState = MockWebSocket.CLOSED; |
| 69 | + setTimeout(() => { |
| 70 | + this.emit("close", 1000, Buffer.from("")); |
| 71 | + }, 0); |
| 72 | + } |
51 | 73 | } |
52 | | - } |
53 | | - |
54 | | - close(): void { |
55 | | - lifecycleEvents.push("close"); |
56 | | - this.readyState = MockWebSocket.CLOSED; |
57 | | - setTimeout(() => { |
58 | | - this.emit("close", 1000, Buffer.from("")); |
59 | | - }, 0); |
60 | | - } |
61 | | -} |
| 74 | + |
| 75 | + return { sentMessages, lifecycleEvents, wsUrls, MockWebSocket }; |
| 76 | + }); |
62 | 77 |
|
63 | 78 | vi.mock("ws", () => ({ |
64 | 79 | WebSocket: MockWebSocket |
|
0 commit comments