Files
co-lite/solvway-admin-portal/app/api/whatsapp/[[...path]]/route.test.ts
T
ngqileandClaude Opus 5.5 1d5e792e3d feat: whatsapp portal proxy, client service and polling hook
Part of tasks 5.1/5.2 (not ticked): one catch-all proxy at /api/whatsapp (media
served with nosniff and a sandbox CSP), typed client calls, and usePolling that
pauses while the tab is hidden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-12-02 11:03:56 +08:00

73 lines
3.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), callMobileApiBinary: vi.fn(), getToken: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi, callMobileApiBinary: mocks.callMobileApiBinary }));
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
import { GET, POST } from "./route";
type Req = Parameters<typeof GET>[0];
const ctx = (...path: string[]) => ({ params: Promise.resolve({ path: path.length ? path : undefined }) });
const req = (url: string, init?: RequestInit) => new Request(`http://localhost${url}`, init) as unknown as Req;
beforeEach(() => {
mocks.callMobileApi.mockReset();
mocks.callMobileApiBinary.mockReset();
mocks.getToken.mockReset();
mocks.getToken.mockResolvedValue("jwt-abc");
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { ok: 1 } });
});
describe("/api/whatsapp/[[...path]]", () => {
it("returns 401 without a session", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await GET(req("/api/whatsapp/conversations"), ctx("conversations"))).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("forwards GETs with the query string", async () => {
await GET(req("/api/whatsapp/conversations?tab=mine&q=ais"), ctx("conversations"));
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/whatsapp/conversations?tab=mine&q=ais", { token: "jwt-abc" });
});
it("forwards POST bodies", async () => {
await POST(req("/api/whatsapp/conversations/c1/messages", { method: "POST", body: JSON.stringify({ text: "hi" }) }), ctx("conversations", "c1", "messages"));
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/whatsapp/conversations/c1/messages", { method: "POST", body: { text: "hi" }, token: "jwt-abc" });
});
it("answers an empty upstream body as JSON null", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: undefined });
const res = await POST(req("/api/whatsapp/conversations/c1/read", { method: "POST" }), ctx("conversations", "c1", "read"));
expect(res.status).toBe(200);
expect(await res.json()).toBeNull();
});
it("rejects dot segments", async () => {
expect((await GET(req("/api/whatsapp/x"), ctx("..", "users"))).status).toBe(400);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("passes upstream errors with their code", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "The 24-hour window has closed. Send a template.", status: 409, code: "WINDOW_CLOSED" });
const res = await POST(req("/api/whatsapp/conversations/c1/messages", { method: "POST", body: "{}" }), ctx("conversations", "c1", "messages"));
expect(res.status).toBe(409);
expect(((await res.json()) as Record<string, unknown>).code).toBe("WINDOW_CLOSED");
});
it("serves media as bytes that cannot run as a page", async () => {
mocks.callMobileApiBinary.mockResolvedValue({ ok: true, data: { body: new Uint8Array([1, 2]).buffer, contentType: "text/html", contentDisposition: "attachment" } });
const res = await GET(req("/api/whatsapp/media/m1"), ctx("media", "m1"));
expect(mocks.callMobileApiBinary).toHaveBeenCalledWith("/api/admin/whatsapp/media/m1", { token: "jwt-abc" });
expect(res.headers.get("Content-Type")).toBe("text/html");
expect(res.headers.get("X-Content-Type-Options")).toBe("nosniff");
expect(res.headers.get("Content-Security-Policy")).toBe("sandbox");
expect(res.headers.get("Content-Disposition")).toBe("attachment");
expect(res.headers.get("Cache-Control")).toBe("private, no-store");
});
it("passes a media 410 through", async () => {
mocks.callMobileApiBinary.mockResolvedValue({ ok: false, error: "Media expired", status: 410, code: "MEDIA_EXPIRED" });
expect((await GET(req("/api/whatsapp/media/m1"), ctx("media", "m1"))).status).toBe(410);
});
});