import { describe, expect, it } from 'vitest' import { calculateCancellationPreview, resolveCancellationTicketPrice } from '@/services/bookings' describe('booking cancellation preview', () => { const startsAt = '2026-09-08T12:00:00.000Z' const policy = { cancellationFeePercent: 10, cancellationFeePercent12To24Hours: 10, cancellationFeePercentMoreThan24Hours: 10, } it('matches the backend floor-based commission formula', () => { // 7% mirrors the platform default (backend/src/common/constants/business.constants.ts#COMMISSION_PERCENT); // commissionPercent is always passed in from the booking's resolved rate, never hardcoded here. expect(calculateCancellationPreview(101_000, policy, 7, startsAt, new Date('2026-09-08T11:00:00Z'))).toEqual({ ticketPrice: 101_000, cancellationFeePercent: 10, platformCommission: 707, commissionPercent: 7, hostCancellationFee: 9_393, refundToGuest: 90_900, }) }) it('does not produce fractional Toman values', () => { // A different rate here (5%) than the test above (7%) is deliberate — it // exercises an event-level commission override, not just the platform default. expect( calculateCancellationPreview(999, { ...policy, cancellationFeePercent: 7 }, 5, startsAt, new Date('2026-09-08T11:00:00Z')) ).toEqual({ ticketPrice: 999, cancellationFeePercent: 7, platformCommission: 3, commissionPercent: 5, hostCancellationFee: 66, refundToGuest: 930, }) }) it('uses post-discount paid amount so fees match refund.service gross', () => { // List 10_000, discount 2_000 → paid 8_000; 10% penalty → refund 7_200. expect(calculateCancellationPreview(8_000, policy, 7, startsAt, new Date('2026-09-08T11:00:00Z'))).toEqual({ ticketPrice: 8_000, cancellationFeePercent: 10, platformCommission: 56, commissionPercent: 7, hostCancellationFee: 744, refundToGuest: 7_200, }) }) it('resolves ticket price from payableAmount when a discount was applied', () => { expect(resolveCancellationTicketPrice({ eventPrice: 10_000, payableAmount: 8_000 })).toBe(8_000) expect(resolveCancellationTicketPrice({ eventPrice: 10_000, payableAmount: null })).toBe(10_000) }) })