95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { TelemetryDeduplicator } from "../../../src/platform/telemetry/telemetry-deduplicator.ts";
|
|
import {
|
|
fingerprintTelemetryEvent,
|
|
stableSerialize,
|
|
} from "../../../src/platform/telemetry/telemetry-fingerprint.ts";
|
|
|
|
const FINGERPRINT = fingerprintTelemetryEvent({
|
|
kind: "catalog.load.failed",
|
|
severity: "error",
|
|
payload: { reason: "timeout" },
|
|
});
|
|
|
|
describe("fingerprintTelemetryEvent", () => {
|
|
it("ignores request-cardinality identifiers and key order", () => {
|
|
const a = fingerprintTelemetryEvent({
|
|
kind: "catalog.load.failed",
|
|
severity: "error",
|
|
interactionId: "i-1",
|
|
traceId: "4bf92f3577b34da6a3ce929d0e0e4736",
|
|
payload: { reason: "timeout", operation: "list" },
|
|
});
|
|
const b = fingerprintTelemetryEvent({
|
|
kind: "catalog.load.failed",
|
|
severity: "error",
|
|
interactionId: "i-2",
|
|
payload: { operation: "list", reason: "timeout" },
|
|
});
|
|
expect(a).toBe(b);
|
|
expect(a).toMatch(/^[0-9a-f]{16}$/);
|
|
});
|
|
|
|
it("separates distinct failures", () => {
|
|
expect(
|
|
fingerprintTelemetryEvent({ kind: "a.b", severity: "error", payload: { reason: "x" } }),
|
|
).not.toBe(
|
|
fingerprintTelemetryEvent({ kind: "a.b", severity: "error", payload: { reason: "y" } }),
|
|
);
|
|
});
|
|
|
|
it("serializes structure unambiguously", () => {
|
|
expect(stableSerialize({ b: [1, { c: "x" }], a: null })).toBe(
|
|
'{"a":null,"b":[1,{"c":"x"}]}',
|
|
);
|
|
expect(stableSerialize(["]", ","])).toBe('["]",","]');
|
|
});
|
|
});
|
|
|
|
describe("TelemetryDeduplicator", () => {
|
|
it("collapses repeated identical failures into one active fingerprint entry", () => {
|
|
const dedup = new TelemetryDeduplicator({ maxEntries: 100, windowMs: 60_000 });
|
|
|
|
for (let i = 0; i < 10_000; i += 1) {
|
|
dedup.record(FINGERPRINT, i);
|
|
}
|
|
|
|
expect(dedup.size).toBe(1);
|
|
expect(dedup.count(FINGERPRINT)).toBe(10_000);
|
|
});
|
|
|
|
it("never retains more than maxEntries fingerprints", () => {
|
|
const dedup = new TelemetryDeduplicator({ maxEntries: 2, windowMs: 60_000 });
|
|
dedup.record("a", 1);
|
|
dedup.record("b", 2);
|
|
dedup.record("c", 3);
|
|
|
|
expect(dedup.size).toBe(2);
|
|
});
|
|
|
|
it("admits a fingerprint again once its window expires", () => {
|
|
const dedup = new TelemetryDeduplicator({ maxEntries: 10, windowMs: 1_000 });
|
|
expect(dedup.record("a", 0)).toBe(true);
|
|
expect(dedup.record("a", 999)).toBe(false);
|
|
expect(dedup.record("a", 1_000)).toBe(true);
|
|
expect(dedup.count("a")).toBe(1);
|
|
});
|
|
|
|
it("evicts the least recently seen fingerprint under pressure", () => {
|
|
const dedup = new TelemetryDeduplicator({ maxEntries: 2, windowMs: 60_000 });
|
|
dedup.record("a", 1);
|
|
dedup.record("b", 2);
|
|
dedup.record("a", 3); // refresh a
|
|
dedup.record("c", 4); // evicts b
|
|
expect(dedup.count("a")).toBe(2);
|
|
expect(dedup.count("b")).toBe(0);
|
|
expect(dedup.count("c")).toBe(1);
|
|
});
|
|
|
|
it("rejects unbounded configuration", () => {
|
|
expect(() => new TelemetryDeduplicator({ maxEntries: Infinity, windowMs: 1 })).toThrow(TypeError);
|
|
expect(() => new TelemetryDeduplicator({ maxEntries: 1, windowMs: 0 })).toThrow(TypeError);
|
|
});
|
|
});
|