Skip to content

Commit 313d2d2

Browse files
test(backend): fix repoCompileUtils tests broken by fs.stat directory check
Mocks fs/promises so tests don't hit the real filesystem. The directory check added in #1049 caused all file-based config tests to fail since fake paths like /path/to/valid/repo don't exist on disk. Also adds a test verifying that a file path (non-directory) produces a warning. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 34a1435 commit 313d2d2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

packages/backend/src/repoCompileUtils.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@ vi.mock('glob', () => ({
1414
glob: vi.fn(),
1515
}));
1616

17+
// Mock fs/promises so tests don't touch the real filesystem.
18+
// By default, stat resolves as a directory; individual tests can override this.
19+
vi.mock('fs/promises', () => ({
20+
default: {
21+
stat: vi.fn().mockResolvedValue({ isDirectory: () => true }),
22+
},
23+
}));
24+
1725
import { isPathAValidGitRepoRoot, getOriginUrl, isUrlAValidGitRepo } from './git.js';
1826
import { glob } from 'glob';
27+
import fs from 'fs/promises';
1928

2029
const mockedGlob = vi.mocked(glob);
2130
const mockedIsPathAValidGitRepoRoot = vi.mocked(isPathAValidGitRepoRoot);
2231
const mockedGetOriginUrl = vi.mocked(getOriginUrl);
2332
const mockedIsUrlAValidGitRepo = vi.mocked(isUrlAValidGitRepo);
33+
const mockedFsStat = vi.mocked(fs.stat);
2434

2535
describe('compileGenericGitHostConfig_file', () => {
2636
beforeEach(() => {
2737
vi.clearAllMocks();
38+
// Default: all paths exist and are directories. Override per-test as needed.
39+
mockedFsStat.mockResolvedValue({ isDirectory: () => true } as any);
2840
});
2941

3042
afterEach(() => {
@@ -47,6 +59,22 @@ describe('compileGenericGitHostConfig_file', () => {
4759
expect(result.warnings[0]).toContain('/path/to/nonexistent/repo');
4860
});
4961

62+
test('should return warning when path is a file, not a directory', async () => {
63+
mockedGlob.mockResolvedValue(['/path/to/a-file.txt']);
64+
mockedFsStat.mockResolvedValue({ isDirectory: () => false } as any);
65+
66+
const config = {
67+
type: 'git' as const,
68+
url: 'file:///path/to/a-file.txt',
69+
};
70+
71+
const result = await compileGenericGitHostConfig_file(config, 1);
72+
73+
expect(result.repoData).toHaveLength(0);
74+
expect(result.warnings.length).toBeGreaterThanOrEqual(1);
75+
expect(result.warnings.some(w => w.includes('not a directory'))).toBe(true);
76+
});
77+
5078
test('should return warning when path is not a valid git repo', async () => {
5179
mockedGlob.mockResolvedValue(['/path/to/not-a-repo']);
5280
mockedIsPathAValidGitRepoRoot.mockResolvedValue(false);

0 commit comments

Comments
 (0)