cc-haha/scripts/quality-gate/coverage.test.ts
程序员阿江(Relakkes) 9719726cd2 feat: make quality gates observable and enforceable
The repository now has a measurable PR quality path instead of a loose set of
manual checks. Coverage, quarantine governance, provider smoke, desktop smoke,
and workflow wiring all produce durable reports that contributors and maintainers
can inspect without reconstructing terminal output.

This also fixes the desktop smoke current-runtime path so browser-driven smoke
runs use the desktop default active provider instead of forcing the official
current model, and records that runtime decision as an artifact.

Constraint: Default PR gates must remain non-live and contributor-safe while live model checks stay explicit.
Constraint: Release packaging is still GitHub Actions based, so release preflight must run before the build matrix.
Rejected: Make live provider or desktop smoke mandatory on every PR | secrets, quotas, and model availability are maintainer-controlled.
Rejected: Let PRs lower coverage baselines in the same change | base-branch ratchet comparison must remain authoritative.
Confidence: high
Scope-risk: moderate
Directive: Do not relax coverage or quarantine policy without a maintainer approval label and a fresh quality report.
Tested: ALLOW_CLI_CORE_CHANGE=1 ALLOW_COVERAGE_BASELINE_CHANGE=1 bun run quality:gate --mode pr
Tested: bun run quality:gate --mode baseline --allow-live --only provider-smoke:* --provider-model nvidia-custom:main:nvidia-custom-main --artifacts-dir /tmp/quality-gate-live-smoke
Tested: bun run quality:gate --mode baseline --allow-live --only desktop-smoke:* --provider-model current:current:current-runtime --artifacts-dir /tmp/quality-gate-desktop-smoke-fixed
Tested: git diff --check
Not-tested: Full live release mode with multiple providers in hosted CI; provider credentials and quota remain maintainer-controlled.
2026-05-06 16:25:10 +08:00

108 lines
2.7 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { evaluateThresholds, parseLcov } from './coverage'
describe('coverage gate helpers', () => {
test('parses lcov totals into percentages', () => {
const summary = parseLcov([
'TN:',
'SF:src/a.ts',
'FNF:4',
'FNH:3',
'BRF:10',
'BRH:7',
'LF:20',
'LH:18',
'end_of_record',
'SF:src/b.ts',
'FNF:1',
'FNH:1',
'LF:10',
'LH:5',
'end_of_record',
].join('\n'))
expect(summary.lines.pct).toBe(76.67)
expect(summary.functions.pct).toBe(80)
expect(summary.branches.pct).toBe(70)
})
test('reports minimum threshold failures', () => {
const failures = evaluateThresholds([
{
id: 'root-server',
title: 'Root',
status: 'passed',
command: ['bun', 'test'],
durationMs: 1,
logPath: 'coverage.log',
summary: {
lines: { total: 100, covered: 79, pct: 79 },
functions: { total: 10, covered: 9, pct: 90 },
branches: { total: 10, covered: 8, pct: 80 },
statements: { total: 100, covered: 79, pct: 79 },
},
},
], {
schemaVersion: 1,
minimums: {
'root-server': {
lines: 80,
},
},
})
expect(failures).toEqual(['root-server: lines coverage 79% is below minimum 80%'])
})
test('treats failed suite execution as a coverage failure', () => {
const failures = evaluateThresholds([
{
id: 'desktop',
title: 'Desktop',
status: 'failed',
command: ['bun', 'run', 'test'],
durationMs: 1,
logPath: 'coverage.log',
error: 'coverage command exited with 1',
},
], {
schemaVersion: 1,
minimums: {},
})
expect(failures).toEqual(['desktop: coverage command exited with 1'])
})
test('does not require every suite to exist in the ratchet baseline', () => {
const failures = evaluateThresholds([
{
id: 'new-suite',
title: 'New suite',
status: 'passed',
command: ['bun', 'test'],
durationMs: 1,
logPath: 'coverage.log',
summary: {
lines: { total: 100, covered: 90, pct: 90 },
functions: { total: 10, covered: 9, pct: 90 },
branches: { total: 10, covered: 8, pct: 80 },
statements: { total: 100, covered: 90, pct: 90 },
},
},
], {
schemaVersion: 1,
minimums: {
'new-suite': {
branches: 85,
},
},
ratchet: {
baselinePath: 'scripts/quality-gate/coverage-baseline.json',
allowedDropPercent: 0,
},
})
expect(failures).toEqual(['new-suite: branches coverage 80% is below minimum 85%'])
})
})