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 { GET, POST, PUT } from "./route"; import { DEFAULT_MESSAGES } from "@/app/lib/i18n/defaultMessages"; type Req = Parameters[0]; const req = (url: string, init?: RequestInit) => new Request(url, init) as unknown as Req; const ctx = (path?: string[]) => ({ params: Promise.resolve({ path }) }); const json = (body: unknown, method: string) => ({ method, body: JSON.stringify(body) }); beforeEach(() => { mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: {} }); mocks.getToken.mockReset().mockResolvedValue("jwt"); }); describe("/api/messages", () => { it("returns 401 without a session", async () => { mocks.getToken.mockResolvedValue(null); expect((await GET(req("http://localhost/api/messages"), ctx())).status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("forwards only the allowed list filters, with page defaults", async () => { await GET(req("http://localhost/api/messages?searchKey=线索&missingLocale=zh_CN&area=AP&evil=1"), ctx()); expect(mocks.callMobileApi).toHaveBeenCalledWith( `/api/admin/messages?searchKey=${encodeURIComponent("线索")}&missingLocale=zh_CN&page=0&size=20`, { token: "jwt" }); }); it("own=1 searches the portal's own label codes, built on the server", async () => { await GET(req("http://localhost/api/messages?own=1&searchKey=lead&missingLocale=zh_CN&page=2&size=50&codes=Evil"), ctx()); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/search", { method: "POST", body: { searchKey: "lead", missingLocale: "zh_CN", page: 2, size: 50, codes: Object.keys(DEFAULT_MESSAGES) }, token: "jwt", }); }); it("own=1 without filters sends page defaults and no empty filters", async () => { await GET(req("http://localhost/api/messages?own=1"), ctx()); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/search", { method: "POST", body: { page: 0, size: 20, codes: Object.keys(DEFAULT_MESSAGES) }, token: "jwt", }); }); it("forwards detail and export reads", async () => { await GET(req("http://localhost/api/messages/7"), ctx(["7"])); expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/messages/7", { token: "jwt" }); await GET(req("http://localhost/api/messages/export"), ctx(["export"])); expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/messages/export", { token: "jwt" }); }); it("refuses unknown or nested paths", async () => { expect((await GET(req("http://localhost/api/messages/.."), ctx([".."]))).status).toBe(400); expect((await GET(req("http://localhost/api/messages/7/x"), ctx(["7", "x"]))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("PUT sends only the content to the path id", async () => { const res = await PUT(req("http://localhost/api/messages/7", json({ id: 9, code: "X", content: "{\"zh_CN\":\"线索\"}" }, "PUT")), ctx(["7"])); expect(res.status).toBe(200); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/7", { method: "PUT", body: { content: "{\"zh_CN\":\"线索\"}" }, token: "jwt" }); }); it("PUT needs a numeric id and a content string", async () => { expect((await PUT(req("http://localhost/api/messages/x", json({ content: "{}" }, "PUT")), ctx(["x"]))).status).toBe(400); expect((await PUT(req("http://localhost/api/messages/7", json({}, "PUT")), ctx(["7"]))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("POST is import only, with a non-empty array", async () => { expect((await POST(req("http://localhost/api/messages", json({ code: "A" }, "POST")), ctx())).status).toBe(400); expect((await POST(req("http://localhost/api/messages/import", json([], "POST")), ctx(["import"]))).status).toBe(400); const rows = [{ code: "Leads", content: "{}" }]; await POST(req("http://localhost/api/messages/import", json(rows, "POST")), ctx(["import"])); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/messages/import", { method: "POST", body: rows, token: "jwt" }); }); it("passes a refusal through with its code", async () => { mocks.callMobileApi.mockResolvedValue({ ok: false, status: 409, error: "MsgsCodeAreaLocked", code: "MsgsCodeAreaLocked" }); const res = await PUT(req("http://localhost/api/messages/7", json({ content: "{}" }, "PUT")), ctx(["7"])); expect(res.status).toBe(409); expect(await res.json()).toMatchObject({ code: "MsgsCodeAreaLocked" }); }); });