-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathutils.test.ts
More file actions
327 lines (266 loc) · 10.5 KB
/
utils.test.ts
File metadata and controls
327 lines (266 loc) · 10.5 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
import { describe, expect, it, vi } from 'vitest'
import {
addVercelMetadata,
buildCommentBody,
buildCommentPrefix,
buildHtmlTableComment,
escapeHtml,
getGithubCommentInput,
isPullRequestType,
joinDeploymentUrls,
parseArgs,
retry,
slugify,
} from '../utils'
// Mock @actions/core
vi.mock('@actions/core', () => ({
debug: vi.fn(),
info: vi.fn(),
}))
describe('isPullRequestType', () => {
it('returns true for pull_request', () => {
expect(isPullRequestType('pull_request')).toBe(true)
})
it('returns true for pull_request_target', () => {
expect(isPullRequestType('pull_request_target')).toBe(true)
})
it('returns false for push', () => {
expect(isPullRequestType('push')).toBe(false)
})
it('returns false for release', () => {
expect(isPullRequestType('release')).toBe(false)
})
})
describe('slugify', () => {
it('converts spaces to hyphens', () => {
expect(slugify('hello world')).toBe('hello-world')
})
it('converts underscores to hyphens', () => {
expect(slugify('hello_world')).toBe('hello-world')
})
it('removes special characters', () => {
expect(slugify('hello@world!')).toBe('helloworld')
})
it('converts to lowercase', () => {
expect(slugify('Hello World')).toBe('hello-world')
})
it('removes leading and trailing hyphens', () => {
expect(slugify('-hello-world-')).toBe('hello-world')
})
it('collapses multiple hyphens', () => {
expect(slugify('hello---world')).toBe('hello-world')
})
it('handles empty strings', () => {
expect(slugify('')).toBe('')
})
it('handles already slugified strings', () => {
expect(slugify('hello-world')).toBe('hello-world')
})
it('handles feature branch names', () => {
expect(slugify('feature/add-login')).toBe('featureadd-login')
})
it('trims whitespace', () => {
expect(slugify(' hello world ')).toBe('hello-world')
})
})
describe('parseArgs', () => {
it('splits simple arguments', () => {
expect(parseArgs('--env foo')).toEqual(['--env', 'foo'])
})
it('preserves single-quoted strings', () => {
expect(parseArgs('\'hello world\'')).toEqual(['hello world'])
})
it('preserves double-quoted strings', () => {
expect(parseArgs('"hello world"')).toEqual(['hello world'])
})
it('handles nested quotes in single quotes', () => {
expect(parseArgs(`'foo="bar baz"'`)).toEqual(['foo="bar baz"'])
})
it('handles nested quotes in double quotes', () => {
expect(parseArgs(`"foo='bar baz'"`)).toEqual([`foo='bar baz'`])
})
it('handles mixed arguments', () => {
expect(parseArgs(`--env foo=bar "foo=bar baz" 'foo="bar baz"'`))
.toEqual(['--env', 'foo=bar', 'foo=bar baz', 'foo="bar baz"'])
})
it('handles empty input', () => {
expect(parseArgs('')).toEqual([])
})
it('handles multiple spaces between arguments', () => {
expect(parseArgs('arg1 arg2')).toEqual(['arg1', 'arg2'])
})
})
describe('retry', () => {
it('returns result on first success', async () => {
const fn = vi.fn().mockResolvedValue('success')
const result = await retry(fn, 3)
expect(result).toBe('success')
expect(fn).toHaveBeenCalledTimes(1)
})
it('retries on failure', async () => {
const fn = vi.fn()
.mockRejectedValueOnce(new Error('fail'))
.mockResolvedValue('success')
const result = await retry(fn, 3)
expect(result).toBe('success')
expect(fn).toHaveBeenCalledTimes(2)
}, 10000)
it('throws after max retries', async () => {
const error = new Error('persistent failure')
const fn = vi.fn().mockRejectedValue(error)
await expect(retry(fn, 2)).rejects.toThrow('persistent failure')
expect(fn).toHaveBeenCalledTimes(3) // initial + 2 retries
}, 15000)
})
describe('addVercelMetadata', () => {
it('returns metadata args when key not provided', () => {
expect(addVercelMetadata('githubCommitSha', 'abc123', ['--prod']))
.toEqual(['-m', 'githubCommitSha=abc123'])
})
it('returns empty array when key already exists', () => {
expect(addVercelMetadata('githubCommitSha', 'abc123', ['githubCommitSha=existing']))
.toEqual([])
})
it('handles numeric values', () => {
expect(addVercelMetadata('githubDeployment', 1, []))
.toEqual(['-m', 'githubDeployment=1'])
})
it('is case-sensitive for key matching', () => {
expect(addVercelMetadata('githubCommitSha', 'abc123', ['GITHUBCOMMITSHA=existing']))
.toEqual(['-m', 'githubCommitSha=abc123'])
})
})
describe('joinDeploymentUrls', () => {
it('returns deployment URL when no aliases', () => {
expect(joinDeploymentUrls('https://example.vercel.app', []))
.toBe('https://example.vercel.app')
})
it('joins deployment URL with aliases', () => {
const result = joinDeploymentUrls('https://example.vercel.app', ['custom.com', 'alias.com'])
expect(result).toBe('https://example.vercel.app\nhttps://custom.com\nhttps://alias.com')
})
it('adds https protocol to alias domains', () => {
const result = joinDeploymentUrls('https://example.vercel.app', ['custom.com'])
expect(result).toContain('https://custom.com')
})
})
describe('buildCommentPrefix', () => {
it('builds correct prefix with deployment name', () => {
expect(buildCommentPrefix('my-app'))
.toBe('Deploy preview for _my-app_ ready!')
})
})
describe('escapeHtml', () => {
it('escapes angle brackets', () => {
expect(escapeHtml('<script>')).toBe('<script>')
})
it('escapes quotes', () => {
expect(escapeHtml('\'"')).toBe(''"')
})
it('escapes ampersands', () => {
expect(escapeHtml('a&b')).toBe('a&b')
})
it('leaves safe strings unchanged', () => {
expect(escapeHtml('https://example.vercel.app')).toBe('https://example.vercel.app')
})
})
describe('buildHtmlTableComment', () => {
it('renders HTML table with all fields', () => {
const result = buildHtmlTableComment('abc123def', 'https://example.vercel.app', 'my-app', [])
expect(result).toContain('<table>')
expect(result).toContain('</table>')
expect(result).toContain('<code>my-app</code>')
expect(result).toContain('Deploy successful!')
expect(result).toContain('href=\'https://example.vercel.app\'')
expect(result).toContain('<code>abc123d</code>')
})
it('omits alias rows when no aliases configured', () => {
const result = buildHtmlTableComment('abc123', 'https://example.vercel.app', 'my-app', [])
expect(result).not.toContain('Alias')
})
it('escapes HTML special characters in all fields', () => {
const result = buildHtmlTableComment('abc<123', 'https://example.com?x=\'><img>', 'app<xss>', [])
expect(result).not.toContain('<xss>')
expect(result).not.toContain('<img>')
expect(result).toContain('<xss>')
expect(result).toContain(''><img>')
})
it('includes alias rows when configured', () => {
const result = buildHtmlTableComment('abc123', 'https://example.vercel.app', 'my-app', ['custom.com', 'alias.com'])
expect(result).toContain('https://custom.com')
expect(result).toContain('https://alias.com')
expect(result).toContain('Alias')
})
it('omits inspect row when no inspect URL', () => {
const result = buildHtmlTableComment('abc123', 'https://example.vercel.app', 'my-app', [])
expect(result).not.toContain('Inspect')
})
it('includes inspect row when URL provided', () => {
const result = buildHtmlTableComment('abc123', 'https://example.vercel.app', 'my-app', [], 'https://vercel.com/team/project/dpl_123')
expect(result).toContain('Inspect')
expect(result).toContain('href=\'https://vercel.com/team/project/dpl_123\'')
expect(result).toContain('View deployment')
})
})
describe('buildCommentBody', () => {
const defaultTemplate = `✅ Preview
{{deploymentUrl}}
Built with commit {{deploymentCommit}}.`
it('returns undefined when githubComment is false', () => {
expect(buildCommentBody('abc123', 'https://example.com', 'app', false, [], defaultTemplate))
.toBeUndefined()
})
it('uses custom template when string provided', () => {
const customTemplate = 'Custom: {{deploymentUrl}}'
const result = buildCommentBody('abc123', 'https://example.com', 'app', customTemplate, [], defaultTemplate)
expect(result).toContain('Custom: https://example.com')
})
it('uses HTML table when githubComment is true', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'app', true, [], defaultTemplate)
expect(result).toContain('<table>')
expect(result).toContain('https://example.com')
expect(result).toContain('Deploy successful!')
})
it('includes all data in HTML table', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'my-app', true, [], defaultTemplate)
expect(result).toContain('abc123')
expect(result).toContain('https://example.com')
expect(result).toContain('my-app')
})
it('includes alias domains in HTML table', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'app', true, ['custom.com'], defaultTemplate)
expect(result).toContain('https://custom.com')
})
it('includes prefix with deployment name', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'my-app', true, [], defaultTemplate)
expect(result).toContain('Deploy preview for _my-app_ ready!')
})
it('includes footer branding link', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'my-app', true, [], defaultTemplate)
expect(result).toContain('Deployed with [vercel-action]')
expect(result).toContain('github.com/marketplace/actions/vercel-action')
})
it('passes inspect URL to HTML table', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'my-app', true, [], defaultTemplate, 'https://vercel.com/inspect')
expect(result).toContain('Inspect')
expect(result).toContain('https://vercel.com/inspect')
})
it('custom template still uses variable substitution', () => {
const result = buildCommentBody('abc123', 'https://example.com', 'my-app', '{{deploymentName}}: {{deploymentUrl}}', [], defaultTemplate)
expect(result).toContain('my-app: https://example.com')
})
})
describe('getGithubCommentInput', () => {
it('returns true for "true" string', () => {
expect(getGithubCommentInput('true')).toBe(true)
})
it('returns false for "false" string', () => {
expect(getGithubCommentInput('false')).toBe(false)
})
it('returns custom string for other values', () => {
expect(getGithubCommentInput('custom template')).toBe('custom template')
})
it('returns empty string as-is', () => {
expect(getGithubCommentInput('')).toBe('')
})
})