Files
co-lite/solvway-admin-portal/app/api/messages/[[...path]]/route.test.ts
T
ngqileandClaude Opus 5.5 a8bfcf5f6d feat(portal): Translations and Languages pages
Ported from babyboo and trimmed to back-office labels: list with search
and the Missing Chinese filter, edit panel with placeholder checks and an
unsaved-changes guard, export, import with preview and summary, and the
Languages page with guarded switches. A blank translation is no longer
treated as an old placeholder mismatch, so Save blocks a broken new one.

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

73 lines
3.9 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 { GET, POST, PUT } from "./route";
type Req = Parameters<typeof GET>[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("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" });
});
});