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/flock/checkout-service", () => ({ submitFlockCheckoutFill: vi.fn(), })); import { parseServiceAuth, assertCustomerMatch } from "@/lib/api/auth-context"; import { POST } from "@/app/api/quotes/flock-checkout/route"; import { submitFlockCheckoutFill } from "@/modules/flock/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/flock-checkout", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body), }); } describe("POST /api/quotes/flock-checkout", () => { beforeEach(() => { vi.clearAllMocks(); authOk(); }); it("缺 preferred_tier → 400", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", }), ); expect(res.status).toBe(400); }); it("缺 preferred_flexibility 且无 preferred_carrier → 400", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_tier: "flock_direct", }), ); expect(res.status).toBe(400); const body = await res.json(); expect(body.message).toMatch(/灵活性|承运商/); expect(submitFlockCheckoutFill).not.toHaveBeenCalled(); }); it("preferred_carrier 可入队", async () => { vi.mocked(submitFlockCheckoutFill).mockResolvedValue({ quote_id: "q1", status: "processing", }); const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_tier: "standard", preferred_carrier: "Forward Air", }), ); const body = await res.json(); expect(body.code).toBe(0); expect(submitFlockCheckoutFill).toHaveBeenCalledWith( expect.objectContaining({ preferredCarrier: "Forward Air" }), ); }); it("成功入队", async () => { vi.mocked(submitFlockCheckoutFill).mockResolvedValue({ quote_id: "q1", status: "processing", }); const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_tier: "flock_direct", preferred_flexibility: "2_day", }), ); const body = await res.json(); expect(body.code).toBe(0); expect(body.data.status).toBe("processing"); expect(submitFlockCheckoutFill).toHaveBeenCalled(); }); it("非法 tier → 400", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_tier: "express", preferred_flexibility: "none", }), ); expect(res.status).toBe(400); expect(submitFlockCheckoutFill).not.toHaveBeenCalled(); }); it("flock_details 任意键拒绝", async () => { const res = await POST( jsonReq({ customer_id: "CUST_001", quote_id: "q1", preferred_tier: "standard", preferred_flexibility: "1_day", flock_details: { evil: true, pickup: { hack: 1 } }, }), ); expect(res.status).toBe(400); expect(submitFlockCheckoutFill).not.toHaveBeenCalled(); }); });