|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import * as core from '@actions/core'; |
| 4 | + |
| 5 | +import * as index from '../src/find-workflow'; |
| 6 | +import { findWorkflow } from '../src/lib'; |
| 7 | +import { mockGetBooleanInput, mockGetInput } from './utils'; |
| 8 | + |
| 9 | +vi.mock('@actions/core'); |
| 10 | +vi.mock('../src/lib'); |
| 11 | + |
| 12 | +// Spy the action's entrypoint |
| 13 | +const findWorkflowActionSpy = vi.spyOn(index, 'findWorkflowAction'); |
| 14 | + |
| 15 | +const owner = 'dsanders11'; |
| 16 | +const projectNumber = '94'; |
| 17 | +const name = 'workflow-name'; |
| 18 | +const projectId = 'project-id'; |
| 19 | +const workflowId = 'workflow-id'; |
| 20 | +const workflowNumber = 42; |
| 21 | + |
| 22 | +describe('findWorkflowAction', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('requires the project-number input', async () => { |
| 28 | + mockGetInput({ owner }); |
| 29 | + |
| 30 | + await index.findWorkflowAction(); |
| 31 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 32 | + |
| 33 | + expect(core.setFailed).toHaveBeenCalledTimes(1); |
| 34 | + expect(core.setFailed).toHaveBeenLastCalledWith( |
| 35 | + 'Input required and not supplied: project-number' |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('requires the name input', async () => { |
| 40 | + mockGetInput({ owner, 'project-number': projectNumber }); |
| 41 | + |
| 42 | + await index.findWorkflowAction(); |
| 43 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 44 | + |
| 45 | + expect(core.setFailed).toHaveBeenCalledTimes(1); |
| 46 | + expect(core.setFailed).toHaveBeenLastCalledWith( |
| 47 | + 'Input required and not supplied: name' |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('handles workflow not found', async () => { |
| 52 | + mockGetInput({ owner, 'project-number': projectNumber, name }); |
| 53 | + mockGetBooleanInput({ 'fail-if-workflow-not-found': true }); |
| 54 | + vi.mocked(findWorkflow).mockResolvedValue(null); |
| 55 | + |
| 56 | + await index.findWorkflowAction(); |
| 57 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 58 | + |
| 59 | + expect(core.setFailed).toHaveBeenCalledTimes(1); |
| 60 | + expect(core.setFailed).toHaveBeenLastCalledWith( |
| 61 | + `Workflow not found: ${name}` |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('can ignore item not found', async () => { |
| 66 | + mockGetInput({ owner, 'project-number': projectNumber, name }); |
| 67 | + mockGetBooleanInput({ 'fail-if-workflow-not-found': false }); |
| 68 | + vi.mocked(findWorkflow).mockResolvedValue(null); |
| 69 | + |
| 70 | + await index.findWorkflowAction(); |
| 71 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 72 | + |
| 73 | + expect(core.setFailed).not.toHaveBeenCalled(); |
| 74 | + expect(core.setOutput).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('handles generic errors', async () => { |
| 78 | + mockGetInput({ owner, 'project-number': projectNumber, name }); |
| 79 | + vi.mocked(findWorkflow).mockImplementation(() => { |
| 80 | + throw new Error('Server error'); |
| 81 | + }); |
| 82 | + |
| 83 | + await index.findWorkflowAction(); |
| 84 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 85 | + |
| 86 | + expect(core.setFailed).toHaveBeenCalledTimes(1); |
| 87 | + expect(core.setFailed).toHaveBeenLastCalledWith('Server error'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('stringifies non-errors', async () => { |
| 91 | + mockGetInput({ owner, 'project-number': projectNumber, name }); |
| 92 | + vi.mocked(findWorkflow).mockImplementation(() => { |
| 93 | + throw 42; // eslint-disable-line no-throw-literal |
| 94 | + }); |
| 95 | + |
| 96 | + await index.findWorkflowAction(); |
| 97 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 98 | + |
| 99 | + expect(core.setFailed).toHaveBeenCalledTimes(1); |
| 100 | + expect(core.setFailed).toHaveBeenLastCalledWith('42'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('sets output', async () => { |
| 104 | + mockGetInput({ owner, 'project-number': projectNumber, name }); |
| 105 | + vi.mocked(findWorkflow).mockResolvedValue({ |
| 106 | + id: workflowId, |
| 107 | + name, |
| 108 | + number: workflowNumber, |
| 109 | + enabled: true, |
| 110 | + projectId |
| 111 | + }); |
| 112 | + |
| 113 | + await index.findWorkflowAction(); |
| 114 | + expect(findWorkflowActionSpy).toHaveReturned(); |
| 115 | + |
| 116 | + expect(core.setOutput).toHaveBeenCalledTimes(5); |
| 117 | + expect(core.setOutput).toHaveBeenCalledWith('id', workflowId); |
| 118 | + expect(core.setOutput).toHaveBeenCalledWith('name', name); |
| 119 | + expect(core.setOutput).toHaveBeenCalledWith('number', workflowNumber); |
| 120 | + expect(core.setOutput).toHaveBeenCalledWith('enabled', true); |
| 121 | + expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId); |
| 122 | + }); |
| 123 | +}); |
0 commit comments