import { describe, expect, it } from "vitest"; import { DIAGNOSTIC_CONTEXT_ALLOWLIST, DIAGNOSTIC_CONTEXT_CARDINALITY, projectDiagnosticRecord, } from "../../src/contracts/diagnostics.ts"; import { TELEMETRY_ATTRIBUTE_ALLOWLIST } from "../../src/contracts/telemetry.ts"; describe("observability cardinality policy", () => { it("classifies every diagnostic context key exactly once", () => { expect(Object.keys(DIAGNOSTIC_CONTEXT_CARDINALITY).sort()).toEqual( [...DIAGNOSTIC_CONTEXT_ALLOWLIST].sort(), ); expect(DIAGNOSTIC_CONTEXT_CARDINALITY.correlation_id).toBe("request"); expect(DIAGNOSTIC_CONTEXT_CARDINALITY.route_id).toBe("topology"); expect(DIAGNOSTIC_CONTEXT_CARDINALITY.operation_id).toBe("topology"); expect(DIAGNOSTIC_CONTEXT_CARDINALITY.duration_bucket).toBe("bounded"); }); it("keeps request-cardinality keys out of telemetry dimensions", () => { for (const key of TELEMETRY_ATTRIBUTE_ALLOWLIST) { expect(DIAGNOSTIC_CONTEXT_CARDINALITY[key]).not.toBe("request"); } expect(TELEMETRY_ATTRIBUTE_ALLOWLIST).not.toContain("correlation_id"); }); it("rejects free-form values for bounded diagnostic dimensions", () => { expect( projectDiagnosticRecord({ level: "info", eventId: "http.request.completed", context: { outcome: "customer-123", http_status_group: "599", attempt_count_bucket: "37", duration_bucket: "12345ms", }, }), ).toEqual({ success: false, reason: "invalid-context" }); }); it("allows request correlation only in bounded diagnostics evidence", () => { const projected = projectDiagnosticRecord({ level: "info", eventId: "http.request.completed", context: { correlation_id: "request-a1b2c3", outcome: "success", http_status_group: "2xx", attempt_count_bucket: "1", duration_bucket: "lt100ms", }, }); expect(projected.success).toBe(true); }); });