|
| 1 | +import * as fs from "node:fs/promises"; |
| 2 | +import * as os from "node:os"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +import { toStoredTelemetryEvent } from "@/telemetry/export/files"; |
| 7 | +import { writeJsonArrayExport } from "@/telemetry/export/writers"; |
| 8 | + |
| 9 | +import type { ExportTelemetryEvent } from "@/telemetry/export/types"; |
| 10 | + |
| 11 | +let tmpDir: string; |
| 12 | + |
| 13 | +beforeEach(async () => { |
| 14 | + tmpDir = await fs.mkdtemp( |
| 15 | + path.join(os.tmpdir(), "telemetry-export-writers-"), |
| 16 | + ); |
| 17 | +}); |
| 18 | + |
| 19 | +afterEach(async () => { |
| 20 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe("telemetry export writers", () => { |
| 24 | + it("writes telemetry events as a JSON array using the stored event shape", async () => { |
| 25 | + const outputPath = path.join(tmpDir, "telemetry.json"); |
| 26 | + |
| 27 | + const events = [ |
| 28 | + makeEvent({ |
| 29 | + eventId: "1111111111111111", |
| 30 | + eventName: "first", |
| 31 | + properties: { result: "success" }, |
| 32 | + measurements: { durationMs: 12 }, |
| 33 | + traceId: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 34 | + }), |
| 35 | + makeEvent({ |
| 36 | + eventId: "2222222222222222", |
| 37 | + eventName: "second", |
| 38 | + parentEventId: "1111111111111111", |
| 39 | + error: { message: "boom", type: "Error" }, |
| 40 | + }), |
| 41 | + ]; |
| 42 | + |
| 43 | + const counts = await writeJsonArrayExport(outputPath, asyncEvents(events)); |
| 44 | + |
| 45 | + expect(counts.events).toBe(2); |
| 46 | + expect(JSON.parse(await fs.readFile(outputPath, "utf8"))).toEqual( |
| 47 | + events.map(toStoredTelemetryEvent), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it("writes a valid empty JSON array", async () => { |
| 52 | + const outputPath = path.join(tmpDir, "empty.json"); |
| 53 | + |
| 54 | + const counts = await writeJsonArrayExport(outputPath, asyncEvents([])); |
| 55 | + |
| 56 | + expect(counts.events).toBe(0); |
| 57 | + expect(JSON.parse(await fs.readFile(outputPath, "utf8"))).toEqual([]); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +async function* asyncEvents( |
| 62 | + events: readonly ExportTelemetryEvent[], |
| 63 | +): AsyncGenerator<ExportTelemetryEvent> { |
| 64 | + for (const event of events) { |
| 65 | + await Promise.resolve(); |
| 66 | + yield event; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function makeEvent( |
| 71 | + overrides: Partial<ExportTelemetryEvent>, |
| 72 | +): ExportTelemetryEvent { |
| 73 | + return { |
| 74 | + eventId: "1111111111111111", |
| 75 | + eventName: "test.event", |
| 76 | + timestamp: "2026-05-12T12:00:00.000Z", |
| 77 | + eventSequence: 1, |
| 78 | + context: { |
| 79 | + extensionVersion: "1.2.3", |
| 80 | + machineId: "machine", |
| 81 | + sessionId: "session", |
| 82 | + osType: "linux", |
| 83 | + osVersion: "6.0.0", |
| 84 | + hostArch: "x64", |
| 85 | + platformName: "VS Code", |
| 86 | + platformVersion: "1.100.0", |
| 87 | + deploymentUrl: "https://coder.example.com", |
| 88 | + }, |
| 89 | + properties: {}, |
| 90 | + measurements: {}, |
| 91 | + ...overrides, |
| 92 | + }; |
| 93 | +} |
0 commit comments