|
| 1 | +import { expect, test, vi, describe, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { compileGenericGitHostConfig_file } from './repoCompileUtils'; |
| 3 | + |
| 4 | +// Mock the git module |
| 5 | +vi.mock('./git.js', () => ({ |
| 6 | + isPathAValidGitRepoRoot: vi.fn(), |
| 7 | + getOriginUrl: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +// Mock the glob module |
| 11 | +vi.mock('glob', () => ({ |
| 12 | + glob: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +import { isPathAValidGitRepoRoot, getOriginUrl } from './git.js'; |
| 16 | +import { glob } from 'glob'; |
| 17 | + |
| 18 | +const mockedGlob = vi.mocked(glob); |
| 19 | +const mockedIsPathAValidGitRepoRoot = vi.mocked(isPathAValidGitRepoRoot); |
| 20 | +const mockedGetOriginUrl = vi.mocked(getOriginUrl); |
| 21 | + |
| 22 | +describe('compileGenericGitHostConfig_file', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.resetAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should return warning when glob pattern matches no paths', async () => { |
| 32 | + mockedGlob.mockResolvedValue([]); |
| 33 | + |
| 34 | + const config = { |
| 35 | + type: 'git' as const, |
| 36 | + url: 'file:///path/to/nonexistent/repo', |
| 37 | + }; |
| 38 | + |
| 39 | + const result = await compileGenericGitHostConfig_file(config, 1); |
| 40 | + |
| 41 | + expect(result.repoData).toHaveLength(0); |
| 42 | + expect(result.warnings).toHaveLength(1); |
| 43 | + expect(result.warnings[0]).toContain('No paths matched the pattern'); |
| 44 | + expect(result.warnings[0]).toContain('/path/to/nonexistent/repo'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should return warning when path is not a valid git repo', async () => { |
| 48 | + mockedGlob.mockResolvedValue(['/path/to/not-a-repo']); |
| 49 | + mockedIsPathAValidGitRepoRoot.mockResolvedValue(false); |
| 50 | + |
| 51 | + const config = { |
| 52 | + type: 'git' as const, |
| 53 | + url: 'file:///path/to/not-a-repo', |
| 54 | + }; |
| 55 | + |
| 56 | + const result = await compileGenericGitHostConfig_file(config, 1); |
| 57 | + |
| 58 | + expect(result.repoData).toHaveLength(0); |
| 59 | + expect(result.warnings.length).toBeGreaterThanOrEqual(1); |
| 60 | + expect(result.warnings.some(w => w.includes('not a git repository'))).toBe(true); |
| 61 | + expect(result.warnings.some(w => w.includes('No valid git repositories found'))).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should return warning when git repo has no origin url', async () => { |
| 65 | + mockedGlob.mockResolvedValue(['/path/to/repo']); |
| 66 | + mockedIsPathAValidGitRepoRoot.mockResolvedValue(true); |
| 67 | + mockedGetOriginUrl.mockResolvedValue(null); |
| 68 | + |
| 69 | + const config = { |
| 70 | + type: 'git' as const, |
| 71 | + url: 'file:///path/to/repo', |
| 72 | + }; |
| 73 | + |
| 74 | + const result = await compileGenericGitHostConfig_file(config, 1); |
| 75 | + |
| 76 | + expect(result.repoData).toHaveLength(0); |
| 77 | + expect(result.warnings.length).toBeGreaterThanOrEqual(1); |
| 78 | + expect(result.warnings.some(w => w.includes('remote.origin.url not found'))).toBe(true); |
| 79 | + expect(result.warnings.some(w => w.includes('No valid git repositories found'))).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + test('should successfully compile when valid git repo is found', async () => { |
| 83 | + mockedGlob.mockResolvedValue(['/path/to/valid/repo']); |
| 84 | + mockedIsPathAValidGitRepoRoot.mockResolvedValue(true); |
| 85 | + mockedGetOriginUrl.mockResolvedValue('https://github.com/test/repo.git'); |
| 86 | + |
| 87 | + const config = { |
| 88 | + type: 'git' as const, |
| 89 | + url: 'file:///path/to/valid/repo', |
| 90 | + }; |
| 91 | + |
| 92 | + const result = await compileGenericGitHostConfig_file(config, 1); |
| 93 | + |
| 94 | + expect(result.repoData).toHaveLength(1); |
| 95 | + expect(result.warnings).toHaveLength(0); |
| 96 | + expect(result.repoData[0].cloneUrl).toBe('file:///path/to/valid/repo'); |
| 97 | + expect(result.repoData[0].name).toBe('github.com/test/repo'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('should return warnings for invalid repos and success for valid ones', async () => { |
| 101 | + mockedGlob.mockResolvedValue(['/path/to/valid/repo', '/path/to/invalid/repo']); |
| 102 | + mockedIsPathAValidGitRepoRoot.mockImplementation(async ({ path }) => { |
| 103 | + return path === '/path/to/valid/repo'; |
| 104 | + }); |
| 105 | + mockedGetOriginUrl.mockImplementation(async (path: string) => { |
| 106 | + if (path === '/path/to/valid/repo') { |
| 107 | + return 'https://github.com/test/repo.git'; |
| 108 | + } |
| 109 | + return null; |
| 110 | + }); |
| 111 | + |
| 112 | + const config = { |
| 113 | + type: 'git' as const, |
| 114 | + url: 'file:///path/to/**/repo', |
| 115 | + }; |
| 116 | + |
| 117 | + const result = await compileGenericGitHostConfig_file(config, 1); |
| 118 | + |
| 119 | + expect(result.repoData).toHaveLength(1); |
| 120 | + expect(result.warnings).toHaveLength(1); |
| 121 | + expect(result.warnings[0]).toContain('/path/to/invalid/repo'); |
| 122 | + expect(result.warnings[0]).toContain('not a git repository'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments