diff --git a/scripts/lib/ci-gate-failure.ts b/scripts/lib/ci-gate-failure.ts index e795dec1..c5454200 100644 --- a/scripts/lib/ci-gate-failure.ts +++ b/scripts/lib/ci-gate-failure.ts @@ -1,7 +1,28 @@ +import { stripVTControlCharacters } from "node:util"; + +/** The most lines of the first failure block the summary quotes. */ +export const FIRST_FAILURE_MAX_LINES = 40; + +// Where Playwright or vitest starts a failure block. Playwright numbers each +// failed test (` 1) [chromium] › tests/x.spec.ts:3:1 › title`). vitest prints +// a `Failed Suites 1` or `Failed Tests 2` rule and then a ` FAIL file > test` +// line for each, and an `Unhandled Errors` rule when an error escaped the +// tests (vitest 4.1, probed on desktop 2026-11-19). +const PLAYWRIGHT_FAILURE = /^\s*\d+\) .*:\d+:\d+ › /u; +const VITEST_FAILURES = /⎯ (Failed (Tests|Suites) \d+|Unhandled Errors) ⎯/u; +// Where that block ends: the next failure, Playwright's count lines +// (` 1 failed`, ` 68 passed (1.2m)`), vitest's `[1/6]` rule after each +// failure or the bare rule after the unhandled errors. +const PLAYWRIGHT_COUNT = /^\s*\d+ (failed|flaky|interrupted|skipped|did not run|passed)\b/u; +const VITEST_FAILURE_END = /⎯\[\d+\/\d+\]⎯|^⎯+$/u; + /** * 1112/08-13. What a failed `ci:gate` prints to stderr: the gate, the command - * that failed (or that none did), the log with the whole output, and the end - * of that output, where the cause usually is. + * that failed (or that none did), the log with the whole output, the first + * Playwright or vitest failure block in that command's output, and the end + * of the output. The end alone lost the assertion: a reporter prints counts, + * attachments and its own stderr after the failure, and a drill runs more + * commands after the test that failed (runner runs 371 and 376). */ export function formatGateFailure(input: Readonly<{ gateId: string; @@ -11,15 +32,64 @@ export function formatGateFailure(input: Readonly<{ output: readonly string[]; tailLines?: number; }>): string { - const tail = input.output + const lines = input.output .flatMap((chunk) => chunk.split(/\r?\n/u)) - .filter((line) => line.trim() !== "") - .slice(-(input.tailLines ?? 20)); + .filter((line) => line.trim() !== ""); + const tailStart = Math.max(0, lines.length - (input.tailLines ?? 20)); + const failure = firstFailureBlock(lines, input.failedCommand); + // The part of the block that the last lines show anyway is not repeated. + const quoted = failure + ? lines.slice(failure.start, Math.min(failure.end, tailStart)) + : []; + const omitted = Math.max(0, quoted.length - FIRST_FAILURE_MAX_LINES); return [ `${input.gateId} ${input.gateName}: FAIL`, ` failed command: ${input.failedCommand ?? "none (a check outside the commands)"}`, ` log: ${input.logPath}`, + ...(quoted.length > 0 + ? [ + " first failure in the log:", + ...quoted.slice(0, FIRST_FAILURE_MAX_LINES).map((line) => ` ${line}`), + ...(omitted > 0 ? [` … ${omitted} more lines of this failure in the log`] : []), + ] + : []), " last lines of the log:", - ...tail.map((line) => ` ${line}`), + ...lines.slice(tailStart).map((line) => ` ${line}`), ].join("\n"); } + +/** + * Where the first reporter failure block after the failed command's own line + * starts and ends (exclusive), as indexes into `lines`. An earlier command met + * its expectation, so a failure it printed (a negative fixture) is not the + * cause. Without a failed command there is no block. + */ +function firstFailureBlock( + lines: readonly string[], + failedCommand: string | undefined, +): { start: number; end: number } | undefined { + if (failedCommand === undefined) return undefined; + const commandIndex = lines.lastIndexOf(failedCommand); + if (commandIndex === -1) return undefined; + const plain = lines.map((line) => stripVTControlCharacters(line)); + const endOf = (start: number, isEnd: (line: string) => boolean) => { + let end = start + 1; + while (end < plain.length && !isEnd(plain[end] ?? "")) end += 1; + return end; + }; + for (let start = commandIndex + 1; start < plain.length; start += 1) { + const line = plain[start] ?? ""; + if (PLAYWRIGHT_FAILURE.test(line)) { + // The next failure or a count line is not part of this one. + return { + start, + end: endOf(start, (next) => PLAYWRIGHT_FAILURE.test(next) || PLAYWRIGHT_COUNT.test(next)), + }; + } + if (VITEST_FAILURES.test(line)) { + // The closing rule is. + return { start, end: Math.min(plain.length, endOf(start, (next) => VITEST_FAILURE_END.test(next)) + 1) }; + } + } + return undefined; +} diff --git a/tests/fixtures/ci-gate-failure/playwright-assertion.txt b/tests/fixtures/ci-gate-failure/playwright-assertion.txt new file mode 100644 index 00000000..b1a66868 --- /dev/null +++ b/tests/fixtures/ci-gate-failure/playwright-assertion.txt @@ -0,0 +1,63 @@ +$ corepack pnpm test:browser-capabilities + +Running 69 tests using 6 workers + + ✓ 5 [chromium] › tests/browser-capabilities/local-draft-composition.spec.ts:10:1 › composes the local-draft feature over the real browser IndexedDB runtime (1.4s) + ✓ 67 [webkit] › tests/browser-capabilities/opfs-runtime.spec.ts:456:1 › 05-01 a reconcile in another tab skips a live put and the put stays readable (2.1s) + ✓ 68 [webkit] › tests/browser-capabilities/indexeddb-runtime.spec.ts:1078:1 › 05-03 deletes a partition's dataset for an authorized ACCOUNT_DELETION when its budget row is corrupt (1.6s) + ✓ 69 [webkit] › tests/browser-capabilities/indexeddb-runtime.spec.ts:1187:1 › 05-06 an older tab meets a schema a newer tab upgraded and goes online-only (1.6s) + + + 1) [chromium] › tests/browser-capabilities/cross-context-invalidation.spec.ts:223:1 › falls back to native localStorage events and removes the pulse during cleanup + + Error: expect(received).toEqual(expected) // deep equality + + - Expected - 8 + + Received + 1 + + - Array [ + - Object { + - "ordering": "NEXT", + - "sequence": 1, + - "topic": "sample-topic-alpha", + - "transport": "STORAGE", + - }, + - ] + + Array [] + + Call Log: + - Timeout 5000ms exceeded while waiting on the predicate + + 247 | await expect + 248 | .poll(async () => (await snapshot(secondPage)).deliveries) + > 249 | .toEqual([ + | ^ + 250 | { + 251 | ordering: "NEXT", + 252 | sequence: 1, + at /workspace/repo/tests/browser-capabilities/cross-context-invalidation.spec.ts:249:8 + + attachment #1: screenshot (image/png) ────────────────────────────────────────────────────────── + artifacts/tests/browser-capabilities/results/cross-context-invalidation-f6965-es-the-pulse-during-cleanup-chromium/test-failed-1.png + ──────────────────────────────────────────────────────────────────────────────────────────────── + + Error Context: artifacts/tests/browser-capabilities/results/cross-context-invalidation-f6965-es-the-pulse-during-cleanup-chromium/error-context.md + + attachment #3: trace (application/zip) ───────────────────────────────────────────────────────── + artifacts/tests/browser-capabilities/results/cross-context-invalidation-f6965-es-the-pulse-during-cleanup-chromium/trace.zip + Usage: + + pnpm exec playwright show-trace artifacts/tests/browser-capabilities/results/cross-context-invalidation-f6965-es-the-pulse-during-cleanup-chromium/trace.zip + + ──────────────────────────────────────────────────────────────────────────────────────────────── + + 1 failed + [chromium] › tests/browser-capabilities/cross-context-invalidation.spec.ts:223:1 › falls back to native localStorage events and removes the pulse during cleanup + 68 passed (1.2m) +[ELIFECYCLE] Command failed with exit code 1. + +$ playwright test --config playwright.capabilities.config.ts +[WebServer] $ node scripts/generate-product-feature-index.ts && vite --host 127.0.0.1 --port 43989 --strictPort + +classification: UNEXPECTED_EXIT +expectation failed: expected pass, exit=1, signal=none diff --git a/tests/fixtures/ci-gate-failure/vitest-in-removal-drill.txt b/tests/fixtures/ci-gate-failure/vitest-in-removal-drill.txt new file mode 100644 index 00000000..892c840c --- /dev/null +++ b/tests/fixtures/ci-gate-failure/vitest-in-removal-drill.txt @@ -0,0 +1,108 @@ +$ corepack pnpm test:browser-file-storage-removal +Typecheck inputs: PASS (tsconfig.app.json 309, tsconfig.node.json 315, tsconfig.service-worker.json 22, tsconfig.test.json 800) +Static import graph: 310 modules, 862 dependencies, all imports resolved +Architecture graph fixtures: 31 regression checks PASS +TypeScript-only source policy: PASS (.storybook, scripts, src, tests, recipes; no fixture exceptions) + ✓ passes when a product feature needs a product capability  12423ms + + Test Files  1 failed | 193 passed (194) + Tests  6 failed | 2245 passed (2251) + Start at  04:04:27 + Duration  38.86s (transform 19.29s, setup 9.86s, import 62.35s, tests 240.80s, environment 2.10s) + +JUNIT report written to /workspace/repo/.tmp/browser-file-storage-runtime-removal-o54ad2/artifacts/tests/unit.xml +$ corepack pnpm check:types:app && corepack pnpm check:types:node && corepack pnpm check:types:test && corepack pnpm check:types:service-worker && node scripts/check-typecheck-inputs.ts +$ node scripts/generate-product-feature-index.ts && tsc --project tsconfig.app.json +$ node scripts/generate-product-feature-index.ts && tsc --project tsconfig.node.json +$ node scripts/generate-product-feature-index.ts && tsc --project tsconfig.test.json +$ tsc --project tsconfig.service-worker.json +$ eslint src scripts tests recipes .storybook vite.config.ts vitest.config.ts playwright*.config.ts --max-warnings=0 +$ node scripts/generate-product-feature-index.ts && node scripts/check-architecture.ts +$ corepack pnpm test:runtime-schema && corepack pnpm test:unit && corepack pnpm test:contract && corepack pnpm test:component && corepack pnpm test:integration && node scripts/run-vitest.ts run tests/features tests/scripts tests/scale/browser-soak.contract.test.ts --reporter=default && corepack pnpm test:platform && corepack pnpm check:release-coherence && corepack pnpm test:recipes +$ node scripts/run-vitest.ts run tests/runtime-schema --reporter=default --reporter=junit --outputFile.junit=artifacts/tests/runtime-schema.xml --passWithNoTests +$ node scripts/run-vitest.ts run tests/unit --reporter=default --reporter=junit --outputFile.junit=artifacts/tests/unit.xml + +⎯⎯⎯⎯⎯⎯⎯ Failed Tests 6 ⎯⎯⎯⎯⎯⎯⎯ + + FAIL  tests/unit/test-evidence-source-rules.test.ts > test evidence source rules > rejects a focused test +AssertionError: expected 'node:internal/fs/promises:637\n retu…' to contain 'focused test' + +- Expected ++ Received + +- focused test ++ node:internal/fs/promises:637 ++ return new FileHandle(await PromisePrototypeThen( ++ ^ ++ ++ Error: ENOENT: no such file or directory, open 'playwright.capabilities.config.ts' ++ at async open (node:internal/fs/promises:637:25) ++ at async readFile (node:internal/fs/promises:1269:14) ++ at async missingForbidOnly (file:///workspace/repo/.tmp/browser-file-storage-runtime-removal-o54ad2/scripts/check-test-evidence.ts:378:11) ++ at async file:///workspace/repo/.tmp/browser-file-storage-runtime-removal-o54ad2/scripts/check-test-evidence.ts:392:21 { ++ errno: -2, ++ code: 'ENOENT', ++ syscall: 'open', ++ path: 'playwright.capabilities.config.ts' ++ } ++ ++ Node.js v24.14.0 ++ + + ❯ tests/unit/test-evidence-source-rules.test.ts:40:27 +  38| const result = checkSource(`${FORBIDDEN}/focused-test.fixture`); +  39| expect(result.status).toBe(1); +  40| expect(result.stderr).toContain("focused test"); +  | ^ +  41| }, childProcessTimeout(1_333)); +  42| + +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/6]⎯ + +  92| + +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/6]⎯ + + FAIL  tests/unit/test-evidence-source-rules.test.ts > test evidence source rules > accepts a skip under an unexpired quarantine +AssertionError: expected 'node:internal/fs/promises:637\n retu…' to be '' // Object.is equality + +- Expected ++ Received + ++ node:internal/fs/promises:637 ++ return new FileHandle(await PromisePrototypeThen( ++ ^ ++ ++ Error: ENOENT: no such file or directory, open 'playwright.capabilities.config.ts' ++ at async open (node:internal/fs/promises:637:25) ++ at async readFile (node:internal/fs/promises:1269:14) ++ at async missingForbidOnly (file:///workspace/repo/.tmp/browser-file-storage-runtime-removal-o54ad2/scripts/check-test-evidence.ts:378:11) ++ at async file:///workspace/repo/.tmp/browser-file-storage-runtime-removal-o54ad2/scripts/check-test-evidence.ts:392:21 { ++ errno: -2, ++ code: 'ENOENT', ++ syscall: 'open', ++ path: 'playwright.capabilities.config.ts' ++ } ++ ++ Node.js v24.14.0 ++ + + ❯ tests/unit/test-evidence-source-rules.test.ts:108:27 + 106| ); + 107| const result = checkSource(file); + 108| expect(result.stderr).toBe(""); +  | ^ + 109| expect(result.status).toBe(0); + 110| }, childProcessTimeout(842)); + +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/6]⎯ + +$ node scripts/generate-product-feature-index.ts && node scripts/build-frontend.ts +$ node scripts/generate-product-feature-index.ts && node scripts/check-optional-recipes.ts +$ corepack pnpm check:artifact-schemas && node scripts/check-ci-contract.ts && corepack pnpm check:ci-workflow +$ node scripts/generate-product-feature-index.ts && node scripts/generate-artifact-schemas.ts --check +$ node scripts/generate-ci-workflow.ts --check +Browser file/storage runtime removal failed: test + +classification: UNEXPECTED_EXIT +expectation failed: expected pass, exit=1, signal=none diff --git a/tests/fixtures/ci-gate-failure/vitest-suites-tests-unhandled.txt b/tests/fixtures/ci-gate-failure/vitest-suites-tests-unhandled.txt new file mode 100644 index 00000000..dd213889 --- /dev/null +++ b/tests/fixtures/ci-gate-failure/vitest-suites-tests-unhandled.txt @@ -0,0 +1,103 @@ +$ corepack pnpm test:unit + RUN  v4.1.12 /workspace/repo + + ❯ tests/unit/zz-probe-suite.test.ts (0 test) + ❯ tests/unit/zz-probe-unhandled.test.ts (3 tests | 2 failed) 20ms + ✓ passes but leaves an unhandled rejection 13ms + × fails one 5ms + × fails two 1ms + +⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯ + + FAIL  tests/unit/zz-probe-suite.test.ts [ tests/unit/zz-probe-suite.test.ts ] +Error: probe suite failure at import + ❯ tests/unit/zz-probe-suite.test.ts:3:7 +  1| import { it } from "vitest"; +  2| +  3| throw new Error("probe suite failure at import"); +  | ^ +  4| +  5| it("never runs", () => {}); + +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/3]⎯ + + +⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯ + + FAIL  tests/unit/zz-probe-unhandled.test.ts > fails one +AssertionError: expected { a: 1 } to deeply equal { a: 2 } + +- Expected ++ Received + + { +- "a": 2, ++ "a": 1, + } + + ❯ tests/unit/zz-probe-unhandled.test.ts:10:20 +  8| +  9| it("fails one", () => { +  10| expect({ a: 1 }).toEqual({ a: 2 }); +  | ^ +  11| }); +  12| + +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/3]⎯ + + FAIL  tests/unit/zz-probe-unhandled.test.ts > fails two +AssertionError: expected [ 1, 2 ] to deeply equal [ 1, 3 ] + +- Expected ++ Received + + [ + 1, +- 3, ++ 2, + ] + + ❯ tests/unit/zz-probe-unhandled.test.ts:14:18 +  12| +  13| it("fails two", () => { +  14| expect([1, 2]).toEqual([1, 3]); +  | ^ +  15| }); +  16| + +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/3]⎯ + +⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯ + +Vitest caught 1 unhandled error during the test run. +This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected. + +⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯ +Error: probe unhandled rejection + ❯ tests/unit/zz-probe-unhandled.test.ts:4:23 +  2| +  3| it("passes but leaves an unhandled rejection", async () => { +  4| void Promise.reject(new Error("probe unhandled rejection")); +  | ^ +  5| await new Promise((resolve) => setTimeout(resolve, 10)); +  6| expect(1).toBe(1); + ❯ node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:302:11 + ❯ node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:1903:26 + ❯ node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:2326:20 + ❯ runWithCancel node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:2323:10 + ❯ node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:2305:20 + ❯ runWithTimeout node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:2272:10 + ❯ node_modules/.pnpm/@vitest+runner@4.1.12/node_modules/@vitest/runner/dist/chunk-artifact.js:2956:64 + +This error originated in "tests/unit/zz-probe-unhandled.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. +The latest test that might've caused the error is "passes but leaves an unhandled rejection". It might mean one of the following: +- The error was thrown, while Vitest was running this test. +- If the error occurred after the test had been completed, this was the last documented test before it was thrown. +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ + + + Test Files  2 failed (2) + Tests  2 failed | 1 passed (3) + Errors  1 error + Start at  18:03:15 + Duration  160ms (transform 27ms, setup 42ms, import 5ms, tests 20ms, environment 0ms) diff --git a/tests/unit/ci-gate-failure.test.ts b/tests/unit/ci-gate-failure.test.ts index e74abdac..308d26cb 100644 --- a/tests/unit/ci-gate-failure.test.ts +++ b/tests/unit/ci-gate-failure.test.ts @@ -2,11 +2,12 @@ import { spawnSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; +import { stripVTControlCharacters } from "node:util"; import { describe, expect, it } from "vitest"; import { loadCiGateContract } from "../../scripts/contracts/ci-gates.ts"; -import { formatGateFailure } from "../../scripts/lib/ci-gate-failure.ts"; +import { FIRST_FAILURE_MAX_LINES, formatGateFailure } from "../../scripts/lib/ci-gate-failure.ts"; import { FIXTURE_PNPM_ENVIRONMENT } from "../../scripts/lib/fixture-node-modules.ts"; import { PR_BASE_REF_VARIABLE } from "../../scripts/lib/pr-gate-selection.ts"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; @@ -70,6 +71,200 @@ describe("formatGateFailure", () => { }); }); +// Runner runs 371 and 376. The last 20 lines held the reporter's summary, the +// pnpm stderr and the classification, not the assertion: run 371's FE-GATE-008 +// job log showed the code frame of a failed `expect.poll(...).toEqual(...)` +// but neither the expected nor the received value, and run 376's FE-GATE-030 +// lost the vitest `FAIL` line and its ENOENT to the removal drill's later +// commands. Two fixtures are those gate logs (artifacts/quality/gates/), cut +// down, with the checkout path shortened; the third is vitest 4.1's own output +// for a suite that fails to load, two failed tests and an unhandled rejection +// (probed on desktop 2026-11-19). +describe("formatGateFailure with a reporter's failure block", () => { + const fixture = (name: string) => + readFile(path.join("tests/fixtures/ci-gate-failure", name), "utf8"); + // The reporters color their output; the assertions read the text. + const plain = (text: string) => stripVTControlCharacters(text); + /** The quoted first failure, without its four-space indent. */ + const firstFailure = (text: string) => { + const lines = plain(text).split("\n"); + const from = lines.indexOf(" first failure in the log:"); + const to = lines.indexOf(" last lines of the log:"); + return from === -1 ? [] : lines.slice(from + 1, to).map((line) => line.slice(4)); + }; + // Lines a reporter prints after the failures, enough to push them out of + // the last 20 lines. + const laterOutput = Array.from({ length: 25 }, (_, index) => `[WebServer] request ${index + 1}`); + const passedCommand = [ + "$ corepack pnpm test:e2e", + " 189 passed (1.8m)", + "classification: EXPECTED_PASS", + ]; + const failing = (command: string, output: readonly string[]) => + formatGateFailure({ + gateId: "FE-GATE-008", + gateName: "e2e", + logPath: "artifacts/quality/gates/FE-GATE-008.txt", + failedCommand: command, + output: [command, ...output], + }); + + it("quotes the first Playwright failure with its expected and received values", async () => { + const text = formatGateFailure({ + gateId: "FE-GATE-008", + gateName: "e2e", + logPath: "artifacts/quality/gates/FE-GATE-008.txt", + failedCommand: "$ corepack pnpm test:browser-capabilities", + output: [...passedCommand, await fixture("playwright-assertion.txt")], + }); + + expect(text).toContain( + "1) [chromium] › tests/browser-capabilities/cross-context-invalidation.spec.ts:223:1", + ); + expect(text).toContain("Error: expect(received).toEqual(expected) // deep equality"); + expect(text).toContain("- Expected - 8"); + expect(text).toContain("+ Received + 1"); + expect(text).toContain('- "sequence": 1,'); + expect(text).toContain("+ Array []"); + // The end of the log is still there. + expect(text).toContain("expectation failed: expected pass, exit=1, signal=none"); + }); + + it("quotes the first vitest failure that a drill printed before its later commands", async () => { + const text = plain(formatGateFailure({ + gateId: "FE-GATE-030", + gateName: "optional-removability", + logPath: "artifacts/quality/gates/FE-GATE-030.txt", + failedCommand: "$ corepack pnpm test:browser-file-storage-removal", + output: [await fixture("vitest-in-removal-drill.txt")], + })); + + expect(text).toContain( + "FAIL tests/unit/test-evidence-source-rules.test.ts > test evidence source rules > rejects a focused test", + ); + expect(text).toContain("AssertionError: expected 'node:internal/fs/promises:637"); + expect(text).toContain("to contain 'focused test'"); + expect(text).toContain( + "Error: ENOENT: no such file or directory, open 'playwright.capabilities.config.ts'", + ); + expect(text).toContain("Browser file/storage runtime removal failed: test"); + }); + + it("ends a Playwright failure where the next one starts", () => { + const quoted = firstFailure(failing("$ corepack pnpm test:e2e", [ + " 1) [chromium] › tests/e2e/first.spec.ts:3:1 › fails first", + " Error: the first failure", + " 2) [firefox] › tests/e2e/second.spec.ts:5:1 › fails second", + " Error: the second failure", + " 2 failed", + ...laterOutput, + ])); + + expect(quoted).toEqual([ + " 1) [chromium] › tests/e2e/first.spec.ts:3:1 › fails first", + " Error: the first failure", + ]); + }); + + it("ends a Playwright failure before the count lines", () => { + const quoted = firstFailure(failing("$ corepack pnpm test:e2e", [ + " 1) [chromium] › tests/e2e/only.spec.ts:3:1 › fails", + " Error: the only failure", + " 1 failed", + " [chromium] › tests/e2e/only.spec.ts:3:1 › fails", + " 68 passed (1.2m)", + ...laterOutput, + ])); + + expect(quoted).toEqual([ + " 1) [chromium] › tests/e2e/only.spec.ts:3:1 › fails", + " Error: the only failure", + ]); + }); + + it("ends a vitest failure with its closing rule", async () => { + const quoted = firstFailure(formatGateFailure({ + gateId: "FE-GATE-005", + gateName: "unit", + logPath: "artifacts/quality/gates/FE-GATE-005.txt", + failedCommand: "$ corepack pnpm test:unit", + output: [await fixture("vitest-suites-tests-unhandled.txt"), ...laterOutput], + })); + + expect(quoted[0]).toMatch(/⎯ Failed Suites 1 ⎯/u); + expect(quoted).toContain("Error: probe suite failure at import"); + expect(quoted.at(-1)).toMatch(/⎯\[1\/3\]⎯$/u); + expect(quoted.join("\n")).not.toContain("fails one"); + }); + + it("quotes vitest's unhandled errors when every test passed", async () => { + const probe = (await fixture("vitest-suites-tests-unhandled.txt")).split("\n"); + const unhandled = probe.findIndex((line) => plain(line).includes("⎯ Unhandled Errors ⎯")); + expect(unhandled).toBeGreaterThan(0); + const quoted = firstFailure(failing("$ corepack pnpm test:unit", [ + " ✓ tests/unit/passes.test.ts (1 test) 13ms", + ...probe.slice(unhandled), + ...laterOutput, + ])); + + expect(quoted[0]).toMatch(/⎯ Unhandled Errors ⎯/u); + expect(quoted).toContain("Error: probe unhandled rejection"); + expect(quoted.at(-1)).toMatch(/^⎯+$/u); + expect(quoted.join("\n")).not.toContain("Test Files"); + }); + + it("looks only in the output of the command that failed", async () => { + const text = formatGateFailure({ + gateId: "FE-GATE-008", + gateName: "e2e", + logPath: "artifacts/quality/gates/FE-GATE-008.txt", + failedCommand: "$ corepack pnpm test:browser-capabilities", + output: [ + // A negative fixture fails on purpose and meets its expectation. + "$ corepack pnpm check:fails-on-purpose", + " 1) [chromium] › tests/e2e/negative.spec.ts:3:1 › fails on purpose", + " Error: a failure this command was expected to print", + " 1 failed", + "classification: EXPECTED_FAILURE", + await fixture("playwright-assertion.txt"), + ], + }); + + expect(text).not.toContain("a failure this command was expected to print"); + expect(text).toContain("Error: expect(received).toEqual(expected) // deep equality"); + }); + + it("bounds a long failure and says how much more the log holds", () => { + const diff = Array.from({ length: 120 }, (_, index) => ` + line ${index + 1}`); + const block = [ + " 1) [chromium] › tests/e2e/long.spec.ts:3:1 › prints a long diff", + " Error: expect(received).toEqual(expected) // deep equality", + ...diff, + ]; + const text = failing("$ corepack pnpm test:e2e", [...block, " 1 failed", ...laterOutput]); + const quoted = firstFailure(text); + + expect(quoted).toEqual([ + ...block.slice(0, FIRST_FAILURE_MAX_LINES), + `… ${block.length - FIRST_FAILURE_MAX_LINES} more lines of this failure in the log`, + ]); + // Three heading lines, the quoted failure, the end of the log. + expect(text.split("\n")).toHaveLength(3 + 1 + FIRST_FAILURE_MAX_LINES + 1 + 1 + 20); + }); + + it("does not repeat a failure that the last lines already show", () => { + const text = failing("$ corepack pnpm test:e2e", [ + " 1) [chromium] › tests/e2e/short.spec.ts:3:1 › fails", + " Error: the one failure", + " 1 failed", + "classification: UNEXPECTED_EXIT", + ]); + + expect(firstFailure(text)).toEqual([]); + expect(text.split("Error: the one failure").length - 1).toBe(1); + }); +}); + // round9 confirm review. The runner itself hands the failed command to that // text: a gate whose one command fails names it, its log and its last line. // Same temp-root runner as tests/system/ci-runner/ci-evidence-freshness.test.ts,