Files
lunch/tests/pricing.test.ts
T
kai 6cfdac2041
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m4s
release / tag and release (push) Successful in 2s
Prices and costs per dish, round total with budget light (26.11.16) (#17)
2026-11-07 06:18:41 +00:00

58 lines
2.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { currentPrices, dishCost, expensiveAllowed, priceClasses, traffic, type PricePoint } from "@/lib/pricing";
const p = (id: string, price: number, over: Partial<PricePoint> = {}): PricePoint => ({
ingredientId: id, pricePerUnit: price, unit: "g", source: "default", observedAt: "2026-11-01", mainStore: false, ...over,
});
describe("pricing", () => {
it("takes the receipt from the main store before other receipts, own prices and defaults", () => {
const m = currentPrices([
p("hack", 0.008),
p("hack", 0.01, { source: "manual", observedAt: "2026-11-05" }),
p("hack", 0.012, { source: "receipt", observedAt: "2026-11-06" }),
p("hack", 0.009, { source: "receipt", observedAt: "2026-11-02", mainStore: true }),
]);
expect(m.get("hack")!.pricePerUnit).toBe(0.009);
const n = currentPrices([p("x", 1, { source: "manual", observedAt: "2026-01-01" }), p("x", 2, { source: "manual", observedAt: "2026-06-01" })]);
expect(n.get("x")!.pricePerUnit).toBe(2); // der jüngste
});
it("sums per portion, adds 5 % for staples, and counts receipt prices as confidence", () => {
const prices = currentPrices([p("hack", 0.01, { source: "receipt" }), p("nudeln", 0.002), p("oel", 0.01, { unit: "ml" })]);
const c = dishCost(
[
{ ingredientId: "hack", qtyPerPortion: 125, unit: "g", staple: false },
{ ingredientId: "nudeln", qtyPerPortion: 125, unit: "g", staple: false },
{ ingredientId: "oel", qtyPerPortion: 5, unit: "ml", staple: true },
{ ingredientId: "unbekannt", qtyPerPortion: 10, unit: "g", staple: false },
],
prices,
);
expect(c.perPortion).toBe(1.58); // (1,25 + 0,25) × 1,05
expect(c).toMatchObject({ priced: 2, total: 3, fromReceipts: 1, confidence: 1 });
expect(dishCost([{ ingredientId: "unbekannt", qtyPerPortion: 1, unit: "g", staple: false }], prices).perPortion).toBeNull();
// Einheit passt nicht (Preis je g, Rezept in Stück): nicht raten.
expect(dishCost([{ ingredientId: "hack", qtyPerPortion: 1, unit: "piece", staple: false }], prices).perPortion).toBeNull();
});
it("splits dishes into thirds of the household's own costs", () => {
const classes = priceClasses(new Map([["a", 1], ["b", 2], ["c", 3], ["d", 4], ["e", 5], ["f", 6], ["g", null]]));
expect(classes.get("a")).toBe("cheap");
expect(classes.get("c")).toBe("normal");
expect(classes.get("f")).toBe("expensive");
expect(classes.has("g")).toBe(false);
expect(priceClasses(new Map([["a", 1], ["b", 2]])).size).toBe(0);
});
it("shows the budget traffic light and allows one expensive dish per four slots", () => {
expect(traffic(50, 60)).toBe("green");
expect(traffic(66, 60)).toBe("yellow");
expect(traffic(70, 60)).toBe("red");
expect(traffic(70, null)).toBeNull();
expect(expensiveAllowed(5, 4)).toBe(1);
expect(expensiveAllowed(8, 4)).toBe(2);
expect(expensiveAllowed(2, 4)).toBe(1);
});
});