diff --git a/TELEMETRY.md b/TELEMETRY.md index a98f672..e1b5a0b 100644 --- a/TELEMETRY.md +++ b/TELEMETRY.md @@ -66,7 +66,7 @@ A telemetry request has the following structure: |---|---|---| | `client` | Always | Identifies the event as coming from the Chargebee CLI. | | `visitor_id` | Always | Random install ID generated by the CLI. It is not derived from your account, user, or machine. | -| `site_name` | Always | Site name of the active Chargebee profile, or `unconfigured`. | +| `site_name` | Always | Site name of the active Chargebee profile, or `unconfigured`. For `auth add` and `auth switch`, the site that is active once the command succeeds. | | `cli_version` | Always | Installed CLI version. | | `name` | Always | Command path, such as `customer list`. Arguments and values are not included. | | `timestamp` | Always | Time the event was recorded. | diff --git a/src/commands/auth.ts b/src/commands/auth.ts index a8a696b..698ea6a 100644 --- a/src/commands/auth.ts +++ b/src/commands/auth.ts @@ -16,6 +16,7 @@ import { setCommandGroup } from "./help.js"; import { sectionTitle } from "../lib/help-style.js"; import { switchProfileAction } from "../lib/config/switch-profile.js"; import { EXIT_CODES } from "../lib/exit-codes.js"; +import { noteActiveSite } from "../lib/telemetry/active-site.js"; const DEFAULT_PROFILE = "default"; @@ -192,6 +193,7 @@ export function registerAuthCommand(program: Command): void { ...(savedRegion === DEFAULT_REGION ? {} : { region: savedRegion }), }); await writeConfig({ domain: site, activeProfile: profileName }); + noteActiveSite(site); jsonResult({ configured: true, profile: profileName, site, region: savedRegion, host: persistableHost(apiHost), product_catalog_version: catalog.productCatalogVersion ?? null, chargebee_response_schema_type: catalog.responseSchemaType ?? null, key_storage: saved.backend }); diff --git a/src/lib/config/switch-profile.ts b/src/lib/config/switch-profile.ts index a405685..7f94947 100644 --- a/src/lib/config/switch-profile.ts +++ b/src/lib/config/switch-profile.ts @@ -2,6 +2,7 @@ import { humanLog, diagnostic, exitCommand, jsonResult } from "../output.js"; import { assertValidProfileName, listProfiles, loadProfile } from "./profiles.js"; import { readConfig, writeConfig } from "./store.js"; import { siteMode } from "../api/write-gate.js"; +import { noteActiveSite } from "../telemetry/active-site.js"; /** `auth switch`. `domain` is still written as `CHARGEBEE_DOMAIN`. */ export async function switchProfileAction(name?: string): Promise { @@ -66,6 +67,7 @@ export async function switchProfileAction(name?: string): Promise { domain: p.site, activeProfile: chosen, }); + noteActiveSite(p.site); const mode = siteMode(p.site, p.api_key); jsonResult({ active_profile: chosen, site: p.site, mode }); diff --git a/src/lib/telemetry/active-site.ts b/src/lib/telemetry/active-site.ts new file mode 100644 index 0000000..53f4552 --- /dev/null +++ b/src/lib/telemetry/active-site.ts @@ -0,0 +1,23 @@ +/** + * Shared, dependency-free slot for a site the current invocation made active + * (`auth add`, `auth switch`). + * + * Lives in its own module (importing nothing) so commands can set it without + * importing the recorder (`telemetry/index.ts`, which imports `sdk.ts`). The + * recorder reads the active site before the command runs, so without this the + * event would carry the previous site (or `unconfigured`). It reads this slot at + * finalize time and prefers it. + */ +let _site: string | undefined; + +/** Record the site this invocation made active. Call only after it is saved. */ +export function noteActiveSite(site: string): void { + _site = site; +} + +/** Read and clear the noted site. */ +export function takeActiveSite(): string | undefined { + const v = _site; + _site = undefined; + return v; +} diff --git a/src/lib/telemetry/index.ts b/src/lib/telemetry/index.ts index ea31bd4..8659b39 100644 --- a/src/lib/telemetry/index.ts +++ b/src/lib/telemetry/index.ts @@ -18,6 +18,7 @@ import { } from "./constants.js"; import { buildMetadata, normalizeGeneratedResource } from "./metadata.js"; import { recordTelemetryError, takeTelemetryError } from "./error.js"; +import { takeActiveSite } from "./active-site.js"; import { wasInteractive } from "./interactive.js"; import { isTelemetryDisabled, maybeShowFirstRunNotice } from "./optout.js"; import { getVisitorId } from "./identity.js"; @@ -57,6 +58,7 @@ export function __resetTelemetryRecorderForTest(): void { finalized = false; listenLifecycleEmitted = false; _version = "0.0.0"; + takeActiveSite(); if (exitHandler) { process.removeListener("exit", exitHandler); exitHandler = null; @@ -291,7 +293,7 @@ function finalize(exitCode: number): void { const record: SpoolRecord = { env: pending.env, - site_name: pending.siteName, + site_name: takeActiveSite() ?? pending.siteName, visitor_id: pending.visitorId, cli_version: _version, event: { name: pending.command, timestamp: new Date().toISOString(), metadata }, diff --git a/src/lib/test-support/_helpers.ts b/src/lib/test-support/_helpers.ts index b931b8d..087c0d3 100644 --- a/src/lib/test-support/_helpers.ts +++ b/src/lib/test-support/_helpers.ts @@ -1,3 +1,5 @@ +import type { Command } from "commander"; + import { buildProgram } from "../../program.js"; import { __setClientFactory, __resetRuntimeState } from "../api/sdk.js"; @@ -130,9 +132,13 @@ export function createEnvPatcher(): { /** * Run the CLI in-process with the given args. Captures stdout/stderr (both * `console.*` and direct `process.std*.write`) and the exit code, restoring all - * globals afterwards. + * globals afterwards. `configure` runs on the built program before parsing + * (e.g. to install the telemetry recorder, which `buildProgram` leaves out). */ -export async function runCli(args: string[]): Promise { +export async function runCli( + args: string[], + opts: { configure?: (program: Command) => void } = {}, +): Promise { __resetRuntimeState(); let stdout = ""; @@ -173,6 +179,7 @@ export async function runCli(args: string[]): Promise { let exitCode = 0; try { const program = buildProgram("0.0.0-test"); + opts.configure?.(program); await program.parseAsync(args, { from: "user" }); exitCode = Number(process.exitCode ?? 0); } catch (err) { diff --git a/src/tests/integration/telemetry-site-label.test.ts b/src/tests/integration/telemetry-site-label.test.ts new file mode 100644 index 0000000..f9c05f5 --- /dev/null +++ b/src/tests/integration/telemetry-site-label.test.ts @@ -0,0 +1,130 @@ +/** + * The telemetry event for a command that changes the active site (`auth add`, + * `auth switch`) must carry the site that is active after it succeeds, not the + * one that was active before it ran. + */ +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { spoolPath } from "../../lib/telemetry/constants.js"; +import { + __finalizeForTest, + __resetTelemetryRecorderForTest, + __setSpawnForTest, + installTelemetry, +} from "../../lib/telemetry/index.js"; +import { writeState } from "../../lib/telemetry/state.js"; +import type { SpoolRecord } from "../../lib/telemetry/types.js"; +import { + clearCiEnv, + createEnvPatcher, + installFakeClient, + runCli, + setStdinIsTTY, + uninstallFakeClient, + type ClientConstruction, +} from "../../lib/test-support/_helpers.js"; + +const env = createEnvPatcher(); +let configDir: string; +let restoreCiEnv: () => void = () => undefined; +let restoreStdin: (() => void) | undefined; + +beforeEach(() => { + restoreCiEnv = clearCiEnv(); + configDir = mkdtempSync(join(tmpdir(), "cb-telemetry-site-")); + env.set("CHARGEBEE_CONFIG_DIR", configDir); + env.set("CHARGEBEE_SITE", undefined); + env.set("CHARGEBEE_API_KEY", undefined); + env.set("DO_NOT_TRACK", undefined); + env.set("CHARGEBEE_CLI_TELEMETRY", undefined); + // A later run: the first-run notice has been shown, so events are recorded. + writeState({ notice_shown: true }); + const constructions: ClientConstruction[] = []; + installFakeClient({ constructions }); + restoreStdin = setStdinIsTTY(false); + __resetTelemetryRecorderForTest(); + __setSpawnForTest(() => ({ unref() {} })); +}); + +afterEach(() => { + __resetTelemetryRecorderForTest(); + __setSpawnForTest(null); + restoreStdin?.(); + uninstallFakeClient(); + env.restore(); + restoreCiEnv(); + rmSync(configDir, { recursive: true, force: true }); +}); + +/** Run one command with the recorder installed and return its spooled event. */ +async function runRecorded(args: string[]): Promise<{ exitCode: number; record: SpoolRecord }> { + const { exitCode } = await runCli(args, { + configure: (program) => installTelemetry(program, "0.0.0-test"), + }); + __finalizeForTest(exitCode); + __resetTelemetryRecorderForTest(); + const lines = readFileSync(spoolPath(), "utf-8").trim().split("\n"); + return { exitCode, record: JSON.parse(lines.at(-1)!) as SpoolRecord }; +} + +/** Save a profile without recording an event. */ +async function addProfile(profile: string, site: string, apiKey: string): Promise { + const { exitCode } = await runCli(["auth", "add", "--profile", profile, "--site", site, "--api-key", apiKey]); + expect(exitCode).toBe(0); +} + +describe("telemetry site label for commands that change the active site", () => { + it("labels the first auth add with the added site, not unconfigured", async () => { + const { exitCode, record } = await runRecorded([ + "auth", "add", "--profile", "dev", "--site", "acme-test", "--api-key", "test_key", + ]); + expect(exitCode).toBe(0); + expect(record.event.name).toBe("auth add"); + expect(record.site_name).toBe("acme-test"); + }); + + it("labels auth add with the new site when another profile was active", async () => { + await addProfile("dev", "acme-test", "test_key"); + const { record } = await runRecorded([ + "auth", "add", "--profile", "other", "--site", "globex-test", "--api-key", "test_key_2", + ]); + expect(record.site_name).toBe("globex-test"); + }); + + it("labels auth switch with the site switched to", async () => { + await addProfile("dev", "acme-test", "test_key"); + await addProfile("other", "globex-test", "test_key_2"); + const { exitCode, record } = await runRecorded(["auth", "switch", "dev"]); + expect(exitCode).toBe(0); + expect(record.event.name).toBe("auth switch"); + expect(record.site_name).toBe("acme-test"); + }); + + it("keeps the previous site when auth add fails", async () => { + await addProfile("dev", "acme-test", "test_key"); + const { exitCode, record } = await runRecorded([ + "auth", "add", "--profile", "bad", "--site", "not a site!", "--api-key", "test_key_2", + ]); + expect(exitCode).toBe(1); + expect(record.site_name).toBe("acme-test"); + }); + + it("labels the next command from the active profile as before", async () => { + await addProfile("dev", "acme-test", "test_key"); + const { record } = await runRecorded(["customer", "list"]); + expect(record.site_name).toBe("acme-test"); + }); + + it("records nothing when telemetry is disabled", async () => { + env.set("CHARGEBEE_CLI_TELEMETRY", "0"); + const { exitCode } = await runCli(["auth", "add", "--profile", "dev", "--site", "acme-test", "--api-key", "test_key"], { + configure: (program) => installTelemetry(program, "0.0.0-test"), + }); + __finalizeForTest(exitCode); + expect(exitCode).toBe(0); + expect(existsSync(spoolPath())).toBe(false); + }); +}); diff --git a/src/tests/unit/lib/telemetry/active-site.test.ts b/src/tests/unit/lib/telemetry/active-site.test.ts new file mode 100644 index 0000000..8b6c898 --- /dev/null +++ b/src/tests/unit/lib/telemetry/active-site.test.ts @@ -0,0 +1,25 @@ +import { afterEach, describe, expect, it } from "bun:test"; + +import { noteActiveSite, takeActiveSite } from "../../../../lib/telemetry/active-site.js"; + +afterEach(() => { + takeActiveSite(); +}); + +describe("active-site slot", () => { + it("is empty by default", () => { + expect(takeActiveSite()).toBeUndefined(); + }); + + it("returns the noted site once, then clears", () => { + noteActiveSite("acme-test"); + expect(takeActiveSite()).toBe("acme-test"); + expect(takeActiveSite()).toBeUndefined(); + }); + + it("keeps the latest noted site", () => { + noteActiveSite("acme-test"); + noteActiveSite("globex-test"); + expect(takeActiveSite()).toBe("globex-test"); + }); +}); diff --git a/src/tests/unit/lib/telemetry/index.test.ts b/src/tests/unit/lib/telemetry/index.test.ts index d2fe77b..41eb5ef 100644 --- a/src/tests/unit/lib/telemetry/index.test.ts +++ b/src/tests/unit/lib/telemetry/index.test.ts @@ -19,6 +19,7 @@ import { appendRecord } from "../../../../lib/telemetry/spool.js"; import { setTelemetryEnabled } from "../../../../lib/telemetry/optout.js"; import { readState, writeState } from "../../../../lib/telemetry/state.js"; import { recordTelemetryError, takeTelemetryError } from "../../../../lib/telemetry/error.js"; +import { noteActiveSite, takeActiveSite } from "../../../../lib/telemetry/active-site.js"; import type { SpoolRecord } from "../../../../lib/telemetry/types.js"; import { __resetKeychainForTest, __setKeychainForTest, type KeychainStore } from "../../../../lib/config/keychain.js"; import { saveProfile } from "../../../../lib/config/profiles.js"; @@ -588,5 +589,23 @@ describe("installTelemetry recorder", () => { const rec = await recordOneCommand(); expect(rec.site_name).toBe("env-test"); }); + + it("prefers a site the command noted as newly active", async () => { + const program = new Command(); + program.exitOverride(); + program.command("connect").action(() => noteActiveSite("globex-test")); + installTelemetry(program, "1.0.0-test"); + await program.parseAsync(["connect"], { from: "user" }); + __finalizeForTest(0); + const rec = JSON.parse(readFileSync(spoolPath(), "utf-8").trim()) as SpoolRecord; + expect(rec.site_name).toBe("globex-test"); + expect(takeActiveSite()).toBeUndefined(); + }); + + it("drops a noted site on recorder reset", () => { + noteActiveSite("globex-test"); + __resetTelemetryRecorderForTest(); + expect(takeActiveSite()).toBeUndefined(); + }); }); });