import { Prisma } from '../src/generated/prisma/client'; import { calculatePriceCheck } from '../src/b2b/price-check'; import { priceCheckSchema, priceListItemSchema, } from '../src/b2b/price-list.schemas'; const first = '11111111-1111-4111-8111-111111111111'; const second = '22222222-2222-4222-8222-222222222222'; const list = { id: '33333333-3333-4333-8333-333333333333', currency: 'INR', items: [ { variantId: first, unitPrice: new Prisma.Decimal('12.35'), minQuantity: 10, }, { variantId: second, unitPrice: new Prisma.Decimal('0.10'), minQuantity: 2, }, ], }; describe('B2B price check', () => { it('adds exact decimal line totals without floating-point rounding', () => { expect( calculatePriceCheck(list, { lines: [ { variantId: first, quantity: 10 }, { variantId: second, quantity: 3 }, ], }), ).toEqual({ priceListId: list.id, currency: 'INR', merchandiseTotal: '123.80', lines: [ { variantId: first, quantity: 10, minQuantity: 10, unitPrice: '12.35', lineTotal: '123.50', }, { variantId: second, quantity: 3, minQuantity: 2, unitPrice: '0.10', lineTotal: '0.30', }, ], }); }); it('uses distinct errors for missing terms and unmet minimums', () => { expect(() => calculatePriceCheck(list, { lines: [{ variantId: first, quantity: 9 }] }), ).toThrow(expect.objectContaining({ code: 'B2B_PRICE_MINIMUM_QUANTITY' })); expect(() => calculatePriceCheck(list, { lines: [{ variantId: list.id, quantity: 10 }], }), ).toThrow(expect.objectContaining({ code: 'B2B_PRICE_ITEM_NOT_FOUND' })); }); it('rejects duplicate variant requests and invalid monetary inputs', () => { expect( priceCheckSchema.safeParse({ lines: [ { variantId: first, quantity: 10 }, { variantId: first, quantity: 20 }, ], }).success, ).toBe(false); expect( priceListItemSchema.safeParse({ unitPrice: '0.00', minQuantity: 1 }) .success, ).toBe(false); expect( priceListItemSchema.safeParse({ unitPrice: '1.001', minQuantity: 1 }) .success, ).toBe(false); expect( priceListItemSchema.safeParse({ unitPrice: '1.00', minQuantity: 0 }) .success, ).toBe(false); }); });