|
| 1 | +import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest' |
| 2 | + |
| 3 | +// --- Mock the issue-mutating helpers so we can assert they are NEVER called in dry run --- |
| 4 | +const openIssue = vi.fn() |
| 5 | +const reopenIssue = vi.fn() |
| 6 | +const closeIssue = vi.fn() |
| 7 | +vi.mock('../src/openIssue.js', () => ({openIssue: (...args: unknown[]) => openIssue(...args)})) |
| 8 | +vi.mock('../src/reopenIssue.js', () => ({reopenIssue: (...args: unknown[]) => reopenIssue(...args)})) |
| 9 | +vi.mock('../src/closeIssue.js', () => ({closeIssue: (...args: unknown[]) => closeIssue(...args)})) |
| 10 | + |
| 11 | +// --- Mock @actions/core: control inputs, capture logs/outputs --- |
| 12 | +const inputs: Record<string, string> = {} |
| 13 | +const infoLines: string[] = [] |
| 14 | +const outputs: Record<string, string> = {} |
| 15 | +vi.mock('@actions/core', () => ({ |
| 16 | + getInput: (name: string) => inputs[name] ?? '', |
| 17 | + getBooleanInput: (name: string) => (inputs[name] ?? 'false') === 'true', |
| 18 | + setOutput: (name: string, value: string) => { |
| 19 | + outputs[name] = value |
| 20 | + }, |
| 21 | + info: (msg: string) => { |
| 22 | + infoLines.push(msg) |
| 23 | + }, |
| 24 | + debug: () => {}, |
| 25 | + warning: () => {}, |
| 26 | + setFailed: () => {}, |
| 27 | +})) |
| 28 | + |
| 29 | +// --- Mock fs: feed findings/cached filings in, swallow the output write --- |
| 30 | +const files: Record<string, string> = {} |
| 31 | +vi.mock('node:fs', () => ({ |
| 32 | + default: { |
| 33 | + readFileSync: (p: string) => files[p], |
| 34 | + writeFileSync: (p: string, data: string) => { |
| 35 | + files[p] = data |
| 36 | + }, |
| 37 | + }, |
| 38 | +})) |
| 39 | + |
| 40 | +// --- Stub Octokit so constructing it in index.ts doesn't do anything real --- |
| 41 | +const octokitRequest = vi.fn() |
| 42 | +vi.mock('@octokit/core', () => ({ |
| 43 | + Octokit: { |
| 44 | + plugin: () => |
| 45 | + class { |
| 46 | + request = octokitRequest |
| 47 | + }, |
| 48 | + }, |
| 49 | +})) |
| 50 | +vi.mock('@octokit/plugin-throttling', () => ({throttling: {}})) |
| 51 | + |
| 52 | +import runFileAction from '../src/index.ts' |
| 53 | + |
| 54 | +const finding = { |
| 55 | + scannerType: 'axe', |
| 56 | + ruleId: 'color-contrast', |
| 57 | + url: 'https://example.com/page', |
| 58 | + html: '<span>Low contrast</span>', |
| 59 | + problemShort: 'elements must meet minimum color contrast ratio thresholds', |
| 60 | + problemUrl: 'https://dequeuniversity.com/rules/axe/4.10/color-contrast', |
| 61 | + solutionShort: 'ensure sufficient contrast', |
| 62 | +} |
| 63 | + |
| 64 | +// A second finding with no matching cached filing -> NEW (open) |
| 65 | +const newFinding = {...finding, ruleId: 'heading-order', html: '<h3>Skipped</h3>'} |
| 66 | + |
| 67 | +// A cached filing whose finding matches `finding` -> REPEATED (reopen) |
| 68 | +const repeatedCached = { |
| 69 | + issue: {id: 1, nodeId: 'N1', url: 'https://github.com/org/repo/issues/1', title: 'repeat'}, |
| 70 | + findings: [finding], |
| 71 | +} |
| 72 | + |
| 73 | +// A cached filing with NO matching finding this run -> RESOLVED (close) |
| 74 | +const resolvedCached = { |
| 75 | + issue: {id: 2, nodeId: 'N2', url: 'https://github.com/org/repo/issues/2', title: 'resolved'}, |
| 76 | + findings: [{...finding, ruleId: 'landmark-one-main', html: '<div>old</div>'}], |
| 77 | +} |
| 78 | + |
| 79 | +function setup() { |
| 80 | + // findings file: includes `finding` (matches repeatedCached) and `newFinding` (brand new) |
| 81 | + files['/tmp/findings.json'] = JSON.stringify([finding, newFinding]) |
| 82 | + // cached filings: one repeated, one resolved (its finding is absent from findings file) |
| 83 | + files['/tmp/cached.json'] = JSON.stringify([repeatedCached, resolvedCached]) |
| 84 | + inputs.findings_file = '/tmp/findings.json' |
| 85 | + inputs.cached_filings_file = '/tmp/cached.json' |
| 86 | + inputs.repository = 'org/repo' |
| 87 | + inputs.token = 'fake-token' |
| 88 | +} |
| 89 | + |
| 90 | +describe('file action — dry_run', () => { |
| 91 | + beforeEach(() => { |
| 92 | + vi.clearAllMocks() |
| 93 | + infoLines.length = 0 |
| 94 | + for (const k of Object.keys(inputs)) delete inputs[k] |
| 95 | + for (const k of Object.keys(outputs)) delete outputs[k] |
| 96 | + vi.spyOn(console, 'table').mockImplementation(() => {}) |
| 97 | + }) |
| 98 | + afterEach(() => { |
| 99 | + vi.restoreAllMocks() |
| 100 | + }) |
| 101 | + |
| 102 | + it('does not open, reopen, or close any issues when dry_run is true', async () => { |
| 103 | + setup() |
| 104 | + inputs.dry_run = 'true' |
| 105 | + |
| 106 | + await runFileAction() |
| 107 | + |
| 108 | + expect(openIssue).not.toHaveBeenCalled() |
| 109 | + expect(reopenIssue).not.toHaveBeenCalled() |
| 110 | + expect(closeIssue).not.toHaveBeenCalled() |
| 111 | + expect(octokitRequest).not.toHaveBeenCalled() |
| 112 | + }) |
| 113 | + |
| 114 | + it('logs the intended action for each filing type', async () => { |
| 115 | + setup() |
| 116 | + inputs.dry_run = 'true' |
| 117 | + |
| 118 | + await runFileAction() |
| 119 | + |
| 120 | + const log = infoLines.join('\n') |
| 121 | + expect(log).toContain( |
| 122 | + '[dry run] Would OPEN a new issue for: elements must meet minimum color contrast ratio thresholds (https://example.com/page)', |
| 123 | + ) |
| 124 | + expect(log).toContain('[dry run] Would REOPEN issue: https://github.com/org/repo/issues/1') |
| 125 | + expect(log).toContain('[dry run] Would CLOSE issue: https://github.com/org/repo/issues/2') |
| 126 | + }) |
| 127 | + |
| 128 | + it('logs a summary table with counts', async () => { |
| 129 | + setup() |
| 130 | + inputs.dry_run = 'true' |
| 131 | + |
| 132 | + await runFileAction() |
| 133 | + |
| 134 | + expect(vi.mocked(console.table)).toHaveBeenCalledWith( |
| 135 | + expect.objectContaining({open: 1, reopen: 1, close: 1, total: 3}), |
| 136 | + ) |
| 137 | + }) |
| 138 | + |
| 139 | + it('still writes the filings_file output in dry run', async () => { |
| 140 | + setup() |
| 141 | + inputs.dry_run = 'true' |
| 142 | + |
| 143 | + await runFileAction() |
| 144 | + |
| 145 | + expect(outputs.filings_file).toBeDefined() |
| 146 | + }) |
| 147 | + |
| 148 | + it('updates in-memory issue state for an accurate preview without mutating remotely', async () => { |
| 149 | + setup() |
| 150 | + inputs.dry_run = 'true' |
| 151 | + |
| 152 | + await runFileAction() |
| 153 | + |
| 154 | + // The path written is `${RUNNER_TEMP||'/tmp'}/filings-<uuid>.json`; grab it from the output. |
| 155 | + const writtenPath = outputs.filings_file |
| 156 | + const writtenFilings = JSON.parse(files[writtenPath]) |
| 157 | + |
| 158 | + // Resolved cached filing (issues/2) -> would be CLOSED |
| 159 | + const resolved = writtenFilings.find( |
| 160 | + (f: {issue?: {url?: string}}) => f.issue?.url === 'https://github.com/org/repo/issues/2', |
| 161 | + ) |
| 162 | + expect(resolved?.issue.state).toBe('closed') |
| 163 | + |
| 164 | + // Repeated cached filing (issues/1) -> would be REOPENED |
| 165 | + const repeated = writtenFilings.find( |
| 166 | + (f: {issue?: {url?: string}}) => f.issue?.url === 'https://github.com/org/repo/issues/1', |
| 167 | + ) |
| 168 | + expect(repeated?.issue.state).toBe('reopened') |
| 169 | + |
| 170 | + // New filing -> issue object created with state 'open' |
| 171 | + const opened = writtenFilings.find((f: {issue?: {state?: string}}) => f.issue?.state === 'open') |
| 172 | + expect(opened).toBeDefined() |
| 173 | + |
| 174 | + // And confirm we still didn't actually mutate anything remotely |
| 175 | + expect(openIssue).not.toHaveBeenCalled() |
| 176 | + expect(reopenIssue).not.toHaveBeenCalled() |
| 177 | + expect(closeIssue).not.toHaveBeenCalled() |
| 178 | + }) |
| 179 | + |
| 180 | + it('does call the mutating helpers when dry_run is false (regression guard)', async () => { |
| 181 | + setup() |
| 182 | + inputs.dry_run = 'false' |
| 183 | + // helpers return a minimal Octokit-style response so index.ts can read response.data |
| 184 | + const resp = {data: {id: 9, node_id: 'N', number: 9, html_url: 'https://github.com/org/repo/issues/9', title: 't'}} |
| 185 | + openIssue.mockResolvedValue(resp) |
| 186 | + reopenIssue.mockResolvedValue(resp) |
| 187 | + closeIssue.mockResolvedValue(resp) |
| 188 | + |
| 189 | + await runFileAction() |
| 190 | + |
| 191 | + expect(openIssue).toHaveBeenCalled() |
| 192 | + expect(reopenIssue).toHaveBeenCalled() |
| 193 | + expect(closeIssue).toHaveBeenCalled() |
| 194 | + }) |
| 195 | +}) |
0 commit comments