Skip to content

Commit 6d33976

Browse files
steipetePeter Steinberger
andauthored
fix(qa): authenticate actual scenario execution evidence (#117004)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent e84e2a4 commit 6d33976

2 files changed

Lines changed: 91 additions & 12 deletions

File tree

‎extensions/qa-lab/src/suite-summary.test.ts‎

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,69 @@ describe("qa suite summary helpers", () => {
4949
}
5050
});
5151

52+
it.each([
53+
["failure", readQaSuiteFailedScenarioCountFromFile],
54+
["failure and skip", readQaSuiteFailedOrSkippedScenarioCountFromFile],
55+
] as const)(
56+
"does not authenticate a positive total without completed %s evidence",
57+
async (_name, reader) => {
58+
await expect(
59+
readSummary({ counts: { total: 1, passed: 0, failed: 0, skipped: 0 } }, reader),
60+
).rejects.toThrow("did not include any executed scenarios");
61+
},
62+
);
63+
64+
it("does not let a claimed passed count override observed skipped-only scenarios", async () => {
65+
const summary = {
66+
counts: { total: 1, passed: 1, failed: 0, skipped: 0 },
67+
scenarios: [{ name: "never executed", status: "skip" }],
68+
};
69+
70+
await expect(readSummary(summary, readQaSuiteFailedScenarioCountFromFile)).rejects.toThrow(
71+
"did not include any executed scenarios",
72+
);
73+
await expect(
74+
readSummary(summary, readQaSuiteFailedOrSkippedScenarioCountFromFile),
75+
).resolves.toBe(1);
76+
});
77+
78+
it.each([
79+
["null", null],
80+
["array", []],
81+
["string", "corrupt counts"],
82+
["number", 1],
83+
] as const)(
84+
"rejects %s counts containers even when a scenario claims to pass",
85+
async (_name, counts) => {
86+
const summary = { counts, scenarios: [{ status: "pass" }] };
87+
88+
await expect(readSummary(summary, readQaSuiteFailedScenarioCountFromFile)).rejects.toThrow(
89+
"counts must be a non-array object",
90+
);
91+
await expect(
92+
readSummary(summary, readQaSuiteFailedOrSkippedScenarioCountFromFile),
93+
).rejects.toThrow("counts must be a non-array object");
94+
},
95+
);
96+
97+
it.each([
98+
["negative failed", { total: 1, passed: 1, failed: -1, skipped: 0 }],
99+
["fractional failed", { total: 1, passed: 1, failed: 0.75, skipped: 0 }],
100+
["unsafe passed", { total: 1, passed: Number.MAX_SAFE_INTEGER + 1, failed: 0, skipped: 0 }],
101+
] as const)(
102+
"rejects %s instead of normalizing an invalid execution count",
103+
async (_name, counts) => {
104+
const summary = { counts, scenarios: [{ status: "pass" }] };
105+
106+
await expect(readSummary(summary, readQaSuiteFailedScenarioCountFromFile)).rejects.toThrow(
107+
"must be a non-negative safe integer",
108+
);
109+
await expect(
110+
readSummary(summary, readQaSuiteFailedOrSkippedScenarioCountFromFile),
111+
).rejects.toThrow("must be a non-negative safe integer");
112+
},
113+
);
114+
52115
it("counts failed and skipped scenarios from scenario statuses", async () => {
53116
await expect(
54117
readSummary(
@@ -379,7 +442,7 @@ describe("qa suite summary helpers", () => {
379442
).resolves.toBe(1);
380443
await expect(
381444
readSummary(
382-
{ counts: { failed: 3.8 }, scenarios: [{ status: "pass" }, { status: "fail" }] },
445+
{ counts: { failed: 3 }, scenarios: [{ status: "pass" }, { status: "fail" }] },
383446
readQaSuiteFailedScenarioCountFromFile,
384447
),
385448
).resolves.toBe(3);
@@ -426,7 +489,7 @@ describe("qa suite summary helpers", () => {
426489

427490
it("rejects unsupported summary shapes", async () => {
428491
await expect(
429-
readSummary({ counts: { total: 2 } }, readQaSuiteFailedScenarioCountFromFile),
492+
readSummary({ counts: { total: 2, passed: 2 } }, readQaSuiteFailedScenarioCountFromFile),
430493
).rejects.toThrow("did not include counts.failed");
431494
await expect(
432495
readSummary("not-json-object", readQaSuiteFailedScenarioCountFromFile),
@@ -474,7 +537,7 @@ describe("qa suite summary helpers", () => {
474537
it("fails summary files without a failure signal", async () => {
475538
const outputDir = await fs.mkdtemp(path.join(os.tmpdir(), "qa-suite-summary-"));
476539
const summaryPath = path.join(outputDir, "qa-suite-summary.json");
477-
await fs.writeFile(summaryPath, JSON.stringify({ counts: { total: 1 } }), "utf8");
540+
await fs.writeFile(summaryPath, JSON.stringify({ counts: { total: 1, passed: 1 } }), "utf8");
478541

479542
try {
480543
await expect(readQaSuiteFailedScenarioCountFromFile(summaryPath)).rejects.toThrow(

‎extensions/qa-lab/src/suite-summary.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Qa Lab plugin module implements suite summary behavior.
22
import fs from "node:fs/promises";
33
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
4+
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
45
import { QaSuiteArtifactError } from "./errors.js";
56
import type { QaEvidenceSummaryJson, QaEvidenceTiming } from "./evidence-summary.js";
67
import type { QaProviderMode } from "./model-selection.js";
@@ -108,9 +109,7 @@ async function readQaSuiteSummaryFile(summaryPath: string): Promise<unknown> {
108109
}
109110

110111
function readNonNegativeCount(value: unknown): number | null {
111-
return typeof value === "number" && Number.isFinite(value)
112-
? Math.max(0, Math.floor(value))
113-
: null;
112+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : null;
114113
}
115114

116115
function assertQaSuiteSummaryHasExecutedScenarios(
@@ -128,25 +127,42 @@ function assertQaSuiteSummaryHasExecutedScenarios(
128127
scenarios?: unknown;
129128
entries?: unknown;
130129
};
130+
if (payload.counts !== undefined) {
131+
if (!isRecord(payload.counts)) {
132+
throw new QaSuiteArtifactError(
133+
errorCode,
134+
`QA summary at ${summaryPath} counts must be a non-array object.`,
135+
);
136+
}
137+
for (const countName of ["total", "passed", "failed", "skipped"] as const) {
138+
if (
139+
Object.hasOwn(payload.counts, countName) &&
140+
readNonNegativeCount(payload.counts[countName]) === null
141+
) {
142+
throw new QaSuiteArtifactError(
143+
errorCode,
144+
`QA summary at ${summaryPath} counts.${countName} must be a non-negative safe integer.`,
145+
);
146+
}
147+
}
148+
}
131149
const total = readNonNegativeCount(payload.counts?.total);
132150
const passed = readNonNegativeCount(payload.counts?.passed);
133151
const failed = readNonNegativeCount(payload.counts?.failed);
134-
const skipped = readNonNegativeCount(payload.counts?.skipped);
135152
const scenarios = Array.isArray(payload.scenarios)
136153
? (payload.scenarios as QaSuiteReportOnlyScenario[])
137154
: undefined;
138155
const entries = Array.isArray(payload.entries)
139156
? (payload.entries as QaEvidenceEntryStatus[])
140157
: undefined;
158+
const hasObservedOutcomeRows = (scenarios?.length ?? 0) > 0 || (entries?.length ?? 0) > 0;
141159
const hasCompletedScenario =
142160
scenarios?.some((scenario) => scenario.status === "pass" || scenario.status === "fail") ===
143161
true ||
144162
entries?.some((entry) => entry.result?.status === "pass" || entry.result?.status === "fail") ===
145163
true ||
146-
(passed ?? 0) > 0 ||
147-
(failed ?? 0) > 0;
148-
const hasExecutedScenario =
149-
hasCompletedScenario || (total !== null && total > 0 && (skipped === null || total > skipped));
164+
(failed ?? 0) > 0 ||
165+
(!hasObservedOutcomeRows && (passed ?? 0) > 0);
150166
const hasBlockingNonOptionalSkip =
151167
errorCode === "summary_blocking_count_missing" &&
152168
scenarios?.some(
@@ -173,7 +189,7 @@ function assertQaSuiteSummaryHasExecutedScenarios(
173189
scenarios?.length === 0 ||
174190
// A tolerated blocking result cannot authenticate a campaign that never completed a scenario.
175191
(requireExecutedScenario && !hasCompletedScenario) ||
176-
(!hasExecutedScenario &&
192+
(!hasCompletedScenario &&
177193
!hasBlockingUnknownOrFailedScenario &&
178194
!hasBlockingNonOptionalSkip &&
179195
!hasBlockingNonPassEvidence)

0 commit comments

Comments
 (0)