|
1 | 1 | import { GitHub } from '../../infra/github'; |
2 | 2 | import { getLatestSuccessfulRunISODate } from '../../utils-infra/getLatestSuccessfulRunISODate'; |
3 | 3 |
|
4 | | -import { beforeEach, expect, it, vi } from 'vitest'; |
5 | | - |
6 | | -const ACTION_NAME = 'yuki-no'; |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
7 | 5 |
|
8 | 6 | const mockListWorkflowRunsForRepo = vi.fn(); |
9 | 7 |
|
10 | 8 | vi.mock('../../infra/github', () => ({ |
11 | 9 | GitHub: vi.fn().mockImplementation(() => ({ |
12 | | - api: { actions: { listWorkflowRunsForRepo: mockListWorkflowRunsForRepo } }, |
| 10 | + api: { |
| 11 | + rest: { |
| 12 | + actions: { |
| 13 | + listWorkflowRunsForRepo: mockListWorkflowRunsForRepo, |
| 14 | + }, |
| 15 | + }, |
| 16 | + }, |
| 17 | + ownerAndRepo: { owner: 'test-owner', repo: 'test-repo' }, |
| 18 | + configuredLabels: ['label1', 'label2'], |
13 | 19 | })), |
14 | 20 | })); |
15 | 21 |
|
16 | | -const MOCK_CONFIG = { |
17 | | - accessToken: 'test-token', |
18 | | - labels: ['test-label'], |
19 | | - repoSpec: { |
20 | | - owner: 'test-owner', |
21 | | - name: 'test-repo', |
22 | | - branch: 'main', |
23 | | - }, |
24 | | -}; |
| 22 | +vi.mock('../../utils-infra/getTranslationIssues', () => ({ |
| 23 | + getTranslationIssues: vi.fn(), |
| 24 | +})); |
25 | 25 |
|
26 | | -const mockGitHub = new GitHub(MOCK_CONFIG); |
| 26 | +const mockGitHub = new GitHub({} as any); |
27 | 27 |
|
28 | | -beforeEach(() => { |
29 | | - vi.clearAllMocks(); |
30 | | -}); |
| 28 | +const mockGetTranslationIssues = ( |
| 29 | + await import('../../utils-infra/getTranslationIssues') |
| 30 | +).getTranslationIssues as unknown as ReturnType<typeof vi.fn>; |
31 | 31 |
|
32 | | -it('Should return undefined when there are no successful workflow runs', async () => { |
33 | | - mockListWorkflowRunsForRepo.mockResolvedValue({ |
34 | | - data: { |
35 | | - workflow_runs: [], |
36 | | - }, |
| 32 | +describe('getLatestSuccessfulRunISODate', () => { |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
37 | 35 | }); |
38 | 36 |
|
39 | | - const result = await getLatestSuccessfulRunISODate(mockGitHub); |
| 37 | + it('First execution confirmed when (hint && no previous success && no issues) -> returns undefined', async () => { |
| 38 | + const hint = true; |
40 | 39 |
|
41 | | - expect(result).toBeUndefined(); |
42 | | -}); |
| 40 | + mockListWorkflowRunsForRepo.mockResolvedValue({ |
| 41 | + data: { total_count: 0, workflow_runs: [] }, |
| 42 | + }); |
| 43 | + mockGetTranslationIssues.mockResolvedValue([]); |
43 | 44 |
|
44 | | -it('Should return the last execution time when an action with the matching workflow name exists', async () => { |
45 | | - const EXPECTED_LAST_CREATED_AT = '2023-01-04T12:00:00Z'; |
46 | | - |
47 | | - mockListWorkflowRunsForRepo.mockResolvedValue({ |
48 | | - data: { |
49 | | - workflow_runs: [ |
50 | | - { name: ACTION_NAME, created_at: '2023-01-03T12:00:00Z' }, |
51 | | - { name: 'other-action', created_at: '2023-01-03T12:00:00Z' }, |
52 | | - { name: 'another-action', created_at: '2023-01-02T12:00:00Z' }, |
53 | | - { name: ACTION_NAME, created_at: EXPECTED_LAST_CREATED_AT }, |
54 | | - ], |
55 | | - }, |
| 45 | + const result = await getLatestSuccessfulRunISODate(mockGitHub, hint); |
| 46 | + |
| 47 | + expect(result).toBeUndefined(); |
| 48 | + expect(mockGetTranslationIssues).toHaveBeenCalledWith(mockGitHub, 'all'); |
56 | 49 | }); |
57 | 50 |
|
58 | | - const result = await getLatestSuccessfulRunISODate(mockGitHub); |
| 51 | + it('Not first execution when previous successful runs exist -> returns latest created_at by name match', async () => { |
| 52 | + const hint = true; |
| 53 | + const EXPECTED_LAST_CREATED_AT = '2023-01-04T12:00:00Z'; |
| 54 | + |
| 55 | + mockListWorkflowRunsForRepo.mockResolvedValue({ |
| 56 | + data: { |
| 57 | + total_count: 3, |
| 58 | + workflow_runs: [ |
| 59 | + { |
| 60 | + created_at: '2023-01-05T12:00:00Z', |
| 61 | + conclusion: 'success', |
| 62 | + name: 'other', |
| 63 | + }, |
| 64 | + { |
| 65 | + created_at: EXPECTED_LAST_CREATED_AT, |
| 66 | + conclusion: 'success', |
| 67 | + name: 'yuki-no', |
| 68 | + }, |
| 69 | + { |
| 70 | + created_at: '2023-01-03T12:00:00Z', |
| 71 | + conclusion: 'failure', |
| 72 | + name: 'yuki-no', |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + }); |
59 | 77 |
|
60 | | - expect(result).toBe(EXPECTED_LAST_CREATED_AT); |
| 78 | + const result = await getLatestSuccessfulRunISODate(mockGitHub, hint); |
| 79 | + |
| 80 | + expect(result).toBe(EXPECTED_LAST_CREATED_AT); |
| 81 | + }); |
| 82 | + |
| 83 | + it('No successful runs (but completed exist) and not first-run -> throws due to API inconsistency', async () => { |
| 84 | + mockListWorkflowRunsForRepo.mockResolvedValue({ |
| 85 | + data: { |
| 86 | + total_count: 2, |
| 87 | + workflow_runs: [ |
| 88 | + { |
| 89 | + created_at: '2023-01-05T12:00:00Z', |
| 90 | + conclusion: 'failure', |
| 91 | + name: 'yuki-no', |
| 92 | + }, |
| 93 | + { |
| 94 | + created_at: '2023-01-04T12:00:00Z', |
| 95 | + conclusion: 'cancelled', |
| 96 | + name: 'yuki-no', |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + }); |
| 101 | + mockGetTranslationIssues.mockResolvedValue([ |
| 102 | + { |
| 103 | + number: 1, |
| 104 | + body: 'existing', |
| 105 | + labels: ['l1'], |
| 106 | + hash: 'h', |
| 107 | + isoDate: '2023-01-01T00:00:00Z', |
| 108 | + }, |
| 109 | + ]); |
| 110 | + |
| 111 | + await expect( |
| 112 | + getLatestSuccessfulRunISODate(mockGitHub, false), |
| 113 | + ).rejects.toThrow(); |
| 114 | + }); |
61 | 115 | }); |
0 commit comments