|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const clientState = vi.hoisted(() => ({ |
| 4 | + options: null as Record<string, unknown> | null, |
| 5 | + startMode: "hello" as "hello" | "close", |
| 6 | + close: { code: 1008, reason: "pairing required" }, |
| 7 | + requestSpy: vi.fn(), |
| 8 | + stopSpy: vi.fn(), |
| 9 | + stopAndWaitSpy: vi.fn(async () => undefined), |
| 10 | +})); |
| 11 | + |
| 12 | +class MockGatewayClient { |
| 13 | + private readonly opts: Record<string, unknown>; |
| 14 | + |
| 15 | + constructor(opts: Record<string, unknown>) { |
| 16 | + this.opts = opts; |
| 17 | + clientState.options = opts; |
| 18 | + } |
| 19 | + |
| 20 | + start(): void { |
| 21 | + void Promise.resolve() |
| 22 | + .then(async () => { |
| 23 | + if (clientState.startMode === "close") { |
| 24 | + const onClose = this.opts.onClose; |
| 25 | + if (typeof onClose === "function") { |
| 26 | + onClose(clientState.close.code, clientState.close.reason); |
| 27 | + } |
| 28 | + return; |
| 29 | + } |
| 30 | + const onHelloOk = this.opts.onHelloOk; |
| 31 | + if (typeof onHelloOk === "function") { |
| 32 | + await onHelloOk(); |
| 33 | + } |
| 34 | + }) |
| 35 | + .catch(() => {}); |
| 36 | + } |
| 37 | + |
| 38 | + async request(method: string, params: unknown): Promise<unknown> { |
| 39 | + return await clientState.requestSpy(method, params); |
| 40 | + } |
| 41 | + |
| 42 | + stop(): void { |
| 43 | + clientState.stopSpy(); |
| 44 | + } |
| 45 | + |
| 46 | + async stopAndWait(): Promise<void> { |
| 47 | + await clientState.stopAndWaitSpy(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +vi.mock("./client-bootstrap.js", () => ({ |
| 52 | + resolveGatewayClientBootstrap: vi.fn(async () => ({ |
| 53 | + url: "ws://127.0.0.1:18789", |
| 54 | + auth: { token: "secret", password: undefined }, |
| 55 | + })), |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock("./client.js", () => ({ |
| 59 | + GatewayClient: MockGatewayClient, |
| 60 | +})); |
| 61 | + |
| 62 | +const { withOperatorApprovalsGatewayClient } = await import("./operator-approvals-client.js"); |
| 63 | + |
| 64 | +describe("withOperatorApprovalsGatewayClient", () => { |
| 65 | + beforeEach(() => { |
| 66 | + clientState.options = null; |
| 67 | + clientState.startMode = "hello"; |
| 68 | + clientState.close = { code: 1008, reason: "pairing required" }; |
| 69 | + clientState.requestSpy.mockReset().mockResolvedValue(undefined); |
| 70 | + clientState.stopSpy.mockReset(); |
| 71 | + clientState.stopAndWaitSpy.mockReset().mockResolvedValue(undefined); |
| 72 | + }); |
| 73 | + |
| 74 | + it("waits for hello before running the callback and stops cleanly", async () => { |
| 75 | + await withOperatorApprovalsGatewayClient( |
| 76 | + { |
| 77 | + config: {} as never, |
| 78 | + clientDisplayName: "Matrix approval (@owner:example.org)", |
| 79 | + }, |
| 80 | + async (client) => { |
| 81 | + await client.request("exec.approval.resolve", { |
| 82 | + id: "req-123", |
| 83 | + decision: "allow-once", |
| 84 | + }); |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + expect(clientState.options?.scopes).toEqual(["operator.approvals"]); |
| 89 | + expect(clientState.requestSpy).toHaveBeenCalledWith("exec.approval.resolve", { |
| 90 | + id: "req-123", |
| 91 | + decision: "allow-once", |
| 92 | + }); |
| 93 | + expect(clientState.stopAndWaitSpy).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it("surfaces close failures before hello", async () => { |
| 97 | + clientState.startMode = "close"; |
| 98 | + |
| 99 | + await expect( |
| 100 | + withOperatorApprovalsGatewayClient( |
| 101 | + { |
| 102 | + config: {} as never, |
| 103 | + clientDisplayName: "Matrix approval (@owner:example.org)", |
| 104 | + }, |
| 105 | + async () => undefined, |
| 106 | + ), |
| 107 | + ).rejects.toThrow("gateway closed (1008): pairing required"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments