|
2 | 2 |
|
3 | 3 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 4 |
|
| 5 | +const { realtimeTalkCtor, startMock, stopMock } = vi.hoisted(() => ({ |
| 6 | + realtimeTalkCtor: vi.fn(), |
| 7 | + startMock: vi.fn(), |
| 8 | + stopMock: vi.fn(), |
| 9 | +})); |
| 10 | + |
5 | 11 | describe("OpenClawApp Talk controls", () => { |
6 | 12 | beforeEach(() => { |
7 | | - vi.restoreAllMocks(); |
| 13 | + vi.resetModules(); |
| 14 | + vi.doMock("./chat/realtime-talk.ts", () => ({ |
| 15 | + RealtimeTalkSession: realtimeTalkCtor, |
| 16 | + })); |
| 17 | + realtimeTalkCtor.mockReset(); |
| 18 | + startMock.mockReset(); |
| 19 | + stopMock.mockReset(); |
| 20 | + realtimeTalkCtor.mockImplementation( |
| 21 | + function MockRealtimeTalkSession(this: { start: typeof startMock; stop: typeof stopMock }) { |
| 22 | + this.start = startMock; |
| 23 | + this.stop = stopMock; |
| 24 | + }, |
| 25 | + ); |
| 26 | + startMock.mockResolvedValue(undefined); |
8 | 27 | }); |
9 | 28 |
|
10 | 29 | it("retries Talk immediately when the previous session is already in error state", async () => { |
11 | | - await import("./app.ts"); |
12 | | - const app = document.createElement("openclaw-app") as unknown as { |
| 30 | + const { OpenClawApp } = await import("./app.ts"); |
| 31 | + const staleStop = vi.fn(); |
| 32 | + const app: { |
13 | 33 | client: unknown; |
14 | 34 | connected: boolean; |
| 35 | + lastError: string | null; |
15 | 36 | realtimeTalkActive: boolean; |
16 | 37 | realtimeTalkStatus: string; |
17 | 38 | realtimeTalkSession: { stop(): void } | null; |
18 | 39 | sessionKey: string; |
19 | | - toggleRealtimeTalk(): Promise<void>; |
| 40 | + } = { |
| 41 | + client: { request: vi.fn() }, |
| 42 | + connected: true, |
| 43 | + lastError: null, |
| 44 | + realtimeTalkActive: true, |
| 45 | + realtimeTalkStatus: "error", |
| 46 | + realtimeTalkSession: { stop: staleStop }, |
| 47 | + sessionKey: "main", |
20 | 48 | }; |
21 | | - const staleStop = vi.fn(); |
22 | | - const request = vi.fn().mockRejectedValue(new Error("session unavailable")); |
23 | | - app.client = { request } as never; |
24 | | - app.connected = true; |
25 | | - app.sessionKey = "main"; |
26 | | - app.realtimeTalkActive = true; |
27 | | - app.realtimeTalkStatus = "error"; |
28 | | - app.realtimeTalkSession = { stop: staleStop }; |
29 | 49 |
|
30 | | - await app.toggleRealtimeTalk(); |
| 50 | + await OpenClawApp.prototype.toggleRealtimeTalk.call(app as never); |
31 | 51 |
|
32 | 52 | expect(staleStop).toHaveBeenCalledOnce(); |
33 | | - expect(request).toHaveBeenCalledOnce(); |
34 | | - expect(request).toHaveBeenCalledWith("talk.realtime.session", { sessionKey: "main" }); |
35 | | - expect(app.realtimeTalkStatus).toBe("error"); |
36 | | - expect(app.realtimeTalkSession).toBeNull(); |
| 53 | + expect(realtimeTalkCtor).toHaveBeenCalledOnce(); |
| 54 | + expect(startMock).toHaveBeenCalledOnce(); |
| 55 | + expect(stopMock).not.toHaveBeenCalled(); |
| 56 | + expect(app.realtimeTalkStatus).toBe("connecting"); |
| 57 | + expect(app.realtimeTalkSession).not.toBeNull(); |
37 | 58 | }); |
38 | 59 | }); |
0 commit comments