Files
co-lite/solvway-admin-portal/app/lib/services/notifications.test.ts
T
ngqileandClaude Opus 5.5 a1d13fe8d7 feat(notifications): header bell and notifications page
The bell polls the unread count every 30 s while the tab is visible. Opening it
loads the newest 10 and marks the unread ones read; items link only to paths on
this site. /notifications lists all with paging and Mark all read. Titles and
bodies render from message codes, in English and Chinese.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-02 20:14:48 +08:00

49 lines
2.2 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { noticeText, type AdminNotification } from "./notifications";
const t = (code: string, params?: Record<string, string | number>) => `${code}${params ? JSON.stringify(params) : ""}`;
const item = (over: Partial<AdminNotification>): 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");
});
});