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