-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathvercel.test.ts
More file actions
464 lines (382 loc) · 15 KB
/
vercel.test.ts
File metadata and controls
464 lines (382 loc) · 15 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import type { ActionConfig, DeploymentContext } from '../types'
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { aliasDomainsToDeployment, vercelDeploy, vercelInspect } from '../vercel'
import { VercelCliClient } from '../vercel-cli'
vi.mock('@actions/core', () => ({
info: vi.fn(),
debug: vi.fn(),
warning: vi.fn(),
}))
vi.mock('@actions/exec', () => ({
exec: vi.fn(),
}))
vi.mock('@actions/github', () => ({
context: {
actor: 'test-user',
repo: { owner: 'test-owner', repo: 'test-repo' },
},
}))
function createConfig(overrides: Partial<ActionConfig> = {}): ActionConfig {
return {
githubToken: '',
githubComment: false,
workingDirectory: '',
vercelToken: 'test-token',
vercelArgs: '',
vercelOrgId: '',
vercelProjectId: '',
vercelScope: '',
vercelProjectName: '',
vercelBin: 'vercel@latest',
aliasDomains: [],
...overrides,
}
}
function createClient(config?: ActionConfig): VercelCliClient {
return new VercelCliClient(config ?? createConfig())
}
function createDeployContext(overrides: Partial<DeploymentContext> = {}): DeploymentContext {
return {
ref: 'refs/heads/main',
sha: 'abc123',
commit: 'test commit',
commitOrg: 'test-owner',
commitRepo: 'test-repo',
...overrides,
}
}
describe('vercelDeploy', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('extracts deployment URL from last line of stdout', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
const stdout = options?.listeners?.stdout
if (stdout) {
stdout(Buffer.from('Vercel CLI 30.0.0\n'))
stdout(Buffer.from('Deploying...\n'))
stdout(Buffer.from('https://my-app-abc123.vercel.app\n'))
}
return 0
})
const config = createConfig()
const url = await vercelDeploy(
createClient(config),
config,
createDeployContext(),
)
expect(url).toBe('https://my-app-abc123.vercel.app')
})
it('accumulates stdout from multiple chunks', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
const stdout = options?.listeners?.stdout
if (stdout) {
stdout(Buffer.from('https://my-'))
stdout(Buffer.from('app.vercel.app'))
}
return 0
})
const config = createConfig()
const url = await vercelDeploy(
createClient(config),
config,
createDeployContext(),
)
expect(url).toBe('https://my-app.vercel.app')
})
it('throws when stdout contains no URL', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
const stdout = options?.listeners?.stdout
if (stdout) {
stdout(Buffer.from('Some error output\n'))
}
return 0
})
await expect(
vercelDeploy(createClient(), createConfig(), createDeployContext()),
).rejects.toThrow('Failed to extract deployment URL')
})
it('throws when stdout is empty', async () => {
vi.mocked(exec.exec).mockResolvedValue(0)
await expect(
vercelDeploy(createClient(), createConfig(), createDeployContext()),
).rejects.toThrow('Failed to extract deployment URL')
})
it('passes vercel token and metadata args to exec', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stdout?.(Buffer.from('https://deploy.vercel.app\n'))
return 0
})
const cfg = createConfig({ vercelToken: 'my-secret-token', vercelBin: 'vercel@30' })
await vercelDeploy(
createClient(cfg),
cfg,
createDeployContext({ sha: 'sha123', commitOrg: 'org', commitRepo: 'repo' }),
)
const call = vi.mocked(exec.exec).mock.calls[0]
expect(call[0]).toBe('npx')
const args = call[1] as string[]
expect(args[0]).toBe('vercel@30')
expect(args).toContain('-t')
expect(args).toContain('my-secret-token')
expect(args).toContain('-m')
})
it('sets cwd when workingDirectory is provided', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stdout?.(Buffer.from('https://deploy.vercel.app\n'))
return 0
})
const cfg = createConfig({ workingDirectory: '/custom/dir' })
await vercelDeploy(createClient(cfg), cfg, createDeployContext())
const options = vi.mocked(exec.exec).mock.calls[0][2]
expect(options?.cwd).toBe('/custom/dir')
})
it('includes scope when provided', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stdout?.(Buffer.from('https://deploy.vercel.app\n'))
return 0
})
const cfg = createConfig({ vercelScope: 'my-team' })
await vercelDeploy(createClient(cfg), cfg, createDeployContext())
const args = vi.mocked(exec.exec).mock.calls[0][1] as string[]
expect(args).toContain('--scope')
expect(args).toContain('my-team')
})
it('routes stderr to core.info', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from('warning message'))
options?.listeners?.stdout?.(Buffer.from('https://deploy.vercel.app\n'))
return 0
})
await vercelDeploy(createClient(), createConfig(), createDeployContext())
expect(core.info).toHaveBeenCalledWith('warning message')
})
it('retries without org ID on personal account scope error', async () => {
let callCount = 0
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
callCount++
if (callCount === 1) {
options?.listeners?.stderr?.(
Buffer.from('You cannot set your Personal Account as the scope'),
)
return 1
}
options?.listeners?.stdout?.(Buffer.from('https://retry-deploy.vercel.app\n'))
return 0
})
const cfg = createConfig({ vercelProjectId: 'proj-123' })
const url = await vercelDeploy(createClient(cfg), cfg, createDeployContext())
expect(url).toBe('https://retry-deploy.vercel.app')
expect(exec.exec).toHaveBeenCalledTimes(2)
expect(core.warning).toHaveBeenCalledWith(
expect.stringContaining('Retrying without VERCEL_ORG_ID'),
)
})
it('throws on personal account scope error without project ID', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(
Buffer.from('You cannot set your Personal Account as the scope'),
)
return 1
})
await expect(
vercelDeploy(createClient(), createConfig({ vercelProjectId: '' }), createDeployContext()),
).rejects.toThrow('no vercel-project-id was provided')
})
it('throws on non-zero exit code for non-scope errors', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from('some other error'))
return 1
})
await expect(
vercelDeploy(createClient(), createConfig(), createDeployContext()),
).rejects.toThrow('failed with exit code 1')
})
it('sanitizes commit message in metadata', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stdout?.(Buffer.from('https://deploy.vercel.app\n'))
return 0
})
await vercelDeploy(createClient(), createConfig(), createDeployContext({ commit: 'line1\nline2\r\n"quoted"' }))
const args = vi.mocked(exec.exec).mock.calls[0][1] as string[]
const metaArgs = args.filter(a => a.startsWith('"'))
for (const arg of metaArgs) {
expect(arg).not.toMatch(/[\r\n]/)
}
})
})
describe('vercelInspect', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('extracts project name from stderr output', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from(' name my-project\n'))
return 0
})
const result = await vercelInspect(createClient(), 'https://deploy.vercel.app')
expect(result.name).toBe('my-project')
})
it('returns null name when name not found in output', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from('some other output\n'))
return 0
})
const result = await vercelInspect(createClient(), 'https://deploy.vercel.app')
expect(result.name).toBeNull()
})
it('returns null values and warns when exec fails', async () => {
vi.mocked(exec.exec).mockRejectedValue(new Error('command failed'))
const result = await vercelInspect(createClient(), 'https://deploy.vercel.app')
expect(result).toEqual({ name: null, inspectUrl: null })
expect(core.warning).toHaveBeenCalledWith(
'vercel inspect failed: command failed',
)
})
it('does not throw when exec fails', async () => {
vi.mocked(exec.exec).mockRejectedValue(new Error('network error'))
const result = await vercelInspect(createClient(), 'https://deploy.vercel.app')
expect(result).toEqual({ name: null, inspectUrl: null })
})
it('extracts inspectUrl from stderr when available', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from(' name my-project\n inspectorUrl https://vercel.com/team/project/dpl_123\n'))
return 0
})
const result = await vercelInspect(createClient(), 'https://deploy.vercel.app')
expect(result.name).toBe('my-project')
expect(result.inspectUrl).toBe('https://vercel.com/team/project/dpl_123')
})
it('passes correct args including token and inspect command', async () => {
vi.mocked(exec.exec).mockResolvedValue(0)
await vercelInspect(
createClient(createConfig({ vercelBin: 'vercel@30', vercelToken: 'tok' })),
'https://deploy.vercel.app',
)
const call = vi.mocked(exec.exec).mock.calls[0]
expect(call[0]).toBe('npx')
const args = call[1] as string[]
expect(args).toContain('vercel@30')
expect(args).toContain('inspect')
expect(args).toContain('https://deploy.vercel.app')
expect(args).toContain('-t')
expect(args).toContain('tok')
})
it('includes scope in args when provided', async () => {
vi.mocked(exec.exec).mockResolvedValue(0)
await vercelInspect(
createClient(createConfig({ vercelScope: 'my-team' })),
'https://deploy.vercel.app',
)
const args = vi.mocked(exec.exec).mock.calls[0][1] as string[]
expect(args).toContain('--scope')
expect(args).toContain('my-team')
})
it('extracts name with varying whitespace', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from(' name my-project-name\n'))
return 0
})
const result = await vercelInspect(createClient(), 'https://deploy.vercel.app')
expect(result.name).toBe('my-project-name')
})
})
describe('aliasDomainsToDeployment', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('throws when deploymentUrl is empty', async () => {
await expect(
aliasDomainsToDeployment(createClient(), createConfig({ aliasDomains: ['example.com'] }), ''),
).rejects.toThrow('Deployment URL is required for aliasing domains')
})
it('calls exec for each alias domain', async () => {
vi.mocked(exec.exec).mockResolvedValue(0)
const cfg = createConfig({ aliasDomains: ['a.com', 'b.com'] })
await aliasDomainsToDeployment(createClient(cfg), cfg, 'https://deploy.vercel.app')
expect(exec.exec).toHaveBeenCalledTimes(2)
const firstCall = vi.mocked(exec.exec).mock.calls[0][1] as string[]
expect(firstCall).toContain('alias')
expect(firstCall).toContain('https://deploy.vercel.app')
expect(firstCall).toContain('a.com')
const secondCall = vi.mocked(exec.exec).mock.calls[1][1] as string[]
expect(secondCall).toContain('b.com')
})
it('includes scope when provided', async () => {
vi.mocked(exec.exec).mockResolvedValue(0)
const cfg = createConfig({ aliasDomains: ['a.com'], vercelScope: 'my-team' })
await aliasDomainsToDeployment(createClient(cfg), cfg, 'https://deploy.vercel.app')
const args = vi.mocked(exec.exec).mock.calls[0][1] as string[]
expect(args).toContain('--scope')
expect(args).toContain('my-team')
})
it('retries on general failure', async () => {
let callCount = 0
vi.mocked(exec.exec).mockImplementation(async () => {
callCount++
if (callCount === 1) {
throw new Error('network error')
}
return 0
})
const cfg = createConfig({ aliasDomains: ['a.com'] })
await aliasDomainsToDeployment(createClient(cfg), cfg, 'https://deploy.vercel.app')
expect(exec.exec).toHaveBeenCalledTimes(2)
}, 15000)
it('retries without scope on personal account scope error', async () => {
let callCount = 0
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
callCount++
if (callCount === 1) {
options?.listeners?.stderr?.(
Buffer.from('You cannot set your Personal Account as the scope'),
)
return 1
}
return 0
})
const cfg = createConfig({ aliasDomains: ['a.com'], vercelScope: 'my-team' })
await aliasDomainsToDeployment(createClient(cfg), cfg, 'https://deploy.vercel.app')
expect(exec.exec).toHaveBeenCalledTimes(2)
expect(core.warning).toHaveBeenCalledWith(
expect.stringContaining('Retrying without --scope'),
)
// Second call should NOT contain --scope
const retryArgs = vi.mocked(exec.exec).mock.calls[1][1] as string[]
expect(retryArgs).not.toContain('--scope')
})
it('throws when alias retry also fails', async () => {
let callCount = 0
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
callCount++
if (callCount === 1) {
options?.listeners?.stderr?.(
Buffer.from('You cannot set your Personal Account as the scope'),
)
return 1
}
options?.listeners?.stderr?.(Buffer.from('another error'))
return 1
})
await expect(
aliasDomainsToDeployment(createClient(), createConfig({ aliasDomains: ['a.com'] }), 'https://deploy.vercel.app'),
).rejects.toThrow('Alias command failed for domain a.com')
})
it('throws on non-scope alias failure', async () => {
vi.mocked(exec.exec).mockImplementation(async (_cmd, _args, options) => {
options?.listeners?.stderr?.(Buffer.from('permission denied'))
return 1
})
await expect(
aliasDomainsToDeployment(createClient(), createConfig({ aliasDomains: ['a.com'] }), 'https://deploy.vercel.app'),
).rejects.toThrow('Alias command failed for domain a.com')
})
it('logs success message after all aliases configured', async () => {
vi.mocked(exec.exec).mockResolvedValue(0)
const cfg = createConfig({ aliasDomains: ['a.com'] })
await aliasDomainsToDeployment(createClient(cfg), cfg, 'https://deploy.vercel.app')
expect(core.info).toHaveBeenCalledWith('All alias domains configured successfully')
})
})