diff --git a/packages/sim-cli/src/commands/telemetry.test.ts b/packages/sim-cli/src/commands/telemetry.test.ts index 6df6124fed7..9db6087285e 100644 --- a/packages/sim-cli/src/commands/telemetry.test.ts +++ b/packages/sim-cli/src/commands/telemetry.test.ts @@ -12,6 +12,10 @@ let output: string[] beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'sim-telemetry-')) vi.stubEnv('SIM_CONFIG_DIR', dir) + /** Pinned so the result never depends on the shell or CI job running the suite. */ + vi.stubEnv('SIM_CLI_TELEMETRY_KEY', '') + vi.stubEnv('DO_NOT_TRACK', '') + vi.stubEnv('SIM_TELEMETRY_DISABLED', '') output = [] vi.spyOn(console, 'log').mockImplementation((line: string) => { output.push(line) @@ -54,4 +58,12 @@ describe('sim telemetry', () => { expect(output[0]).toMatch(/off: this build has no reporting destination/) expect(output[1]).toContain('https://docs.sim.ai/cli/usage-data') }) + + it('reports on for a build with a destination', async () => { + vi.stubEnv('SIM_CLI_TELEMETRY_KEY', 'phc_test') + + await run('status') + + expect(output[0]).toBe('Usage reporting is on.') + }) }) diff --git a/packages/sim-cli/src/telemetry/transport.ts b/packages/sim-cli/src/telemetry/transport.ts index 32d35dca2e0..ac5f5f03e92 100644 --- a/packages/sim-cli/src/telemetry/transport.ts +++ b/packages/sim-cli/src/telemetry/transport.ts @@ -1,19 +1,6 @@ import { type ChildProcess, spawn } from 'node:child_process' import { childProcessEnv, proxyExecArgv } from '../environment' -/** - * Where events go. The key is a PostHog project token — a public, write-only - * value — baked into the published build by `bun build --env='SIM_CLI_TELEMETRY_*'` - * the way the Supabase CLI injects its own at link time. A checkout built - * without one has no destination and reports nothing, and a self-hosted - * deployment can point its own build at its own project. - * - * These two reads must stay literal `process.env.` expressions: that is - * the only form the bundler substitutes. - */ -const BUILT_IN_KEY = process.env.SIM_CLI_TELEMETRY_KEY -const BUILT_IN_HOST = process.env.SIM_CLI_TELEMETRY_HOST - export const DEFAULT_INGEST_HOST = 'https://us.i.posthog.com' /** The single-event capture endpoint, relative to the ingest host. */ @@ -33,10 +20,22 @@ export interface IngestTarget { host: string } -/** The destination this build was made with, if any. */ +/** + * The destination this build was made with, if any. The key is a PostHog + * project token — a public, write-only value — baked into the published build + * by `bun build --env='SIM_CLI_TELEMETRY_*'` the way the Supabase CLI injects + * its own at link time. A checkout built without one has no destination and + * reports nothing, and a self-hosted deployment can point its own build at its + * own project. + * + * Both reads must stay literal `process.env.` expressions, the only form + * the bundler substitutes. They are made on each call rather than at module + * load so that, unbundled, the answer follows the environment a caller set. + */ export function builtInIngestTarget(): IngestTarget | undefined { - if (!BUILT_IN_KEY) return undefined - return { key: BUILT_IN_KEY, host: BUILT_IN_HOST || DEFAULT_INGEST_HOST } + const key = process.env.SIM_CLI_TELEMETRY_KEY + if (!key) return undefined + return { key, host: process.env.SIM_CLI_TELEMETRY_HOST || DEFAULT_INGEST_HOST } } /** One event in the shape PostHog's capture endpoint accepts. */