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
69 lines
3.3 KiB
TypeScript
69 lines
3.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import * as list from "./route";
|
|
import * as count from "./unread-count/route";
|
|
import * as readAll from "./read-all/route";
|
|
import * as readOne from "./[id]/read/route";
|
|
|
|
type Req = Parameters<typeof list.GET>[0];
|
|
const req = (url: string, init: RequestInit = {}) => new Request(`http://localhost${url}`, init) as unknown as Req;
|
|
const ctx = (id: string) => ({ params: Promise.resolve({ id }) });
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.getToken.mockResolvedValue("jwt");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { items: [], total: 0 } });
|
|
});
|
|
|
|
// add-lead-notifications: the bell's routes only ever act on the signed-in user's own notifications.
|
|
describe("notification routes", () => {
|
|
it("refuse without a session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
expect((await list.GET(req("/api/notifications"))).status).toBe(401);
|
|
expect((await count.GET()).status).toBe(401);
|
|
expect((await readAll.POST()).status).toBe(401);
|
|
expect((await readOne.POST(req("/api/notifications/1/read", { method: "POST" }), ctx("1"))).status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("list forwards only numeric page and size", async () => {
|
|
const res = await list.GET(req("/api/notifications?page=2&size=10&receiver=9&x=1"));
|
|
expect(res.status).toBe(200);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/notifications?page=2&size=10", { token: "jwt" });
|
|
|
|
await list.GET(req("/api/notifications?page=-1&size=abc"));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/notifications?", { token: "jwt" });
|
|
});
|
|
|
|
it("unread count and read-all call mobile-api", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { count: 3 } });
|
|
expect(await (await count.GET()).json()).toEqual({ count: 3 });
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/notifications/unread-count", { token: "jwt" });
|
|
|
|
await readAll.POST();
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/notifications/read-all", { method: "POST", token: "jwt" });
|
|
});
|
|
|
|
it("mark read takes a numeric id only and answers 204", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: undefined });
|
|
const ok = await readOne.POST(req("/api/notifications/42/read", { method: "POST" }), ctx("42"));
|
|
expect(ok.status).toBe(204);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/notifications/42/read", { method: "POST", token: "jwt" });
|
|
|
|
mocks.callMobileApi.mockClear();
|
|
const bad = await readOne.POST(req("/api/notifications/..%2Fx/read", { method: "POST" }), ctx("../x"));
|
|
expect(bad.status).toBe(400);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("pass an upstream failure through", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Not found", status: 404 });
|
|
const res = await readOne.POST(req("/api/notifications/7/read", { method: "POST" }), ctx("7"));
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|