import assert from 'node:assert/strict' import { mkdtempSync, readFileSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import test from 'node:test' import type { Suite, TestCase } from '@playwright/test/reporter' import A11yFailuresReporter from './a11y-failures-reporter.ts' import { EVERY_TEST_TIMED_OUT, baselineFor, compareFailures, failuresFromJobs, failuresFromVitest, nextLastChecked, stateFromPassingRun, type ActionsJob, type FailureState, type OpenFailure, type RunFailures, type RunRef, } from './open-failures.ts' // Runs every short sequence of run outcomes through the same steps as docs-alert.ts, and checks // the result against a model that records, for each failure, what every run observed. type Observation = 'pass' | 'fail' | 'unchecked' type Outcome = { name: string results: () => RunFailures observed: Record } const runRef = (runNumber: number): RunRef => ({ id: runNumber, attempt: 1, runNumber, headSha: `sha${runNumber}`, startedAt: new Date(Date.UTC(2026, 11, 1, runNumber)).toISOString(), }) function* sequences(alphabet: T[], length: number): Generator { if (length === 0) return yield [] for (const rest of sequences(alphabet, length - 1)) { for (const item of alphabet) yield [...rest, item] } } // With a saved list, run 0 stands for a run that checked every key and found nothing. With // coldStart, there's no saved list, and run 0 is a passing run the alert reads from the API. function checkSequence(outcomes: Outcome[], keys: string[], coldStart?: Outcome) { const label = [coldStart && `cold start after ${coldStart.name}`, ...outcomes.map((o) => o.name)] .filter(Boolean) .join(' → ') const start = runRef(0) let state: FailureState = coldStart ? stateFromPassingRun(start, coldStart.results().unchecked) : { version: 1, lastRun: start, open: [], lastChecked: {} } const history: Record[] = [ coldStart?.observed ?? Object.fromEntries(keys.map((key) => [key, 'pass'])), ] outcomes.forEach((outcome, index) => { const run = runRef(index + 1) const current = outcome.results() const { added, stillOpen, fixed } = compareFailures(state.open, current) history.push(outcome.observed) const lastObserved = (key: string, before: number) => { for (let n = before; n >= 0; n--) { if (history[n][key] !== 'unchecked') return { run: n, observation: history[n][key] } } return null } const expectedOpen = keys.filter((key) => lastObserved(key, index + 1)?.observation === 'fail') const open: OpenFailure[] = [ ...stillOpen, ...added.map((failure) => ({ ...failure, firstSeen: run.startedAt, attribution: { kind: 'preexisting' as const }, })), ] assert.deepEqual( open.map((failure) => failure.key).sort(), expectedOpen.sort(), `open list after ${label}, run ${index + 1}` ) for (const failure of fixed) { assert.equal(outcome.observed[failure.key], 'pass', `fixed ${failure.key} in ${label}`) } for (const failure of added) { const baseline = baselineFor(failure, state) const expected = lastObserved(failure.key, index) if (expected) assert.equal(expected.observation, 'pass', `${failure.key} open in ${label}`) assert.equal( baseline?.runNumber ?? null, expected?.run ?? null, `baseline for ${failure.key} at run ${index + 1} of ${label}` ) } state = { version: 1, lastRun: run, open, lastChecked: nextLastChecked(state.lastChecked, current.unchecked, state.lastRun), } }) } const UPLOAD = 'publish › Upload' const BUILD = 'publish › Build' const JOB_KEYS = [UPLOAD, BUILD, 'publish'] type StepConclusion = 'success' | 'failure' | 'skipped' | 'cancelled' | null function jobOutcome( name: string, steps: [StepConclusion, StepConclusion] | 'skipped' | 'empty', jobConclusion?: string ) { let jobs: ActionsJob[] let observed: Record if (steps === 'skipped' || steps === 'empty') { const listed = steps === 'skipped' const failed = jobConclusion === 'failure' jobs = [ { name: 'publish', conclusion: failed ? 'failure' : 'skipped', steps: listed ? [ { name: 'Upload', conclusion: 'skipped' }, { name: 'Build', conclusion: 'skipped' }, ] : [], }, ] observed = { [UPLOAD]: 'unchecked', [BUILD]: 'unchecked', publish: failed ? 'fail' : 'unchecked', } } else { const [upload, build] = steps const conclusion = jobConclusion ?? (steps.includes('failure') ? 'failure' : 'success') jobs = [ { name: 'publish', conclusion, steps: [ { name: 'Upload', conclusion: upload }, { name: 'Build', conclusion: build }, ], }, ] const step = (result: StepConclusion): Observation => result === 'failure' ? 'fail' : result === 'success' ? 'pass' : 'unchecked' // The job itself fails only when it failed with no failing step const jobFailed = conclusion !== 'success' && !steps.includes('failure') observed = { [UPLOAD]: step(upload), [BUILD]: step(build), publish: jobFailed ? 'fail' : 'pass', } } return { name, results: () => failuresFromJobs(jobs), observed } } const JOB_OUTCOMES: Outcome[] = [ jobOutcome('job skipped, no steps', 'empty'), jobOutcome('job failed, no steps', 'empty', 'failure'), jobOutcome('job skipped', 'skipped'), jobOutcome('pass', ['success', 'success']), jobOutcome('upload fails', ['failure', 'skipped']), jobOutcome('build fails', ['success', 'failure']), jobOutcome('upload skipped', ['skipped', 'success']), jobOutcome('build skipped', ['success', 'skipped']), jobOutcome('upload skipped, build fails', ['skipped', 'failure']), jobOutcome('job timed out during build', ['success', 'cancelled'], 'timed_out'), jobOutcome('build never reported', ['success', null], 'failure'), ] test('tracks job and step failures across every sequence of four runs', () => { for (const sequence of sequences(JOB_OUTCOMES, 4)) checkSequence(sequence, JOB_KEYS) }) test('starts from a passing run when there is no saved list', () => { const passing = JOB_OUTCOMES.filter((outcome) => outcome.results().failures.length === 0) for (const start of passing) { for (const sequence of sequences(JOB_OUTCOMES, 3)) checkSequence(sequence, JOB_KEYS, start) } }) const FILE = 'apps/docs/app/api/graphql/tests/schema.smoke.test.ts' const SCHEMA = `${FILE} › prod smoke test schema` const SEARCH = `${FILE} › prod smoke test search` const FAILED_TO_RUN = `${FILE}: failed to run` const SMOKE_KEYS = [SCHEMA, SEARCH, FAILED_TO_RUN, EVERY_TEST_TIMED_OUT] type TestStatus = 'passed' | 'failed' | 'timed out' | 'skipped' function smokeOutcome(name: string, tests: [TestStatus, TestStatus] | 'failed to load'): Outcome { const loads = tests !== 'failed to load' const statuses: TestStatus[] = loads ? tests : [] const assertionResults = statuses.map((status, index) => ({ fullName: ['prod smoke test schema', 'prod smoke test search'][index], status: status === 'timed out' ? 'failed' : status, failureMessages: status === 'timed out' ? ['Test timed out in 5000ms.'] : status === 'failed' ? ['500'] : [], })) const report = { testResults: [ { name: `/home/runner/work/supabase/supabase/${FILE}`, status: statuses.includes('failed') || statuses.includes('timed out') || !loads ? 'failed' : 'passed', assertionResults, }, ], } const test = (status: TestStatus | undefined): Observation => status === 'passed' ? 'pass' : status === 'failed' ? 'fail' : 'unchecked' const passed = statuses.filter((status) => status === 'passed').length const timedOut = statuses.filter((status) => status === 'timed out').length return { name, results: () => failuresFromVitest(report) as RunFailures, observed: { [SCHEMA]: test(statuses[0]), [SEARCH]: test(statuses[1]), [FAILED_TO_RUN]: loads ? 'pass' : 'fail', [EVERY_TEST_TIMED_OUT]: passed === 0 && timedOut > 0 ? 'fail' : 'pass', }, } } const SMOKE_OUTCOMES: Outcome[] = [ smokeOutcome('pass', ['passed', 'passed']), smokeOutcome('schema fails', ['failed', 'passed']), smokeOutcome('schema times out', ['timed out', 'passed']), smokeOutcome('schema skipped', ['skipped', 'passed']), smokeOutcome('every test times out', ['timed out', 'timed out']), smokeOutcome('schema fails, search times out', ['failed', 'timed out']), smokeOutcome('file fails to load', 'failed to load'), ] test('tracks smoke tests and their file across every sequence of four runs', () => { for (const sequence of sequences(SMOKE_OUTCOMES, 4)) checkSequence(sequence, SMOKE_KEYS) }) const PAGE = '/docs/guides/a' const NOT_FOUND = `${PAGE}: http-404` const UNAVAILABLE = `${PAGE}: http-503` const HEADING = `${PAGE}: heading-order` const PAGE_KEYS = [NOT_FOUND, UNAVAILABLE, HEADING] const outputDir = mkdtempSync(join(tmpdir(), 'failure-sequences-')) const outputFile = join(outputDir, 'failures.json') test.after(() => rmSync(outputDir, { recursive: true, force: true })) type Scan = { loaded: boolean; status: number | null; elementCount: number; violations: object[] } function scanOutcome( name: string, scan: Scan | null, outcome: string, observed: Record ): Outcome { const testCase = { title: `${PAGE} a11y`, tags: ['@a11y'], outcome: () => outcome, results: [ { attachments: scan ? [{ name: 'axe-results.json', body: Buffer.from(JSON.stringify(scan)) }] : [], }, ], } as unknown as TestCase const results = () => { const reporter = new A11yFailuresReporter({ outputFile }) reporter.onBegin({} as never, { allTests: () => [testCase] } as unknown as Suite) reporter.onEnd() return JSON.parse(readFileSync(outputFile, 'utf8')) as RunFailures } return { name, results, observed } } const errorPage = (status: number): Scan => ({ loaded: false, status, elementCount: 0, violations: [], }) const scanned = (violations: object[], elementCount = 50): Scan => ({ loaded: true, status: 200, elementCount, violations, }) const status = (code: number | null): Record => ({ [NOT_FOUND]: code === null ? 'unchecked' : code === 404 ? 'fail' : 'pass', [UNAVAILABLE]: code === null ? 'unchecked' : code === 503 ? 'fail' : 'pass', }) const SCAN_OUTCOMES: Outcome[] = [ scanOutcome('no response', { ...errorPage(0), status: null }, 'unexpected', { ...status(null), [HEADING]: 'unchecked', }), scanOutcome('timed out mid-scan', null, 'timedOut', { ...status(null), [HEADING]: 'unchecked', }), scanOutcome('404', errorPage(404), 'unexpected', { ...status(404), [HEADING]: 'unchecked' }), scanOutcome('503', errorPage(503), 'unexpected', { ...status(503), [HEADING]: 'unchecked' }), scanOutcome('clean', scanned([]), 'expected', { ...status(200), [HEADING]: 'pass' }), scanOutcome('heading-order', scanned([{ id: 'heading-order' }]), 'unexpected', { ...status(200), [HEADING]: 'fail', }), scanOutcome('empty scan', scanned([], 5), 'expected', { ...status(200), [HEADING]: 'unchecked', }), scanOutcome('skipped', null, 'skipped', { ...status(null), [HEADING]: 'unchecked' }), scanOutcome('failed with no blocking rule', scanned([{ id: 'region' }]), 'unexpected', { ...status(200), [HEADING]: 'unchecked', }), ] test('tracks a page status and its axe rules across every sequence of four runs', () => { for (const sequence of sequences(SCAN_OUTCOMES, 4)) checkSequence(sequence, PAGE_KEYS) })