|
| 1 | +/** |
| 2 | + * Unit tests for runLocalAction. |
| 3 | + * Mocks getActionInputsWithDefaults, ProjectRepository, mainRun, chalk, boxen. |
| 4 | + */ |
| 5 | + |
| 6 | +jest.mock('chalk', () => ({ |
| 7 | + cyan: (s: string) => s, |
| 8 | + gray: (s: string) => s, |
| 9 | + red: (s: string) => s, |
| 10 | + default: { cyan: (s: string) => s, gray: (s: string) => s, red: (s: string) => s }, |
| 11 | +})); |
| 12 | +jest.mock('boxen', () => jest.fn((text: string) => text)); |
| 13 | + |
| 14 | +jest.mock('../../utils/logger', () => ({ |
| 15 | + logInfo: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockGetActionInputsWithDefaults = jest.fn(); |
| 19 | +jest.mock('../../utils/yml_utils', () => ({ |
| 20 | + getActionInputsWithDefaults: () => mockGetActionInputsWithDefaults(), |
| 21 | +})); |
| 22 | + |
| 23 | +const mockMainRun = jest.fn(); |
| 24 | +jest.mock('../common_action', () => ({ |
| 25 | + mainRun: (...args: unknown[]) => mockMainRun(...args), |
| 26 | +})); |
| 27 | + |
| 28 | +const mockGetProjectDetail = jest.fn(); |
| 29 | +jest.mock('../../data/repository/project_repository', () => ({ |
| 30 | + ProjectRepository: jest.fn().mockImplementation(() => ({ |
| 31 | + getProjectDetail: mockGetProjectDetail, |
| 32 | + })), |
| 33 | +})); |
| 34 | + |
| 35 | +import { runLocalAction } from '../local_action'; |
| 36 | +import { INPUT_KEYS } from '../../utils/constants'; |
| 37 | + |
| 38 | +/** Minimal defaults so local_action can run (avoids .split on undefined). */ |
| 39 | +function minimalActionInputs(): Record<string, string> { |
| 40 | + const keys = Object.values(INPUT_KEYS) as string[]; |
| 41 | + return Object.fromEntries(keys.map((k) => [k, ''])); |
| 42 | +} |
| 43 | + |
| 44 | +describe('runLocalAction', () => { |
| 45 | + beforeEach(() => { |
| 46 | + jest.clearAllMocks(); |
| 47 | + mockGetActionInputsWithDefaults.mockReturnValue(minimalActionInputs()); |
| 48 | + mockGetProjectDetail.mockResolvedValue({ id: 'p1', title: 'Board', url: 'https://example.com' }); |
| 49 | + mockMainRun.mockResolvedValue([]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('builds Execution from additionalParams and actionInputs and calls mainRun', async () => { |
| 53 | + const params: Record<string, unknown> = { |
| 54 | + [INPUT_KEYS.TOKEN]: 'local-token', |
| 55 | + repo: { owner: 'o', repo: 'r' }, |
| 56 | + eventName: 'push', |
| 57 | + commits: { ref: 'refs/heads/main' }, |
| 58 | + }; |
| 59 | + |
| 60 | + await runLocalAction(params); |
| 61 | + |
| 62 | + expect(mockMainRun).toHaveBeenCalledTimes(1); |
| 63 | + const execution = mockMainRun.mock.calls[0][0]; |
| 64 | + expect(execution).toBeDefined(); |
| 65 | + expect(execution.tokens).toBeDefined(); |
| 66 | + expect(execution.ai).toBeDefined(); |
| 67 | + expect(execution.welcome).toBeDefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('uses additionalParams over actionInputs defaults', async () => { |
| 71 | + mockGetActionInputsWithDefaults.mockReturnValue({ |
| 72 | + ...minimalActionInputs(), |
| 73 | + [INPUT_KEYS.DEBUG]: 'false', |
| 74 | + [INPUT_KEYS.TOKEN]: 'default-token', |
| 75 | + }); |
| 76 | + const params: Record<string, unknown> = { |
| 77 | + [INPUT_KEYS.TOKEN]: 'override-token', |
| 78 | + [INPUT_KEYS.DEBUG]: 'true', |
| 79 | + repo: { owner: 'x', repo: 'y' }, |
| 80 | + eventName: 'push', |
| 81 | + commits: { ref: 'refs/heads/develop' }, |
| 82 | + }; |
| 83 | + |
| 84 | + await runLocalAction(params); |
| 85 | + |
| 86 | + const execution = mockMainRun.mock.calls[0][0]; |
| 87 | + expect(execution.tokens.token).toBe('override-token'); |
| 88 | + expect(execution.debug).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + it('logs steps and reminders via boxen after mainRun', async () => { |
| 92 | + const boxen = require('boxen'); |
| 93 | + mockMainRun.mockResolvedValue([ |
| 94 | + { executed: true, steps: ['Step 1'], errors: [], reminders: [] }, |
| 95 | + { executed: true, steps: [], errors: [], reminders: ['Reminder 1'] }, |
| 96 | + ]); |
| 97 | + const params: Record<string, unknown> = { |
| 98 | + [INPUT_KEYS.TOKEN]: 't', |
| 99 | + repo: { owner: 'o', repo: 'r' }, |
| 100 | + eventName: 'push', |
| 101 | + commits: { ref: 'refs/heads/main' }, |
| 102 | + }; |
| 103 | + |
| 104 | + await runLocalAction(params); |
| 105 | + |
| 106 | + expect(boxen).toHaveBeenCalled(); |
| 107 | + expect(boxen.mock.calls[0][0]).toContain('Step 1'); |
| 108 | + expect(boxen.mock.calls[0][0]).toContain('Reminder 1'); |
| 109 | + }); |
| 110 | +}); |
0 commit comments