|
| 1 | +// Tests for gateway runtime subscription wiring. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { emitAgentEvent, resetAgentEventsForTest } from "../infra/agent-events.js"; |
| 4 | +import type { SubsystemLogger } from "../logging/subsystem.js"; |
| 5 | +import { emitSessionLifecycleEvent } from "../sessions/session-lifecycle-events.js"; |
| 6 | +import { |
| 7 | + emitInternalSessionTranscriptUpdate, |
| 8 | + type InternalSessionTranscriptUpdate, |
| 9 | +} from "../sessions/transcript-events.js"; |
| 10 | +import { |
| 11 | + createChatRunState, |
| 12 | + createSessionEventSubscriberRegistry, |
| 13 | + createSessionMessageSubscriberRegistry, |
| 14 | + createToolEventRecipientRegistry, |
| 15 | +} from "./server-chat-state.js"; |
| 16 | + |
| 17 | +const warn = vi.fn(); |
| 18 | +const mockLog: SubsystemLogger = { |
| 19 | + subsystem: "gateway-test", |
| 20 | + isEnabled: () => true, |
| 21 | + trace: vi.fn(), |
| 22 | + debug: vi.fn(), |
| 23 | + info: vi.fn(), |
| 24 | + warn, |
| 25 | + error: vi.fn(), |
| 26 | + fatal: vi.fn(), |
| 27 | + raw: vi.fn(), |
| 28 | + child: () => mockLog, |
| 29 | +}; |
| 30 | + |
| 31 | +vi.mock("./server-chat.js", () => { |
| 32 | + throw new Error("server-chat lazy load failure"); |
| 33 | +}); |
| 34 | + |
| 35 | +vi.mock("./server-session-key.js", () => ({ |
| 36 | + resolveSessionKeyForRun: () => "agent:main:main", |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock("./server-session-events.js", () => ({ |
| 40 | + createTranscriptUpdateBroadcastHandler: () => () => { |
| 41 | + throw new Error("transcript handler failure"); |
| 42 | + }, |
| 43 | + createLifecycleEventBroadcastHandler: () => () => { |
| 44 | + throw new Error("lifecycle handler failure"); |
| 45 | + }, |
| 46 | +})); |
| 47 | + |
| 48 | +const { startGatewayEventSubscriptions } = await import("./server-runtime-subscriptions.js"); |
| 49 | +type SubscriptionParams = Parameters<typeof startGatewayEventSubscriptions>[0]; |
| 50 | + |
| 51 | +function createParams(): SubscriptionParams { |
| 52 | + return { |
| 53 | + log: mockLog, |
| 54 | + broadcast: vi.fn(), |
| 55 | + broadcastToConnIds: vi.fn(), |
| 56 | + nodeSendToSession: vi.fn(), |
| 57 | + agentRunSeq: new Map(), |
| 58 | + chatRunState: createChatRunState(), |
| 59 | + toolEventRecipients: createToolEventRecipientRegistry(), |
| 60 | + sessionEventSubscribers: createSessionEventSubscriberRegistry(), |
| 61 | + sessionMessageSubscribers: createSessionMessageSubscriberRegistry(), |
| 62 | + chatAbortControllers: new Map(), |
| 63 | + restartRecoveryCandidates: new Map(), |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +describe("startGatewayEventSubscriptions", () => { |
| 68 | + let unsubs: ReturnType<typeof startGatewayEventSubscriptions> | undefined; |
| 69 | + |
| 70 | + beforeEach(() => { |
| 71 | + vi.clearAllMocks(); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(() => { |
| 75 | + unsubs?.agentUnsub(); |
| 76 | + unsubs?.heartbeatUnsub(); |
| 77 | + unsubs?.transcriptUnsub(); |
| 78 | + unsubs?.lifecycleUnsub(); |
| 79 | + resetAgentEventsForTest(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("logs lazy agent event module failures", async () => { |
| 83 | + unsubs = startGatewayEventSubscriptions(createParams()); |
| 84 | + |
| 85 | + emitAgentEvent({ runId: "run-1", stream: "lifecycle", data: { phase: "start" } }); |
| 86 | + |
| 87 | + await vi.waitFor(() => expect(warn).toHaveBeenCalledTimes(1)); |
| 88 | + expect(warn).toHaveBeenCalledWith( |
| 89 | + "Agent event dispatch failed", |
| 90 | + expect.objectContaining({ runId: "run-1", stream: "lifecycle" }), |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("logs transcript handler failures", async () => { |
| 95 | + unsubs = startGatewayEventSubscriptions(createParams()); |
| 96 | + |
| 97 | + emitInternalSessionTranscriptUpdate({ |
| 98 | + sessionFile: "/tmp/sess.jsonl", |
| 99 | + sessionKey: "agent:main:main", |
| 100 | + } as InternalSessionTranscriptUpdate); |
| 101 | + |
| 102 | + await vi.waitFor(() => expect(warn).toHaveBeenCalledTimes(1)); |
| 103 | + expect(warn).toHaveBeenCalledWith( |
| 104 | + "Transcript update dispatch failed", |
| 105 | + expect.objectContaining({ sessionKey: "agent:main:main" }), |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("logs lifecycle handler failures", async () => { |
| 110 | + unsubs = startGatewayEventSubscriptions(createParams()); |
| 111 | + |
| 112 | + emitSessionLifecycleEvent({ sessionKey: "agent:main:main", reason: "created" }); |
| 113 | + |
| 114 | + await vi.waitFor(() => expect(warn).toHaveBeenCalledTimes(1)); |
| 115 | + expect(warn).toHaveBeenCalledWith( |
| 116 | + "Lifecycle event dispatch failed", |
| 117 | + expect.objectContaining({ sessionKey: "agent:main:main" }), |
| 118 | + ); |
| 119 | + }); |
| 120 | +}); |
0 commit comments