Kuma/tests/courses/seat-cancellation.test.ts
2026-10-02 01:28:49 +02:00

64 lines
2.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
resolveSeatCancellation,
seatShareCents,
} from "@/src/modules/courses/lib/seat-cancellation";
describe("seatShareCents", () => {
// 2 Plätze, fester Rabatt von 5,01 €: 52,99 € Kurs + 1,00 € Servicegebühr
const booking = {
amountPaid: 5399,
platformFee: 100,
platformFeeOnTop: true,
seats: 2,
seatsPurchased: 2,
};
it("teilt den Kursanteil auf die Plätze und gibt den Rundungsrest dem letzten", () => {
const first = seatShareCents(booking, 1);
const last = seatShareCents({ ...booking, seats: 1 }, 1);
expect(first).toEqual({ courseCents: 2649, feeCents: 50 });
expect(last).toEqual({ courseCents: 2650, feeCents: 50 });
// In Summe genau, was gezahlt wurde
expect(first.courseCents + last.courseCents).toBe(5299);
});
it("rechnet Altbuchungen ohne aufgeschlagene Gebühr ohne Gebührenanteil", () => {
const legacy = { ...booking, platformFeeOnTop: false, seats: 1, seatsPurchased: 1 };
expect(seatShareCents(legacy, 1)).toEqual({ courseCents: 5399, feeCents: 0 });
});
});
describe("resolveSeatCancellation", () => {
const withSpots = {
seats: 3,
seatsPurchased: 3,
spotIds: ["r1", "r2", "r3"],
spotLabels: ["Reformer 1", "Reformer 2", "Reformer 3"],
};
it("storniert ohne Auswahl alle Plätze", () => {
const result = resolveSeatCancellation(withSpots);
expect(result).toMatchObject({ ok: true, value: { count: 3, cancelsAll: true } });
});
it("gibt bei einer reinen Anzahl die zuletzt gebuchten Geräte frei", () => {
const result = resolveSeatCancellation(withSpots, { seats: 1 });
expect(result).toMatchObject({
ok: true,
value: { count: 1, cancelsAll: false, keptSpotIds: ["r1", "r2"] },
});
});
it("akzeptiert nur Geräte aus dieser Buchung", () => {
expect(resolveSeatCancellation(withSpots, { spotIds: ["r9"] }).ok).toBe(false);
expect(resolveSeatCancellation(withSpots, { spotIds: ["r1", "r9"] }).ok).toBe(false);
});
it("lehnt mehr Plätze ab, als gebucht sind", () => {
const noSpots = { seats: 2, seatsPurchased: 3, spotIds: [], spotLabels: [] };
expect(resolveSeatCancellation(noSpots, { seats: 3 }).ok).toBe(false);
expect(resolveSeatCancellation(noSpots, { seats: 0 }).ok).toBe(false);
});
});