|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createQaBusState } from "./bus-state.js"; |
| 3 | +import type { QaLabServerHandle } from "./lab-server.types.js"; |
| 4 | +import type { QaTransportAdapterFactory } from "./qa-transport-registry.js"; |
| 5 | +import { runQaFlowSuiteIsolated } from "./suite-run-isolated.js"; |
| 6 | +import { makeQaSuiteTestScenario } from "./suite-test-helpers.js"; |
| 7 | +import type { QaSuiteResolvedRunContext, QaSuiteRunner } from "./suite-types.js"; |
| 8 | + |
| 9 | +function createCleanupTestLab(): QaLabServerHandle { |
| 10 | + return { |
| 11 | + baseUrl: "http://127.0.0.1:43123", |
| 12 | + listenUrl: "http://127.0.0.1:43123", |
| 13 | + state: createQaBusState(), |
| 14 | + setControlUi: vi.fn(), |
| 15 | + setScenarioRun: vi.fn(), |
| 16 | + setLatestReport: vi.fn(), |
| 17 | + runSelfCheck: vi.fn(), |
| 18 | + stop: vi.fn(async () => {}), |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function createCleanupTestContext(): QaSuiteResolvedRunContext { |
| 23 | + return { |
| 24 | + startedAt: new Date("2026-08-04T00:00:00.000Z"), |
| 25 | + repoRoot: "/qa-repo", |
| 26 | + outputDir: "/qa-output", |
| 27 | + transportId: "qa-channel", |
| 28 | + selectedScenarios: [makeQaSuiteTestScenario("leased-channel-scenario")], |
| 29 | + providerMode: "mock-openai", |
| 30 | + primaryModel: "mock-openai/test-model", |
| 31 | + alternateModel: "mock-openai/test-model-alt", |
| 32 | + fastMode: true, |
| 33 | + channelDriver: "live", |
| 34 | + enabledPluginIds: [], |
| 35 | + gatewayConfigPatch: undefined, |
| 36 | + gatewayRuntimeOptions: undefined, |
| 37 | + concurrency: 1, |
| 38 | + progressEnabled: false, |
| 39 | + gatewayHeapCheckpointsEnabled: false, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe("isolated QA suite transport cleanup", () => { |
| 44 | + it.each(["cleanup", "cleanupAfterGatewayStop"] as const)( |
| 45 | + "retries a failed parent %s phase before disposing its owned lab", |
| 46 | + async (cleanupPhase) => { |
| 47 | + const lab = createCleanupTestLab(); |
| 48 | + const releaseError = new Error("credential release failed"); |
| 49 | + const release = vi |
| 50 | + .fn<() => Promise<void>>() |
| 51 | + .mockRejectedValueOnce(releaseError) |
| 52 | + .mockResolvedValueOnce(undefined); |
| 53 | + const factory: QaTransportAdapterFactory = { |
| 54 | + id: "leased", |
| 55 | + matches: ({ channelId, driver }) => channelId === "leased" && driver === "live", |
| 56 | + async create() { |
| 57 | + return { |
| 58 | + id: "leased", |
| 59 | + label: "Leased channel", |
| 60 | + accountId: "sut", |
| 61 | + requiredPluginIds: [], |
| 62 | + supportedActions: [], |
| 63 | + sendInbound: async (input) => lab.state.addInboundMessage(input), |
| 64 | + createGatewayConfig: () => ({}), |
| 65 | + async waitReady() {}, |
| 66 | + buildAgentDelivery: ({ target }) => ({ |
| 67 | + channel: "leased", |
| 68 | + to: target, |
| 69 | + replyChannel: "leased", |
| 70 | + replyTo: target, |
| 71 | + }), |
| 72 | + async handleAction() {}, |
| 73 | + createReportNotes: () => [], |
| 74 | + [cleanupPhase]: release, |
| 75 | + }; |
| 76 | + }, |
| 77 | + }; |
| 78 | + const runChild = vi.fn<QaSuiteRunner>(); |
| 79 | + |
| 80 | + await expect( |
| 81 | + runQaFlowSuiteIsolated( |
| 82 | + { |
| 83 | + adapterFactories: [factory], |
| 84 | + channelDriver: "live", |
| 85 | + channelId: "leased", |
| 86 | + startLab: async () => lab, |
| 87 | + }, |
| 88 | + createCleanupTestContext(), |
| 89 | + runChild, |
| 90 | + ), |
| 91 | + ).rejects.toBe(releaseError); |
| 92 | + |
| 93 | + expect(release).toHaveBeenCalledTimes(2); |
| 94 | + expect(runChild).not.toHaveBeenCalled(); |
| 95 | + expect(lab.stop).toHaveBeenCalledOnce(); |
| 96 | + }, |
| 97 | + ); |
| 98 | +}); |
0 commit comments