SPB Git forge

spb/websensor

Public
33commits 1branches 0releases
3.4 MBsize
maindefault branch
10 days agolast push
TypeScript 55.4% Python 43.2% SQL 1.2%
3.0 KB · 74 lines typescript
Raw Blame History
1import { describe, expect, it } from "vitest";2import { matchesRule, type PublishedEvent } from "./alerts";3import { inferCountry, inferLanguage, priorityFor } from "./registry";45const ev: PublishedEvent = {6  id: "evt_1",7  slug: "openai-pricing",8  type: "pricing_change",9  group: "commercial",10  title: "OpenAI: price changed $10 → $8",11  summary: "Input tokens now $8 per million.",12  importance: 84,13  signal: 88,14  confidence: 90,15  silent: true,16  evidence: "OBSERVED",17  firstParty: true,18  country: "US",19  source: { id: "openai", name: "OpenAI", domain: "openai.com", tier: "S" },20  entities: [{ id: "org_openai", name: "OpenAI", type: "organization" }],21  categories: ["ai", "technology"],22  url: "https://openai.com/api/pricing/",23  detectedAt: new Date().toISOString(),24};2526describe("alert rules (spec §43)", () => {27  it("matches 'OpenAI changes pricing'", () => {28    expect(matchesRule({ entities: ["org_openai"], event_types: ["pricing_change"] }, ev)).toBe(true);29  });30  it("matches importance thresholds and silent-only", () => {31    expect(matchesRule({ importance_min: 80 }, ev)).toBe(true);32    expect(matchesRule({ importance_min: 90 }, ev)).toBe(false);33    expect(matchesRule({ silent_only: true, categories: ["ai"] }, ev)).toBe(true);34  });35  it("matches groups, countries, keywords and first-party", () => {36    expect(matchesRule({ groups: ["commercial"] }, ev)).toBe(true);37    expect(matchesRule({ groups: ["security"] }, ev)).toBe(false);38    expect(matchesRule({ countries: ["US"] }, ev)).toBe(true);39    expect(matchesRule({ keywords: ["million"] }, ev)).toBe(true);40    expect(matchesRule({ first_party_only: true, sources: ["openai"] }, ev)).toBe(true);41    expect(matchesRule({ confirmed_only: true, sources: ["openai"] }, ev)).toBe(false);42  });43  it("rejects a rule without any positive constraint", () => {44    expect(matchesRule({}, ev)).toBe(false);45    expect(matchesRule({ first_party_only: true }, ev)).toBe(false);46  });47});4849describe("registry inference", () => {50  it("infers country from unambiguous TLDs only", () => {51    expect(inferCountry("canada.ca", [])).toBe("CA");52    expect(inferCountry("quebec.ca", [])).toBe("CA");53    expect(inferCountry("gov.uk", [])).toBe("GB");54    expect(inferCountry("legifrance.gouv.fr", [])).toBe("FR");55    expect(inferCountry("cisa.gov", [])).toBe("US");56    expect(inferCountry("europa.eu", [])).toBe("EU");57    expect(inferCountry("who.int", [])).toBe("INT");58    expect(inferCountry("openai.com", [])).toBeNull();59    expect(inferCountry("example.io", [])).toBeNull();60  });61  it("infers language for francophone and other national domains", () => {62    expect(inferLanguage("quebec.ca")).toBe("fr");63    expect(inferLanguage("spiegel.de")).toBe("de");64    expect(inferLanguage("openai.com")).toBeNull();65  });66  it("assigns priority tiers", () => {67    expect(priorityFor("S", ["cloud"])).toBe(0);68    expect(priorityFor("A", ["cyber"])).toBe(0);69    expect(priorityFor("A", ["retail"])).toBe(1);70    expect(priorityFor("B", ["retail"])).toBe(2);71    expect(priorityFor("D", ["web-policy"])).toBe(3);72  });73});74