diff --git a/src/commands/analytics/cmd-analytics.test.ts b/src/commands/analytics/cmd-analytics.test.ts index b4fc6ed3..234dbdfd 100644 --- a/src/commands/analytics/cmd-analytics.test.ts +++ b/src/commands/analytics/cmd-analytics.test.ts @@ -75,8 +75,6 @@ describe('socket analytics', async () => { \\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\\x1b[22m - - Scope must be "repo" or "org" (\\x1b[32mok\\x1b[39m) - - 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)" @@ -89,7 +87,6 @@ describe('socket analytics', async () => { cmdit( [ 'analytics', - 'boo', '--scope', 'org', '--repo', @@ -113,4 +110,302 @@ describe('socket analytics', async () => { expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) } ) + + cmdit( + ['analytics', '--help', '--config', '{"isTestingV1": true}'], + 'should support --help in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot( + ` + "Look up analytics data + + Usage + $ socket analytics [ org | repo ] [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 7. + + Options + --file Filepath to save output. Only valid with --json/--markdown. Defaults to stdout. + --help Print this help + --json Output result as json + --markdown Output result as markdown + --repo Name of the repository. Only valid when scope=repo + --scope Scope of the analytics data - either 'org' or 'repo', default: org + --time Time filter - either 7, 30 or 90, default: 7 + + Examples + $ socket analytics org 7 + $ socket analytics repo test-repo 30 + $ socket analytics 90" + ` + ) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'help should exit with code 2').toBe(2) + expect(stderr, 'banner includes base command').toContain( + '`socket analytics`' + ) + } + ) + + cmdit( + [ + 'analytics', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should run to dryrun without args in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + 'org', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept org arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + 'repo', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should ask for repo name with repo arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`""`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m + \\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\\x1b[22m + + - Scope must be "repo" or "org" (\\x1b[32mok\\x1b[39m) + + - 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)" + `) + + expect(code, 'dry-run should exit with code 2 if missing input').toBe(2) + } + ) + + cmdit( + [ + 'analytics', + 'repo', + 'daname', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept repo with arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + '7', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept time 7 arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + '30', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept time 30 arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + '90', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept time 90 arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + 'org', + '7', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept org and time arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) + + cmdit( + [ + 'analytics', + 'repo', + 'slowpo', + '30', + '--dry-run', + '--config', + '{"isTestingV1": true, "apiToken":"anything"}' + ], + 'should accept repo and time arg in v1', + async cmd => { + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) + expect(`\n ${stderr}`).toMatchInlineSnapshot(` + " + _____ _ _ /--------------- + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver (is testing v1) + |__ | * | _| '_| -_| _| | Node: , API token set: + |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: + \\x1b[32m (Thank you for testing the v1 bump! Please send us any feedback you might have!) + \\x1b[39m" + `) + + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) + } + ) }) diff --git a/src/commands/analytics/cmd-analytics.ts b/src/commands/analytics/cmd-analytics.ts index 8de228d7..9e5e5d2b 100644 --- a/src/commands/analytics/cmd-analytics.ts +++ b/src/commands/analytics/cmd-analytics.ts @@ -1,10 +1,9 @@ -import assert from 'node:assert' - import { logger } from '@socketsecurity/registry/lib/logger' import { displayAnalytics } from './display-analytics' import constants from '../../constants' import { commonFlags, outputFlags } from '../../flags' +import { isTestingV1 } from '../../utils/config' import { handleBadInput } from '../../utils/handle-bad-input' import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' @@ -48,25 +47,34 @@ const config: CliCommandConfig = { description: 'Time filter - either 7, 30 or 90, default: 7' } }, - help: (command, { flags }) => ` + help: (command, { flags }) => + ` Usage - $ ${command} --scope= --time=