|
| 1 | +import { mkdtempSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { beforeAll, describe, expect, test } from "vitest"; |
| 5 | + |
| 6 | +import { getRequiredEnv, run, type CommandError } from "./utils"; |
| 7 | + |
| 8 | +const userAccessToken = getRequiredEnv("USER_ACCESS_TOKEN"); |
| 9 | +const projectToken = getRequiredEnv("ARGOS_TOKEN"); |
| 10 | +const buildNumber = process.env.ARGOS_BUILD_NUMBER || "27748"; |
| 11 | + |
| 12 | +const baseEnv: NodeJS.ProcessEnv = { |
| 13 | + ...process.env, |
| 14 | + HOME: mkdtempSync(join(tmpdir(), "argos-cli-e2e-")), |
| 15 | + ARGOS_API_BASE_URL: process.env.ARGOS_API_BASE_URL, |
| 16 | + ARGOS_TOKEN: "", |
| 17 | +}; |
| 18 | + |
| 19 | +function expectRunToFail( |
| 20 | + args: string[], |
| 21 | + overrideEnv?: NodeJS.ProcessEnv, |
| 22 | +): CommandError { |
| 23 | + try { |
| 24 | + run(args, { ...baseEnv, ...overrideEnv }); |
| 25 | + } catch (error) { |
| 26 | + return error as CommandError; |
| 27 | + } |
| 28 | + throw new Error( |
| 29 | + `Expected command to fail: node bin/argos-cli.js ${args.join(" ")}`, |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +let account: string; |
| 34 | + |
| 35 | +beforeAll(() => { |
| 36 | + // Derive the account slug from the seeded build's URL, so the analytics |
| 37 | + // account always matches the account the tokens belong to. |
| 38 | + const build = JSON.parse( |
| 39 | + run(["build", "get", buildNumber, "--json"], { |
| 40 | + ...baseEnv, |
| 41 | + ARGOS_TOKEN: projectToken, |
| 42 | + }).stdout, |
| 43 | + ); |
| 44 | + const match = build.url.match( |
| 45 | + /app\.argos-ci\.(?:com|dev(?::\d+)?)\/([^/?#]+)\//, |
| 46 | + ); |
| 47 | + if (!match) { |
| 48 | + throw new Error(`Could not parse account from build URL: ${build.url}`); |
| 49 | + } |
| 50 | + account = match[1]; |
| 51 | +}); |
| 52 | + |
| 53 | +describe("argos analytics", () => { |
| 54 | + test("fails when no token is provided", () => { |
| 55 | + const error = expectRunToFail(["analytics", "--account", account]); |
| 56 | + expect(error.status).not.toBe(0); |
| 57 | + expect(error.stderr).toContain("No Argos token found"); |
| 58 | + }); |
| 59 | + |
| 60 | + test("fails when no account is provided", () => { |
| 61 | + const error = expectRunToFail(["analytics", "--token", userAccessToken]); |
| 62 | + expect(error.status).not.toBe(0); |
| 63 | + expect(error.stderr).toContain("An account is required"); |
| 64 | + }); |
| 65 | + |
| 66 | + test("rejects an invalid --group-by value", () => { |
| 67 | + const error = expectRunToFail([ |
| 68 | + "analytics", |
| 69 | + "--account", |
| 70 | + account, |
| 71 | + "--token", |
| 72 | + userAccessToken, |
| 73 | + "--group-by", |
| 74 | + "year", |
| 75 | + ]); |
| 76 | + expect(error.status).not.toBe(0); |
| 77 | + expect(error.stderr).toContain("Allowed choices are day, week, month"); |
| 78 | + }); |
| 79 | + |
| 80 | + test("rejects an invalid --from date", () => { |
| 81 | + const error = expectRunToFail([ |
| 82 | + "analytics", |
| 83 | + "--account", |
| 84 | + account, |
| 85 | + "--token", |
| 86 | + userAccessToken, |
| 87 | + "--from", |
| 88 | + "not-a-date", |
| 89 | + ]); |
| 90 | + expect(error.status).not.toBe(0); |
| 91 | + expect(error.stderr).toContain('Invalid --from value: "not-a-date"'); |
| 92 | + }); |
| 93 | + |
| 94 | + test("returns account analytics in JSON mode", () => { |
| 95 | + const output = run( |
| 96 | + ["analytics", "--account", account, "--token", userAccessToken, "--json"], |
| 97 | + baseEnv, |
| 98 | + ); |
| 99 | + const analytics = JSON.parse(output.stdout); |
| 100 | + |
| 101 | + expect(typeof analytics.screenshots.all.total).toBe("number"); |
| 102 | + expect(Array.isArray(analytics.screenshots.series)).toBe(true); |
| 103 | + expect(Array.isArray(analytics.screenshots.projects)).toBe(true); |
| 104 | + |
| 105 | + expect(typeof analytics.builds.all.total).toBe("number"); |
| 106 | + expect(typeof analytics.builds.all.changesDetected).toBe("number"); |
| 107 | + expect(Array.isArray(analytics.builds.series)).toBe(true); |
| 108 | + expect(Array.isArray(analytics.builds.projects)).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + test("prints human-readable analytics", () => { |
| 112 | + const output = run( |
| 113 | + ["analytics", "--account", account, "--token", userAccessToken], |
| 114 | + baseEnv, |
| 115 | + ); |
| 116 | + expect(output.stdout).toContain(`Analytics for ${account}`); |
| 117 | + expect(output.stdout).toContain("Builds:"); |
| 118 | + expect(output.stdout).toContain("Screenshots:"); |
| 119 | + }); |
| 120 | + |
| 121 | + test("accepts a bounded date range and --group-by", () => { |
| 122 | + // The API caps the range at 365 days, so use a fixed sub-year window. |
| 123 | + const output = run( |
| 124 | + [ |
| 125 | + "analytics", |
| 126 | + "--account", |
| 127 | + account, |
| 128 | + "--token", |
| 129 | + userAccessToken, |
| 130 | + "--from", |
| 131 | + "2024-01-01", |
| 132 | + "--to", |
| 133 | + "2024-06-01", |
| 134 | + "--group-by", |
| 135 | + "week", |
| 136 | + "--json", |
| 137 | + ], |
| 138 | + baseEnv, |
| 139 | + ); |
| 140 | + const analytics = JSON.parse(output.stdout); |
| 141 | + expect(typeof analytics.builds.all.total).toBe("number"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments