import { beforeEach, describe, expect, it, vi } from "vitest"; import { GET } from "@/app/api/admin/markup-configs/route"; import { PUT } from "@/app/api/admin/markup-configs/[customer_id]/route"; import { resetServiceTokenCache } from "@/modules/auth/service-token"; vi.mock("@/lib/prisma", () => ({ prisma: { markupConfig: { findMany: vi.fn(), upsert: vi.fn(), }, hostCustomer: { findMany: vi.fn().mockResolvedValue([]), findFirst: vi.fn().mockResolvedValue(null), }, auditLog: { create: vi.fn(), }, }, })); import { prisma } from "@/lib/prisma"; const ADMIN_HEADERS = { authorization: "Bearer admin-jwt", "x-auth-type": "admin", "x-user-id": "admin_demo", "x-user-role": "admin", }; describe("admin markup-configs API", () => { beforeEach(() => { vi.clearAllMocks(); resetServiceTokenCache(); process.env.HOST_SERVICE_TOKENS = JSON.stringify({ "demo-host-token": { customerId: "CUST_001", permissions: ["pricing:markup:write"], }, }); process.env.CUSTOMER_REGISTRY = "CUST_002,CUST_003"; }); it("GET 返回注册客户合并配置", async () => { vi.mocked(prisma.markupConfig.findMany).mockResolvedValue([ { customerId: "CUST_002", markupType: "percent", markupPercent: 10, markupFixedAmount: null, operatorId: "admin_demo", remark: null, updatedAt: new Date("2026-06-15T10:00:00Z"), }, ] as never); const res = await GET( new Request("http://localhost/api/admin/markup-configs?page=1&size=10", { headers: ADMIN_HEADERS, }), ); const body = await res.json(); expect(res.status).toBe(200); expect(body.code).toBe(0); expect(body.data.total).toBe(3); expect(body.data.list[1].customer_id).toBe("CUST_002"); expect(body.data.list[1].markup_percent).toBe(10); }); it("PUT 固定金额加价成功", async () => { vi.mocked(prisma.markupConfig.upsert).mockResolvedValue({ customerId: "CUST_001", markupType: "fixed", markupPercent: 0, markupFixedAmount: 25, operatorId: "admin_demo", remark: "测试", updatedAt: new Date("2026-06-24T10:00:00Z"), } as never); const res = await PUT( new Request("http://localhost/api/admin/markup-configs/CUST_001", { method: "PUT", headers: { ...ADMIN_HEADERS, "Content-Type": "application/json" }, body: JSON.stringify({ markup_type: "fixed", markup_fixed_amount: 25, remark: "测试", }), }), { params: Promise.resolve({ customer_id: "CUST_001" }) }, ); const body = await res.json(); expect(res.status).toBe(200); expect(body.data.markup_type).toBe("fixed"); expect(body.data.markup_fixed_amount).toBe(25); }); it("PUT 未知客户 400", async () => { const res = await PUT( new Request("http://localhost/api/admin/markup-configs/CUST_999", { method: "PUT", headers: { ...ADMIN_HEADERS, "Content-Type": "application/json" }, body: JSON.stringify({ markup_percent: 5 }), }), { params: Promise.resolve({ customer_id: "CUST_999" }) }, ); const body = await res.json(); expect(res.status).toBe(400); expect(body.message).toBe("客户不存在"); }); });