|
| 1 | +// TUI PTY evidence producer tests cover validation, command routing, and reports. |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + validateQaEvidenceSummaryJson, |
| 7 | + type QaSeedScenarioWithSource, |
| 8 | +} from "../../../../extensions/qa-lab/api.js"; |
| 9 | +import { useAutoCleanupTempDirTracker } from "../../../helpers/temp-dir.js"; |
| 10 | +import { |
| 11 | + buildTuiPtyVitestCommand, |
| 12 | + parseTuiPtyProducerOptions, |
| 13 | + runTuiPtyEvidenceProducer, |
| 14 | + validateTuiPtyScenario, |
| 15 | + verifyTuiPtyVitestReport, |
| 16 | + type TuiPtyCase, |
| 17 | +} from "./tui-pty-evidence-producer.js"; |
| 18 | + |
| 19 | +const SOURCE_PATH = "test/e2e/qa-lab/tui/tui-pty-evidence-producer.ts"; |
| 20 | +const HARNESS_FILE = "src/tui/tui-pty-harness.e2e.test.ts"; |
| 21 | +const LOCAL_FILE = "src/tui/tui-pty-local.e2e.test.ts"; |
| 22 | +const RESET_FILE = "src/tui/tui-reset-transition-pty.e2e.test.ts"; |
| 23 | +const COVERAGE_ID = "tui.message-composition"; |
| 24 | +const TEST_NAME = "TUI PTY harness drives the real TUI terminal loop"; |
| 25 | +const TEST_PATTERN = `^${TEST_NAME}$`; |
| 26 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 27 | +afterEach(() => vi.unstubAllEnvs()); |
| 28 | + |
| 29 | +function makeScenario( |
| 30 | + params: { |
| 31 | + cases?: unknown[]; |
| 32 | + primary?: string[]; |
| 33 | + secondary?: string[]; |
| 34 | + executionKind?: "script" | "vitest"; |
| 35 | + executionPath?: string; |
| 36 | + } = {}, |
| 37 | +): QaSeedScenarioWithSource { |
| 38 | + const execution = |
| 39 | + params.executionKind === "vitest" |
| 40 | + ? { |
| 41 | + kind: "vitest" as const, |
| 42 | + path: params.executionPath ?? SOURCE_PATH, |
| 43 | + config: { tuiPtyCases: params.cases ?? [makeCase()] }, |
| 44 | + } |
| 45 | + : { |
| 46 | + kind: "script" as const, |
| 47 | + path: params.executionPath ?? SOURCE_PATH, |
| 48 | + config: { tuiPtyCases: params.cases ?? [makeCase()] }, |
| 49 | + }; |
| 50 | + return { |
| 51 | + id: "tui-pty-producer-test", |
| 52 | + title: "TUI PTY producer test", |
| 53 | + surface: "tui", |
| 54 | + objective: "Prove the TUI PTY evidence producer contract.", |
| 55 | + successCriteria: ["The configured PTY assertion passes."], |
| 56 | + sourcePath: "qa/scenarios/ui/tui-pty-producer-test.yaml", |
| 57 | + coverage: { |
| 58 | + primary: params.primary ?? [], |
| 59 | + ...(params.secondary ? { secondary: params.secondary } : { secondary: [COVERAGE_ID] }), |
| 60 | + }, |
| 61 | + execution, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +function makeCase(overrides: Partial<TuiPtyCase> = {}): TuiPtyCase { |
| 66 | + return { |
| 67 | + coverageId: COVERAGE_ID, |
| 68 | + testFile: HARNESS_FILE, |
| 69 | + testNamePattern: TEST_PATTERN, |
| 70 | + ...overrides, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +async function makeTempRepo() { |
| 75 | + const repoRoot = tempDirs.make("openclaw-tui-pty-producer-"); |
| 76 | + for (const testFile of [HARNESS_FILE, LOCAL_FILE, RESET_FILE]) { |
| 77 | + const absolutePath = path.join(repoRoot, testFile); |
| 78 | + await fs.mkdir(path.dirname(absolutePath), { recursive: true }); |
| 79 | + await fs.writeFile(absolutePath, "// fixture\n", "utf8"); |
| 80 | + } |
| 81 | + return repoRoot; |
| 82 | +} |
| 83 | + |
| 84 | +async function writeReport( |
| 85 | + reportPath: string, |
| 86 | + params: { |
| 87 | + assertionName?: string; |
| 88 | + failed?: number; |
| 89 | + passed?: number; |
| 90 | + testFile?: string; |
| 91 | + } = {}, |
| 92 | +) { |
| 93 | + const assertionName = params.assertionName ?? TEST_NAME; |
| 94 | + await fs.mkdir(path.dirname(reportPath), { recursive: true }); |
| 95 | + await fs.writeFile( |
| 96 | + reportPath, |
| 97 | + `${JSON.stringify({ |
| 98 | + numFailedTests: params.failed ?? 0, |
| 99 | + numPassedTests: params.passed ?? 1, |
| 100 | + success: (params.failed ?? 0) === 0, |
| 101 | + testResults: [ |
| 102 | + { |
| 103 | + name: params.testFile ?? HARNESS_FILE, |
| 104 | + assertionResults: |
| 105 | + (params.passed ?? 1) > 0 |
| 106 | + ? [{ fullName: assertionName, status: "passed", title: assertionName }] |
| 107 | + : [], |
| 108 | + }, |
| 109 | + ], |
| 110 | + })}\n`, |
| 111 | + "utf8", |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +describe("TUI PTY evidence producer", () => { |
| 116 | + it("accepts only the two required CLI arguments exactly once", () => { |
| 117 | + expect( |
| 118 | + parseTuiPtyProducerOptions( |
| 119 | + ["--artifact-base", ".artifacts/pty", "--scenario-id", "scenario-id"], |
| 120 | + "/repo", |
| 121 | + ), |
| 122 | + ).toEqual({ |
| 123 | + artifactBase: path.resolve("/repo/.artifacts/pty"), |
| 124 | + repoRoot: path.resolve("/repo"), |
| 125 | + scenarioId: "scenario-id", |
| 126 | + }); |
| 127 | + expect(() => parseTuiPtyProducerOptions([], "/repo")).toThrow("--artifact-base is required"); |
| 128 | + expect(() => |
| 129 | + parseTuiPtyProducerOptions(["--artifact-base", "out", "--extra", "value"], "/repo"), |
| 130 | + ).toThrow("unsupported TUI PTY evidence producer argument"); |
| 131 | + expect(() => |
| 132 | + parseTuiPtyProducerOptions( |
| 133 | + ["--artifact-base", "one", "--artifact-base", "two", "--scenario-id", "id"], |
| 134 | + "/repo", |
| 135 | + ), |
| 136 | + ).toThrow("--artifact-base was provided more than once"); |
| 137 | + }); |
| 138 | + |
| 139 | + it("rejects arbitrary paths, traversal, malformed cases, and invalid patterns", () => { |
| 140 | + expect(() => |
| 141 | + validateTuiPtyScenario( |
| 142 | + makeScenario({ |
| 143 | + cases: [makeCase({ testFile: "../outside.test.ts" as typeof HARNESS_FILE })], |
| 144 | + }), |
| 145 | + ), |
| 146 | + ).toThrow("testFile is not allowlisted"); |
| 147 | + expect(() => |
| 148 | + validateTuiPtyScenario( |
| 149 | + makeScenario({ |
| 150 | + cases: [makeCase({ testFile: "src/tui/other.e2e.test.ts" as typeof HARNESS_FILE })], |
| 151 | + }), |
| 152 | + ), |
| 153 | + ).toThrow("testFile is not allowlisted"); |
| 154 | + expect(() => |
| 155 | + validateTuiPtyScenario(makeScenario({ cases: [{ ...makeCase(), extra: true }] })), |
| 156 | + ).toThrow("contains unsupported key extra"); |
| 157 | + expect(() => |
| 158 | + validateTuiPtyScenario(makeScenario({ cases: [makeCase({ testNamePattern: "[" })] })), |
| 159 | + ).toThrow("testNamePattern is invalid"); |
| 160 | + }); |
| 161 | + |
| 162 | + it("requires declared coverage, every primary ID, unique cases, and the producer path", () => { |
| 163 | + expect(() => |
| 164 | + validateTuiPtyScenario( |
| 165 | + makeScenario({ cases: [makeCase({ coverageId: "tui.output-safety" })] }), |
| 166 | + ), |
| 167 | + ).toThrow("coverage ID not owned by scenario"); |
| 168 | + expect(() => |
| 169 | + validateTuiPtyScenario( |
| 170 | + makeScenario({ |
| 171 | + primary: [COVERAGE_ID, "tui.output-safety"], |
| 172 | + cases: [makeCase()], |
| 173 | + }), |
| 174 | + ), |
| 175 | + ).toThrow("primary coverage IDs without TUI PTY cases: tui.output-safety"); |
| 176 | + expect(() => validateTuiPtyScenario(makeScenario({ cases: [makeCase(), makeCase()] }))).toThrow( |
| 177 | + "duplicate TUI PTY case", |
| 178 | + ); |
| 179 | + expect(() => validateTuiPtyScenario(makeScenario({ executionKind: "vitest" }))).toThrow( |
| 180 | + "execution.kind=script", |
| 181 | + ); |
| 182 | + expect(() => validateTuiPtyScenario(makeScenario({ executionPath: "test/other.ts" }))).toThrow( |
| 183 | + `must execute ${SOURCE_PATH}`, |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it("builds fake and local PTY commands with the required environment", () => { |
| 188 | + vi.stubEnv("OPENCLAW_TUI_PTY_INCLUDE_LOCAL", "1"); |
| 189 | + const fake = buildTuiPtyVitestCommand({ |
| 190 | + cases: [makeCase()], |
| 191 | + repoRoot: "/repo", |
| 192 | + reportPath: "/artifacts/report.json", |
| 193 | + }); |
| 194 | + expect(fake.args).toEqual( |
| 195 | + expect.arrayContaining([ |
| 196 | + "scripts/run-vitest.mjs", |
| 197 | + "run", |
| 198 | + "--config", |
| 199 | + "test/vitest/vitest.tui-pty.config.ts", |
| 200 | + HARNESS_FILE, |
| 201 | + "--reporter=json", |
| 202 | + "--outputFile.json=/artifacts/report.json", |
| 203 | + ]), |
| 204 | + ); |
| 205 | + expect(fake.env.OPENCLAW_BEHAVIOR_EVIDENCE).toBe("1"); |
| 206 | + expect(fake.env.OPENCLAW_TUI_PTY_INCLUDE_LOCAL).toBeUndefined(); |
| 207 | + |
| 208 | + const local = buildTuiPtyVitestCommand({ |
| 209 | + cases: [makeCase({ testFile: LOCAL_FILE })], |
| 210 | + repoRoot: "/repo", |
| 211 | + reportPath: "/artifacts/report.json", |
| 212 | + }); |
| 213 | + expect(local.args).toContain(LOCAL_FILE); |
| 214 | + expect(local.env.OPENCLAW_TUI_PTY_INCLUDE_LOCAL).toBe("1"); |
| 215 | + }); |
| 216 | + |
| 217 | + it("rejects wrong-file and unmatched-pattern reports", async () => { |
| 218 | + const repoRoot = await makeTempRepo(); |
| 219 | + await expect( |
| 220 | + verifyTuiPtyVitestReport({ |
| 221 | + cases: [makeCase()], |
| 222 | + repoRoot, |
| 223 | + report: { |
| 224 | + success: true, |
| 225 | + numFailedTests: 0, |
| 226 | + numPassedTests: 1, |
| 227 | + testResults: [ |
| 228 | + { |
| 229 | + name: RESET_FILE, |
| 230 | + assertionResults: [{ fullName: TEST_NAME, status: "passed" }], |
| 231 | + }, |
| 232 | + ], |
| 233 | + }, |
| 234 | + }), |
| 235 | + ).rejects.toThrow(`no result for configured test file ${HARNESS_FILE}`); |
| 236 | + await expect( |
| 237 | + verifyTuiPtyVitestReport({ |
| 238 | + cases: [makeCase()], |
| 239 | + repoRoot, |
| 240 | + report: { |
| 241 | + success: true, |
| 242 | + numFailedTests: 0, |
| 243 | + numPassedTests: 1, |
| 244 | + testResults: [ |
| 245 | + { |
| 246 | + name: HARNESS_FILE, |
| 247 | + assertionResults: [{ fullName: "another assertion", status: "passed" }], |
| 248 | + }, |
| 249 | + ], |
| 250 | + }, |
| 251 | + }), |
| 252 | + ).rejects.toThrow("no passed assertion"); |
| 253 | + }); |
| 254 | + |
| 255 | + it("fails when the child writes no fresh report or writes a stale report", async () => { |
| 256 | + const repoRoot = await makeTempRepo(); |
| 257 | + const artifactBase = path.join(repoRoot, ".artifacts", "missing-report"); |
| 258 | + const scenario = makeScenario(); |
| 259 | + const missing = await runTuiPtyEvidenceProducer( |
| 260 | + { artifactBase, repoRoot, scenarioId: scenario.id }, |
| 261 | + { |
| 262 | + loadScenario: () => scenario, |
| 263 | + runCommand: async () => ({ exitCode: 0, signal: null }), |
| 264 | + }, |
| 265 | + ); |
| 266 | + expect(missing.entries[0]?.result).toMatchObject({ |
| 267 | + status: "fail", |
| 268 | + failure: { reason: expect.stringContaining("did not write vitest-report.json") }, |
| 269 | + }); |
| 270 | + |
| 271 | + const staleBase = path.join(repoRoot, ".artifacts", "stale-report"); |
| 272 | + const stale = await runTuiPtyEvidenceProducer( |
| 273 | + { artifactBase: staleBase, repoRoot, scenarioId: scenario.id }, |
| 274 | + { |
| 275 | + loadScenario: () => scenario, |
| 276 | + now: () => 10_000, |
| 277 | + runCommand: async (command) => { |
| 278 | + const reportPath = command.args |
| 279 | + .find((arg) => arg.startsWith("--outputFile.json=")) |
| 280 | + ?.slice("--outputFile.json=".length); |
| 281 | + await writeReport(reportPath ?? ""); |
| 282 | + await fs.utimes(reportPath ?? "", new Date(0), new Date(0)); |
| 283 | + return { exitCode: 0, signal: null }; |
| 284 | + }, |
| 285 | + }, |
| 286 | + ); |
| 287 | + expect(stale.entries[0]?.result).toMatchObject({ |
| 288 | + status: "fail", |
| 289 | + failure: { reason: expect.stringContaining("report is stale") }, |
| 290 | + }); |
| 291 | + }); |
| 292 | + |
| 293 | + it("writes a sanitized proof matrix and passing QA evidence", async () => { |
| 294 | + const repoRoot = await makeTempRepo(); |
| 295 | + const artifactBase = path.join(repoRoot, ".artifacts", "passing-report"); |
| 296 | + const scenario = makeScenario(); |
| 297 | + const evidence = await runTuiPtyEvidenceProducer( |
| 298 | + { artifactBase, repoRoot, scenarioId: scenario.id }, |
| 299 | + { |
| 300 | + loadScenario: () => scenario, |
| 301 | + runCommand: async (command) => { |
| 302 | + const reportPath = command.args |
| 303 | + .find((arg) => arg.startsWith("--outputFile.json=")) |
| 304 | + ?.slice("--outputFile.json=".length); |
| 305 | + await writeReport(reportPath ?? "", { |
| 306 | + testFile: path.join(repoRoot, HARNESS_FILE), |
| 307 | + }); |
| 308 | + return { exitCode: 0, signal: null }; |
| 309 | + }, |
| 310 | + }, |
| 311 | + ); |
| 312 | + |
| 313 | + expect(validateQaEvidenceSummaryJson(evidence)).toEqual(evidence); |
| 314 | + expect(evidence.entries[0]).toMatchObject({ |
| 315 | + coverage: [{ id: COVERAGE_ID, role: "secondary" }], |
| 316 | + result: { status: "pass" }, |
| 317 | + }); |
| 318 | + const proofMatrix = JSON.parse( |
| 319 | + await fs.readFile(path.join(artifactBase, "proof-matrix.json"), "utf8"), |
| 320 | + ) as { cases: Array<{ coverageId: string; matchedAssertions: string[] }> }; |
| 321 | + expect(proofMatrix.cases).toEqual([ |
| 322 | + expect.objectContaining({ |
| 323 | + coverageId: COVERAGE_ID, |
| 324 | + matchedAssertions: [TEST_NAME], |
| 325 | + }), |
| 326 | + ]); |
| 327 | + const reportText = await fs.readFile(path.join(artifactBase, "vitest-report.json"), "utf8"); |
| 328 | + expect(reportText).toContain(`"name": "${HARNESS_FILE}"`); |
| 329 | + expect(reportText).not.toContain(repoRoot); |
| 330 | + await expect(fs.access(path.join(artifactBase, "latest-run.json"))).resolves.toBeUndefined(); |
| 331 | + await expect(fs.access(path.join(artifactBase, "qa-evidence.json"))).resolves.toBeUndefined(); |
| 332 | + }); |
| 333 | +}); |
0 commit comments