|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const gatewayClientMock = vi.hoisted(() => { |
| 4 | + const request = vi.fn(async (_method?: string, _params?: unknown, _opts?: unknown) => ({ |
| 5 | + ok: true, |
| 6 | + })); |
| 7 | + const stopAndWait = vi.fn(async () => {}); |
| 8 | + const stop = vi.fn(); |
| 9 | + const constructorCalls: Array<Record<string, unknown>> = []; |
| 10 | + let startMode: "hello" | "connect-error" = "hello"; |
| 11 | + |
| 12 | + class MockGatewayClient { |
| 13 | + private readonly options: Record<string, unknown>; |
| 14 | + |
| 15 | + constructor(options: Record<string, unknown>) { |
| 16 | + this.options = options; |
| 17 | + constructorCalls.push(options); |
| 18 | + } |
| 19 | + |
| 20 | + start() { |
| 21 | + queueMicrotask(() => { |
| 22 | + if (startMode === "connect-error") { |
| 23 | + const onConnectError = this.options.onConnectError; |
| 24 | + if (typeof onConnectError === "function") { |
| 25 | + onConnectError(new Error("connect boom")); |
| 26 | + } |
| 27 | + return; |
| 28 | + } |
| 29 | + const onHelloOk = this.options.onHelloOk; |
| 30 | + if (typeof onHelloOk === "function") { |
| 31 | + onHelloOk({}); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + async request(method: string, params?: unknown, opts?: unknown) { |
| 37 | + return await request(method, params, opts); |
| 38 | + } |
| 39 | + |
| 40 | + async stopAndWait() { |
| 41 | + await stopAndWait(); |
| 42 | + } |
| 43 | + |
| 44 | + stop() { |
| 45 | + stop(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return { |
| 50 | + MockGatewayClient, |
| 51 | + request, |
| 52 | + stopAndWait, |
| 53 | + stop, |
| 54 | + constructorCalls, |
| 55 | + reset() { |
| 56 | + request.mockReset().mockResolvedValue({ ok: true }); |
| 57 | + stopAndWait.mockReset().mockResolvedValue(undefined); |
| 58 | + stop.mockReset(); |
| 59 | + constructorCalls.splice(0, constructorCalls.length); |
| 60 | + startMode = "hello"; |
| 61 | + }, |
| 62 | + setStartMode(mode: "hello" | "connect-error") { |
| 63 | + startMode = mode; |
| 64 | + }, |
| 65 | + }; |
| 66 | +}); |
| 67 | + |
| 68 | +vi.mock("./runtime-api.js", () => ({ |
| 69 | + GatewayClient: gatewayClientMock.MockGatewayClient, |
| 70 | +})); |
| 71 | + |
| 72 | +import { startQaGatewayRpcClient } from "./gateway-rpc-client.js"; |
| 73 | + |
| 74 | +describe("startQaGatewayRpcClient", () => { |
| 75 | + beforeEach(() => { |
| 76 | + gatewayClientMock.reset(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("starts a gateway client without device identity and forwards requests", async () => { |
| 80 | + const client = await startQaGatewayRpcClient({ |
| 81 | + wsUrl: "ws://127.0.0.1:18789", |
| 82 | + token: "qa-token", |
| 83 | + logs: () => "qa logs", |
| 84 | + }); |
| 85 | + |
| 86 | + expect(gatewayClientMock.constructorCalls[0]).toEqual( |
| 87 | + expect.objectContaining({ |
| 88 | + url: "ws://127.0.0.1:18789", |
| 89 | + token: "qa-token", |
| 90 | + deviceIdentity: null, |
| 91 | + scopes: [ |
| 92 | + "operator.admin", |
| 93 | + "operator.read", |
| 94 | + "operator.write", |
| 95 | + "operator.approvals", |
| 96 | + "operator.pairing", |
| 97 | + "operator.talk.secrets", |
| 98 | + ], |
| 99 | + }), |
| 100 | + ); |
| 101 | + |
| 102 | + await expect( |
| 103 | + client.request("agent.run", { prompt: "hi" }, { expectFinal: true, timeoutMs: 45_000 }), |
| 104 | + ).resolves.toEqual({ ok: true }); |
| 105 | + |
| 106 | + expect(gatewayClientMock.request).toHaveBeenCalledWith( |
| 107 | + "agent.run", |
| 108 | + { prompt: "hi" }, |
| 109 | + { |
| 110 | + expectFinal: true, |
| 111 | + timeoutMs: 45_000, |
| 112 | + }, |
| 113 | + ); |
| 114 | + |
| 115 | + await client.stop(); |
| 116 | + expect(gatewayClientMock.stopAndWait).toHaveBeenCalledTimes(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it("wraps request failures with gateway logs", async () => { |
| 120 | + gatewayClientMock.request.mockRejectedValueOnce(new Error("gateway not connected")); |
| 121 | + const client = await startQaGatewayRpcClient({ |
| 122 | + wsUrl: "ws://127.0.0.1:18789", |
| 123 | + token: "qa-token", |
| 124 | + logs: () => "qa logs", |
| 125 | + }); |
| 126 | + |
| 127 | + await expect(client.request("health")).rejects.toThrow( |
| 128 | + "gateway not connected\nGateway logs:\nqa logs", |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("wraps connect failures with gateway logs", async () => { |
| 133 | + gatewayClientMock.setStartMode("connect-error"); |
| 134 | + |
| 135 | + await expect( |
| 136 | + startQaGatewayRpcClient({ |
| 137 | + wsUrl: "ws://127.0.0.1:18789", |
| 138 | + token: "qa-token", |
| 139 | + logs: () => "qa logs", |
| 140 | + }), |
| 141 | + ).rejects.toThrow("connect boom\nGateway logs:\nqa logs"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments