import { beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("@/lib/api/auth-context", () => ({ parseServiceAuth: vi.fn(), assertCustomerMatch: vi.fn(), })); vi.mock("@/lib/api/rate-limit", () => ({ enforceRateLimits: vi.fn().mockResolvedValue(null), })); vi.mock("@/modules/mothership/checkout-service", () => ({ submitMsCheckoutFill: vi.fn(), })); import { parseServiceAuth, assertCustomerMatch } from "@/lib/api/auth-context"; import { POST } from "@/app/api/quotes/ms-checkout/route"; import { submitMsCheckoutFill } from "@/modules/mothership/checkout-service"; function authOk() { vi.mocked(parseServiceAuth).mockResolvedValue({ authType: "service", customerId: "CUST_001", permissions: [], } as never); vi.mocked(assertCustomerMatch).mockImplementation(() => undefined); } function jsonReq(body: unknown) { return new Request("http://localhost/api/quotes/ms-checkout", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body), }); } describe("POST /api/quotes/ms-checkout", () => { beforeEach(() => { vi.clearAllMocks(); authOk(); }); it("freight_protect 缺货值 → 400", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_carrier: "ABF Direct", coverage: "freight_protect", }), ); expect(res.status).toBe(400); }); it("成功入队", async () => { vi.mocked(submitMsCheckoutFill).mockResolvedValue({ quote_id: "q1", status: "processing", }); const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_carrier: "Roadrunner Interline", coverage: "freight_protect", cargo_value_usd: 5000, }), ); const body = await res.json(); expect(body.code).toBe(0); expect(body.data.status).toBe("processing"); expect(submitMsCheckoutFill).toHaveBeenCalled(); }); it("承运商过长 → 400", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_carrier: "x".repeat(129), coverage: "basic", }), ); expect(res.status).toBe(400); expect(submitMsCheckoutFill).not.toHaveBeenCalled(); }); it("mothership_details 任意键拒绝", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_carrier: "ABF", coverage: "basic", mothership_details: { evil: true, pickup: { hack: 1 } }, }), ); expect(res.status).toBe(400); expect(submitMsCheckoutFill).not.toHaveBeenCalled(); }); });