|
| 1 | +// QA Lab product proof exercises hook delivery through a real Gateway child and qa-channel bus. |
| 2 | +import { setTimeout as sleep } from "node:timers/promises"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { startQaGatewayChild } from "./gateway-child.js"; |
| 6 | +import { startQaLabServer } from "./lab-server.js"; |
| 7 | +import { startQaProviderServer } from "./providers/server-runtime.js"; |
| 8 | +import { createQaChannelTransport } from "./qa-channel-transport.js"; |
| 9 | + |
| 10 | +const HOOK_TOKEN = "qa-hook-account-routing-token"; |
| 11 | +const MARKER = "QA_HOOK_ACCOUNT_ROUTING_OK"; |
| 12 | +const MODEL = "mock-openai/gpt-5.6-luna"; |
| 13 | + |
| 14 | +async function postJson(url: string, body: unknown, headers: Record<string, string>) { |
| 15 | + const response = await fetch(url, { |
| 16 | + method: "POST", |
| 17 | + headers: { |
| 18 | + "Content-Type": "application/json", |
| 19 | + ...headers, |
| 20 | + }, |
| 21 | + body: JSON.stringify(body), |
| 22 | + signal: AbortSignal.timeout(30_000), |
| 23 | + }); |
| 24 | + return { |
| 25 | + status: response.status, |
| 26 | + json: (await response.json()) as unknown, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe("hook agent account routing product proof", () => { |
| 31 | + it( |
| 32 | + "delivers exactly once through the selected qa-channel account", |
| 33 | + { timeout: 180_000 }, |
| 34 | + async () => { |
| 35 | + const repoRoot = fileURLToPath(new URL("../../../", import.meta.url)); |
| 36 | + const lab = await startQaLabServer({ |
| 37 | + repoRoot, |
| 38 | + embeddedGateway: "disabled", |
| 39 | + }); |
| 40 | + const mock = await startQaProviderServer("mock-openai", { |
| 41 | + modelRefs: [MODEL], |
| 42 | + }); |
| 43 | + let gateway: Awaited<ReturnType<typeof startQaGatewayChild>> | undefined; |
| 44 | + |
| 45 | + try { |
| 46 | + if (!mock) { |
| 47 | + throw new Error("mock-openai provider server did not start"); |
| 48 | + } |
| 49 | + const transport = createQaChannelTransport(lab.state); |
| 50 | + gateway = await startQaGatewayChild({ |
| 51 | + repoRoot, |
| 52 | + useRepoCli: true, |
| 53 | + providerBaseUrl: `${mock.baseUrl}/v1`, |
| 54 | + providerMode: "mock-openai", |
| 55 | + primaryModel: MODEL, |
| 56 | + alternateModel: MODEL, |
| 57 | + transportBaseUrl: lab.listenUrl, |
| 58 | + transport, |
| 59 | + controlUiEnabled: false, |
| 60 | + mutateConfig: (config) => { |
| 61 | + const qaChannel = config.channels?.["qa-channel"]; |
| 62 | + if (!qaChannel) { |
| 63 | + throw new Error("qa-channel transport config missing"); |
| 64 | + } |
| 65 | + return { |
| 66 | + ...config, |
| 67 | + hooks: { |
| 68 | + enabled: true, |
| 69 | + token: HOOK_TOKEN, |
| 70 | + path: "/hooks", |
| 71 | + }, |
| 72 | + channels: { |
| 73 | + ...config.channels, |
| 74 | + "qa-channel": { |
| 75 | + ...qaChannel, |
| 76 | + defaultAccount: "default", |
| 77 | + accounts: { |
| 78 | + default: { name: "Default" }, |
| 79 | + work: { name: "Work" }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }; |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const response = await postJson( |
| 88 | + `${gateway.baseUrl}/hooks/agent`, |
| 89 | + { |
| 90 | + message: `Reply exactly: ${MARKER}`, |
| 91 | + deliver: true, |
| 92 | + channel: "qa-channel", |
| 93 | + to: "dm:hook-recipient", |
| 94 | + accountId: "work", |
| 95 | + }, |
| 96 | + { Authorization: `Bearer ${HOOK_TOKEN}` }, |
| 97 | + ); |
| 98 | + expect(response.status, JSON.stringify(response.json)).toBe(200); |
| 99 | + |
| 100 | + const body = response.json as { ok?: boolean; runId?: string }; |
| 101 | + expect(body.ok).toBe(true); |
| 102 | + expect(body.runId).toEqual(expect.any(String)); |
| 103 | + |
| 104 | + await vi.waitFor( |
| 105 | + () => { |
| 106 | + const outbound = lab.state |
| 107 | + .getSnapshot() |
| 108 | + .messages.filter((message) => message.direction === "outbound"); |
| 109 | + expect(outbound).toHaveLength(1); |
| 110 | + expect(outbound[0]).toMatchObject({ |
| 111 | + accountId: "work", |
| 112 | + conversation: { id: "hook-recipient", kind: "direct" }, |
| 113 | + text: MARKER, |
| 114 | + }); |
| 115 | + }, |
| 116 | + { interval: 50, timeout: 60_000 }, |
| 117 | + ); |
| 118 | + await sleep(500); |
| 119 | + |
| 120 | + const outbound = lab.state |
| 121 | + .getSnapshot() |
| 122 | + .messages.filter((message) => message.direction === "outbound"); |
| 123 | + expect(outbound).toHaveLength(1); |
| 124 | + expect(outbound.filter((message) => message.accountId === "default")).toHaveLength(0); |
| 125 | + } finally { |
| 126 | + await gateway?.stop().catch(() => undefined); |
| 127 | + await mock?.stop().catch(() => undefined); |
| 128 | + await lab.stop().catch(() => undefined); |
| 129 | + } |
| 130 | + }, |
| 131 | + ); |
| 132 | +}); |
0 commit comments