-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcmd-analytics.test.mts
More file actions
347 lines (300 loc) · 12.9 KB
/
cmd-analytics.test.mts
File metadata and controls
347 lines (300 loc) · 12.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
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
import semver from 'semver'
import { describe, expect } from 'vitest'
import constants from '../../../src/constants.mts'
import { cmdit, invokeNpm } from '../../../test/utils.mts'
describe('socket analytics', async () => {
// Lazily access constants.binCliPath.
const { binCliPath } = constants
cmdit(
['analytics', '--help', '--config', '{}'],
'should support --help',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(
`
"Look up analytics data
Usage
$ socket analytics [options] [ "org" | "repo" <reponame>] [TIME]
API Token Requirements
- Quota: 1 unit
- Permissions: report:write
The scope is either org or repo level, defaults to org.
When scope is repo, a repo slug must be given as well.
The TIME argument must be number 7, 30, or 90 and defaults to 30.
Options
--file Path to store result, only valid with --json/--markdown
--json Output result as json
--markdown Output result as markdown
Examples
$ socket analytics org 7
$ socket analytics repo test-repo 30
$ socket analytics 90"
`,
)
// Node 24 on Windows currently fails this test with added stderr:
// Assertion failed: !(handle->flags & UV_HANDLE_CLOSING), file src\win\async.c, line 76
const skipOnWin32Node24 =
constants.WIN32 && semver.parse(constants.NODE_VERSION)!.major >= 24
if (!skipOnWin32Node24) {
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'explicit help should exit with code 0').toBe(0)
}
expect(stderr, 'banner includes base command').toContain(
'`socket analytics`',
)
},
)
cmdit(
['analytics', '--dry-run', '--config', '{}'],
'should report missing token with just dry-run',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`""`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again
- The time filter must either be 7, 30 or 90 (\\x1b[32mok\\x1b[39m)
- You need to be logged in to use this command. See \`socket login\`. (\\x1b[31mmissing API token\\x1b[39m)
\\x1b[22m"
`)
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)
},
)
cmdit(
[
'analytics',
'--scope',
'org',
'--repo',
'bar',
'--dry-run',
'--config',
'{"apiToken":"anything"}',
],
'should reject legacy flags',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`""`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again
- Legacy flags are no longer supported. See v1 migration guide. (\\x1b[31mreceived legacy flags\\x1b[39m)
- The time filter must either be 7, 30 or 90 (\\x1b[32mok\\x1b[39m)
\\x1b[22m"
`)
expect(code, 'dry-run should reject legacy flags with code 2').toBe(2)
},
)
cmdit(
['analytics', '--dry-run', '--config', '{"apiToken":"anything"}'],
'should run to dryrun without args',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
['analytics', 'org', '--dry-run', '--config', '{"apiToken":"anything"}'],
'should accept org arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
['analytics', 'repo', '--dry-run', '--config', '{"apiToken":"anything"}'],
'should ask for repo name with repo arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`""`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again
- When scope=repo, repo name should be the second argument (\\x1b[31mmissing\\x1b[39m)
- The time filter must either be 7, 30 or 90 (\\x1b[32mok\\x1b[39m)
\\x1b[22m"
`)
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)
},
)
cmdit(
[
'analytics',
'repo',
'daname',
'--dry-run',
'--config',
'{"apiToken":"anything"}',
],
'should accept repo with arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
['analytics', '7', '--dry-run', '--config', '{"apiToken":"anything"}'],
'should accept time 7 arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
['analytics', '30', '--dry-run', '--config', '{"apiToken":"anything"}'],
'should accept time 30 arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
['analytics', '90', '--dry-run', '--config', '{"apiToken":"anything"}'],
'should accept time 90 arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
[
'analytics',
'org',
'--time',
'7',
'--dry-run',
'--config',
'{"apiToken":"anything"}',
],
'should report legacy flag',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`""`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again
- Legacy flags are no longer supported. See v1 migration guide. (\\x1b[31mreceived legacy flags\\x1b[39m)
- The time filter must either be 7, 30 or 90 (\\x1b[32mok\\x1b[39m)
\\x1b[22m"
`)
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)
},
)
cmdit(
[
'analytics',
'org',
'7',
'--dry-run',
'--config',
'{"apiToken":"anything"}',
],
'should accept org and time arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
cmdit(
[
'analytics',
'repo',
'slowpo',
'30',
'--dry-run',
'--config',
'{"apiToken":"anything"}',
],
'should accept repo and time arg',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
`)
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)
})