import assert from 'node:assert/strict' import { describe, it } from 'node:test' import { evaluateHeadCi, findAddressingPull, recheckIdempotencyKey } from './ci-gate.ts' import type { WorkflowRunSummary } from './github.ts' function run(p: Partial): WorkflowRunSummary { return { id: 1, workflow_id: 1, name: '✅ Validate', path: '.github/workflows/validate.yml', event: 'push', head_branch: 'main', head_sha: 'sha', status: 'completed', conclusion: 'success', run_attempt: 1, created_at: '2026-10-22T06:57:42Z', html_url: 'https://github.com/acme/widgets/actions/runs/1', ...p, } } describe('evaluateHeadCi', () => { it('red when a push workflow failed, even if another passed', () => { const v = evaluateHeadCi({ branch: 'main', runs: [run({ conclusion: 'failure' }), run({ id: 2, workflow_id: 2, name: 'Report merge conflicts' })], }) assert.equal(v.state, 'red') assert.deepEqual(v.failing.map((f) => f.name), ['✅ Validate']) }) it('ignores non-push runs (chained deploys, CLA comments) and other branches', () => { const v = evaluateHeadCi({ branch: 'main', runs: [ run({ conclusion: 'success' }), run({ id: 2, workflow_id: 2, event: 'issue_comment', conclusion: 'failure' }), run({ id: 3, workflow_id: 3, event: 'workflow_run', conclusion: 'failure' }), run({ id: 4, workflow_id: 4, head_branch: 'feature', conclusion: 'failure' }), ], }) assert.equal(v.state, 'green') }) it('judges each workflow by its newest attempt (a passing re-run clears red)', () => { const v = evaluateHeadCi({ branch: 'main', runs: [run({ conclusion: 'failure', run_attempt: 1 }), run({ id: 2, conclusion: 'success', run_attempt: 2 })], }) assert.equal(v.state, 'green') const rerunning = evaluateHeadCi({ branch: 'main', runs: [run({ conclusion: 'failure', run_attempt: 1 }), run({ id: 2, status: 'in_progress', conclusion: null, run_attempt: 2 })], }) assert.equal(rerunning.state, 'pending') }) it('ciWorkflows narrows by name substring or workflow file', () => { const runs = [run({ conclusion: 'success' }), run({ id: 2, workflow_id: 2, name: 'Lint extra', path: '.github/workflows/extra.yml', conclusion: 'failure' })] assert.equal(evaluateHeadCi({ branch: 'main', runs }).state, 'red') assert.equal(evaluateHeadCi({ branch: 'main', runs, ciWorkflows: ['validate'] }).state, 'green') assert.equal(evaluateHeadCi({ branch: 'main', runs, ciWorkflows: ['extra.yml'] }).state, 'red') }) it('unknown with no runs or only cancelled runs', () => { assert.equal(evaluateHeadCi({ branch: 'main', runs: [] }).state, 'unknown') assert.equal(evaluateHeadCi({ branch: 'main', runs: [run({ conclusion: 'cancelled' })] }).state, 'unknown') }) }) describe('findAddressingPull', () => { const pulls = [ { number: 1, title: 'Unrelated', body: '', html_url: 'u1', head_ref: 'a', created_at: '' }, { number: 2, title: 'Fix CI', body: 'See https://github.com/acme/widgets/actions/runs/39737331492', html_url: 'u2', head_ref: 'b', created_at: '' }, ] it('matches a PR naming a failing run', () => { const pr = findAddressingPull({ pulls, headSha: 'b85e9e878a', failing: [{ name: 'Validate', conclusion: 'failure', url: 'x', runId: 39737331492 }], }) assert.equal(pr?.number, 2) }) it('returns null when nothing names the failure', () => { assert.equal(findAddressingPull({ pulls, headSha: 'deadbeefcafe', failing: [] }), null) }) }) describe('recheckIdempotencyKey', () => { it('is per repo, branch, SHA and suite', () => { const base = { repo: 'acme/widgets', branch: 'main', headSha: 's', event: 'check_suite', conclusion: 'failure' } assert.equal( recheckIdempotencyKey({ ...base, checkSuiteId: '9' }), recheckIdempotencyKey({ ...base, event: 'workflow_run', checkSuiteId: '9' }), ) assert.notEqual( recheckIdempotencyKey({ ...base, checkSuiteId: '9' }), recheckIdempotencyKey({ ...base, checkSuiteId: '10' }), ) }) })