|
| 1 | +import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest' |
| 2 | + |
| 3 | +const openIssue = vi.fn() |
| 4 | +const reopenIssue = vi.fn() |
| 5 | +const closeIssue = vi.fn() |
| 6 | +vi.mock('../src/openIssue.js', () => ({openIssue: (...args: unknown[]) => openIssue(...args)})) |
| 7 | +vi.mock('../src/reopenIssue.js', () => ({reopenIssue: (...args: unknown[]) => reopenIssue(...args)})) |
| 8 | +vi.mock('../src/closeIssue.js', () => ({closeIssue: (...args: unknown[]) => closeIssue(...args)})) |
| 9 | + |
| 10 | +const inputs: Record<string, string> = {} |
| 11 | +const infoLines: string[] = [] |
| 12 | +const outputs: Record<string, string> = {} |
| 13 | +vi.mock('@actions/core', () => ({ |
| 14 | + getInput: (name: string) => inputs[name] ?? '', |
| 15 | + getBooleanInput: (name: string) => (inputs[name] ?? 'false') === 'true', |
| 16 | + setOutput: (name: string, value: string) => { |
| 17 | + outputs[name] = value |
| 18 | + }, |
| 19 | + info: (msg: string) => { |
| 20 | + infoLines.push(msg) |
| 21 | + }, |
| 22 | + debug: () => {}, |
| 23 | + warning: () => {}, |
| 24 | + setFailed: () => {}, |
| 25 | +})) |
| 26 | + |
| 27 | +// Feed findings/cached filings in |
| 28 | +const files: Record<string, string> = {} |
| 29 | +vi.mock('node:fs', () => ({ |
| 30 | + default: { |
| 31 | + readFileSync: (p: string) => files[p], |
| 32 | + writeFileSync: (p: string, data: string) => { |
| 33 | + files[p] = data |
| 34 | + }, |
| 35 | + }, |
| 36 | +})) |
| 37 | + |
| 38 | +// Stub Octokit: `request` serves the GET that isWontfixIssue makes |
| 39 | +const octokitRequest = vi.fn() |
| 40 | +vi.mock('@octokit/core', () => ({ |
| 41 | + Octokit: { |
| 42 | + plugin: () => |
| 43 | + class { |
| 44 | + request = octokitRequest |
| 45 | + }, |
| 46 | + }, |
| 47 | +})) |
| 48 | +vi.mock('@octokit/plugin-throttling', () => ({throttling: {}})) |
| 49 | + |
| 50 | +import runFileAction from '../src/index.ts' |
| 51 | + |
| 52 | +const wontfixFinding = { |
| 53 | + scannerType: 'axe', |
| 54 | + ruleId: 'color-contrast', |
| 55 | + url: 'https://example.com/page', |
| 56 | + html: '<span>Low contrast</span>', |
| 57 | + problemShort: 'elements must meet minimum color contrast ratio thresholds', |
| 58 | + problemUrl: 'https://dequeuniversity.com/rules/axe/4.10/color-contrast', |
| 59 | + solutionShort: 'ensure sufficient contrast', |
| 60 | +} |
| 61 | +const normalFinding = {...wontfixFinding, ruleId: 'heading-order', html: '<h3>Skipped</h3>'} |
| 62 | + |
| 63 | +// Both cached filings' findings reappear this run, so both are repeated |
| 64 | +const wontfixCached = { |
| 65 | + issue: {id: 1, nodeId: 'N1', url: 'https://github.com/org/repo/issues/1', title: 'wontfix'}, |
| 66 | + findings: [wontfixFinding], |
| 67 | +} |
| 68 | +const normalCached = { |
| 69 | + issue: {id: 3, nodeId: 'N3', url: 'https://github.com/org/repo/issues/3', title: 'normal'}, |
| 70 | + findings: [normalFinding], |
| 71 | +} |
| 72 | + |
| 73 | +function setup() { |
| 74 | + files['/tmp/findings.json'] = JSON.stringify([wontfixFinding, normalFinding]) |
| 75 | + files['/tmp/cached.json'] = JSON.stringify([wontfixCached, normalCached]) |
| 76 | + inputs.findings_file = '/tmp/findings.json' |
| 77 | + inputs.cached_filings_file = '/tmp/cached.json' |
| 78 | + inputs.repository = 'org/repo' |
| 79 | + inputs.token = 'fake-token' |
| 80 | + // GET issue: issue 1 is labeled wontfix, issue 3 is not |
| 81 | + octokitRequest.mockImplementation((route: string) => |
| 82 | + route.includes('/issues/1') |
| 83 | + ? Promise.resolve({data: {labels: [{name: 'wontfix'}]}}) |
| 84 | + : Promise.resolve({data: {labels: []}}), |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +describe('file action — wontfix label', () => { |
| 89 | + beforeEach(() => { |
| 90 | + vi.clearAllMocks() |
| 91 | + infoLines.length = 0 |
| 92 | + for (const k of Object.keys(inputs)) delete inputs[k] |
| 93 | + for (const k of Object.keys(outputs)) delete outputs[k] |
| 94 | + }) |
| 95 | + afterEach(() => { |
| 96 | + vi.restoreAllMocks() |
| 97 | + }) |
| 98 | + |
| 99 | + it('reopens the unlabeled issue but not the one labeled wontfix', async () => { |
| 100 | + setup() |
| 101 | + |
| 102 | + await runFileAction() |
| 103 | + |
| 104 | + expect(reopenIssue).toHaveBeenCalledTimes(1) |
| 105 | + const reopenedIssue = reopenIssue.mock.calls[0][1] as {url: string} |
| 106 | + expect(reopenedIssue.url).toBe('https://github.com/org/repo/issues/3') |
| 107 | + expect(openIssue).not.toHaveBeenCalled() |
| 108 | + expect(closeIssue).not.toHaveBeenCalled() |
| 109 | + }) |
| 110 | + |
| 111 | + it('logs that it skipped the wontfix issue', async () => { |
| 112 | + setup() |
| 113 | + |
| 114 | + await runFileAction() |
| 115 | + |
| 116 | + expect(infoLines.join('\n')).toContain( |
| 117 | + "Skipping reopen of issue labeled 'wontfix': https://github.com/org/repo/issues/1", |
| 118 | + ) |
| 119 | + }) |
| 120 | +}) |
0 commit comments