import { describe, it, expect } from "vitest"; import { noticeText, type AdminNotification } from "./notifications"; const t = (code: string, params?: Record) => `${code}${params ? JSON.stringify(params) : ""}`; const item = (over: Partial): AdminNotification => ({ id: 1, title: "NoticeLeadAssigned", message: null, type: "LEAD_ASSIGNED", link: "/leads/9", seen: false, createDateTime: "2026-12-02T09:00:00", ...over, }); // add-lead-notifications: rows store codes, so the reader's language decides the text. describe("noticeText", () => { it("renders the title code and the body code with its parameters", () => { expect(noticeText(item({ message: '{"code":"NoticeLeadBody","params":{"lead":"Ali","event":"Expo"}}' }), t)).toEqual({ title: "NoticeLeadAssigned", body: 'NoticeLeadBody{"lead":"Ali","event":"Expo"}', }); }); it("shows no body for a missing or unreadable message, never the raw JSON", () => { expect(noticeText(item({ message: null }), t).body).toBe(""); expect(noticeText(item({ message: "{not json" }), t).body).toBe(""); expect(noticeText(item({ message: '{"params":{}}' }), t).body).toBe(""); }); it("keeps only string and number parameters", () => { expect(noticeText(item({ message: '{"code":"B","params":{"a":1,"b":null,"c":{"x":1}}}' }), t).body).toBe('B{"a":1}'); }); }); describe("isSitePath", () => { it("accepts a path on this site only", async () => { const { isSitePath } = await import("./notifications"); expect(isSitePath("/leads/1")).toBe(true); expect(isSitePath("/whatsapp?c=x")).toBe(true); for (const bad of [null, "", "//evil.example", "/\\evil.example", "https://evil.example", "javascript:alert(1)"]) { expect(isSitePath(bad)).toBe(false); } }); }); describe("noticeTime", () => { it("formats in the stored locale and falls back to the raw value", async () => { const { noticeTime } = await import("./notifications"); expect(noticeTime("2026-12-02T09:00:00", "en_US")).toMatch(/2026/); expect(noticeTime("2026-12-02T09:00:00", "zh_CN")).toMatch(/2026/); expect(noticeTime("2026-12-02T09:00:00", "not a locale!")).toBe("2026-12-02T09:00:00"); }); });