|
| 1 | +import {describe, it, expect, vi, beforeEach} from 'vitest' |
| 2 | + |
| 3 | +import {getWontfixIssueNumbers, shouldReopenIssue, WONTFIX_LABEL} from '../src/shouldReopenIssue.ts' |
| 4 | +import {Issue} from '../src/Issue.ts' |
| 5 | + |
| 6 | +function issueAt(issueNumber: number): Issue { |
| 7 | + return new Issue({ |
| 8 | + id: issueNumber, |
| 9 | + nodeId: `node-${issueNumber}`, |
| 10 | + url: `https://github.com/org/filing-repo/issues/${issueNumber}`, |
| 11 | + title: `Accessibility issue ${issueNumber}`, |
| 12 | + state: 'closed', |
| 13 | + }) |
| 14 | +} |
| 15 | + |
| 16 | +// `pages` is consumed one response per request call, in order. |
| 17 | +function mockOctokit(pages: Array<Array<{number: number; pull_request?: unknown}>>) { |
| 18 | + const request = vi.fn() |
| 19 | + for (const page of pages) { |
| 20 | + request.mockResolvedValueOnce({data: page}) |
| 21 | + } |
| 22 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 23 | + return {request} as any |
| 24 | +} |
| 25 | + |
| 26 | +describe('getWontfixIssueNumbers', () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + }) |
| 30 | + |
| 31 | + it('returns the numbers of closed wontfix issues as a set', async () => { |
| 32 | + const octokit = mockOctokit([[{number: 1}, {number: 5}, {number: 9}]]) |
| 33 | + |
| 34 | + const result = await getWontfixIssueNumbers(octokit, {owner: 'org', repository: 'repo'}) |
| 35 | + |
| 36 | + expect(result).toEqual(new Set([1, 5, 9])) |
| 37 | + }) |
| 38 | + |
| 39 | + it('requests closed issues filtered by the wontfix label', async () => { |
| 40 | + const octokit = mockOctokit([[]]) |
| 41 | + |
| 42 | + await getWontfixIssueNumbers(octokit, {owner: 'org', repository: 'repo'}) |
| 43 | + |
| 44 | + expect(octokit.request).toHaveBeenCalledWith( |
| 45 | + 'GET /repos/org/repo/issues', |
| 46 | + expect.objectContaining({state: 'closed', labels: WONTFIX_LABEL}), |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + it('returns an empty set when no issues are labeled wontfix', async () => { |
| 51 | + const octokit = mockOctokit([[]]) |
| 52 | + |
| 53 | + const result = await getWontfixIssueNumbers(octokit, {owner: 'org', repository: 'repo'}) |
| 54 | + |
| 55 | + expect(result.size).toBe(0) |
| 56 | + }) |
| 57 | + |
| 58 | + it('paginates until a short page is returned', async () => { |
| 59 | + const firstPage = Array.from({length: 100}, (_, i) => ({number: i + 1})) |
| 60 | + const octokit = mockOctokit([firstPage, [{number: 101}]]) |
| 61 | + |
| 62 | + const result = await getWontfixIssueNumbers(octokit, {owner: 'org', repository: 'repo'}) |
| 63 | + |
| 64 | + expect(octokit.request).toHaveBeenCalledTimes(2) |
| 65 | + expect(result.has(1)).toBe(true) |
| 66 | + expect(result.has(101)).toBe(true) |
| 67 | + }) |
| 68 | + |
| 69 | + it('ignores pull requests returned by the issues endpoint', async () => { |
| 70 | + const octokit = mockOctokit([[{number: 2}, {number: 3, pull_request: {url: 'https://example.com/pull/3'}}]]) |
| 71 | + |
| 72 | + const result = await getWontfixIssueNumbers(octokit, {owner: 'org', repository: 'repo'}) |
| 73 | + |
| 74 | + expect(result).toEqual(new Set([2])) |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +describe('shouldReopenIssue', () => { |
| 79 | + it('returns false when the issue is in the wontfix set', () => { |
| 80 | + expect(shouldReopenIssue(issueAt(7), new Set([7]))).toBe(false) |
| 81 | + }) |
| 82 | + |
| 83 | + it('returns true when the issue is not in the wontfix set', () => { |
| 84 | + expect(shouldReopenIssue(issueAt(7), new Set([1, 2, 3]))).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('returns true when the wontfix set is empty', () => { |
| 88 | + expect(shouldReopenIssue(issueAt(7), new Set())).toBe(true) |
| 89 | + }) |
| 90 | +}) |
0 commit comments