|
| 1 | +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +type GatewayClientCallbacks = { |
| 4 | + onHelloOk?: () => void; |
| 5 | + onConnectError?: (err: Error) => void; |
| 6 | + onClose?: (code: number, reason: string) => void; |
| 7 | +}; |
| 8 | + |
| 9 | +const mockState = { |
| 10 | + gateways: [] as MockGatewayClient[], |
| 11 | + agentSideConnectionCtor: vi.fn(), |
| 12 | + agentStart: vi.fn(), |
| 13 | +}; |
| 14 | + |
| 15 | +class MockGatewayClient { |
| 16 | + private callbacks: GatewayClientCallbacks; |
| 17 | + |
| 18 | + constructor(opts: GatewayClientCallbacks) { |
| 19 | + this.callbacks = opts; |
| 20 | + mockState.gateways.push(this); |
| 21 | + } |
| 22 | + |
| 23 | + start(): void {} |
| 24 | + |
| 25 | + stop(): void { |
| 26 | + this.callbacks.onClose?.(1000, "gateway stopped"); |
| 27 | + } |
| 28 | + |
| 29 | + emitHello(): void { |
| 30 | + this.callbacks.onHelloOk?.(); |
| 31 | + } |
| 32 | + |
| 33 | + emitConnectError(message: string): void { |
| 34 | + this.callbacks.onConnectError?.(new Error(message)); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +vi.mock("@agentclientprotocol/sdk", () => ({ |
| 39 | + AgentSideConnection: class { |
| 40 | + constructor(factory: (conn: unknown) => unknown, stream: unknown) { |
| 41 | + mockState.agentSideConnectionCtor(factory, stream); |
| 42 | + factory({}); |
| 43 | + } |
| 44 | + }, |
| 45 | + ndJsonStream: vi.fn(() => ({ type: "mock-stream" })), |
| 46 | +})); |
| 47 | + |
| 48 | +vi.mock("../config/config.js", () => ({ |
| 49 | + loadConfig: () => ({ |
| 50 | + gateway: { |
| 51 | + mode: "local", |
| 52 | + }, |
| 53 | + }), |
| 54 | +})); |
| 55 | + |
| 56 | +vi.mock("../gateway/auth.js", () => ({ |
| 57 | + resolveGatewayAuth: () => ({}), |
| 58 | +})); |
| 59 | + |
| 60 | +vi.mock("../gateway/call.js", () => ({ |
| 61 | + buildGatewayConnectionDetails: () => ({ |
| 62 | + url: "ws://127.0.0.1:18789", |
| 63 | + }), |
| 64 | +})); |
| 65 | + |
| 66 | +vi.mock("../gateway/client.js", () => ({ |
| 67 | + GatewayClient: MockGatewayClient, |
| 68 | +})); |
| 69 | + |
| 70 | +vi.mock("./translator.js", () => ({ |
| 71 | + AcpGatewayAgent: class { |
| 72 | + start(): void { |
| 73 | + mockState.agentStart(); |
| 74 | + } |
| 75 | + |
| 76 | + handleGatewayReconnect(): void {} |
| 77 | + |
| 78 | + handleGatewayDisconnect(): void {} |
| 79 | + |
| 80 | + async handleGatewayEvent(): Promise<void> {} |
| 81 | + }, |
| 82 | +})); |
| 83 | + |
| 84 | +describe("serveAcpGateway startup", () => { |
| 85 | + let serveAcpGateway: typeof import("./server.js").serveAcpGateway; |
| 86 | + |
| 87 | + beforeAll(async () => { |
| 88 | + ({ serveAcpGateway } = await import("./server.js")); |
| 89 | + }); |
| 90 | + |
| 91 | + beforeEach(() => { |
| 92 | + mockState.gateways.length = 0; |
| 93 | + mockState.agentSideConnectionCtor.mockReset(); |
| 94 | + mockState.agentStart.mockReset(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("waits for gateway hello before creating AgentSideConnection", async () => { |
| 98 | + const signalHandlers = new Map<NodeJS.Signals, () => void>(); |
| 99 | + const onceSpy = vi.spyOn(process, "once").mockImplementation((( |
| 100 | + signal: NodeJS.Signals, |
| 101 | + handler: () => void, |
| 102 | + ) => { |
| 103 | + signalHandlers.set(signal, handler); |
| 104 | + return process; |
| 105 | + }) as typeof process.once); |
| 106 | + |
| 107 | + try { |
| 108 | + const servePromise = serveAcpGateway({}); |
| 109 | + await Promise.resolve(); |
| 110 | + |
| 111 | + expect(mockState.agentSideConnectionCtor).not.toHaveBeenCalled(); |
| 112 | + const gateway = mockState.gateways[0]; |
| 113 | + if (!gateway) { |
| 114 | + throw new Error("Expected mocked gateway instance"); |
| 115 | + } |
| 116 | + |
| 117 | + gateway.emitHello(); |
| 118 | + await vi.waitFor(() => { |
| 119 | + expect(mockState.agentSideConnectionCtor).toHaveBeenCalledTimes(1); |
| 120 | + }); |
| 121 | + |
| 122 | + signalHandlers.get("SIGINT")?.(); |
| 123 | + await servePromise; |
| 124 | + } finally { |
| 125 | + onceSpy.mockRestore(); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + it("rejects startup when gateway connect fails before hello", async () => { |
| 130 | + const onceSpy = vi |
| 131 | + .spyOn(process, "once") |
| 132 | + .mockImplementation( |
| 133 | + ((_signal: NodeJS.Signals, _handler: () => void) => process) as typeof process.once, |
| 134 | + ); |
| 135 | + |
| 136 | + try { |
| 137 | + const servePromise = serveAcpGateway({}); |
| 138 | + await Promise.resolve(); |
| 139 | + |
| 140 | + const gateway = mockState.gateways[0]; |
| 141 | + if (!gateway) { |
| 142 | + throw new Error("Expected mocked gateway instance"); |
| 143 | + } |
| 144 | + |
| 145 | + gateway.emitConnectError("connect failed"); |
| 146 | + await expect(servePromise).rejects.toThrow("connect failed"); |
| 147 | + expect(mockState.agentSideConnectionCtor).not.toHaveBeenCalled(); |
| 148 | + } finally { |
| 149 | + onceSpy.mockRestore(); |
| 150 | + } |
| 151 | + }); |
| 152 | +}); |
0 commit comments