Files
errors.bitreport.pt/ingest-api/src/lib/normalize.test.ts
T
David Santos c4e0b03ed8 ingest-api: versioned noise rules and CRM-style fingerprint normalization
- Add NOISE_RULESET_VERSION 1.0.0 with CRM/WDMI parity (external resource,
  ResizeObserver, WDMI media prefix, stale-bundle and transient JS strings).
- Skip ingest for matched noise; never suppress when level maps to critical.
- FINGERPRINT_POLICY_VERSION 1: NFKC/whitespace-normalized first line plus
  project_slug|message|file|line basis (SHA-256) when client omits fingerprint.
- Response includes noise_filtered_count and policy version fields; OpenAPI
  documents stored vs skipped result shapes.

Paperclip-Issue: BIT-727
Paperclip-Run-Id: ae412312-f71a-4efd-93e0-ca7289c93362
Made-with: Cursor
2026-04-02 10:55:11 +00:00

63 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
computeFingerprint,
incidentTitleFromMessage,
normalizeLevelToSeverity,
parseOccurredAt,
} from "./normalize.js";
describe("normalizeLevelToSeverity", () => {
it("maps common levels", () => {
expect(normalizeLevelToSeverity("error")).toBe("high");
expect(normalizeLevelToSeverity("warn")).toBe("medium");
expect(normalizeLevelToSeverity("info")).toBe("info");
expect(normalizeLevelToSeverity("debug")).toBe("low");
expect(normalizeLevelToSeverity("fatal")).toBe("critical");
});
});
describe("computeFingerprint", () => {
it("uses client fingerprint when provided", () => {
expect(
computeFingerprint({
clientFingerprint: "my-fp",
message: "x",
}),
).toBe("my-fp");
});
it("hashes when fingerprint missing (CRM-style basis)", () => {
const _ctx = { project_slug: "acme", file_path: "/src/a.php", line_number: 9 };
const _a = computeFingerprint({ message: "same", context: _ctx });
const _b = computeFingerprint({ message: "same", context: _ctx });
expect(_a).toBe(_b);
expect(_a).toMatch(/^[a-f0-9]{64}$/);
});
it("collapses whitespace in message for fingerprint", () => {
const _ctx = { file_path: "x", line_number: 0 };
expect(computeFingerprint({ message: "a b", context: _ctx })).toBe(
computeFingerprint({ message: "a b", context: _ctx }),
);
});
});
describe("incidentTitleFromMessage", () => {
it("takes first line and truncates", () => {
expect(incidentTitleFromMessage("one\ntwo")).toBe("one");
const _long = "x".repeat(400);
expect(incidentTitleFromMessage(_long).length).toBe(300);
});
});
describe("parseOccurredAt", () => {
it("accepts ISO strings", () => {
const _ms = parseOccurredAt("2026-03-22T04:00:00.000Z");
expect(_ms).toBe(Date.parse("2026-03-22T04:00:00.000Z"));
});
it("rejects invalid", () => {
expect(() => parseOccurredAt("not-a-date")).toThrow("invalid_occurred_at");
});
});