|
| 1 | +import simpleGit from 'simple-git' |
| 2 | +import { Git } from './git' |
| 3 | + |
| 4 | +jest.mock('simple-git') |
| 5 | + |
| 6 | +const mockRaw = jest.fn() |
| 7 | +const mockPush = jest.fn() |
| 8 | +const mockRemote = jest.fn() |
| 9 | +const mockFetch = jest.fn() |
| 10 | +const mockGitInstance = { |
| 11 | + env: jest.fn().mockReturnThis(), |
| 12 | + raw: mockRaw, |
| 13 | + push: mockPush, |
| 14 | + remote: mockRemote, |
| 15 | + fetch: mockFetch, |
| 16 | +} |
| 17 | +;(simpleGit as jest.Mock).mockReturnValue(mockGitInstance) |
| 18 | + |
| 19 | +function makeRepo(): Git { |
| 20 | + return new Git('/tmp/test', 'https://origin.example.com/repo.git', 'user', 'user@example.com', undefined, 'main') |
| 21 | +} |
| 22 | + |
| 23 | +describe('Git.testRemoteConnection', () => { |
| 24 | + beforeEach(() => jest.clearAllMocks()) |
| 25 | + |
| 26 | + it('returns false when remote is empty (no refs)', async () => { |
| 27 | + mockRaw.mockResolvedValue('') |
| 28 | + const repo = makeRepo() |
| 29 | + const result = await repo.testRemoteConnection('https://example.com/repo.git', 'mypass', 'main', 'myuser') |
| 30 | + expect(result).toBe(false) |
| 31 | + expect(mockRaw).toHaveBeenCalledWith(['ls-remote', expect.stringContaining('myuser'), 'refs/heads/main']) |
| 32 | + }) |
| 33 | + |
| 34 | + it('returns true when remote has existing refs', async () => { |
| 35 | + mockRaw.mockResolvedValue('abc123\trefs/heads/main\n') |
| 36 | + const repo = makeRepo() |
| 37 | + const result = await repo.testRemoteConnection('https://example.com/repo.git', 'mypass', 'main', 'myuser') |
| 38 | + expect(result).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + it('calls ls-remote with PAT only (no username) in url', async () => { |
| 42 | + mockRaw.mockResolvedValue('abc123\trefs/heads/main\n') |
| 43 | + const repo = makeRepo() |
| 44 | + const result = await repo.testRemoteConnection('https://example.com/repo.git', 'mytoken', 'main') |
| 45 | + expect(result).toBe(true) |
| 46 | + expect(mockRaw).toHaveBeenCalledWith(['ls-remote', 'https://mytoken@example.com/repo.git', 'refs/heads/main']) |
| 47 | + }) |
| 48 | + |
| 49 | + it('returns false when branch does not exist but remote has other refs', async () => { |
| 50 | + mockRaw.mockResolvedValue('') |
| 51 | + const repo = makeRepo() |
| 52 | + const result = await repo.testRemoteConnection('https://example.com/repo.git', 'mypass', 'feature-branch', 'myuser') |
| 53 | + expect(result).toBe(false) |
| 54 | + expect(mockRaw).toHaveBeenCalledWith(['ls-remote', expect.stringContaining('myuser'), 'refs/heads/feature-branch']) |
| 55 | + }) |
| 56 | + |
| 57 | + it('throws when ls-remote fails (unreachable remote)', async () => { |
| 58 | + mockRaw.mockRejectedValue(new Error('exit code 128')) |
| 59 | + const repo = makeRepo() |
| 60 | + await expect(repo.testRemoteConnection('https://bad.example.com/repo.git', 'p', 'main', 'u')).rejects.toThrow() |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +describe('Git.pushToNewRemote', () => { |
| 65 | + beforeEach(() => jest.clearAllMocks()) |
| 66 | + |
| 67 | + it('unshallows, adds migration-remote, pushes, then removes in finally', async () => { |
| 68 | + mockFetch.mockResolvedValue({}) |
| 69 | + mockRemote.mockResolvedValue('') |
| 70 | + mockPush.mockResolvedValue({}) |
| 71 | + const repo = makeRepo() |
| 72 | + await repo.pushToNewRemote('https://example.com/repo.git', 'main', 'p', 'u') |
| 73 | + |
| 74 | + expect(mockFetch).toHaveBeenCalledWith(['origin', '--unshallow']) |
| 75 | + expect(mockRemote).toHaveBeenCalledWith( |
| 76 | + expect.arrayContaining(['add', 'migration-remote', expect.stringContaining('u')]), |
| 77 | + ) |
| 78 | + expect(mockPush).toHaveBeenCalledWith('migration-remote', 'HEAD:refs/heads/main') |
| 79 | + expect(mockRemote).toHaveBeenCalledWith(['remove', 'migration-remote']) |
| 80 | + }) |
| 81 | + |
| 82 | + it('continues when unshallow fails (repo already complete)', async () => { |
| 83 | + mockFetch.mockRejectedValue(new Error('--unshallow on a complete repository does not make sense')) |
| 84 | + mockRemote.mockResolvedValue('') |
| 85 | + mockPush.mockResolvedValue({}) |
| 86 | + const repo = makeRepo() |
| 87 | + await repo.pushToNewRemote('https://example.com/repo.git', 'main', 'p', 'u') |
| 88 | + |
| 89 | + expect(mockPush).toHaveBeenCalledWith('migration-remote', 'HEAD:refs/heads/main') |
| 90 | + }) |
| 91 | + |
| 92 | + it('uses PAT only (no username) in migration-remote url', async () => { |
| 93 | + mockFetch.mockResolvedValue({}) |
| 94 | + mockRemote.mockResolvedValue('') |
| 95 | + mockPush.mockResolvedValue({}) |
| 96 | + const repo = makeRepo() |
| 97 | + await repo.pushToNewRemote('https://example.com/repo.git', 'main', 'mytoken') |
| 98 | + |
| 99 | + expect(mockRemote).toHaveBeenCalledWith( |
| 100 | + expect.arrayContaining(['add', 'migration-remote', 'https://mytoken@example.com/repo.git']), |
| 101 | + ) |
| 102 | + }) |
| 103 | + |
| 104 | + it('pushes local branch to target branch using refspec when branch names differ', async () => { |
| 105 | + mockFetch.mockResolvedValue({}) |
| 106 | + mockRemote.mockResolvedValue('') |
| 107 | + mockPush.mockResolvedValue({}) |
| 108 | + const repo = makeRepo() // local branch is 'main' |
| 109 | + await repo.pushToNewRemote('https://example.com/repo.git', 'feature-branch', 'p', 'u') |
| 110 | + |
| 111 | + expect(mockPush).toHaveBeenCalledWith('migration-remote', 'HEAD:refs/heads/feature-branch') |
| 112 | + }) |
| 113 | + |
| 114 | + it('removes migration-remote in finally even when push fails', async () => { |
| 115 | + mockFetch.mockResolvedValue({}) |
| 116 | + mockRemote.mockResolvedValue('') |
| 117 | + mockPush.mockRejectedValue(new Error('push failed')) |
| 118 | + const repo = makeRepo() |
| 119 | + await expect(repo.pushToNewRemote('https://example.com/repo.git', 'main', 'p', 'u')).rejects.toThrow('push failed') |
| 120 | + expect(mockRemote).toHaveBeenCalledWith(['remove', 'migration-remote']) |
| 121 | + }) |
| 122 | +}) |
0 commit comments