You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { beforeEach, describe, expect, it, vi } from "vitest";
import {
applyMarkup,
applyMarkupToTier,
getMarkupPercent,
getMarkupRule,
} from "@/modules/pricing/engine";
vi.mock("@/lib/prisma", () => ({
prisma: {
markupConfig: {
findFirst: vi.fn(),
},
},
}));
import { prisma } from "@/lib/prisma";
const mockedFindFirst = vi.mocked(prisma.markupConfig.findFirst);
describe("applyMarkup", () => {
it("TC-103:raw_freight=320,surcharges=15,markup=10% → 32.00/367.00", () => {
const rawTotal = 320 + 15;
const result = applyMarkup(320, rawTotal, 10);
expect(result.markupAmount).toBe(32.0);
expect(result.finalTotal).toBe(367.0);
});
it("raw_freight=380,markup=10% → 38.00/418.00", () => {
const result = applyMarkup(380, 380, 10);
expect(result.markupAmount).toBe(38.0);
expect(result.finalTotal).toBe(418.0);
});
it("surcharge=0 仅对 raw_freight 加价", () => {
const result = applyMarkup(320, 320, 10);
expect(result.markupAmount).toBe(32.0);
expect(result.finalTotal).toBe(352.0);
});
it("0% 加价", () => {
const result = applyMarkup(320, 335, 0);
expect(result.markupAmount).toBe(0);
expect(result.finalTotal).toBe(335);
});
it("30% 边界", () => {
const result = applyMarkup(100, 100, 30);
expect(result.markupAmount).toBe(30);
expect(result.finalTotal).toBe(130);
});
it("0.01% 细粒度加价", () => {
const result = applyMarkup(1000, 1000, 0.01);
expect(result.markupAmount).toBe(0.1);
expect(result.finalTotal).toBe(1000.1);
});
it("固定金额加价", () => {
const result = applyMarkup(320, 335, {
type: "fixed",
percent: 0,
fixedAmount: 50,
});
expect(result.markupAmount).toBe(50);
expect(result.finalTotal).toBe(385);
});
});
describe("applyMarkupToTier", () => {
it("生成 breakdown 三项", () => {
const tier = applyMarkupToTier(
{
service_level: "standard",
rate_option: "lowest",
carrier: "Mothership",
transit_days: "5-7",
transit_description: "最低价格",
raw_freight: 320,
surcharges: 15,
raw_total: 335,
},
10,
);
expect(tier.breakdown).toHaveLength(3);
expect(tier.final_total).toBe(367);
});
});
describe("getMarkupPercent", () => {
beforeEach(() => {
mockedFindFirst.mockReset();
});
it("未传业务客户 → 0.0(租户不加价)", async () => {
expect(await getMarkupPercent("CUST_002")).toBe(0);
expect(mockedFindFirst).not.toHaveBeenCalled();
});
});
describe("getMarkupRule", () => {
beforeEach(() => {
mockedFindFirst.mockReset();
});
it("仅业务客户配置生效", async () => {
mockedFindFirst.mockResolvedValue({
markupType: "percent",
markupPercent: 5,
markupFixedAmount: null,
} as never);
const rule = await getMarkupRule("CUST_004", "BC_001");
expect(rule.percent).toBe(5);
expect(mockedFindFirst).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
customerId: "CUST_004",
businessCustomerId: "BC_001",
}),
}),
);
});
it("未配置业务客户加价 → 0", async () => {
mockedFindFirst.mockResolvedValue(null);
const rule = await getMarkupRule("CUST_004", "BC_001");
expect(rule.percent).toBe(0);
});
it("不传业务客户 → 直接 0,不回退租户配置", async () => {
const rule = await getMarkupRule("CUST_004");
expect(rule.percent).toBe(0);
expect(mockedFindFirst).not.toHaveBeenCalled();
});
});