Files
co-lite/solvway-admin-portal/app/api/locales/[[...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

50 lines
2.6 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/locales", () => {
it("returns 401 without a session", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await GET(req("http://localhost/api/locales"), ctx())).status).toBe(401);
});
it("lists", async () => {
await GET(req("http://localhost/api/locales"), ctx());
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/locales", { token: "jwt" });
});
it("creates with code and label only from the allowed fields", async () => {
expect((await POST(req("http://localhost/api/locales", json({ code: "ms_MY" }, "POST")), ctx())).status).toBe(400);
await POST(req("http://localhost/api/locales", json({ id: 3, code: " ms_MY ", label: "Bahasa", enabled: false, sortOrder: 3 }, "POST")), ctx());
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/locales",
{ method: "POST", body: { code: "ms_MY", label: "Bahasa", enabled: false, sortOrder: 3 }, token: "jwt" });
});
it("updates label, enabled and order at the path id; the code is never sent", async () => {
await PUT(req("http://localhost/api/locales/2", json({ id: 9, code: "zh_TW", enabled: false }, "PUT")), ctx(["2"]));
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/locales/2", { method: "PUT", body: { enabled: false }, token: "jwt" });
expect((await PUT(req("http://localhost/api/locales/x", json({ enabled: true }, "PUT")), ctx(["x"]))).status).toBe(400);
});
it("passes a refusal through with its code", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 409, error: "LocalesFallbackLocked", code: "LocalesFallbackLocked" });
const res = await PUT(req("http://localhost/api/locales/1", json({ enabled: false }, "PUT")), ctx(["1"]));
expect(res.status).toBe(409);
expect(await res.json()).toMatchObject({ code: "LocalesFallbackLocked" });
});
});