import { GitAdapter } from '../../../src/adapters/outbound/GitAdapter'; import { simpleGit } from 'simple-git'; // Mock simple-git jest.mock('simple-git'); const mockSimpleGit = simpleGit as jest.MockedFunction; describe('GitAdapter', () => { let adapter: GitAdapter; let mockGit: any; beforeEach(() => { mockGit = { diff: jest.fn(), log: jest.fn(), status: jest.fn(), }; mockSimpleGit.mockReturnValue(mockGit); adapter = new GitAdapter('/test/repo'); }); afterEach(() => { jest.clearAllMocks(); }); it('should initialize with correct repository path', () => { expect(mockSimpleGit).toHaveBeenCalledWith('/test/repo'); }); it('should get diffs between commits', async () => { const mockDiffResult = { diff: `diff --git a/src/test.ts b/src/test.ts index 1234567..abcdefg 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,3 +1,4 @@ function test() { + console.log('test'); return true; }`, }; mockGit.diff.mockResolvedValue(mockDiffResult); const diffs = await adapter.getCommitDiff('abc123', 'def456'); expect(mockGit.diff).toHaveBeenCalledWith(['abc123', 'def456']); expect(diffs).toHaveLength(1); expect(diffs[0].filePath).toBe('src/test.ts'); }); it('should get staged diffs', async () => { const mockDiffResult = { diff: `diff --git a/src/test.ts b/src/test.ts index 1234567..abcdefg 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,3 +1,4 @@ function test() { + console.log('test'); return true; }`, }; mockGit.diff.mockResolvedValue(mockDiffResult); const diffs = await adapter.getStagedDiff(); expect(mockGit.diff).toHaveBeenCalledWith(['--cached']); expect(diffs).toHaveLength(1); }); it('should get unstaged diffs', async () => { const mockDiffResult = { diff: `diff --git a/src/test.ts b/src/test.ts index 1234567..abcdefg 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,3 +1,4 @@ function test() { + console.log('test'); return true; }`, }; mockGit.diff.mockResolvedValue(mockDiffResult); const diffs = await adapter.getUnstagedDiff(); expect(mockGit.diff).toHaveBeenCalledWith(); expect(diffs).toHaveLength(1); }); it('should get commit info', async () => { const mockLogResult = { total: 1, latest: { hash: 'abc123', message: 'Test commit', author_name: 'Test Author', date: new Date('2023-01-01'), }, }; const mockDiffResult = 'src/test.ts\nsrc/another.ts'; mockGit.log.mockResolvedValue(mockLogResult); mockGit.diff.mockResolvedValue(mockDiffResult); const commitInfo = await adapter.getCommitInfo('abc123'); expect(commitInfo).toEqual({ hash: 'abc123', message: 'Test commit', author: 'Test Author', date: new Date('2023-01-01'), files: ['src/test.ts', 'src/another.ts'], }); }); it('should return null for non-existent commit', async () => { const mockLogResult = { total: 0, latest: null }; mockGit.log.mockResolvedValue(mockLogResult); const commitInfo = await adapter.getCommitInfo('nonexistent'); expect(commitInfo).toBeNull(); }); });