|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { FollowupImportantsUseCase } from '../../../../usecases/cli/followupImportants.usecase.js'; |
| 3 | + |
| 4 | +function createMockFetch(response: object) { |
| 5 | + return vi.fn().mockResolvedValue({ |
| 6 | + ok: true, |
| 7 | + json: () => Promise.resolve(response), |
| 8 | + }); |
| 9 | +} |
| 10 | + |
| 11 | +function createDeps(overrides: Partial<Parameters<typeof FollowupImportantsUseCase.prototype.execute>[1]> = {}) { |
| 12 | + return { |
| 13 | + serverPort: 3000, |
| 14 | + log: vi.fn(), |
| 15 | + error: vi.fn(), |
| 16 | + fetch: createMockFetch({ success: true, triggered: 0, candidates: [], failed: [] }), |
| 17 | + ...overrides, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe('FollowupImportantsUseCase', () => { |
| 22 | + it('should call the batch API endpoint', async () => { |
| 23 | + const fetch = createMockFetch({ success: true, triggered: 0, candidates: [], failed: [] }); |
| 24 | + const deps = createDeps({ fetch }); |
| 25 | + const usecase = new FollowupImportantsUseCase(deps); |
| 26 | + |
| 27 | + await usecase.execute({}); |
| 28 | + |
| 29 | + expect(fetch).toHaveBeenCalledWith( |
| 30 | + 'http://localhost:3000/api/mr-tracking/followup-importants', |
| 31 | + expect.objectContaining({ method: 'POST' }), |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should pass projectPath when provided', async () => { |
| 36 | + const fetch = createMockFetch({ success: true, triggered: 0, candidates: [], failed: [] }); |
| 37 | + const deps = createDeps({ fetch }); |
| 38 | + const usecase = new FollowupImportantsUseCase(deps); |
| 39 | + |
| 40 | + await usecase.execute({ project: '/path/to/repo' }); |
| 41 | + |
| 42 | + expect(fetch).toHaveBeenCalledWith( |
| 43 | + expect.any(String), |
| 44 | + expect.objectContaining({ |
| 45 | + body: JSON.stringify({ projectPath: '/path/to/repo' }), |
| 46 | + }), |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should log message when no candidates found', async () => { |
| 51 | + const deps = createDeps(); |
| 52 | + const usecase = new FollowupImportantsUseCase(deps); |
| 53 | + |
| 54 | + await usecase.execute({}); |
| 55 | + |
| 56 | + expect(deps.log).toHaveBeenCalledWith(expect.stringContaining('No pending-approval')); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should log triggered count and candidate details', async () => { |
| 60 | + const fetch = createMockFetch({ |
| 61 | + success: true, |
| 62 | + triggered: 2, |
| 63 | + candidates: [ |
| 64 | + { mrId: 'gitlab-proj-10', mrNumber: 10, title: 'Fix auth' }, |
| 65 | + { mrId: 'gitlab-proj-11', mrNumber: 11, title: 'Add tests' }, |
| 66 | + ], |
| 67 | + failed: [], |
| 68 | + }); |
| 69 | + const deps = createDeps({ fetch }); |
| 70 | + const usecase = new FollowupImportantsUseCase(deps); |
| 71 | + |
| 72 | + await usecase.execute({}); |
| 73 | + |
| 74 | + expect(deps.log).toHaveBeenCalledWith(expect.stringContaining('2')); |
| 75 | + expect(deps.log).toHaveBeenCalledWith(expect.stringContaining('!10')); |
| 76 | + expect(deps.log).toHaveBeenCalledWith(expect.stringContaining('!11')); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should log failures when some followups fail', async () => { |
| 80 | + const fetch = createMockFetch({ |
| 81 | + success: true, |
| 82 | + triggered: 1, |
| 83 | + candidates: [ |
| 84 | + { mrId: 'gitlab-proj-10', mrNumber: 10, title: 'Fix auth' }, |
| 85 | + ], |
| 86 | + failed: [{ mrId: 'gitlab-proj-11', error: 'timeout' }], |
| 87 | + }); |
| 88 | + const deps = createDeps({ fetch }); |
| 89 | + const usecase = new FollowupImportantsUseCase(deps); |
| 90 | + |
| 91 | + await usecase.execute({}); |
| 92 | + |
| 93 | + expect(deps.error).toHaveBeenCalledWith(expect.stringContaining('1 failed')); |
| 94 | + }); |
| 95 | +}); |
0 commit comments