|
| 1 | +/** |
| 2 | + * Unit tests for the CI-native output layer attached to `test run --all` |
| 3 | + * (issue #99, reshaped from the withdrawn top-level `ci` command). The heavy |
| 4 | + * lifting (trigger + poll) is the batch command's, already covered by its own |
| 5 | + * suites; these tests cover the presentation seams: payload reduction, the |
| 6 | + * job-summary Markdown, and the GitHub gating (env-driven and `--gh-output` |
| 7 | + * forced). |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, expect, it } from 'vitest'; |
| 11 | +import { |
| 12 | + emitGithubOutputs, |
| 13 | + renderJobSummaryMarkdown, |
| 14 | + summarizeAcceptedPayload, |
| 15 | + type CiSummary, |
| 16 | +} from './gh-output.js'; |
| 17 | + |
| 18 | +const PAYLOAD = JSON.stringify({ |
| 19 | + accepted: [ |
| 20 | + { |
| 21 | + testId: 'test_a', |
| 22 | + runId: 'run_a', |
| 23 | + status: 'passed', |
| 24 | + dashboardUrl: 'https://portal.example.com/a', |
| 25 | + }, |
| 26 | + { |
| 27 | + testId: 'test_b', |
| 28 | + runId: 'run_b', |
| 29 | + status: 'failed', |
| 30 | + error: { code: 'INTERNAL', message: 'boom', exitCode: 1 }, |
| 31 | + }, |
| 32 | + { testId: 'test_c', runId: 'run_c', status: 'timeout' }, |
| 33 | + ], |
| 34 | + conflicts: [], |
| 35 | +}); |
| 36 | + |
| 37 | +describe('summarizeAcceptedPayload', () => { |
| 38 | + it('reduces accepted[] rows into counts and rows', () => { |
| 39 | + const summary = summarizeAcceptedPayload(PAYLOAD); |
| 40 | + expect(summary).toMatchObject({ total: 3, passed: 1, failed: 1, timedOut: 1 }); |
| 41 | + expect(summary.runs[1]).toMatchObject({ testId: 'test_b', status: 'failed', error: 'boom' }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('unparseable or non-batch output reduces to an empty summary (never throws)', () => { |
| 45 | + expect(summarizeAcceptedPayload('')).toMatchObject({ total: 0, passed: 0 }); |
| 46 | + expect(summarizeAcceptedPayload('{"method":"POST"}')).toMatchObject({ total: 0 }); |
| 47 | + expect(summarizeAcceptedPayload('not json')).toMatchObject({ total: 0 }); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('renderJobSummaryMarkdown', () => { |
| 52 | + it('renders the counts headline and one table row per run', () => { |
| 53 | + const md = renderJobSummaryMarkdown(summarizeAcceptedPayload(PAYLOAD)); |
| 54 | + expect(md).toContain('**1/3 passed** (1 failed, 1 timed out)'); |
| 55 | + expect(md).toContain('| test_a | passed | [dashboard](https://portal.example.com/a) |'); |
| 56 | + expect(md).toContain('| test_c | timeout | run_c |'); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('emitGithubOutputs', () => { |
| 61 | + const summary: CiSummary = summarizeAcceptedPayload(PAYLOAD); |
| 62 | + |
| 63 | + function makeSinks() { |
| 64 | + const stdout: string[] = []; |
| 65 | + const stderr: string[] = []; |
| 66 | + const appended: Array<{ path: string; content: string }> = []; |
| 67 | + return { |
| 68 | + stdout, |
| 69 | + stderr, |
| 70 | + appended, |
| 71 | + sinks: { |
| 72 | + stdout: (line: string) => stdout.push(line), |
| 73 | + stderr: (line: string) => stderr.push(line), |
| 74 | + appendFile: (path: string, content: string) => appended.push({ path, content }), |
| 75 | + }, |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + it('appends the job summary and annotates only non-passed runs under Actions', () => { |
| 80 | + const { stdout, appended, sinks } = makeSinks(); |
| 81 | + emitGithubOutputs( |
| 82 | + summary, |
| 83 | + { GITHUB_ACTIONS: 'true', GITHUB_STEP_SUMMARY: '/gh/summary.md' }, |
| 84 | + sinks, |
| 85 | + ); |
| 86 | + expect(appended).toHaveLength(1); |
| 87 | + expect(appended[0]!.path).toBe('/gh/summary.md'); |
| 88 | + expect(appended[0]!.content).toContain('TestSprite results'); |
| 89 | + const annotations = stdout.filter(line => line.startsWith('::error')); |
| 90 | + expect(annotations).toHaveLength(2); |
| 91 | + expect(annotations[0]).toContain('test_b'); |
| 92 | + expect(annotations[0]).toContain('boom'); |
| 93 | + expect(annotations[1]).toContain('test_c'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('emits nothing off-CI, and a broken summary file downgrades to stderr', () => { |
| 97 | + const offCi = makeSinks(); |
| 98 | + emitGithubOutputs(summary, {}, offCi.sinks); |
| 99 | + expect(offCi.stdout).toHaveLength(0); |
| 100 | + expect(offCi.appended).toHaveLength(0); |
| 101 | + |
| 102 | + const broken = makeSinks(); |
| 103 | + emitGithubOutputs( |
| 104 | + summary, |
| 105 | + { GITHUB_STEP_SUMMARY: '/gh/summary.md' }, |
| 106 | + { |
| 107 | + ...broken.sinks, |
| 108 | + appendFile: () => { |
| 109 | + throw new Error('EROFS'); |
| 110 | + }, |
| 111 | + }, |
| 112 | + ); |
| 113 | + expect(broken.stderr.join('\n')).toContain('could not append'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('force (--gh-output) emits annotations off-Actions; the step summary still needs its env path', () => { |
| 117 | + const forced = makeSinks(); |
| 118 | + emitGithubOutputs(summary, {}, forced.sinks, { force: true }); |
| 119 | + const annotations = forced.stdout.filter(line => line.startsWith('::error')); |
| 120 | + expect(annotations).toHaveLength(2); |
| 121 | + expect(forced.appended).toHaveLength(0); |
| 122 | + }); |
| 123 | +}); |
0 commit comments