import { describe, expect, it } from "vitest";
import { computeCartTotals } from "@/lib/cart-totals";

describe("computeCartTotals", () => {
  it("sums lines with quantities", () => {
    expect(computeCartTotals([{ unitPrice: 850000, quantity: 1 }, { unitPrice: 12000, quantity: 3 }]))
      .toEqual({ subtotal: 886000, discount: 0, shipping: 0, total: 886000 });
  });

  it("applies discount and shipping", () => {
    expect(computeCartTotals([{ unitPrice: 100000, quantity: 2 }], { discount: 15000, shipping: 4500 }))
      .toEqual({ subtotal: 200000, discount: 15000, shipping: 4500, total: 189500 });
  });

  it("caps discounts at the subtotal and never returns negative shipping", () => {
    expect(computeCartTotals([{ unitPrice: 5000, quantity: 1 }], { discount: 999999, shipping: -100 }))
      .toEqual({ subtotal: 5000, discount: 5000, shipping: 0, total: 0 });
  });

  it("ignores negative discounts", () => {
    expect(computeCartTotals([{ unitPrice: 5000, quantity: 1 }], { discount: -500 }))
      .toMatchObject({ discount: 0, total: 5000 });
  });
});
