|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const baseConfig = { |
| 4 | + telemetryEnabled: true, |
| 5 | + anonymousId: "test-install", |
| 6 | + telemetryNoticeShown: true, |
| 7 | + commandCount: 7, |
| 8 | + renderSuccessCount: 0, |
| 9 | + lastFeedbackPromptAt: 0, |
| 10 | +}; |
| 11 | + |
| 12 | +async function loadTelemetryCommand(options?: { |
| 13 | + writeSucceeds?: boolean; |
| 14 | + configEnabled?: boolean; |
| 15 | +}) { |
| 16 | + const config = { |
| 17 | + ...baseConfig, |
| 18 | + telemetryEnabled: options?.configEnabled ?? true, |
| 19 | + }; |
| 20 | + const writeConfig = vi.fn(() => options?.writeSucceeds ?? true); |
| 21 | + vi.resetModules(); |
| 22 | + vi.doMock("../telemetry/config.js", () => ({ |
| 23 | + CONFIG_PATH: "/test/.hyperframes/config.json", |
| 24 | + readConfig: () => { |
| 25 | + throw new Error("telemetry commands must bypass stale cached config"); |
| 26 | + }, |
| 27 | + readConfigFresh: () => ({ ...config }), |
| 28 | + writeConfig, |
| 29 | + })); |
| 30 | + const module = await import("./telemetry.js"); |
| 31 | + return { command: module.default, writeConfig }; |
| 32 | +} |
| 33 | + |
| 34 | +async function runSubcommand( |
| 35 | + command: Awaited<ReturnType<typeof loadTelemetryCommand>>["command"], |
| 36 | + subcommand: string, |
| 37 | +): Promise<void> { |
| 38 | + await command.run?.({ |
| 39 | + args: { subcommand }, |
| 40 | + rawArgs: [subcommand], |
| 41 | + cmd: command, |
| 42 | + } as never); |
| 43 | +} |
| 44 | + |
| 45 | +describe("telemetry command", () => { |
| 46 | + afterEach(() => { |
| 47 | + vi.doUnmock("../telemetry/config.js"); |
| 48 | + vi.restoreAllMocks(); |
| 49 | + vi.resetModules(); |
| 50 | + delete process.env["HYPERFRAMES_NO_TELEMETRY"]; |
| 51 | + delete process.env["DO_NOT_TRACK"]; |
| 52 | + }); |
| 53 | + |
| 54 | + it("persists disable from a fresh config snapshot", async () => { |
| 55 | + const { command, writeConfig } = await loadTelemetryCommand(); |
| 56 | + vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 57 | + |
| 58 | + await runSubcommand(command, "disable"); |
| 59 | + |
| 60 | + expect(writeConfig).toHaveBeenCalledWith(expect.objectContaining({ telemetryEnabled: false })); |
| 61 | + }); |
| 62 | + |
| 63 | + it("fails instead of claiming success when the preference cannot be persisted", async () => { |
| 64 | + const { command } = await loadTelemetryCommand({ writeSucceeds: false }); |
| 65 | + const stdout = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 66 | + const stderr = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 67 | + |
| 68 | + await expect(runSubcommand(command, "disable")).rejects.toMatchObject({ |
| 69 | + name: "CliRuntimeError", |
| 70 | + }); |
| 71 | + |
| 72 | + expect(stderr).toHaveBeenCalledWith(expect.stringContaining("Could not persist")); |
| 73 | + expect(stdout).not.toHaveBeenCalledWith(expect.stringContaining("Telemetry disabled")); |
| 74 | + }); |
| 75 | + |
| 76 | + it("reports the effective env-var opt-out instead of the stored preference", async () => { |
| 77 | + process.env["HYPERFRAMES_NO_TELEMETRY"] = "1"; |
| 78 | + const { command } = await loadTelemetryCommand({ configEnabled: true }); |
| 79 | + const lines: string[] = []; |
| 80 | + vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => { |
| 81 | + lines.push(args.map(String).join(" ")); |
| 82 | + }); |
| 83 | + |
| 84 | + await runSubcommand(command, "status"); |
| 85 | + |
| 86 | + const output = lines.join("\n"); |
| 87 | + expect(output).toContain("disabled"); |
| 88 | + expect(output).toContain("HYPERFRAMES_NO_TELEMETRY"); |
| 89 | + expect(output).toContain("Tracked commands:"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("reports DO_NOT_TRACK as the effective opt-out source", async () => { |
| 93 | + process.env["DO_NOT_TRACK"] = "1"; |
| 94 | + const { command } = await loadTelemetryCommand({ configEnabled: true }); |
| 95 | + const lines: string[] = []; |
| 96 | + vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => { |
| 97 | + lines.push(args.map(String).join(" ")); |
| 98 | + }); |
| 99 | + |
| 100 | + await runSubcommand(command, "status"); |
| 101 | + |
| 102 | + expect(lines.join("\n")).toContain("DO_NOT_TRACK"); |
| 103 | + }); |
| 104 | +}); |
0 commit comments