|
| 1 | +jest.mock('execa'); |
| 2 | + |
| 3 | +import { execa } from 'execa'; |
| 4 | +import { |
| 5 | + sanitizeRepoName, |
| 6 | + checkGhAuth, |
| 7 | + repoExists, |
| 8 | + createRepo, |
| 9 | +} from '../github.js'; |
| 10 | + |
| 11 | +const mockExeca = execa as jest.MockedFunction<typeof execa>; |
| 12 | + |
| 13 | +describe('sanitizeRepoName', () => { |
| 14 | + it('returns lowercase name with invalid chars replaced by hyphen', () => { |
| 15 | + expect(sanitizeRepoName('My Project')).toBe('my-project'); |
| 16 | + expect(sanitizeRepoName('my_project')).toBe('my_project'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('strips npm scope and uses package name only', () => { |
| 20 | + expect(sanitizeRepoName('@my-org/my-package')).toBe('my-package'); |
| 21 | + expect(sanitizeRepoName('@scope/package-name')).toBe('package-name'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('collapses multiple hyphens', () => { |
| 25 | + expect(sanitizeRepoName('my---project')).toBe('my-project'); |
| 26 | + expect(sanitizeRepoName(' spaces ')).toBe('spaces'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('strips leading and trailing hyphens', () => { |
| 30 | + expect(sanitizeRepoName('--my-project--')).toBe('my-project'); |
| 31 | + expect(sanitizeRepoName('-single-')).toBe('single'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('allows alphanumeric, hyphens, underscores, and dots', () => { |
| 35 | + expect(sanitizeRepoName('my.project_1')).toBe('my.project_1'); |
| 36 | + expect(sanitizeRepoName('v1.0.0')).toBe('v1.0.0'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns "my-project" when result would be empty', () => { |
| 40 | + expect(sanitizeRepoName('@scope/---')).toBe('my-project'); |
| 41 | + expect(sanitizeRepoName('!!!')).toBe('my-project'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('handles scoped package with only special chars after scope', () => { |
| 45 | + expect(sanitizeRepoName('@org/---')).toBe('my-project'); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('checkGhAuth', () => { |
| 50 | + beforeEach(() => { |
| 51 | + mockExeca.mockReset(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns ok: false when gh auth status fails', async () => { |
| 55 | + mockExeca.mockRejectedValueOnce(new Error('not logged in')); |
| 56 | + |
| 57 | + const result = await checkGhAuth(); |
| 58 | + |
| 59 | + expect(result).toEqual({ |
| 60 | + ok: false, |
| 61 | + message: expect.stringContaining('GitHub CLI (gh) is not installed'), |
| 62 | + }); |
| 63 | + expect(mockExeca).toHaveBeenCalledWith('gh', ['auth', 'status'], { reject: true }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns ok: false when gh api user returns empty login', async () => { |
| 67 | + mockExeca.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }); |
| 68 | + mockExeca.mockResolvedValueOnce({ stdout: '\n \n', stderr: '', exitCode: 0 }); |
| 69 | + |
| 70 | + const result = await checkGhAuth(); |
| 71 | + |
| 72 | + expect(result).toEqual({ |
| 73 | + ok: false, |
| 74 | + message: expect.stringContaining('Could not determine your GitHub username'), |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns ok: false when gh api user throws', async () => { |
| 79 | + mockExeca.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }); |
| 80 | + mockExeca.mockRejectedValueOnce(new Error('API error')); |
| 81 | + |
| 82 | + const result = await checkGhAuth(); |
| 83 | + |
| 84 | + expect(result).toEqual({ |
| 85 | + ok: false, |
| 86 | + message: expect.stringContaining('Could not fetch your GitHub username'), |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns ok: true with username when auth and api succeed', async () => { |
| 91 | + mockExeca.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }); |
| 92 | + mockExeca.mockResolvedValueOnce({ |
| 93 | + stdout: ' octocat ', |
| 94 | + stderr: '', |
| 95 | + exitCode: 0, |
| 96 | + } as Awaited<ReturnType<typeof execa>>); |
| 97 | + |
| 98 | + const result = await checkGhAuth(); |
| 99 | + |
| 100 | + expect(result).toEqual({ ok: true, username: 'octocat' }); |
| 101 | + expect(mockExeca).toHaveBeenNthCalledWith(2, 'gh', ['api', 'user', '--jq', '.login'], { |
| 102 | + encoding: 'utf8', |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('repoExists', () => { |
| 108 | + beforeEach(() => { |
| 109 | + mockExeca.mockReset(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('returns true when gh api repos/owner/repo succeeds', async () => { |
| 113 | + mockExeca.mockResolvedValueOnce({ stdout: '{}', stderr: '', exitCode: 0 }); |
| 114 | + |
| 115 | + const result = await repoExists('octocat', 'my-repo'); |
| 116 | + |
| 117 | + expect(result).toBe(true); |
| 118 | + expect(mockExeca).toHaveBeenCalledWith('gh', ['api', 'repos/octocat/my-repo'], { |
| 119 | + reject: true, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns false when gh api throws (e.g. 404)', async () => { |
| 124 | + mockExeca.mockRejectedValueOnce(new Error('Not Found')); |
| 125 | + |
| 126 | + const result = await repoExists('octocat', 'nonexistent'); |
| 127 | + |
| 128 | + expect(result).toBe(false); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('createRepo', () => { |
| 133 | + const projectPath = '/tmp/my-app'; |
| 134 | + const username = 'octocat'; |
| 135 | + |
| 136 | + beforeEach(() => { |
| 137 | + mockExeca.mockReset(); |
| 138 | + mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited< |
| 139 | + ReturnType<typeof execa> |
| 140 | + >); |
| 141 | + }); |
| 142 | + |
| 143 | + it('calls gh repo create with expected args and returns repo URL', async () => { |
| 144 | + const url = await createRepo({ |
| 145 | + repoName: 'my-app', |
| 146 | + projectPath, |
| 147 | + username, |
| 148 | + }); |
| 149 | + |
| 150 | + expect(url).toBe('https://github.com/octocat/my-app.git'); |
| 151 | + expect(mockExeca).toHaveBeenCalledWith( |
| 152 | + 'gh', |
| 153 | + [ |
| 154 | + 'repo', |
| 155 | + 'create', |
| 156 | + 'my-app', |
| 157 | + '--public', |
| 158 | + '--source', |
| 159 | + projectPath, |
| 160 | + '--description', |
| 161 | + '', |
| 162 | + '--remote', |
| 163 | + 'origin', |
| 164 | + ], |
| 165 | + { stdio: 'inherit', cwd: projectPath }, |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it('passes description when provided', async () => { |
| 170 | + await createRepo({ |
| 171 | + repoName: 'my-app', |
| 172 | + projectPath, |
| 173 | + username, |
| 174 | + description: 'My cool project', |
| 175 | + }); |
| 176 | + |
| 177 | + expect(mockExeca).toHaveBeenCalledWith( |
| 178 | + 'gh', |
| 179 | + expect.arrayContaining(['--description', 'My cool project']), |
| 180 | + expect.any(Object), |
| 181 | + ); |
| 182 | + }); |
| 183 | +}); |
0 commit comments