import { getPwnedMessage } from "../hibp"; import { clearCachedPolicy, setCachedPolicy } from "../hibp-cache"; beforeEach(() => { clearCachedPolicy(); }); describe("getPwnedMessage", () => { it("reports info when the lookup failed", () => { const r = getPwnedMessage({ isPwned: null, count: 0, error: "offline" }); expect(r.severity).toBe("info"); expect(r.title).toMatch(/Unable/i); }); it("reports info when the password is safe", () => { const r = getPwnedMessage({ isPwned: false, count: 0 }); expect(r.severity).toBe("info"); expect(r.title).toMatch(/Safe/i); }); it("reports warning by default on a breach hit (no enforcement)", () => { setCachedPolicy({ enabled: true, enforce: false, block_threshold: 0 }); const r = getPwnedMessage({ isPwned: true, count: 42 }); expect(r.severity).toBe("warning"); }); it("escalates to error when the policy enforces and count exceeds the threshold", () => { setCachedPolicy({ enabled: true, enforce: true, block_threshold: 100 }); const r = getPwnedMessage({ isPwned: true, count: 101 }); expect(r.severity).toBe("error"); expect(r.title).toMatch(/Compromised/i); }); it("stays at warning when the count is below the enforcement threshold", () => { setCachedPolicy({ enabled: true, enforce: true, block_threshold: 100 }); const r = getPwnedMessage({ isPwned: true, count: 50 }); expect(r.severity).toBe("warning"); }); });