import assert from 'node:assert/strict' import test from 'node:test' import { EVERY_TEST_TIMED_OUT, attribute, baselineFor, compareFailures, deployedCommitsBetween, failuresFromJobs, failuresFromVitest, formatAlert, formatCrashAlert, nextLastChecked, parseFailureState, parseRunFailures, pickDeployedHead, pushCandidates, type FailureState, type HistoryCommit, type OpenFailure, type RunRef, } from './open-failures.ts' const commit = (sha: string) => ({ sha, title: `commit ${sha}` }) const open = (key: string, page?: string): OpenFailure => ({ key, page, firstSeen: '2026-12-01T05:17:00Z', attribution: { kind: 'introduced', commit: commit('aaaaaaa') }, }) test('adds a second failure to a workflow that is already failing, and keeps the first', () => { const first = open('/docs/guides/a: heading-order', '/docs/guides/a') const result = compareFailures([first], { failures: [ { key: '/docs/guides/a: heading-order', page: '/docs/guides/a' }, { key: '/docs/guides/b: duplicate-id', page: '/docs/guides/b' }, ], unchecked: [], }) assert.deepEqual( result.added.map((failure) => failure.key), ['/docs/guides/b: duplicate-id'] ) assert.deepEqual(result.stillOpen, [first]) assert.deepEqual(result.fixed, []) }) test('drops a failure that no longer fails', () => { const first = open('docs › Sync') const result = compareFailures([first], { failures: [], unchecked: [] }) assert.deepEqual(result.fixed, [first]) assert.deepEqual(result.stillOpen, []) }) test('keeps a failure open when its page timed out instead of passing', () => { const first = open('/docs/guides/a: heading-order', '/docs/guides/a') const result = compareFailures([first], { failures: [], unchecked: ['/docs/guides/a'] }) assert.deepEqual(result.stillOpen, [first]) assert.deepEqual(result.fixed, []) assert.deepEqual(result.added, []) }) test('names the one commit when the range has one', () => { assert.deepEqual(attribute([commit('1111111')], null), { kind: 'introduced', commit: commit('1111111'), }) }) test('lists every commit as unconfirmed when the range has several and nothing narrows it', () => { const result = attribute([commit('1'), commit('2')], null) assert.equal(result.kind, 'unconfirmed') assert.equal(result.kind === 'unconfirmed' && result.total, 2) }) test('counts the whole range when only its newest commits were fetched', () => { const result = attribute([commit('3'), commit('4')], null, 300) assert.equal(result.kind === 'unconfirmed' && result.total, 300) }) test('names the one commit that touched the failing page', () => { const range = [commit('1'), commit('2'), commit('3')] assert.deepEqual(attribute(range, [commit('2')]), { kind: 'introduced', commit: commit('2') }) }) test('falls back to the whole range when no commit touched the failing page', () => { const range = [commit('1'), commit('2')] const result = attribute(range, []) assert.equal(result.kind, 'unconfirmed') assert.deepEqual(result.kind === 'unconfirmed' && result.commits, range) assert.equal(result.kind === 'unconfirmed' && result.reason, 'no commit touched this page') }) test('says there is no commit when the range is empty', () => { assert.equal(attribute([], null).kind, 'none') }) const built = (sha: string, createdAt: string): HistoryCommit => ({ ...commit(sha), status: { state: 'SUCCESS', description: 'Deployment has completed', createdAt }, }) const skipped = (sha: string, createdAt: string): HistoryCommit => ({ ...commit(sha), status: { state: 'SUCCESS', description: 'Skipped - Not affected', createdAt }, }) const pending = (sha: string): HistoryCommit => ({ ...commit(sha), status: { state: 'PENDING', description: 'Vercel is deploying your app', createdAt: 'z' }, }) test('picks the newest build that finished before the run', () => { const history = [ built('c', '2026-12-02T06:00:00Z'), built('b', '2026-12-02T04:00:00Z'), built('a', '2026-12-01T00:00:00Z'), ] assert.equal(pickDeployedHead(history, '2026-12-02T05:17:00Z'), 'b') }) test('counts skipped builds after a finished build as deployed', () => { const history = [skipped('c', '2026-12-02T03:00:00Z'), built('b', '2026-12-02T02:00:00Z')] assert.equal(pickDeployedHead(history, '2026-12-02T05:17:00Z'), 'c') }) test('stops at a build that had not finished, even if a later build was skipped', () => { const history = [ skipped('c', '2026-12-02T03:00:00Z'), pending('b'), built('a', '2026-12-02T01:00:00Z'), ] assert.equal(pickDeployedHead(history, '2026-12-02T05:17:00Z'), 'a') }) test('lists the built commits that reached production between two runs', () => { const history = [ built('e', '2026-12-02T06:00:00Z'), built('d', '2026-12-02T04:00:00Z'), skipped('c', '2026-12-02T03:00:00Z'), built('b', '2026-12-02T02:00:00Z'), built('a', '2026-12-01T04:00:00Z'), ] const range = deployedCommitsBetween(history, '2026-12-01T05:17:00Z', '2026-12-02T05:17:00Z') assert.deepEqual( range?.map((item) => item.sha), ['b', 'd'] ) }) test('returns null when the earlier deploy is outside the history', () => { const history = [built('b', '2026-12-02T02:00:00Z')] assert.equal( deployedCommitsBetween(history, '2026-12-01T05:17:00Z', '2026-12-02T05:17:00Z'), null ) }) test('reads failed jobs and steps from the Actions API', () => { const result = failuresFromJobs([ { name: 'build', conclusion: 'success', steps: [] }, { name: 'sync', conclusion: 'failure', steps: [ { name: 'Checkout', conclusion: 'success' }, { name: 'Sync docs', conclusion: 'failure' }, ], }, { name: 'setup', conclusion: 'failure', steps: [] }, ]) assert.deepEqual( result.failures.map((failure) => failure.key), ['sync › Sync docs', 'setup'] ) }) test('lists steps and jobs that never ran as unchecked', () => { const result = failuresFromJobs([ { name: 'update', conclusion: 'failure', steps: [ { name: 'Install deps', conclusion: 'failure' }, { name: 'Refresh Management API docs', conclusion: 'skipped' }, ], }, { name: 'publish', conclusion: 'skipped', steps: [] }, ]) assert.deepEqual( result.failures.map((failure) => failure.key), ['update › Install deps'] ) assert.deepEqual(result.unchecked, ['update › Refresh Management API docs', 'publish']) }) test('retires an old error status when the page returns a new one, and keeps its axe failures', () => { const page = '/docs/guides/a' const notFound = open(`${page}: http-404`, page) const headingOrder = open(`${page}: heading-order`, page) const result = compareFailures([notFound, headingOrder], { failures: [{ key: `${page}: http-503`, page }], unchecked: [page], }) assert.deepEqual(result.fixed, [notFound]) assert.deepEqual(result.stillOpen, [headingOrder]) }) test('keeps an error status open when the page timed out', () => { const page = '/docs/guides/a' const notFound = open(`${page}: http-404`, page) const result = compareFailures([notFound], { failures: [], unchecked: [`${page}: http`, page], }) assert.deepEqual(result.stillOpen, [notFound]) }) test('keeps a step failure open when its whole job was skipped', () => { const step = open('publish › Upload') const result = compareFailures([step], { failures: [], unchecked: ['publish'] }) assert.deepEqual(result.stillOpen, [step]) assert.deepEqual(result.fixed, []) }) test('measures a step failure from the last run that ran its job', () => { const ran = runRef(1) const skippedJob = runRef(2) const state: FailureState = { version: 1, lastRun: skippedJob, open: [], lastChecked: nextLastChecked({}, ['publish'], ran), } assert.equal(baselineFor({ key: 'publish › Upload' }, state), ran) assert.equal(baselineFor({ key: 'build › Upload' }, state), skippedJob) }) test('keeps a step baseline when its job is skipped after the step was', () => { const [r1, r2, r3] = [runRef(1), runRef(2), runRef(3)] const afterStepSkipped = nextLastChecked({}, ['publish › Upload'], r1) const afterJobSkipped = nextLastChecked(afterStepSkipped, ['publish'], r2) const state: FailureState = { version: 1, lastRun: r3, open: [], lastChecked: afterJobSkipped } assert.equal(baselineFor({ key: 'publish › Upload' }, state), r1) }) test('takes the job baseline when a step is skipped after its job was', () => { const [r1, r2, r3] = [runRef(1), runRef(2), runRef(3)] const afterJobSkipped = nextLastChecked({}, ['publish'], r1) const afterStepSkipped = nextLastChecked(afterJobSkipped, ['publish › Upload'], r2) const state: FailureState = { version: 1, lastRun: r3, open: [], lastChecked: afterStepSkipped } assert.equal(baselineFor({ key: 'publish › Upload' }, state), r1) }) test('counts a smoke run where no test passed and some timed out as one failure', () => { const result = failuresFromVitest({ testResults: [ { assertionResults: [ { fullName: 'prod smoke test crawlers', status: 'failed', failureMessages: ['Test timed out in 5000ms.'], }, { fullName: 'prod smoke test search', status: 'failed', failureMessages: ['Test timed out in 5000ms.'], }, ], }, ], }) assert.deepEqual(result?.failures, [{ key: EVERY_TEST_TIMED_OUT }]) assert.deepEqual(result?.unchecked, [ 'test file › prod smoke test crawlers', 'test file › prod smoke test search', ]) }) test('treats a timed-out smoke test as unchecked, not failed', () => { const result = failuresFromVitest({ testResults: [ { assertionResults: [ { fullName: 'prod smoke test graphql', status: 'failed', failureMessages: ['expected 200'], }, { fullName: 'prod smoke test crawlers', status: 'failed', failureMessages: ['Test timed out in 5000ms.'], }, { fullName: 'prod smoke test skills', status: 'failed', failureMessages: ['TimeoutError: The operation was aborted due to timeout'], }, { fullName: 'prod smoke test search', status: 'passed' }, ], }, ], }) assert.deepEqual(result?.failures, [{ key: 'test file › prod smoke test graphql' }]) assert.deepEqual(result?.unchecked, [ 'test file › prod smoke test crawlers', 'test file › prod smoke test skills', ]) }) test('counts a smoke test file that failed to run as a failure', () => { const result = failuresFromVitest({ testResults: [ { name: '/home/runner/work/supabase/supabase/apps/docs/app/api/crawlers/route.smoke.test.ts', status: 'failed', message: 'Failed to load url', assertionResults: [], }, ], }) assert.deepEqual(result?.failures, [ { key: 'apps/docs/app/api/crawlers/route.smoke.test.ts: failed to run' }, ]) assert.deepEqual(result?.unchecked, ['apps/docs/app/api/crawlers/route.smoke.test.ts']) }) test('rejects a results file that is not a Vitest report', () => { assert.equal(failuresFromVitest({ failures: [] }), null) // What Vitest writes when global setup crashes or no test file matches assert.equal(failuresFromVitest({ success: false, testResults: [] }), null) assert.equal(parseRunFailures({ failures: [{ page: 'no key' }], unchecked: [] }), null) }) test('lists new failures first and marks fixed ones', () => { const kept = open('/docs/guides/a: heading-order') const added = { ...open('/docs/guides/b: duplicate-id'), firstSeen: '2026-12-02T05:17:00Z' } const fixed = open('/docs/guides/c: button-name') const message = formatAlert( { workflowName: 'Docs Accessibility Scan', runUrl: 'https://example.com/run', runLabel: 'scheduled run', repoUrl: 'https://github.com/supabase/supabase', }, [kept, added], new Set([added.key]), [fixed] ) const body = message.blocks .filter((block) => block.type === 'section') .map((block) => (block as { text: { text: string } }).text.text) .join('\n') .split('\n') assert.equal(message.text, ':rotating_light: Docs Accessibility Scan: 2 open failures') assert.match(body[0], /^:new: `\/docs\/guides\/b: duplicate-id`: introduced by/) assert.match(body[1], /^• `\/docs\/guides\/a: heading-order`: .*first seen Dec 1$/) assert.equal(body[2], ':white_check_mark: fixed: `/docs/guides/c: button-name`') }) const runRef = (runNumber: number, startedAt = '2026-12-01T05:17:00Z'): RunRef => ({ id: runNumber, attempt: 1, runNumber, headSha: `sha${runNumber}`, startedAt, }) test('loads a saved list only when it has the current shape', () => { const state: FailureState = { version: 1, lastRun: runRef(1), open: [open('docs › Sync')], lastChecked: {}, } const saved = JSON.parse(JSON.stringify(state)) assert.deepEqual(parseFailureState(saved), saved) assert.equal(parseFailureState({ ...state, version: 2 }), null) assert.equal(parseFailureState({ ...state, lastRun: { runNumber: 'high' } }), null) assert.equal( parseFailureState({ ...state, open: [{ key: 'x', firstSeen: 'y', attribution: { kind: 'blame' } }], }), null ) }) test('measures a new failure from the last run that checked its page', () => { const checked = runRef(1) const timedOut = runRef(2) const lastChecked = nextLastChecked({}, ['/docs/guides/a'], checked) const afterSecondTimeout = nextLastChecked(lastChecked, ['/docs/guides/a'], timedOut) const state: FailureState = { version: 1, lastRun: timedOut, open: [], lastChecked: afterSecondTimeout, } assert.equal( baselineFor({ key: '/docs/guides/a: heading-order', page: '/docs/guides/a' }, state), checked ) assert.equal( baselineFor({ key: '/docs/guides/b: heading-order', page: '/docs/guides/b' }, state), timedOut ) assert.deepEqual(nextLastChecked(afterSecondTimeout, [], timedOut), {}) }) const push = (run_number: number, conclusion: string | null) => ({ run_number, conclusion, head_sha: `sha${run_number}`, head_commit: { message: `commit ${run_number}\n\nbody` }, }) test('names only the failing push when no run since the baseline went unprocessed', () => { const result = pushCandidates([push(12, 'failure')], 11, push(12, 'failure')) assert.deepEqual(result, [{ sha: 'sha12', title: 'commit 12' }]) }) test('includes every push run since the baseline whose alert never ran, even one that passed', () => { const runs = [ push(14, 'failure'), push(13, 'cancelled'), push(12, 'success'), push(11, 'failure'), ] const result = pushCandidates(runs, 10, push(15, 'failure')) assert.deepEqual( result.map((commit) => commit.sha), ['sha11', 'sha12', 'sha13', 'sha14', 'sha15'] ) }) test('escapes the workflow name in the crash alert', () => { const message = formatCrashAlert({ workflowName: 'Docs ', runUrl: 'https://example.com/run', runLabel: 'scheduled run', repoUrl: 'https://github.com/supabase/supabase', }) const section = message.blocks.find((block) => block.type === 'section') as { text: { text: string } } assert.match(section.text.text, /^\*Docs <scan>\* failed before it reported any results/) }) test('keeps a page that no run has checked without a baseline', () => { const firstRun = runRef(1) const afterFirst = nextLastChecked({}, ['/docs/guides/a'], null) const afterSecond = nextLastChecked(afterFirst, ['/docs/guides/a'], firstRun) const state: FailureState = { version: 1, lastRun: runRef(2), open: [], lastChecked: afterSecond } assert.deepEqual(afterSecond, { '/docs/guides/a': null }) assert.equal( baselineFor({ key: '/docs/guides/a: heading-order', page: '/docs/guides/a' }, state), null ) assert.ok(parseFailureState(state)) })