import { closeSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import { buildSummary } from './report.mjs' import A11yReporter from './reporter.mjs' const directories = [] const temporaryDirectory = () => { const directory = mkdtempSync(path.join(tmpdir(), 'a11y-report-')) directories.push(directory) return directory } afterEach(() => { for (const directory of directories.splice(0)) rmSync(directory, { force: true, recursive: true }) }) const attempt = ({ status = 'passed', retry = 0, scans = [], skippedSteps = [], errors = [], } = {}) => ({ status, retry, scans, skippedSteps, errors, attachments: [] }) const entry = ({ workflow = 'Document editing', attempts = [attempt()], expectedStatus = 'passed', } = {}) => ({ workflow, title: 'should save', file: 'a11y/e2e.spec.ts', line: 12, expectedStatus, attempts, }) const report = ({ tests = [entry()], errors = [], status = 'passed' } = {}) => ({ framework: 'next', tests, errors, status, }) describe('per-job workflow summary', () => { it('should render a single framework with one count per selected test', () => { const output = buildSummary({ report: report({ tests: [entry(), entry()] }) }) expect(output).toContain('Accessibility workflow results — next') expect(output).toContain('**2 tests**') expect(output).toContain('| Document editing | ✅ 2/2 passed |') expect(output).not.toContain('tanstack-start') }) it('should expose retries without inflating tests or final-attempt scans', () => { const output = buildSummary({ report: report({ tests: [ entry({ attempts: [ attempt({ status: 'failed', scans: [{ name: 'form', violations: 2 }], errors: ['Focus lost'], }), attempt({ retry: 1, scans: [{ name: 'form', violations: 0 }] }), ], }), ], }), }) expect(output).toContain('**1 test**') expect(output).toContain('1 passed after retry') expect(output).toContain('1 state scans completed · 0 violations') expect(output).toContain('Passed on attempt 2') expect(output).toContain('Focus lost') }) it('should never mark skipped steps, expected failures or unexecuted tests as passed', () => { for (const test of [ entry({ attempts: [attempt({ skippedSteps: ['focus restoration'] })] }), entry({ attempts: [] }), entry({ expectedStatus: 'failed', attempts: [attempt({ status: 'failed' })] }), entry({ attempts: [attempt({ status: 'interrupted' })] }), ]) { const output = buildSummary({ report: report({ tests: [test] }) }) expect(output).toContain('Incomplete') expect(output).not.toContain('✅ 1/1 passed') } }) it('should separate scanner validation and include runner failures', () => { const output = buildSummary({ report: report({ tests: [entry({ workflow: 'Scanner validation' })], errors: ['Server exited'], status: 'failed', }), }) expect(output).toContain('**Scanner validation**') expect(output).toContain('Server exited') expect(output).toContain('Run: failed') }) it('should preserve literal failure text and newlines while escaping HTML and terminal controls', () => { const output = buildSummary({ report: report({ tests: [ entry({ attempts: [ attempt({ status: 'failed', errors: [ '\u001b[31mtest/__helpers/[name] `code` * _ ~ \\path |\u001b[0m\n\u001b[0m'] }), ], }), ], }), matchedFiles: ['a|.tsx'], repositoryURL: 'https://github.com/payloadcms/payload', sha: 'abc', }) expect(output).not.toContain('') expect(output).not.toContain('