import { beforeEach, describe, expect, it, vi } from "vitest"; const { findFirst } = vi.hoisted(() => ({ findFirst: vi.fn(), })); vi.mock("@/lib/prisma", () => ({ prisma: { businessCustomerUser: { findFirst, findMany: vi.fn(), }, }, })); import { resolveBusinessCustomerByAccount, resolveBusinessCustomerIdForQuote, } from "@/modules/customer/business-customer-user-service"; import { ValidationError } from "@/modules/quote/types"; describe("business-customer-user-service", () => { beforeEach(() => { findFirst.mockReset(); }); it("按账号命中业务客户", async () => { findFirst.mockResolvedValue({ customerId: "CUST_004", account: "demo_user", businessCustomer: { businessCustomerId: "bc-guid-1", externalCode: "76642888", name: "测试客户", }, }); const hit = await resolveBusinessCustomerByAccount("CUST_004", "demo_user"); expect(hit?.business_customer_id).toBe("bc-guid-1"); expect(hit?.external_code).toBe("76642888"); }); it("询价:已有 BC 不覆盖", async () => { const id = await resolveBusinessCustomerIdForQuote({ customerId: "CUST_004", businessCustomerId: "bc-explicit", businessUserAccount: "any", }); expect(id).toBe("bc-explicit"); expect(findFirst).not.toHaveBeenCalled(); }); it("询价:无 BC + 账号未命中 → ValidationError", async () => { findFirst.mockResolvedValue(null); await expect( resolveBusinessCustomerIdForQuote({ customerId: "CUST_004", businessUserAccount: "missing_user", }), ).rejects.toBeInstanceOf(ValidationError); }); it("询价:无 BC + 账号命中 → 回填 BC", async () => { findFirst.mockResolvedValue({ customerId: "CUST_004", account: "demo_user", businessCustomer: { businessCustomerId: "bc-guid-2", externalCode: "CM072", name: "OpenTrucking Inc", }, }); const id = await resolveBusinessCustomerIdForQuote({ customerId: "CUST_004", businessUserAccount: "demo_user", }); expect(id).toBe("bc-guid-2"); }); });