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