import assert from 'node:assert/strict' import { test } from 'node:test' import { analyze, inferChain, isDocsOnly, signalFailures } from './analyze.ts' import { dataset, job, run, typicalRun, workflow } from './fixtures.test-helpers.ts' import { percentile } from './stats.ts' test('percentile uses nearest rank', () => { assert.equal(percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 50), 5) assert.equal(percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 90), 9) assert.equal(percentile([], 50), 0) }) test('signalFailures drops the gate job when a real job failed', () => { const jobs = typicalRun(1, 0, { nodeConclusion: 'failure' }) const failed = signalFailures(jobs, '✅ Validate') assert.deepEqual(failed.map((entry) => entry.name), ['Node (1/2)']) }) test('inferChain walks back from the last job through needs edges', () => { const r = run({ at: 0, wall: 10.2 }) const chain = inferChain(r, typicalRun(r.id, 0)) assert.deepEqual(chain.map((entry) => entry.name), ['Node (2/2)', '✅ Validate']) }) test('catch value: a sole failure fixed by a new commit counts; a rerun pass counts as flaky', () => { const failedThenFixed = run({ at: 0, wall: 10, conclusion: 'failure', headBranch: 'a', headSha: 'a1' }) const fixed = run({ at: 30, wall: 10, headBranch: 'a', headSha: 'a2' }) const failedThenRerun = run({ at: 60, wall: 10, conclusion: 'failure', headBranch: 'b', headSha: 'b1' }) const rerunPass = run({ at: 90, wall: 10, headBranch: 'b', headSha: 'b1' }) const runs = [failedThenFixed, fixed, failedThenRerun, rerunPass] const jobsByRun: Record> = {} jobsByRun[failedThenFixed.id] = typicalRun(failedThenFixed.id, 0, { nodeConclusion: 'failure' }) jobsByRun[fixed.id] = typicalRun(fixed.id, 30) jobsByRun[failedThenRerun.id] = typicalRun(failedThenRerun.id, 60, { nodeConclusion: 'failure' }) jobsByRun[rerunPass.id] = typicalRun(rerunPass.id, 90) const metrics = analyze(dataset({ workflows: [workflow({ name: '✅ Validate', runs })], jobsByRun })) const node = metrics.jobs.find((entry) => entry.job === 'Node (1/2)')! assert.equal(node.failures, 2) assert.equal(node.soleFailures, 2) assert.equal(node.soleFailuresFixedByCommit, 1) assert.equal(node.soleFailuresPassedOnRerun, 1) const wf = metrics.workflows[0] assert.equal(wf.flakyShas, 1) assert.ok(metrics.jobs.find((entry) => entry.job === 'Static')!.failures === 0) }) test('rerun attempts: earlier attempt failures mark the job flaky', () => { const r = run({ at: 0, wall: 10, runAttempt: 2 }) const metrics = analyze( dataset({ workflows: [workflow({ runs: [r] })], jobsByRun: { [r.id]: typicalRun(r.id, 0) }, previousAttemptJobs: { [r.id]: typicalRun(r.id, 0, { nodeConclusion: 'failure' }) }, }), ) assert.equal(metrics.jobs.find((entry) => entry.job === 'Node (1/2)')!.flakyFailures, 1) assert.equal(metrics.workflows[0].rerunRate, 1) assert.equal(metrics.workflows[0].flakyShas, 1) }) test('runner minutes round each job up and split by OS; duplicates and superseded runs are found', () => { const older = run({ at: 0, wall: 10.2, headBranch: 'x', headSha: 'x1' }) const newer = run({ at: 5, wall: 10.2, headBranch: 'x', headSha: 'x2' }) const metrics = analyze( dataset({ workflows: [workflow({ runs: [older, newer] })], jobsByRun: { [older.id]: typicalRun(older.id, 0), [newer.id]: typicalRun(newer.id, 5) }, }), ) const wf = metrics.workflows[0] // Linux: 9 + 10 + 3 + 1 (gate rounds up) = 23; Windows: 8 + 2 = 10. assert.equal(wf.runnerMinutesPerRun, 33) assert.ok(wf.runnerMinutesPerWeek.windows > 0) assert.equal(wf.supersededRuns, 1) assert.ok(metrics.duplicates.some((entry) => entry.step === 'Setup' && entry.jobs === 3)) assert.equal(metrics.criticalPath?.chain[0].job, 'Node (2/2)') assert.equal(metrics.prTimeToGreenMin.n, 2) }) test('co-failing jobs are grouped', () => { const runs = [run({ at: 0, wall: 10, conclusion: 'failure' }), run({ at: 20, wall: 10, conclusion: 'failure' })] const jobsByRun: Record = {} for (const r of runs) { jobsByRun[r.id] = [ job({ runId: r.id, name: 'A (linux)', at: 0, duration: 3, conclusion: 'failure' }), job({ runId: r.id, name: 'A (windows)', at: 0, duration: 3, conclusion: 'failure', labels: ['windows-latest'] }), job({ runId: r.id, name: 'B', at: 0, duration: 3 }), ] } const metrics = analyze(dataset({ workflows: [workflow({ runs })], jobsByRun })) assert.deepEqual(metrics.coFailing[0].jobs.sort(), ['A (linux)', 'A (windows)']) assert.equal(metrics.coFailing[0].failures, 2) }) test('docs-only detection', () => { assert.equal(isDocsOnly(['docs/a.md', 'README.md']), true) assert.equal(isDocsOnly(['docs/a.md', 'src/x.ts']), false) assert.equal(isDocsOnly(['.github/workflows/readme.md']), false) assert.equal(isDocsOnly([]), false) })