|
| 1 | +import { createQaBusState } from "./bus-state.js"; |
| 2 | +import { readQaScenarioById, type QaScenarioFlow } from "./scenario-catalog.js"; |
| 3 | +import { runScenarioFlow } from "./scenario-flow-runner.js"; |
| 4 | + |
| 5 | +type QaFlowStep = { |
| 6 | + name: string; |
| 7 | + run: () => Promise<string | void>; |
| 8 | +}; |
| 9 | + |
| 10 | +function formatTestTranscript(state: ReturnType<typeof createQaBusState>) { |
| 11 | + return state |
| 12 | + .getSnapshot() |
| 13 | + .messages.map((message) => `${message.direction}:${message.conversation.id}:${message.text}`) |
| 14 | + .join("\n"); |
| 15 | +} |
| 16 | + |
| 17 | +export async function runLoadedScenarioFlow( |
| 18 | + scenarioId: string, |
| 19 | + params: { |
| 20 | + flow?: QaScenarioFlow; |
| 21 | + api?: Record<string, unknown>; |
| 22 | + state?: ReturnType<typeof createQaBusState>; |
| 23 | + omitOutboundSequence?: boolean; |
| 24 | + onWaitForOutboundMessage?: (params: { |
| 25 | + waitCount: number; |
| 26 | + state: ReturnType<typeof createQaBusState>; |
| 27 | + }) => void; |
| 28 | + } = {}, |
| 29 | +) { |
| 30 | + const scenario = readQaScenarioById(scenarioId); |
| 31 | + const loadedFlow = scenario.execution.flow; |
| 32 | + if (!loadedFlow) { |
| 33 | + throw new Error(`scenario has no flow: ${scenarioId}`); |
| 34 | + } |
| 35 | + |
| 36 | + const state = params.state ?? createQaBusState(); |
| 37 | + let waitCount = 0; |
| 38 | + const transport = { |
| 39 | + accountId: "qa-channel", |
| 40 | + state, |
| 41 | + reset: async () => { |
| 42 | + state.reset(); |
| 43 | + }, |
| 44 | + sendInbound: async (input: Parameters<typeof state.addInboundMessage>[0]) => |
| 45 | + state.addInboundMessage(input), |
| 46 | + sendNativeCommand: async ( |
| 47 | + input: Omit<Parameters<typeof state.addInboundMessage>[0], "nativeCommand" | "text"> & { |
| 48 | + command: string; |
| 49 | + }, |
| 50 | + ) => { |
| 51 | + const { command, ...message } = input; |
| 52 | + state.addInboundMessage({ |
| 53 | + ...message, |
| 54 | + text: `/${command}`, |
| 55 | + nativeCommand: { name: command }, |
| 56 | + }); |
| 57 | + }, |
| 58 | + waitForNoOutbound: async () => undefined, |
| 59 | + waitForOutbound: async (input: { |
| 60 | + conversation?: { id: string; kind: string }; |
| 61 | + sinceIndex?: number; |
| 62 | + textIncludes?: string; |
| 63 | + timeoutMs?: number; |
| 64 | + }) => { |
| 65 | + waitCount += 1; |
| 66 | + params.onWaitForOutboundMessage?.({ waitCount, state }); |
| 67 | + const match = state |
| 68 | + .getSnapshot() |
| 69 | + .messages.filter((candidate) => candidate.direction === "outbound") |
| 70 | + .slice(input.sinceIndex ?? 0) |
| 71 | + .find( |
| 72 | + (candidate) => |
| 73 | + (!input.conversation || candidate.conversation.id === input.conversation.id) && |
| 74 | + (!input.conversation || candidate.conversation.kind === input.conversation.kind) && |
| 75 | + (!input.textIncludes || candidate.text.includes(input.textIncludes)), |
| 76 | + ); |
| 77 | + if (match) { |
| 78 | + state.resolvePollCursor({ |
| 79 | + accountId: "qa-channel", |
| 80 | + cursor: state.getSnapshot().cursor, |
| 81 | + }); |
| 82 | + return match; |
| 83 | + } |
| 84 | + throw new Error(`timed out after ${input.timeoutMs}ms waiting for outbound marker`); |
| 85 | + }, |
| 86 | + ...(params.omitOutboundSequence |
| 87 | + ? {} |
| 88 | + : { |
| 89 | + waitForOutboundSequence: async () => { |
| 90 | + throw new Error("outbound sequence not configured for this fixture"); |
| 91 | + }, |
| 92 | + }), |
| 93 | + }; |
| 94 | + const api = { |
| 95 | + env: { |
| 96 | + providerMode: "mock-openai", |
| 97 | + gateway: { |
| 98 | + restartAfterStateMutation: async (mutate: (context: unknown) => Promise<void>) => { |
| 99 | + await mutate({}); |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + transport, |
| 104 | + state, |
| 105 | + scenario, |
| 106 | + config: scenario.execution.config ?? {}, |
| 107 | + randomUUID: () => "00000000-0000-4000-8000-000000000000", |
| 108 | + liveTurnTimeoutMs: (_env: unknown, timeoutMs: number) => timeoutMs, |
| 109 | + waitForGatewayHealthy: async () => undefined, |
| 110 | + waitForTransportReady: async () => undefined, |
| 111 | + waitForQaChannelReady: async () => undefined, |
| 112 | + waitForNoOutbound: async () => undefined, |
| 113 | + waitForCondition: async <T>(check: () => T | Promise<T | undefined>) => { |
| 114 | + for (let attempt = 0; attempt < 10; attempt += 1) { |
| 115 | + const value = await check(); |
| 116 | + if (value !== undefined) { |
| 117 | + return value; |
| 118 | + } |
| 119 | + } |
| 120 | + throw new Error("test condition was not met"); |
| 121 | + }, |
| 122 | + sleep: async () => undefined, |
| 123 | + reset: async () => { |
| 124 | + state.reset(); |
| 125 | + }, |
| 126 | + resetBus: async () => { |
| 127 | + state.reset(); |
| 128 | + }, |
| 129 | + runAgentPrompt: async () => undefined, |
| 130 | + formatTransportTranscript: formatTestTranscript, |
| 131 | + waitForOutboundMessage: async ( |
| 132 | + stateLocal: ReturnType<typeof createQaBusState>, |
| 133 | + predicate: (candidate: unknown) => boolean, |
| 134 | + timeoutMs: number, |
| 135 | + options?: { sinceIndex?: number }, |
| 136 | + ) => { |
| 137 | + waitCount += 1; |
| 138 | + params.onWaitForOutboundMessage?.({ waitCount, state: stateLocal }); |
| 139 | + const match = stateLocal |
| 140 | + .getSnapshot() |
| 141 | + .messages.slice(options?.sinceIndex ?? 0) |
| 142 | + .find((candidate) => predicate(candidate)); |
| 143 | + if (match) { |
| 144 | + return match; |
| 145 | + } |
| 146 | + throw new Error(`timed out after ${timeoutMs}ms waiting for outbound marker`); |
| 147 | + }, |
| 148 | + runScenario: async (_name: string, steps: QaFlowStep[]) => { |
| 149 | + const stepResults = []; |
| 150 | + for (const step of steps) { |
| 151 | + const details = await step.run(); |
| 152 | + stepResults.push({ |
| 153 | + name: step.name, |
| 154 | + status: "pass" as const, |
| 155 | + ...(details !== undefined ? { details } : {}), |
| 156 | + }); |
| 157 | + } |
| 158 | + return { |
| 159 | + name: scenario.title, |
| 160 | + status: "pass" as const, |
| 161 | + steps: stepResults, |
| 162 | + }; |
| 163 | + }, |
| 164 | + ...params.api, |
| 165 | + }; |
| 166 | + |
| 167 | + return await runScenarioFlow({ |
| 168 | + api, |
| 169 | + scenarioTitle: scenario.title, |
| 170 | + flow: params.flow ?? loadedFlow, |
| 171 | + }); |
| 172 | +} |
0 commit comments