import { describe, it, expect } from "vitest"; import { invoiceTotalWithVat } from "../services/invoices.service"; describe("invoiceTotalWithVat", () => { it("calculates subtotal without VAT when apply_vat is false", () => { const inv = { apply_vat: false, vat_rate: { toNumber: () => 21 }, currency: "CZK", invoice_items: [ { quantity: { toNumber: () => 2 }, unit_price: { toNumber: () => 100 }, vat_rate: { toNumber: () => 21 }, }, ], }; expect(invoiceTotalWithVat(inv)).toBe(200); }); it("rounds each line VAT to 2 decimals before accumulation", () => { // 3 items @ 33.33 with 21% VAT // Line VAT = 33.33 * 0.21 = 6.9993 -> rounded to 7.00 // Total VAT = 7.00 * 3 = 21.00 // Subtotal = 33.33 * 3 = 99.99 // Total = 99.99 + 21.00 = 120.99 const inv = { apply_vat: true, vat_rate: { toNumber: () => 21 }, currency: "CZK", invoice_items: Array.from({ length: 3 }, () => ({ quantity: { toNumber: () => 1 }, unit_price: { toNumber: () => 33.33 }, vat_rate: { toNumber: () => 21 }, })), }; expect(invoiceTotalWithVat(inv)).toBe(120.99); }); it("handles null quantity and unit_price gracefully", () => { const inv = { apply_vat: true, vat_rate: { toNumber: () => 21 }, currency: "CZK", invoice_items: [ { quantity: { toNumber: () => 0 }, unit_price: { toNumber: () => 0 }, vat_rate: { toNumber: () => 21 }, }, ], }; expect(invoiceTotalWithVat(inv)).toBe(0); }); });