|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { parse as parseYaml } from 'yaml'; |
| 3 | + |
| 4 | +import { UI_COVERAGE, rootWorkflow } from './helpers/root-ci'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The pin on the coverage shard leg's reporter set (objectui#9177). |
| 8 | + * |
| 9 | + * ## What this guards, and why nothing else could |
| 10 | + * |
| 11 | + * The root `ui-coverage-nightly.yml`'s `ui-coverage` job (objectui's `ci.yml` |
| 12 | + * `test-coverage`, ported nightly by objectstack#25924) runs vitest as a |
| 13 | + * sharded matrix and merges |
| 14 | + * every shard's blob report in the job below it, which is where the configured |
| 15 | + * coverage thresholds are enforced. A CLI `--reporter` **replaces** the |
| 16 | + * reporter set rather than adding to it, and vitest's resolution is: |
| 17 | + * |
| 18 | + * if (!resolved.reporters.length) { |
| 19 | + * resolved.reporters.push([isAgent ? "agent" : "default", {}]); |
| 20 | + * if (process.env.GITHUB_ACTIONS === "true") |
| 21 | + * resolved.reporters.push(["github-actions", {}]); |
| 22 | + * } |
| 23 | + * |
| 24 | + * so `--reporter=blob` on its own displaced BOTH halves of that pair. A red |
| 25 | + * shard's log then ended at `blob report written to …` — no failing test name, |
| 26 | + * no assertion text, no timeout message — and the shard job's only annotation |
| 27 | + * was the generic `Process completed with exit code 1.` The failures survived |
| 28 | + * only inside the `coverage-blob-N` artifact, which is download-only. |
| 29 | + * |
| 30 | + * objectui#8545 measured what that costs: two episodes in which one test file |
| 31 | + * held `main`'s coverage gate unevaluated for **84** and **87** consecutive |
| 32 | + * pushes, both found the same way — a person reading a job log by hand, days |
| 33 | + * later. |
| 34 | + * |
| 35 | + * Nothing else in this repository reads what that step runs. |
| 36 | + * `ci-cd-pipeline-doc.test.ts` pins the pairing between `ci.yml` and the |
| 37 | + * pipeline page, but it compares COMMANDS, not flags — its extractor is |
| 38 | + * |
| 39 | + * for (const m of text.matchAll(/\bpnpm\s+([\w:.-]+)/g)) { |
| 40 | + * if (rootScripts.has(m[1])) found.add(`pnpm ${m[1]}`); |
| 41 | + * } |
| 42 | + * |
| 43 | + * whose capture stops at the first space, so `pnpm test:coverage` is all it |
| 44 | + * ever sees. Every flag on that line could be deleted and that file stays |
| 45 | + * green. |
| 46 | + * |
| 47 | + * ## Both directions, because the failure has two shapes |
| 48 | + * |
| 49 | + * Dropping `--reporter=default` / `--reporter=github-actions` restores |
| 50 | + * objectui#9177. Dropping `--reporter=blob` to "simplify" the line is the same |
| 51 | + * defect wearing the other hat: the merge job loses the only input it has, so |
| 52 | + * a readable failure is bought with an unevaluated coverage floor |
| 53 | + * (objectui#5403). Neither is caught by CI going green, because the shard leg |
| 54 | + * passing is exactly the state in which nobody looks at its log. |
| 55 | + * |
| 56 | + * ⛔ If you are deleting this file, you are deleting the only mechanical guard |
| 57 | + * on that reporter set. The measurement behind it is in the ⚠️ comment above |
| 58 | + * the step in `ui-coverage-nightly.yml`; re-run it before you change the line. |
| 59 | + */ |
| 60 | +const workflowSource = rootWorkflow(UI_COVERAGE.file); |
| 61 | + |
| 62 | +/** The reporter the merge job needs, and the two that keep the log readable. */ |
| 63 | +const BLOB_REPORTER = '--reporter=blob'; |
| 64 | +const READABLE_REPORTERS = ['--reporter=default', '--reporter=github-actions']; |
| 65 | + |
| 66 | +interface Step { |
| 67 | + name?: string; |
| 68 | + run?: unknown; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Every `run:` body in the `ui-coverage` job that invokes the coverage suite. |
| 73 | + * |
| 74 | + * Read through the YAML parser rather than off the raw text: the step is a |
| 75 | + * folded block scalar (`run: >-`), so the flags are spread over four source |
| 76 | + * lines and a regex over the file would have to re-implement the folding to see |
| 77 | + * the command the runner actually executes. Matching on `test:coverage` rather |
| 78 | + * than on the full string is what makes a rewrite FAIL here instead of |
| 79 | + * vanishing — a pin that searched for today's exact line would simply stop |
| 80 | + * finding anything and report a healthy green over a step that no longer |
| 81 | + * contains it. |
| 82 | + */ |
| 83 | +function coverageRunSteps(source: string): Array<{ step: string; run: string }> { |
| 84 | + const workflow = parseYaml(source) as { |
| 85 | + jobs?: Record<string, { steps?: Step[] }>; |
| 86 | + }; |
| 87 | + const job = workflow.jobs?.[UI_COVERAGE.shards]; |
| 88 | + expect( |
| 89 | + job, |
| 90 | + `${UI_COVERAGE.file} no longer defines a \`${UI_COVERAGE.shards}\` job. If the coverage lane was restructured, this pin ` + |
| 91 | + 'needs a new referent — do not delete it without giving the reporter set one.', |
| 92 | + ).toBeDefined(); |
| 93 | + const found: Array<{ step: string; run: string }> = []; |
| 94 | + for (const step of job?.steps ?? []) { |
| 95 | + if (typeof step.run !== 'string') continue; |
| 96 | + if (!step.run.includes('test:coverage')) continue; |
| 97 | + found.push({ step: step.name ?? '(unnamed)', run: step.run }); |
| 98 | + } |
| 99 | + return found; |
| 100 | +} |
| 101 | + |
| 102 | +describe("the ui coverage shards keep a red run readable", () => { |
| 103 | + it('still runs the coverage suite at all — the pin is not vacuous', () => { |
| 104 | + // Without this, every assertion below passes over a job that stopped running |
| 105 | + // vitest, which is the one way a `toContain` pin lies. |
| 106 | + const steps = coverageRunSteps(workflowSource); |
| 107 | + expect( |
| 108 | + steps.map((s) => s.step), |
| 109 | + `no step in ${UI_COVERAGE.file}'s \`${UI_COVERAGE.shards}\` job runs \`test:coverage\` any more. If that is ` + |
| 110 | + 'deliberate, this pin is obsolete and should be deleted WITH the reasoning that made it ' + |
| 111 | + 'obsolete written down. If it is not, the nightly lane just lost its coverage measurement.', |
| 112 | + ).not.toEqual([]); |
| 113 | + }); |
| 114 | + |
| 115 | + it('is reading the shard leg, not some other coverage invocation', () => { |
| 116 | + // Names the population the two assertions below judge: the sharded legs. |
| 117 | + // `--merge-reports` in the job below is a different step in a different job |
| 118 | + // and is deliberately out of reach here. |
| 119 | + const sharded = coverageRunSteps(workflowSource).filter((s) => s.run.includes('--shard=')); |
| 120 | + expect( |
| 121 | + sharded.map((s) => s.step), |
| 122 | + `the \`${UI_COVERAGE.shards}\` job no longer passes \`--shard=\` to the coverage suite. The reporter ` + |
| 123 | + 'rule below is about the SHARDED legs specifically — an unsharded lane has no blob to ' + |
| 124 | + 'keep and a different set of reporters is right for it.', |
| 125 | + ).not.toEqual([]); |
| 126 | + }); |
| 127 | + |
| 128 | + it('passes a reporter that prints failing test names alongside the blob', () => { |
| 129 | + const offenders = coverageRunSteps(workflowSource) |
| 130 | + .filter((s) => s.run.includes(BLOB_REPORTER)) |
| 131 | + .flatMap((s) => |
| 132 | + READABLE_REPORTERS.filter((r) => !s.run.includes(r)).map((r) => `${s.step}: missing ${r}`), |
| 133 | + ); |
| 134 | + |
| 135 | + expect( |
| 136 | + offenders, |
| 137 | + 'a coverage shard passes `--reporter=blob` without the reporters that keep its log readable.\n' + |
| 138 | + offenders.map((o) => ` - ${o}`).join('\n') + |
| 139 | + '\n\nA CLI `--reporter` REPLACES the default set rather than adding to it, so blob alone ends a ' + |
| 140 | + 'failing shard at `blob report written to …` with no test name and no assertion text, and leaves ' + |
| 141 | + 'the download-only artifact as the only copy of the failure. `--reporter=default` restores the ' + |
| 142 | + 'job log; `--reporter=github-actions` restores the per-test `::error` annotation, which is the ' + |
| 143 | + 'only form an API reader can get without downloading anything. objectui#9177; the cost is ' + |
| 144 | + 'objectui#8545, where one test file held the coverage gate unevaluated for 84 and 87 pushes.', |
| 145 | + ).toEqual([]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('keeps the blob reporter the merge job depends on', () => { |
| 149 | + const offenders = coverageRunSteps(workflowSource) |
| 150 | + .filter((s) => s.run.includes('--shard=')) |
| 151 | + .filter((s) => !s.run.includes(BLOB_REPORTER)) |
| 152 | + .map((s) => s.step); |
| 153 | + |
| 154 | + expect( |
| 155 | + offenders, |
| 156 | + 'a sharded coverage leg no longer writes a blob report:\n' + |
| 157 | + offenders.map((o) => ` - ${o}`).join('\n') + |
| 158 | + `\n\nThe \`${UI_COVERAGE.report}\` job merges every shard's blob and enforces the configured thresholds over ` + |
| 159 | + 'the merged report — that is the whole reason the lane is sharded (objectui#5403). Restoring log ' + |
| 160 | + 'readability by dropping the blob would trade an unreadable failure for an unevaluated coverage ' + |
| 161 | + 'floor, which is the same defect wearing the other hat (objectui#9177).', |
| 162 | + ).toEqual([]); |
| 163 | + }); |
| 164 | +}); |
0 commit comments