|
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" |
| 2 | +import { mkdtempSync, rmSync } from "fs" |
| 3 | +import os from "os" |
| 4 | +import path from "path" |
| 5 | +import { Telemetry } from "../../../src/altimate/telemetry" |
| 6 | + |
| 7 | +describe("numeric ids are envelope properties, not measurements", () => { |
| 8 | + let home: string |
| 9 | + let posts: string[] |
| 10 | + let fetchSpy: ReturnType<typeof spyOn> |
| 11 | + let homedirSpy: ReturnType<typeof spyOn> |
| 12 | + let savedCs: string | undefined |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + home = mkdtempSync(path.join(os.tmpdir(), "telemetry-ids-")) |
| 16 | + savedCs = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING |
| 17 | + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = |
| 18 | + "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/" |
| 19 | + homedirSpy = spyOn(os, "homedir").mockImplementation(() => home) |
| 20 | + posts = [] |
| 21 | + fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: any, init?: RequestInit) => { |
| 22 | + const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url |
| 23 | + if (url.includes("/v2/track")) posts.push(String(init?.body ?? "")) |
| 24 | + return new Response("", { status: 200 }) |
| 25 | + }) as any) |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(async () => { |
| 29 | + await Telemetry.shutdown() |
| 30 | + fetchSpy.mockRestore() |
| 31 | + homedirSpy.mockRestore() |
| 32 | + if (savedCs === undefined) delete process.env.APPLICATIONINSIGHTS_CONNECTION_STRING |
| 33 | + else process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = savedCs |
| 34 | + rmSync(home, { recursive: true, force: true }) |
| 35 | + }) |
| 36 | + |
| 37 | + test("workspace ids ride in properties as strings; quantities stay measurements", async () => { |
| 38 | + await Telemetry.shutdown() |
| 39 | + await Telemetry.init() |
| 40 | + Telemetry.track({ |
| 41 | + type: "workspace_switched", |
| 42 | + timestamp: Date.now(), |
| 43 | + session_id: "s", |
| 44 | + to_workspace_id: 9, |
| 45 | + from_workspace_id: 4, |
| 46 | + how: "rebind_remote", |
| 47 | + }) |
| 48 | + Telemetry.track({ |
| 49 | + type: "workspace_api_error", |
| 50 | + timestamp: Date.now(), |
| 51 | + session_id: "s", |
| 52 | + endpoint: "POST /datamate-project-bindings/bind", |
| 53 | + status_class: "4xx", |
| 54 | + status: 404, |
| 55 | + workspace_id: 42, |
| 56 | + }) |
| 57 | + await Telemetry.flush() |
| 58 | + const envelopes = posts.flatMap((b) => JSON.parse(b) as any[]) |
| 59 | + const switched = envelopes.find((e) => e.data.baseData.name === "workspace_switched").data.baseData |
| 60 | + expect(switched.properties.to_workspace_id).toBe("9") |
| 61 | + expect(switched.properties.from_workspace_id).toBe("4") |
| 62 | + expect(switched.properties.client).toBe("altimate-code") |
| 63 | + expect("to_workspace_id" in switched.measurements).toBe(false) |
| 64 | + const apiError = envelopes.find((e) => e.data.baseData.name === "workspace_api_error").data.baseData |
| 65 | + expect(apiError.properties.workspace_id).toBe("42") |
| 66 | + expect(apiError.measurements.status).toBe(404) // a quantity, still a measurement |
| 67 | + expect("workspace_id" in apiError.measurements).toBe(false) |
| 68 | + }) |
| 69 | +}) |
0 commit comments