telemetry.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * Anonymous usage telemetry — client module.
  3. *
  4. * Pins the four invariants from docs/design/telemetry.md: zero stdout, off is
  5. * off (no socket, no files), fail silent, and local rollup aggregation with
  6. * completed-days-only sending. All seams (dir, fetch, clock, env, stderr) are
  7. * injected — no network, no real home directory.
  8. */
  9. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  10. import * as fs from 'fs';
  11. import * as path from 'path';
  12. import * as os from 'os';
  13. import { Telemetry, getTelemetry, TELEMETRY_ENDPOINT } from '../src/telemetry';
  14. type FetchCall = { url: string; body: Record<string, unknown> };
  15. function mockFetch(calls: FetchCall[], opts: { fail?: boolean } = {}) {
  16. return vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
  17. if (opts.fail) throw new Error('network down');
  18. calls.push({ url: String(input), body: JSON.parse(String(init?.body)) as Record<string, unknown> });
  19. return new Response(null, { status: 204 });
  20. }) as unknown as typeof globalThis.fetch;
  21. }
  22. describe('Telemetry', () => {
  23. let dir: string;
  24. let calls: FetchCall[];
  25. let stderrLines: string[];
  26. let nowValue: Date;
  27. const make = (overrides: Partial<ConstructorParameters<typeof Telemetry>[0]> = {}) =>
  28. new Telemetry({
  29. dir,
  30. fetchImpl: mockFetch(calls),
  31. now: () => nowValue,
  32. env: {},
  33. stderr: (line) => stderrLines.push(line),
  34. installExitHook: false,
  35. ...overrides,
  36. });
  37. beforeEach(() => {
  38. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-telemetry-'));
  39. calls = [];
  40. stderrLines = [];
  41. nowValue = new Date('2026-06-12T08:00:00.000Z');
  42. });
  43. afterEach(() => {
  44. fs.rmSync(dir, { recursive: true, force: true });
  45. });
  46. describe('consent precedence', () => {
  47. it('defaults to enabled when nothing decides otherwise', () => {
  48. const t = make();
  49. expect(t.getStatus()).toMatchObject({ enabled: true, decidedBy: 'default', machineId: null });
  50. });
  51. it('DO_NOT_TRACK beats everything, including a forced-on env and config', () => {
  52. const t = make({ env: { DO_NOT_TRACK: '1', CODEGRAPH_TELEMETRY: '1' } });
  53. t.setEnabled(true, 'cli');
  54. expect(t.getStatus()).toMatchObject({ enabled: false, decidedBy: 'DO_NOT_TRACK' });
  55. });
  56. it('CODEGRAPH_TELEMETRY env beats the stored config in both directions', () => {
  57. const t = make({ env: { CODEGRAPH_TELEMETRY: '0' } });
  58. t.setEnabled(true, 'cli');
  59. expect(t.getStatus()).toMatchObject({ enabled: false, decidedBy: 'CODEGRAPH_TELEMETRY' });
  60. const t2 = make({ env: { CODEGRAPH_TELEMETRY: '1' } });
  61. t2.setEnabled(false, 'cli');
  62. expect(t2.getStatus()).toMatchObject({ enabled: true, decidedBy: 'CODEGRAPH_TELEMETRY' });
  63. });
  64. it('stored config decides when no env is set', () => {
  65. const t = make();
  66. t.setEnabled(false, 'installer');
  67. expect(t.getStatus()).toMatchObject({ enabled: false, decidedBy: 'config' });
  68. });
  69. });
  70. describe('off is off', () => {
  71. it('disabled: records nothing, sends nothing, creates no files', async () => {
  72. const fetchSpy = mockFetch(calls);
  73. const t = make({ env: { CODEGRAPH_TELEMETRY: '0' }, fetchImpl: fetchSpy });
  74. t.recordUsage('mcp_tool', 'codegraph_explore', true);
  75. t.recordLifecycle('install', { scope: 'local', kind: 'fresh' });
  76. t.persistSync();
  77. await t.flushNow();
  78. expect(fetchSpy).not.toHaveBeenCalled();
  79. expect(fs.existsSync(t.configPath)).toBe(false);
  80. expect(fs.existsSync(t.queuePath)).toBe(false);
  81. expect(stderrLines).toEqual([]);
  82. });
  83. it('turning telemetry off deletes buffered unsent data', () => {
  84. const t = make();
  85. t.recordUsage('cli_command', 'init', true);
  86. t.persistSync();
  87. expect(fs.existsSync(t.queuePath)).toBe(true);
  88. t.setEnabled(false, 'cli');
  89. expect(fs.existsSync(t.queuePath)).toBe(false);
  90. });
  91. });
  92. describe('first-run notice & machine id', () => {
  93. it('recording only buffers — no notice, no config until something is sent', async () => {
  94. const t = make();
  95. t.recordUsage('mcp_tool', 'codegraph_explore', true);
  96. t.recordUsage('mcp_tool', 'codegraph_node', true);
  97. expect(stderrLines).toEqual([]); // local buffering is silent
  98. expect(fs.existsSync(t.configPath)).toBe(false);
  99. // Same-day rollups aren't sendable yet — even a flush stays silent.
  100. await t.flushNow();
  101. expect(stderrLines).toEqual([]);
  102. expect(calls).toHaveLength(0);
  103. });
  104. it('prints the notice exactly once, before the first actual send', async () => {
  105. const t = make();
  106. t.recordLifecycle('index', { languages: ['go'] });
  107. await t.flushNow();
  108. t.recordLifecycle('index', { languages: ['rust'] });
  109. await t.flushNow();
  110. expect(calls).toHaveLength(2);
  111. expect(stderrLines).toHaveLength(1);
  112. expect(stderrLines[0]).toContain('codegraph telemetry off');
  113. expect(stderrLines[0]).toContain('CODEGRAPH_TELEMETRY=0');
  114. const config = JSON.parse(fs.readFileSync(t.configPath, 'utf8'));
  115. expect(config.machine_id).toMatch(/^[0-9a-f-]{36}$/);
  116. expect(config.consent_source).toBe('default-notice');
  117. });
  118. it('keeps the machine id stable across instances and explicit toggles', async () => {
  119. const t = make();
  120. t.recordLifecycle('install', { scope: 'local', kind: 'fresh' });
  121. await t.flushNow();
  122. const id1 = t.getStatus().machineId;
  123. expect(id1).toBeTruthy();
  124. const t2 = make();
  125. t2.setEnabled(true, 'cli');
  126. expect(t2.getStatus().machineId).toBe(id1);
  127. });
  128. it('an explicit installer choice suppresses the notice', async () => {
  129. const t = make();
  130. t.setEnabled(true, 'installer');
  131. t.recordLifecycle('install', { scope: 'local', kind: 'fresh' });
  132. await t.flushNow();
  133. expect(calls).toHaveLength(1); // sent…
  134. expect(stderrLines).toEqual([]); // …without ever showing the notice
  135. });
  136. });
  137. describe('rollups & sending', () => {
  138. it('aggregates per (day, kind, name, client) and sends only completed days', async () => {
  139. const t = make();
  140. const client = { name: 'Claude Code', version: '2.1' };
  141. t.recordUsage('mcp_tool', 'codegraph_explore', true, client);
  142. t.recordUsage('mcp_tool', 'codegraph_explore', false, client);
  143. t.recordUsage('mcp_tool', 'codegraph_explore', true, client);
  144. t.recordUsage('cli_command', 'query', true);
  145. // Same day: nothing is sendable yet.
  146. await t.flushNow();
  147. expect(calls).toHaveLength(0);
  148. // Next day: yesterday's rollups go out.
  149. nowValue = new Date('2026-06-13T08:00:00.000Z');
  150. t.recordUsage('cli_command', 'status', true); // today's — must stay queued
  151. await t.flushNow();
  152. expect(calls).toHaveLength(1);
  153. const body = calls[0]!.body;
  154. expect(body.machine_id).toBe(t.getStatus().machineId);
  155. expect(body.schema_version).toBe(1);
  156. const events = body.events as Array<{ event: string; ts: string; props: Record<string, unknown> }>;
  157. expect(events).toHaveLength(2);
  158. const explore = events.find((e) => e.props.name === 'codegraph_explore')!;
  159. expect(explore).toMatchObject({
  160. event: 'usage_rollup',
  161. ts: '2026-06-12T12:00:00.000Z',
  162. props: { kind: 'mcp_tool', count: 3, error_count: 1, client_name: 'Claude Code', client_version: '2.1' },
  163. });
  164. // Today's delta is still buffered for tomorrow.
  165. expect(fs.readFileSync(t.queuePath, 'utf8')).toContain('"status"');
  166. });
  167. it('lifecycle events send on the next flush regardless of day', async () => {
  168. const t = make();
  169. t.recordLifecycle('install', { targets: ['claude'], scope: 'local', kind: 'fresh' });
  170. await t.flushNow();
  171. expect(calls).toHaveLength(1);
  172. const events = calls[0]!.body.events as Array<{ event: string; props: Record<string, unknown> }>;
  173. expect(events[0]).toMatchObject({ event: 'install', props: { scope: 'local', kind: 'fresh' } });
  174. });
  175. it('uses the production endpoint by default and honors the env override', async () => {
  176. const t = make();
  177. t.recordLifecycle('uninstall', {});
  178. await t.flushNow();
  179. expect(calls[0]!.url).toBe(TELEMETRY_ENDPOINT);
  180. const t2 = make({ env: { CODEGRAPH_TELEMETRY_ENDPOINT: 'http://localhost:9999/v1/events' } });
  181. t2.recordLifecycle('uninstall', {});
  182. await t2.flushNow();
  183. expect(calls[1]!.url).toBe('http://localhost:9999/v1/events');
  184. });
  185. it('re-queues on network failure and delivers on the next flush', async () => {
  186. const t = make({ fetchImpl: mockFetch(calls, { fail: true }) });
  187. t.recordLifecycle('install', { scope: 'global', kind: 'upgrade' });
  188. await expect(t.flushNow()).resolves.toBeUndefined(); // fail silent
  189. expect(calls).toHaveLength(0);
  190. expect(fs.readFileSync(t.queuePath, 'utf8')).toContain('"install"');
  191. // No claim files left behind.
  192. expect(fs.readdirSync(dir).filter((f) => f.includes('.sending.'))).toEqual([]);
  193. const t2 = make();
  194. await t2.flushNow();
  195. expect(calls).toHaveLength(1);
  196. expect(fs.existsSync(t2.queuePath)).toBe(false);
  197. });
  198. it('a hung endpoint is bounded by the flush timeout', async () => {
  199. const hangingFetch = ((_url: RequestInfo | URL, init?: RequestInit) =>
  200. new Promise((_resolve, reject) => {
  201. init?.signal?.addEventListener('abort', () => reject(new Error('aborted')));
  202. })) as unknown as typeof globalThis.fetch;
  203. const t = make({ fetchImpl: hangingFetch });
  204. t.recordLifecycle('install', { scope: 'local', kind: 'fresh' });
  205. const started = Date.now();
  206. await t.flushNow(100);
  207. expect(Date.now() - started).toBeLessThan(2000);
  208. expect(fs.readFileSync(t.queuePath, 'utf8')).toContain('"install"'); // re-queued
  209. });
  210. });
  211. describe('buffer robustness', () => {
  212. it('caps the queue and drops oldest lines without leaving partial JSON', () => {
  213. const t = make();
  214. const bigProps = { targets: Array.from({ length: 50 }, (_, i) => `agent-${i}`) };
  215. for (let i = 0; i < 600; i++) {
  216. t.recordLifecycle('install', { ...bigProps, kind: `fresh`, scope: `local`, seq: i });
  217. t.persistSync();
  218. }
  219. const content = fs.readFileSync(t.queuePath, 'utf8');
  220. expect(content.length).toBeLessThanOrEqual(256 * 1024);
  221. const first = content.slice(0, content.indexOf('\n'));
  222. expect(() => JSON.parse(first)).not.toThrow(); // no partial first line
  223. expect(JSON.parse(first).props.seq).toBeGreaterThan(0); // oldest dropped
  224. });
  225. it('skips corrupt lines and still delivers the valid ones', async () => {
  226. const t = make();
  227. t.recordLifecycle('index', { languages: ['typescript'] });
  228. t.persistSync();
  229. fs.appendFileSync(t.queuePath, 'NOT JSON{{{\n');
  230. await t.flushNow();
  231. expect(calls).toHaveLength(1);
  232. expect((calls[0]!.body.events as unknown[])).toHaveLength(1);
  233. });
  234. it('merges back stale claim files from a crashed sender', async () => {
  235. const t = make();
  236. const stale = path.join(dir, 'telemetry-queue.sending.99999.jsonl');
  237. fs.mkdirSync(dir, { recursive: true });
  238. fs.writeFileSync(stale, JSON.stringify({ v: 1, ev: 'uninstall', ts: '2026-06-11T00:00:00.000Z', props: {} }) + '\n');
  239. const old = new Date(nowValue.getTime() - 2 * 60 * 60_000);
  240. fs.utimesSync(stale, old, old);
  241. t.setEnabled(true, 'cli'); // config so send() has a machine id
  242. await t.flushNow();
  243. expect(fs.existsSync(stale)).toBe(false);
  244. expect(calls).toHaveLength(1);
  245. expect((calls[0]!.body.events as Array<{ event: string }>)[0]!.event).toBe('uninstall');
  246. });
  247. });
  248. describe('protocol safety', () => {
  249. it('never writes to stdout', async () => {
  250. const stdoutSpy = vi.spyOn(process.stdout, 'write');
  251. const t = make({ env: { CODEGRAPH_TELEMETRY_DEBUG: '1' } });
  252. t.recordUsage('mcp_tool', 'codegraph_explore', true);
  253. t.recordLifecycle('install', { scope: 'local', kind: 'fresh' });
  254. await t.flushNow();
  255. expect(stdoutSpy).not.toHaveBeenCalled();
  256. stdoutSpy.mockRestore();
  257. });
  258. });
  259. it('getTelemetry returns a process-wide singleton', () => {
  260. expect(getTelemetry()).toBe(getTelemetry());
  261. });
  262. });