import { describe, expect, it } from "vitest"; import { matchesRule, type PublishedEvent } from "./alerts"; import { inferCountry, inferLanguage, priorityFor } from "./registry"; const ev: PublishedEvent = { id: "evt_1", slug: "openai-pricing", type: "pricing_change", group: "commercial", title: "OpenAI: price changed $10 → $8", summary: "Input tokens now $8 per million.", importance: 84, signal: 88, confidence: 90, silent: true, evidence: "OBSERVED", firstParty: true, country: "US", source: { id: "openai", name: "OpenAI", domain: "openai.com", tier: "S" }, entities: [{ id: "org_openai", name: "OpenAI", type: "organization" }], categories: ["ai", "technology"], url: "https://openai.com/api/pricing/", detectedAt: new Date().toISOString(), }; describe("alert rules (spec §43)", () => { it("matches 'OpenAI changes pricing'", () => { expect(matchesRule({ entities: ["org_openai"], event_types: ["pricing_change"] }, ev)).toBe(true); }); it("matches importance thresholds and silent-only", () => { expect(matchesRule({ importance_min: 80 }, ev)).toBe(true); expect(matchesRule({ importance_min: 90 }, ev)).toBe(false); expect(matchesRule({ silent_only: true, categories: ["ai"] }, ev)).toBe(true); }); it("matches groups, countries, keywords and first-party", () => { expect(matchesRule({ groups: ["commercial"] }, ev)).toBe(true); expect(matchesRule({ groups: ["security"] }, ev)).toBe(false); expect(matchesRule({ countries: ["US"] }, ev)).toBe(true); expect(matchesRule({ keywords: ["million"] }, ev)).toBe(true); expect(matchesRule({ first_party_only: true, sources: ["openai"] }, ev)).toBe(true); expect(matchesRule({ confirmed_only: true, sources: ["openai"] }, ev)).toBe(false); }); it("rejects a rule without any positive constraint", () => { expect(matchesRule({}, ev)).toBe(false); expect(matchesRule({ first_party_only: true }, ev)).toBe(false); }); }); describe("registry inference", () => { it("infers country from unambiguous TLDs only", () => { expect(inferCountry("canada.ca", [])).toBe("CA"); expect(inferCountry("quebec.ca", [])).toBe("CA"); expect(inferCountry("gov.uk", [])).toBe("GB"); expect(inferCountry("legifrance.gouv.fr", [])).toBe("FR"); expect(inferCountry("cisa.gov", [])).toBe("US"); expect(inferCountry("europa.eu", [])).toBe("EU"); expect(inferCountry("who.int", [])).toBe("INT"); expect(inferCountry("openai.com", [])).toBeNull(); expect(inferCountry("example.io", [])).toBeNull(); }); it("infers language for francophone and other national domains", () => { expect(inferLanguage("quebec.ca")).toBe("fr"); expect(inferLanguage("spiegel.de")).toBe("de"); expect(inferLanguage("openai.com")).toBeNull(); }); it("assigns priority tiers", () => { expect(priorityFor("S", ["cloud"])).toBe(0); expect(priorityFor("A", ["cyber"])).toBe(0); expect(priorityFor("A", ["retail"])).toBe(1); expect(priorityFor("B", ["retail"])).toBe(2); expect(priorityFor("D", ["web-policy"])).toBe(3); }); });