|
| 1 | +import {beforeEach, describe, expect, it, vi} from 'vitest' |
| 2 | + |
| 3 | +const {octokitCtorMock, getInputMock, getBooleanInputMock} = vi.hoisted(() => ({ |
| 4 | + octokitCtorMock: vi.fn(), |
| 5 | + getInputMock: vi.fn(), |
| 6 | + getBooleanInputMock: vi.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock('@actions/core', () => ({ |
| 10 | + getInput: getInputMock, |
| 11 | + getBooleanInput: getBooleanInputMock, |
| 12 | + info: vi.fn(), |
| 13 | + debug: vi.fn(), |
| 14 | + warning: vi.fn(), |
| 15 | + setOutput: vi.fn(), |
| 16 | + setFailed: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('@octokit/core', () => ({ |
| 20 | + Octokit: { |
| 21 | + plugin: vi.fn(() => octokitCtorMock), |
| 22 | + }, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@octokit/plugin-throttling', () => ({ |
| 26 | + throttling: vi.fn(), |
| 27 | +})) |
| 28 | + |
| 29 | +describe('file action index', () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.resetModules() |
| 32 | + vi.clearAllMocks() |
| 33 | + }) |
| 34 | + |
| 35 | + it('passes baseUrl to Octokit when base_url input is provided', async () => { |
| 36 | + getInputMock.mockImplementation((name: string) => { |
| 37 | + switch (name) { |
| 38 | + case 'findings': |
| 39 | + return '[]' |
| 40 | + case 'repository': |
| 41 | + return 'org/repo' |
| 42 | + case 'token': |
| 43 | + return 'token' |
| 44 | + case 'base_url': |
| 45 | + return 'https://ghe.example.com/api/v3' |
| 46 | + case 'cached_filings': |
| 47 | + return '[]' |
| 48 | + default: |
| 49 | + return '' |
| 50 | + } |
| 51 | + }) |
| 52 | + getBooleanInputMock.mockReturnValue(false) |
| 53 | + |
| 54 | + const {default: run} = await import('../src/index.ts') |
| 55 | + await run() |
| 56 | + |
| 57 | + expect(octokitCtorMock).toHaveBeenCalledWith( |
| 58 | + expect.objectContaining({ |
| 59 | + auth: 'token', |
| 60 | + baseUrl: 'https://ghe.example.com/api/v3', |
| 61 | + }), |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('uses Octokit default API URL when base_url input is not provided', async () => { |
| 66 | + getInputMock.mockImplementation((name: string) => { |
| 67 | + switch (name) { |
| 68 | + case 'findings': |
| 69 | + return '[]' |
| 70 | + case 'repository': |
| 71 | + return 'org/repo' |
| 72 | + case 'token': |
| 73 | + return 'token' |
| 74 | + case 'cached_filings': |
| 75 | + return '[]' |
| 76 | + default: |
| 77 | + return '' |
| 78 | + } |
| 79 | + }) |
| 80 | + getBooleanInputMock.mockReturnValue(false) |
| 81 | + |
| 82 | + const {default: run} = await import('../src/index.ts') |
| 83 | + await run() |
| 84 | + |
| 85 | + expect(octokitCtorMock).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ |
| 87 | + auth: 'token', |
| 88 | + baseUrl: undefined, |
| 89 | + }), |
| 90 | + ) |
| 91 | + }) |
| 92 | +}) |
0 commit comments