|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { expectDefined } from "@openclaw/normalization-core"; |
| 3 | +import { Command } from "commander"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { registerQaLabCli } from "./cli.js"; |
| 6 | +import { findQaScenarioMatches, renderQaScenarioMatchesMarkdownReport } from "./coverage-report.js"; |
| 7 | +import { defaultQaModelForMode, type QaProviderMode } from "./model-selection.js"; |
| 8 | +import { DEFAULT_QA_LIVE_PROVIDER_MODE } from "./providers/index.js"; |
| 9 | +import { readQaScenarioById, type QaSeedScenarioWithSource } from "./scenario-catalog.js"; |
| 10 | +import type { QaScorecardChannelDriver } from "./scorecard-taxonomy.js"; |
| 11 | +import { selectQaFlowSuiteScenarios } from "./suite-planning.js"; |
| 12 | +import { makeQaSuiteTestScenario } from "./suite-test-helpers.js"; |
| 13 | + |
| 14 | +vi.mock("openclaw/plugin-sdk/qa-runner-runtime", () => ({ |
| 15 | + listQaRunnerCliContributions: () => [], |
| 16 | +})); |
| 17 | + |
| 18 | +function captureReportedArgv(command: string): string[] { |
| 19 | + // Previews use POSIX quoting; Windows test hosts need Git Bash's sh on PATH. |
| 20 | + // Capture the shell-expanded arguments without starting pnpm or a QA provider. |
| 21 | + const argv = execFileSync("sh", ["-c", `pnpm() { printf '%s\\0' pnpm "$@"; }\n${command}`], { |
| 22 | + encoding: "utf8", |
| 23 | + timeout: 5_000, |
| 24 | + }).split("\0"); |
| 25 | + expect(argv.pop()).toBe(""); |
| 26 | + return argv; |
| 27 | +} |
| 28 | + |
| 29 | +function selectReportedCommands(scenarios: QaSeedScenarioWithSource[]) { |
| 30 | + const matches = scenarios.flatMap((scenario) => findQaScenarioMatches([scenario], scenario.id)); |
| 31 | + const report = renderQaScenarioMatchesMarkdownReport({ query: "selected scenarios", matches }); |
| 32 | + const commands = [...report.matchAll(/`(pnpm openclaw qa suite[^`]+)`/gu)]; |
| 33 | + expect(commands.length).toBeGreaterThan(0); |
| 34 | + return commands.map(([, command]) => { |
| 35 | + const argv = captureReportedArgv(command!); |
| 36 | + expect(argv.slice(0, 4)).toEqual(["pnpm", "openclaw", "qa", "suite"]); |
| 37 | + const program = new Command(); |
| 38 | + registerQaLabCli(program); |
| 39 | + const qa = expectDefined( |
| 40 | + program.commands.find((entry) => entry.name() === "qa"), |
| 41 | + "qa", |
| 42 | + ); |
| 43 | + const suite = expectDefined( |
| 44 | + qa.commands.find((entry) => entry.name() === "suite"), |
| 45 | + "suite", |
| 46 | + ); |
| 47 | + // Parse the real options without running the suite action or starting a provider. |
| 48 | + expect(suite.parseOptions(argv.slice(4))).toEqual({ operands: [], unknown: [] }); |
| 49 | + const opts = suite.opts<{ |
| 50 | + scenario: string[]; |
| 51 | + channelDriver?: QaScorecardChannelDriver; |
| 52 | + channel?: string; |
| 53 | + providerMode?: QaProviderMode; |
| 54 | + }>(); |
| 55 | + expect(opts.scenario.length).toBeGreaterThan(0); |
| 56 | + const providerMode = opts.providerMode ?? DEFAULT_QA_LIVE_PROVIDER_MODE; |
| 57 | + const selected = selectQaFlowSuiteScenarios({ |
| 58 | + scenarios, |
| 59 | + scenarioIds: opts.scenario, |
| 60 | + channelDriver: opts.channelDriver, |
| 61 | + channel: opts.channel, |
| 62 | + providerMode, |
| 63 | + primaryModel: defaultQaModelForMode(providerMode), |
| 64 | + }); |
| 65 | + expect( |
| 66 | + selected.map(({ id }) => id), |
| 67 | + command, |
| 68 | + ).toEqual(opts.scenario); |
| 69 | + return { |
| 70 | + driver: opts.channelDriver ?? "qa-channel", |
| 71 | + ids: selected.map(({ id }) => id), |
| 72 | + }; |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +describe("QA coverage command selection", () => { |
| 77 | + it.each([ |
| 78 | + ["telegram-assistant-transcript-role-boundary", "crabline"], |
| 79 | + ["native-command-session-target", "crabline"], |
| 80 | + ["matrix-room-generated-image-delivery", "live"], |
| 81 | + ["remember-across-conversations", "qa-channel"], |
| 82 | + ["whatsapp-access-control-group-disabled", "live"], |
| 83 | + ["instruction-followthrough-repo-contract", "qa-channel"], |
| 84 | + ["agent-startup-instruction-first-action", "qa-channel"], |
| 85 | + ["channel-canary", "qa-channel"], |
| 86 | + ])("selects exactly %s from its generated command", (id, driver) => { |
| 87 | + expect(selectReportedCommands([readQaScenarioById(id)])).toEqual([{ driver, ids: [id] }]); |
| 88 | + }); |
| 89 | + |
| 90 | + it("separates conflicting drivers on the same channel and groups compatible scenarios", () => { |
| 91 | + const scenarios = [ |
| 92 | + ...["crabline", "live"].flatMap((requiredChannelDriver) => |
| 93 | + ["first", "second"].map((suffix) => |
| 94 | + makeQaSuiteTestScenario(`${requiredChannelDriver}-${suffix}`, { |
| 95 | + channel: "telegram", |
| 96 | + config: { requiredChannelDriver, requiredProviderMode: "mock-openai" }, |
| 97 | + }), |
| 98 | + ), |
| 99 | + ), |
| 100 | + makeQaSuiteTestScenario("unconstrained", { channel: "telegram" }), |
| 101 | + ]; |
| 102 | + |
| 103 | + expect(selectReportedCommands(scenarios)).toEqual([ |
| 104 | + { driver: "crabline", ids: ["crabline-first", "crabline-second"] }, |
| 105 | + { driver: "live", ids: ["live-first", "live-second"] }, |
| 106 | + { driver: "live", ids: ["unconstrained"] }, |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("preserves a driver requirement without a pinned channel", () => { |
| 111 | + const scenario = makeQaSuiteTestScenario("driver-only", { |
| 112 | + config: { requiredChannelDriver: "crabline" }, |
| 113 | + }); |
| 114 | + |
| 115 | + expect(selectReportedCommands([scenario])).toEqual([ |
| 116 | + { driver: "crabline", ids: [scenario.id] }, |
| 117 | + ]); |
| 118 | + }); |
| 119 | + |
| 120 | + it("keeps a declared driver as one shell argument", () => { |
| 121 | + const requiredChannelDriver = "crabline' ; printf unexpected #"; |
| 122 | + const scenario = makeQaSuiteTestScenario("quoted-driver", { |
| 123 | + config: { requiredChannelDriver }, |
| 124 | + }); |
| 125 | + const report = renderQaScenarioMatchesMarkdownReport({ |
| 126 | + query: scenario.id, |
| 127 | + matches: findQaScenarioMatches([scenario], scenario.id), |
| 128 | + }); |
| 129 | + const command = expectDefined(report.match(/`(pnpm openclaw qa suite[^`]+)`/u)?.[1], "command"); |
| 130 | + |
| 131 | + expect(captureReportedArgv(command)).toEqual([ |
| 132 | + "pnpm", |
| 133 | + "openclaw", |
| 134 | + "qa", |
| 135 | + "suite", |
| 136 | + "--channel-driver", |
| 137 | + requiredChannelDriver, |
| 138 | + "--scenario", |
| 139 | + scenario.id, |
| 140 | + ]); |
| 141 | + }); |
| 142 | +}); |
0 commit comments