import { STANDARD_RATE, chargeForDistance, describeRate, pickRateCard } from '@/lib/orderPricing'; describe('order pricing', () => { it('charges ₹15 per km on the standard rate', () => { expect(chargeForDistance(1, STANDARD_RATE)).toBe(15); expect(chargeForDistance(10, STANDARD_RATE)).toBe(150); expect(chargeForDistance(3.4, STANDARD_RATE)).toBe(51); }); it('bills at least 1 km on the standard rate, so an order is never ₹0', () => { // A 0 finalprice makes the backend re-price from another client's card. expect(chargeForDistance(0, STANDARD_RATE)).toBe(15); expect(chargeForDistance(0.3, STANDARD_RATE)).toBe(15); expect(chargeForDistance(1.2, STANDARD_RATE)).toBe(18); }); it('ignores a card outside its effective dates, and keeps priority 0 as the top', () => { const now = Date.parse('2026-10-07T12:00:00Z'); const list = [ { pricingid: 1, tenantid: 5, priceperkm: 9, priority: 0, status: 'Active', effectiveto: '2026-09-30T00:00:00Z' }, { pricingid: 2, tenantid: 5, priceperkm: 20, priority: 1, status: 'Active' }, { pricingid: 3, tenantid: 5, priceperkm: 18, priority: 0, status: 'Active', effectivefrom: '2026-10-01T00:00:00Z' }, ]; expect(pickRateCard(list, 5, now).pricingid).toBe(3); }); it('keeps a rate card value of 0 as 0 — no made-up ₹40 base or 2 free km', () => { const card = { tenantid: 5, baseprice: 0, basedistance: 0, priceperkm: 15 }; expect(chargeForDistance(4, card)).toBe(60); // was 40 + (4 - 2) × 15 = 70 }); it('applies a base fee that covers the first km, then the per-km rate', () => { const card = { baseprice: 40, basedistance: 2, priceperkm: 15 }; expect(chargeForDistance(1.5, card)).toBe(40); expect(chargeForDistance(5, card)).toBe(85); }); it("uses the client's own active card, lowest priority first", () => { const list = [ { pricingid: 1, tenantid: 9, priceperkm: 99, status: 'Active' }, { pricingid: 2, tenantid: 5, priceperkm: 20, priority: 2, status: 'Active' }, { pricingid: 3, tenantid: 5, priceperkm: 18, priority: 1, status: 'Active' }, { pricingid: 4, tenantid: 5, priceperkm: 5, priority: 0, status: 'Inactive' }, ]; expect(pickRateCard(list, 5).pricingid).toBe(3); }); it("never prices a client off another client's card", () => { const list = [{ pricingid: 1, tenantid: 9, priceperkm: 99, status: 'Active' }]; expect(pickRateCard(list, 5)).toBe(STANDARD_RATE); expect(pickRateCard([], 5)).toBe(STANDARD_RATE); expect(pickRateCard(undefined, '')).toBe(STANDARD_RATE); }); it('falls back to a Doormile-wide card before the standard rate', () => { const list = [{ pricingid: 7, tenantid: 0, priceperkm: 12, status: 'Active' }]; expect(pickRateCard(list, 5).pricingid).toBe(7); }); it('labels the rate it used', () => { expect(describeRate(STANDARD_RATE)).toEqual({ base: 'No base fee', perKm: '₹15 / km (min 1 km)', source: 'Standard rate (no client rate card)', }); expect(describeRate({ baseprice: 40, basedistance: 2, priceperkm: 15 }).base).toBe('₹40 (first 2 km)'); }); });