-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate-pull-request.test.js
More file actions
303 lines (255 loc) · 9.9 KB
/
create-pull-request.test.js
File metadata and controls
303 lines (255 loc) · 9.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
SUPPORTED_BROWSERS,
SUPPORTED_PLATFORMS,
isHelpRequested,
normalizeGitUrl,
getPullRequestUrl,
getBrowser,
displayHelp,
} from './create-pull-request.js';
// Mock the dependencies
vi.mock('current-git-branch', () => ({
default: vi.fn(() => 'main'),
}));
vi.mock('git-remote-origin-url', () => ({
default: vi.fn(() => Promise.resolve('https://github.com/user/repo.git')),
}));
vi.mock('open', () => ({
default: vi.fn(() => Promise.resolve()),
}));
vi.mock('chalk', () => ({
default: {
hex: vi.fn(() => vi.fn((text) => text)),
red: vi.fn((text) => text),
green: vi.fn((text) => text),
blue: vi.fn((text) => text),
bold: vi.fn((text) => text),
},
}));
describe('create-pull-request CLI', () => {
let originalArgv;
let consoleSpy;
beforeEach(() => {
// Store original process.argv
originalArgv = process.argv;
// Mock console.log to avoid output during tests
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
// Restore original process.argv
process.argv = originalArgv;
// Restore console.log
consoleSpy.mockRestore();
vi.clearAllMocks();
});
describe('Constants', () => {
it('should have correct supported browsers', () => {
expect(SUPPORTED_BROWSERS).toEqual({
chrome: 'google chrome',
firefox: 'firefox',
'firefox-dev': 'firefox developer edition',
edge: 'microsoft edge',
safari: 'safari',
opera: 'opera',
brave: 'brave browser',
});
});
it('should have correct supported platforms', () => {
expect(Object.keys(SUPPORTED_PLATFORMS)).toEqual([
'github.com',
'bitbucket.org',
'gitlab.com',
]);
});
it('should generate correct GitHub URL', () => {
const githubHandler = SUPPORTED_PLATFORMS['github.com'];
expect(githubHandler('https://github.com/user/repo', 'main')).toBe(
'https://github.com/user/repo/pull/new/main'
);
});
it('should generate correct Bitbucket URL', () => {
const bitbucketHandler = SUPPORTED_PLATFORMS['bitbucket.org'];
expect(bitbucketHandler('https://bitbucket.org/user/repo', 'main')).toBe(
'https://bitbucket.org/user/repo/pull-requests/new?source=main&t=1#diff'
);
});
it('should generate correct GitLab URL', () => {
const gitlabHandler = SUPPORTED_PLATFORMS['gitlab.com'];
expect(gitlabHandler('https://gitlab.com/user/repo', 'main')).toBe(
'https://gitlab.com/user/repo/-/merge_requests/new?merge_request[source_branch]=main'
);
});
});
describe('isHelpRequested', () => {
it('should return true for --help flag', () => {
process.argv = ['node', 'script.js', '--help'];
expect(isHelpRequested()).toBe(true);
});
it('should return true for -h flag', () => {
process.argv = ['node', 'script.js', '-h'];
expect(isHelpRequested()).toBe(true);
});
it('should return false for no flags', () => {
process.argv = ['node', 'script.js'];
expect(isHelpRequested()).toBe(false);
});
it('should return false for other flags', () => {
process.argv = ['node', 'script.js', 'chrome'];
expect(isHelpRequested()).toBe(false);
});
it('should return true when help flag is present with other args', () => {
process.argv = ['node', 'script.js', 'chrome', '--help'];
expect(isHelpRequested()).toBe(true);
});
});
describe('normalizeGitUrl', () => {
it('should convert SSH URL to HTTPS', () => {
const sshUrl = 'git@github.com:user/repo.git';
const expected = 'https://github.com/user/repo';
expect(normalizeGitUrl(sshUrl)).toBe(expected);
});
it('should convert SSH URL with .org domain', () => {
const sshUrl = 'git@bitbucket.org:user/repo.git';
const expected = 'https://bitbucket.org/user/repo';
expect(normalizeGitUrl(sshUrl)).toBe(expected);
});
it('should remove .git suffix from HTTPS URL', () => {
const httpsUrl = 'https://github.com/user/repo.git';
const expected = 'https://github.com/user/repo';
expect(normalizeGitUrl(httpsUrl)).toBe(expected);
});
it('should handle HTTPS URL without .git suffix', () => {
const httpsUrl = 'https://github.com/user/repo';
const expected = 'https://github.com/user/repo';
expect(normalizeGitUrl(httpsUrl)).toBe(expected);
});
it('should handle URL with path', () => {
const url = 'git@gitlab.com:group/subgroup/repo.git';
const expected = 'https://gitlab.com/group/subgroup/repo';
expect(normalizeGitUrl(url)).toBe(expected);
});
});
describe('getPullRequestUrl', () => {
it('should generate GitHub pull request URL', () => {
const repoUrl = 'https://github.com/user/repo';
const expected = 'https://github.com/user/repo/pull/new/main';
expect(getPullRequestUrl(repoUrl)).toBe(expected);
});
it('should generate Bitbucket pull request URL', () => {
const repoUrl = 'https://bitbucket.org/user/repo';
const expected = 'https://bitbucket.org/user/repo/pull-requests/new?source=main&t=1#diff';
expect(getPullRequestUrl(repoUrl)).toBe(expected);
});
it('should generate GitLab merge request URL', () => {
const repoUrl = 'https://gitlab.com/user/repo';
const expected = 'https://gitlab.com/user/repo/-/merge_requests/new?merge_request[source_branch]=main';
expect(getPullRequestUrl(repoUrl)).toBe(expected);
});
it('should return empty string for unsupported platform', () => {
const repoUrl = 'https://example.com/user/repo';
expect(getPullRequestUrl(repoUrl)).toBe('');
});
it('should throw error for invalid URL', () => {
const invalidUrl = 'not-a-url';
expect(() => getPullRequestUrl(invalidUrl)).toThrow('Invalid repository URL');
});
it('should handle URLs with paths', () => {
const repoUrl = 'https://github.com/org/team/repo';
const expected = 'https://github.com/org/team/repo/pull/new/main';
expect(getPullRequestUrl(repoUrl)).toBe(expected);
});
});
describe('getBrowser', () => {
it('should return browser name for valid browser', () => {
process.argv = ['node', 'script.js', 'chrome'];
expect(getBrowser()).toBe('google chrome');
});
it('should return firefox developer edition for firefox-dev', () => {
process.argv = ['node', 'script.js', 'firefox-dev'];
expect(getBrowser()).toBe('firefox developer edition');
});
it('should return brave browser for brave', () => {
process.argv = ['node', 'script.js', 'brave'];
expect(getBrowser()).toBe('brave browser');
});
it('should return null for no browser argument', () => {
process.argv = ['node', 'script.js'];
expect(getBrowser()).toBe(null);
});
it('should return null for help flag', () => {
process.argv = ['node', 'script.js', '--help'];
expect(getBrowser()).toBe(null);
});
it('should return null for -h flag', () => {
process.argv = ['node', 'script.js', '-h'];
expect(getBrowser()).toBe(null);
});
it('should return null and log warning for invalid browser', () => {
process.argv = ['node', 'script.js', 'invalid-browser'];
const result = getBrowser();
expect(result).toBe(null);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Browser "invalid-browser" is not supported')
);
});
it('should handle all supported browsers', () => {
const browserTests = [
['chrome', 'google chrome'],
['firefox', 'firefox'],
['firefox-dev', 'firefox developer edition'],
['edge', 'microsoft edge'],
['safari', 'safari'],
['opera', 'opera'],
['brave', 'brave browser'],
];
browserTests.forEach(([input, expected]) => {
process.argv = ['node', 'script.js', input];
expect(getBrowser()).toBe(expected);
});
});
});
describe('displayHelp', () => {
it('should display help information', () => {
displayHelp();
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Create Pull Request CLI')
);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Usage:')
);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('chrome, firefox, firefox-dev, edge, safari, opera, brave')
);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('github.com, bitbucket.org, gitlab.com')
);
});
});
describe('Integration tests', () => {
it('should handle complete workflow for GitHub repo', () => {
const sshUrl = 'git@github.com:user/repo.git';
const normalizedUrl = normalizeGitUrl(sshUrl);
const pullRequestUrl = getPullRequestUrl(normalizedUrl);
expect(normalizedUrl).toBe('https://github.com/user/repo');
expect(pullRequestUrl).toBe('https://github.com/user/repo/pull/new/main');
});
it('should handle complete workflow for GitLab repo', () => {
const httpsUrl = 'https://gitlab.com/group/project.git';
const normalizedUrl = normalizeGitUrl(httpsUrl);
const pullRequestUrl = getPullRequestUrl(normalizedUrl);
expect(normalizedUrl).toBe('https://gitlab.com/group/project');
expect(pullRequestUrl).toBe('https://gitlab.com/group/project/-/merge_requests/new?merge_request[source_branch]=main');
});
it('should handle browser selection with help flag', () => {
process.argv = ['node', 'script.js', '--help'];
expect(isHelpRequested()).toBe(true);
expect(getBrowser()).toBe(null);
});
it('should handle browser selection without help flag', () => {
process.argv = ['node', 'script.js', 'firefox'];
expect(isHelpRequested()).toBe(false);
expect(getBrowser()).toBe('firefox');
});
});
});