|
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs" |
| 3 | +import os from "os" |
| 4 | +import path from "path" |
| 5 | +import { Telemetry } from "../../../src/altimate/telemetry" |
| 6 | +import { TelemetryIdentity } from "../../../src/altimate/telemetry/identity" |
| 7 | +import { OptOutMarker } from "../../../src/altimate/telemetry/opt-out" |
| 8 | + |
| 9 | +const MACHINE = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d" |
| 10 | +const UUID = "0f3c2a1e-6b7d-4c8e-9f10-1a2b3c4d5e6f" |
| 11 | +const API = "https://api.example.invalid" |
| 12 | +const CS = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/" |
| 13 | + |
| 14 | +type Call = { url: string; body: string } |
| 15 | + |
| 16 | +async function waitFor(pred: () => boolean, ms = 2_000) { |
| 17 | + const until = Date.now() + ms |
| 18 | + while (!pred() && Date.now() < until) await new Promise((r) => setTimeout(r, 20)) |
| 19 | + return pred() |
| 20 | +} |
| 21 | + |
| 22 | +describe("telemetry opt-out marker", () => { |
| 23 | + let home: string |
| 24 | + let calls: Call[] |
| 25 | + let trackStatus = 200 |
| 26 | + let fetchSpy: ReturnType<typeof spyOn> |
| 27 | + let homedirSpy: ReturnType<typeof spyOn> |
| 28 | + const saved: Record<string, string | undefined> = {} |
| 29 | + const ENV = [ |
| 30 | + "OPENCODE_TEST_HOME", |
| 31 | + "APPLICATIONINSIGHTS_CONNECTION_STRING", |
| 32 | + "ALTIMATE_TELEMETRY_DISABLED", |
| 33 | + "OPENCODE_DISABLE_TELEMETRY", |
| 34 | + ] |
| 35 | + |
| 36 | + const optOuts = () => |
| 37 | + calls |
| 38 | + .filter((c) => c.url.includes("/v2/track")) |
| 39 | + .flatMap((c) => JSON.parse(c.body) as any[]) |
| 40 | + .filter((e) => e.data?.baseData?.name === "telemetry_opted_out") |
| 41 | + const trackPosts = () => calls.filter((c) => c.url.includes("/v2/track")) |
| 42 | + |
| 43 | + function writeMachineId() { |
| 44 | + mkdirSync(path.join(home, ".altimate"), { recursive: true }) |
| 45 | + writeFileSync(path.join(home, ".altimate", "machine-id"), MACHINE) |
| 46 | + } |
| 47 | + function writeCreds() { |
| 48 | + mkdirSync(path.join(home, ".altimate"), { recursive: true }) |
| 49 | + writeFileSync( |
| 50 | + path.join(home, ".altimate", "altimate.json"), |
| 51 | + JSON.stringify({ altimateUrl: API, altimateInstanceName: "acme", altimateApiKey: "key-1" }), |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + home = mkdtempSync(path.join(os.tmpdir(), "telemetry-opt-out-")) |
| 57 | + for (const k of ENV) saved[k] = process.env[k] |
| 58 | + process.env.OPENCODE_TEST_HOME = home |
| 59 | + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = CS |
| 60 | + delete process.env.ALTIMATE_TELEMETRY_DISABLED |
| 61 | + delete process.env.OPENCODE_DISABLE_TELEMETRY |
| 62 | + homedirSpy = spyOn(os, "homedir").mockImplementation(() => home) |
| 63 | + Telemetry.allowOptOutUnderTestRunner(true) |
| 64 | + calls = [] |
| 65 | + trackStatus = 200 |
| 66 | + fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: any, init?: RequestInit) => { |
| 67 | + const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url |
| 68 | + calls.push({ url, body: String(init?.body ?? "") }) |
| 69 | + if (url === `${API}/users/me`) { |
| 70 | + return new Response(JSON.stringify({ id: 7, uuid: UUID }), { |
| 71 | + status: 200, |
| 72 | + headers: { "content-type": "application/json" }, |
| 73 | + }) |
| 74 | + } |
| 75 | + if (url.includes("/v2/track")) return new Response("", { status: trackStatus }) |
| 76 | + return new Response("", { status: 200 }) |
| 77 | + }) as any) |
| 78 | + }) |
| 79 | + |
| 80 | + afterEach(async () => { |
| 81 | + await Telemetry.shutdown() |
| 82 | + Telemetry.allowOptOutUnderTestRunner(false) |
| 83 | + fetchSpy.mockRestore() |
| 84 | + homedirSpy.mockRestore() |
| 85 | + for (const k of ENV) { |
| 86 | + if (saved[k] === undefined) delete process.env[k] |
| 87 | + else process.env[k] = saved[k] |
| 88 | + } |
| 89 | + rmSync(home, { recursive: true, force: true }) |
| 90 | + }) |
| 91 | + |
| 92 | + test("an opted-out machine sends one final event with its identity, then nothing", async () => { |
| 93 | + writeMachineId() |
| 94 | + writeCreds() |
| 95 | + expect(await TelemetryIdentity.resolve()).toEqual({ user_id: UUID, tenant: "acme" }) |
| 96 | + const meCallsBefore = calls.filter((c) => c.url.endsWith("/users/me")).length |
| 97 | + |
| 98 | + process.env.ALTIMATE_TELEMETRY_DISABLED = "1" |
| 99 | + await Telemetry.shutdown() |
| 100 | + await Telemetry.init() |
| 101 | + expect(await waitFor(() => optOuts().length > 0)).toBe(true) |
| 102 | + const [event] = optOuts() |
| 103 | + expect(event.data.baseData.properties.machine_id).toBe(MACHINE) |
| 104 | + expect(event.data.baseData.properties.opt_out_source).toBe("env") |
| 105 | + expect(event.data.baseData.properties.user_id).toBe(UUID) |
| 106 | + expect(event.data.baseData.properties.tenant).toBe("acme") |
| 107 | + expect(typeof event.data.baseData.properties.cli_version).toBe("string") |
| 108 | + expect(event.tags["ai.user.id"]).toBe(UUID) |
| 109 | + // Identity came from the cache: no request to resolve it while opted out. |
| 110 | + expect(calls.filter((c) => c.url.endsWith("/users/me")).length).toBe(meCallsBefore) |
| 111 | + expect(await waitFor(() => OptOutMarker.exists())).toBe(true) |
| 112 | + |
| 113 | + // Nothing after it: tracked events are dropped, and a new init does not send again. |
| 114 | + const posts = trackPosts().length |
| 115 | + Telemetry.track({ |
| 116 | + type: "startup_ready", |
| 117 | + session_id: "", |
| 118 | + command: "test", |
| 119 | + duration_ms: 1, |
| 120 | + fresh_install: false, |
| 121 | + timestamp: Date.now(), |
| 122 | + }) |
| 123 | + await Telemetry.flush() |
| 124 | + await Telemetry.shutdown() |
| 125 | + await Telemetry.init() |
| 126 | + await new Promise((r) => setTimeout(r, 150)) |
| 127 | + expect(trackPosts().length).toBe(posts) |
| 128 | + expect(optOuts()).toHaveLength(1) |
| 129 | + }) |
| 130 | + |
| 131 | + test("a machine that never sent telemetry sends nothing and gets no marker", async () => { |
| 132 | + process.env.ALTIMATE_TELEMETRY_DISABLED = "1" |
| 133 | + await Telemetry.shutdown() |
| 134 | + await Telemetry.init() |
| 135 | + await new Promise((r) => setTimeout(r, 150)) |
| 136 | + expect(trackPosts()).toHaveLength(0) |
| 137 | + expect(OptOutMarker.exists()).toBe(false) |
| 138 | + expect(existsSync(path.join(home, ".altimate", "machine-id"))).toBe(false) |
| 139 | + }) |
| 140 | + |
| 141 | + test("the marker is written only once the sink accepted the event", async () => { |
| 142 | + writeMachineId() |
| 143 | + process.env.OPENCODE_DISABLE_TELEMETRY = "true" |
| 144 | + trackStatus = 500 |
| 145 | + await Telemetry.shutdown() |
| 146 | + await Telemetry.init() |
| 147 | + expect(await waitFor(() => trackPosts().length === 1)).toBe(true) |
| 148 | + await new Promise((r) => setTimeout(r, 50)) |
| 149 | + expect(OptOutMarker.exists()).toBe(false) |
| 150 | + |
| 151 | + trackStatus = 200 |
| 152 | + await Telemetry.shutdown() |
| 153 | + await Telemetry.init() |
| 154 | + expect(await waitFor(() => trackPosts().length === 2)).toBe(true) |
| 155 | + expect(await waitFor(() => OptOutMarker.exists())).toBe(true) |
| 156 | + expect(optOuts()).toHaveLength(2) |
| 157 | + expect(optOuts()[1].data.baseData.properties.user_id).toBeUndefined() |
| 158 | + expect(optOuts()[1].tags["ai.user.id"]).toBe(MACHINE) |
| 159 | + }) |
| 160 | + |
| 161 | + test("turning telemetry back on clears the marker", async () => { |
| 162 | + writeMachineId() |
| 163 | + OptOutMarker.write() |
| 164 | + expect(OptOutMarker.exists()).toBe(true) |
| 165 | + await Telemetry.shutdown() |
| 166 | + await Telemetry.init() |
| 167 | + expect(Telemetry.isEnabled()).toBe(true) |
| 168 | + expect(OptOutMarker.exists()).toBe(false) |
| 169 | + expect(optOuts()).toHaveLength(0) |
| 170 | + }) |
| 171 | +}) |
0 commit comments