|
| 1 | +import { describe, expect, it, mock } from 'bun:test'; |
| 2 | + |
| 3 | +// Define the mock functions so we can manipulate them in tests |
| 4 | +const spawnSyncMock = mock(() => ({ |
| 5 | + status: 0, |
| 6 | + stdout: Buffer.from('mock-hash-123\n'), |
| 7 | +})); |
| 8 | + |
| 9 | +mock.module('child_process', () => ({ |
| 10 | + spawnSync: spawnSyncMock, |
| 11 | +})); |
| 12 | + |
| 13 | +const listRemotesMock = mock(async () => [ |
| 14 | + { remote: 'origin', url: 'https://github.com/test/repo.git' }, |
| 15 | +]); |
| 16 | +const logMock = mock(async () => [ |
| 17 | + { |
| 18 | + oid: 'mock-commit-hash', |
| 19 | + commit: { |
| 20 | + message: 'mock commit message', |
| 21 | + author: { name: 'Test Author' }, |
| 22 | + committer: { name: 'Test Committer', timestamp: 1625097600 }, |
| 23 | + }, |
| 24 | + }, |
| 25 | +]); |
| 26 | + |
| 27 | +mock.module('isomorphic-git', () => ({ |
| 28 | + default: { |
| 29 | + listRemotes: listRemotesMock, |
| 30 | + log: logMock, |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +describe('git utils', async () => { |
| 35 | + const { getCommitInfo, getCurrentCommit } = await import('../src/utils/git'); |
| 36 | + |
| 37 | + describe('getCurrentCommit', () => { |
| 38 | + it('should return the commit hash when git command succeeds', () => { |
| 39 | + spawnSyncMock.mockImplementationOnce(() => ({ |
| 40 | + status: 0, |
| 41 | + stdout: Buffer.from('abcdef1234567890\n'), |
| 42 | + })); |
| 43 | + |
| 44 | + const commit = getCurrentCommit(); |
| 45 | + expect(commit).toBe('abcdef1234567890'); |
| 46 | + expect(spawnSyncMock).toHaveBeenCalledWith('git', ['rev-parse', 'HEAD']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should throw an error when git command fails', () => { |
| 50 | + spawnSyncMock.mockImplementationOnce(() => ({ |
| 51 | + status: 128, |
| 52 | + stdout: Buffer.from(''), |
| 53 | + stderr: Buffer.from( |
| 54 | + 'fatal: not a git repository (or any of the parent directories): .git\n', |
| 55 | + ), |
| 56 | + })); |
| 57 | + |
| 58 | + expect(() => getCurrentCommit()).toThrow('Not a git repository'); |
| 59 | + expect(spawnSyncMock).toHaveBeenCalledWith('git', ['rev-parse', 'HEAD']); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('getCommitInfo', () => { |
| 64 | + it('should return correct commit info', async () => { |
| 65 | + const info = await getCommitInfo(); |
| 66 | + |
| 67 | + expect(info).toBeDefined(); |
| 68 | + expect(info?.hash).toBe('mock-commit-hash'); |
| 69 | + expect(info?.message).toBe('mock commit message'); |
| 70 | + expect(info?.author).toBe('Test Author'); |
| 71 | + expect(info?.timestamp).toBe('1625097600'); |
| 72 | + expect(info?.origin).toBe('https://github.com/test/repo.git'); |
| 73 | + |
| 74 | + expect(listRemotesMock).toHaveBeenCalled(); |
| 75 | + expect(logMock).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle missing author name by falling back to committer name', async () => { |
| 79 | + logMock.mockImplementationOnce(async () => [ |
| 80 | + { |
| 81 | + oid: 'mock-commit-hash-2', |
| 82 | + commit: { |
| 83 | + message: 'another message', |
| 84 | + author: { name: '' }, |
| 85 | + committer: { name: 'Fallback Committer', timestamp: 1625098000 }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + ]); |
| 89 | + |
| 90 | + const info = await getCommitInfo(); |
| 91 | + expect(info?.author).toBe('Fallback Committer'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return undefined and log error when git operations fail', async () => { |
| 95 | + const originalConsoleError = console.error; |
| 96 | + const consoleErrorMock = mock(); |
| 97 | + console.error = consoleErrorMock; |
| 98 | + |
| 99 | + listRemotesMock.mockImplementationOnce(async () => { |
| 100 | + throw new Error('Git operation failed'); |
| 101 | + }); |
| 102 | + |
| 103 | + const info = await getCommitInfo(); |
| 104 | + |
| 105 | + expect(info).toBeUndefined(); |
| 106 | + expect(consoleErrorMock).toHaveBeenCalled(); |
| 107 | + |
| 108 | + console.error = originalConsoleError; |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments