|
1 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { createQaBusState } from "./bus-state.js"; |
3 | 3 | import type { QaLabServerHandle } from "./lab-server.types.js"; |
4 | 4 | import type { QaTransportAdapterFactory } from "./qa-transport-registry.js"; |
5 | 5 | import { runQaFlowSuiteIsolated } from "./suite-run-isolated.js"; |
| 6 | +import { runQaFlowSuiteStandard } from "./suite-run-standard.js"; |
6 | 7 | import { makeQaSuiteTestScenario } from "./suite-test-helpers.js"; |
7 | | -import type { QaSuiteResolvedRunContext, QaSuiteRunner } from "./suite-types.js"; |
| 8 | +import type { |
| 9 | + QaSuiteResolvedRunContext, |
| 10 | + QaSuiteRunner, |
| 11 | + QaSuiteScenarioRunner, |
| 12 | +} from "./suite-types.js"; |
| 13 | + |
| 14 | +const mocks = vi.hoisted(() => ({ |
| 15 | + disposeRegisteredAgentHarnesses: vi.fn(async () => {}), |
| 16 | + fetchWithSsrFGuard: vi.fn(async () => ({ |
| 17 | + response: new Response(null, { status: 204 }), |
| 18 | + release: vi.fn(async () => {}), |
| 19 | + })), |
| 20 | + startQaGatewayChild: vi.fn(async () => ({ |
| 21 | + baseUrl: "http://127.0.0.1:18789", |
| 22 | + token: "qa-test-token", |
| 23 | + cfg: {}, |
| 24 | + getProcessCpuMs: () => null, |
| 25 | + getProcessRssBytes: () => null, |
| 26 | + stop: vi.fn(async () => {}), |
| 27 | + })), |
| 28 | + writeQaSuiteArtifacts: vi.fn(async () => ({ |
| 29 | + evidence: undefined, |
| 30 | + evidencePath: "/qa-output/qa-evidence.json", |
| 31 | + report: "", |
| 32 | + reportPath: "/qa-output/qa-suite-report.md", |
| 33 | + summaryPath: "/qa-output/qa-suite-summary.json", |
| 34 | + })), |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock("openclaw/plugin-sdk/agent-harness", () => ({ |
| 38 | + disposeRegisteredAgentHarnesses: mocks.disposeRegisteredAgentHarnesses, |
| 39 | +})); |
| 40 | +vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({ |
| 41 | + fetchWithSsrFGuard: mocks.fetchWithSsrFGuard, |
| 42 | +})); |
| 43 | +vi.mock("./gateway-child.js", () => ({ |
| 44 | + startQaGatewayChild: mocks.startQaGatewayChild, |
| 45 | +})); |
| 46 | +vi.mock("./providers/server-runtime.js", () => ({ |
| 47 | + startQaProviderServer: vi.fn(async () => undefined), |
| 48 | +})); |
| 49 | +vi.mock("./suite-artifacts.js", () => ({ |
| 50 | + writeQaSuiteArtifacts: mocks.writeQaSuiteArtifacts, |
| 51 | +})); |
| 52 | +vi.mock("./suite-runtime-gateway.js", () => ({ |
| 53 | + waitForGatewayHealthy: vi.fn(async () => {}), |
| 54 | + waitForTransportReady: vi.fn(async () => {}), |
| 55 | +})); |
| 56 | +vi.mock("./web-runtime.js", () => ({ |
| 57 | + closeQaWebSessions: vi.fn(async () => {}), |
| 58 | +})); |
8 | 59 |
|
9 | 60 | function createCleanupTestLab(): QaLabServerHandle { |
10 | 61 | return { |
@@ -41,6 +92,132 @@ function createCleanupTestContext(): QaSuiteResolvedRunContext { |
41 | 92 | } |
42 | 93 |
|
43 | 94 | describe("isolated QA suite transport cleanup", () => { |
| 95 | + beforeEach(() => { |
| 96 | + vi.clearAllMocks(); |
| 97 | + mocks.disposeRegisteredAgentHarnesses.mockResolvedValue(undefined); |
| 98 | + }); |
| 99 | + |
| 100 | + it("retains passing artifacts and finishes owned cleanup before reporting teardown failure", async () => { |
| 101 | + const lab = createCleanupTestLab(); |
| 102 | + const release = vi.fn(async () => {}); |
| 103 | + const factory: QaTransportAdapterFactory = { |
| 104 | + id: "leased", |
| 105 | + matches: ({ channelId, driver }) => channelId === "leased" && driver === "live", |
| 106 | + async create() { |
| 107 | + return { |
| 108 | + id: "leased", |
| 109 | + label: "Leased channel", |
| 110 | + accountId: "sut", |
| 111 | + requiredPluginIds: [], |
| 112 | + supportedActions: [], |
| 113 | + sendInbound: async (input) => lab.state.addInboundMessage(input), |
| 114 | + createGatewayConfig: () => ({}), |
| 115 | + async waitReady() {}, |
| 116 | + buildAgentDelivery: ({ target }) => ({ |
| 117 | + channel: "leased", |
| 118 | + to: target, |
| 119 | + replyChannel: "leased", |
| 120 | + replyTo: target, |
| 121 | + }), |
| 122 | + async handleAction() {}, |
| 123 | + createReportNotes: () => [], |
| 124 | + cleanup: release, |
| 125 | + }; |
| 126 | + }, |
| 127 | + }; |
| 128 | + const cleanupError = new Error("agent harness disposal failed"); |
| 129 | + mocks.disposeRegisteredAgentHarnesses.mockRejectedValueOnce(cleanupError); |
| 130 | + const stderrWrite = vi.spyOn(process.stderr, "write").mockImplementation(() => true); |
| 131 | + const runChild = vi.fn<QaSuiteRunner>().mockResolvedValue({ |
| 132 | + outputDir: "/qa-child", |
| 133 | + evidencePath: "/qa-child/qa-evidence.json", |
| 134 | + reportPath: "/qa-child/qa-suite-report.md", |
| 135 | + summaryPath: "/qa-child/qa-suite-summary.json", |
| 136 | + report: "", |
| 137 | + scenarios: [{ name: "leased-channel-scenario", status: "pass", steps: [] }], |
| 138 | + watchUrl: lab.baseUrl, |
| 139 | + }); |
| 140 | + const context = createCleanupTestContext(); |
| 141 | + context.progressEnabled = true; |
| 142 | + |
| 143 | + const thrown = await runQaFlowSuiteIsolated( |
| 144 | + { |
| 145 | + adapterFactories: [factory], |
| 146 | + channelDriver: "live", |
| 147 | + channelId: "leased", |
| 148 | + startLab: async () => lab, |
| 149 | + }, |
| 150 | + context, |
| 151 | + runChild, |
| 152 | + ).catch((error: unknown) => error); |
| 153 | + |
| 154 | + expect(release).toHaveBeenCalledOnce(); |
| 155 | + expect(mocks.disposeRegisteredAgentHarnesses).toHaveBeenCalledOnce(); |
| 156 | + expect(lab.stop).toHaveBeenCalledOnce(); |
| 157 | + expect(lab.setLatestReport).toHaveBeenCalledWith( |
| 158 | + expect.objectContaining({ outputPath: "/qa-output/qa-suite-report.md" }), |
| 159 | + ); |
| 160 | + expect((thrown as Error).message.split("\n")[0]).toBe( |
| 161 | + "QA scenarios passed, but cleanup failed", |
| 162 | + ); |
| 163 | + expect((thrown as Error).message).toContain( |
| 164 | + "failed cleanup phases: agent harnesses: agent harness disposal failed", |
| 165 | + ); |
| 166 | + expect((thrown as Error).message).toContain( |
| 167 | + "retained artifacts: output=/qa-output report=/qa-output/qa-suite-report.md summary=/qa-output/qa-suite-summary.json", |
| 168 | + ); |
| 169 | + expect((thrown as Error).cause).toBe(cleanupError); |
| 170 | + expect(stderrWrite.mock.calls.flat().join("")).not.toContain("run complete"); |
| 171 | + stderrWrite.mockRestore(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("prints one generic completion after a real nested standard run and parent cleanup", async () => { |
| 175 | + const parentLab = createCleanupTestLab(); |
| 176 | + const childLab = createCleanupTestLab(); |
| 177 | + const startLab = vi |
| 178 | + .fn<() => Promise<QaLabServerHandle>>() |
| 179 | + .mockResolvedValueOnce(parentLab) |
| 180 | + .mockResolvedValueOnce(childLab); |
| 181 | + const context = createCleanupTestContext(); |
| 182 | + context.channelDriver = undefined; |
| 183 | + context.progressEnabled = true; |
| 184 | + const runScenario = vi |
| 185 | + .fn<QaSuiteScenarioRunner>() |
| 186 | + .mockResolvedValue({ name: "leased-channel-scenario", status: "pass", steps: [] }); |
| 187 | + const runChild: QaSuiteRunner = async (childParams) => { |
| 188 | + if (!childParams) { |
| 189 | + throw new Error("expected nested standard run params"); |
| 190 | + } |
| 191 | + return await runQaFlowSuiteStandard( |
| 192 | + childParams, |
| 193 | + { |
| 194 | + ...context, |
| 195 | + startedAt: new Date("2026-08-04T00:00:01.000Z"), |
| 196 | + outputDir: childParams.outputDir ?? "/qa-output/scenarios/leased-channel-scenario", |
| 197 | + concurrency: 1, |
| 198 | + }, |
| 199 | + runScenario, |
| 200 | + ); |
| 201 | + }; |
| 202 | + const stderrWrite = vi.spyOn(process.stderr, "write").mockImplementation(() => true); |
| 203 | + |
| 204 | + try { |
| 205 | + await runQaFlowSuiteIsolated({ startLab }, context, runChild); |
| 206 | + |
| 207 | + const completionLines = stderrWrite.mock.calls |
| 208 | + .flat() |
| 209 | + .join("") |
| 210 | + .split("\n") |
| 211 | + .filter((line) => line.startsWith("[qa-suite] run complete")); |
| 212 | + expect(completionLines).toEqual(["[qa-suite] run complete"]); |
| 213 | + expect(runScenario).toHaveBeenCalledOnce(); |
| 214 | + expect(childLab.stop).toHaveBeenCalledOnce(); |
| 215 | + expect(parentLab.stop).toHaveBeenCalledOnce(); |
| 216 | + } finally { |
| 217 | + stderrWrite.mockRestore(); |
| 218 | + } |
| 219 | + }); |
| 220 | + |
44 | 221 | it.each(["cleanup", "cleanupAfterGatewayStop"] as const)( |
45 | 222 | "retries a failed parent %s phase before disposing its owned lab", |
46 | 223 | async (cleanupPhase) => { |
|
0 commit comments