74 lines
4.1 KiB
TypeScript
74 lines
4.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { currentPrices, withGroupPrices, 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);
|
||
});
|
||
});
|
||
|
||
describe("group prices (26.11.38)", () => {
|
||
const p = (ingredientId: string, pricePerUnit: number, source: "receipt" | "default", observedAt = "2026-11-01"): PricePoint => ({
|
||
ingredientId, pricePerUnit, unit: "g", source, observedAt, mainStore: false,
|
||
});
|
||
it("a group without a better price takes the mean of its kinds' best prices", () => {
|
||
const prices = new Map([["pasta", p("pasta", 0.003, "default")], ["spaghetti", p("spaghetti", 0.0036, "receipt", "2026-11-06")], ["penne", p("penne", 0.0024, "receipt")], ["fusilli", p("fusilli", 0.01, "default")]]);
|
||
const out = withGroupPrices(prices, new Map([["pasta", ["spaghetti", "penne", "fusilli"]]]));
|
||
expect(out.get("pasta")).toMatchObject({ source: "receipt", observedAt: "2026-11-06" });
|
||
expect(out.get("pasta")!.pricePerUnit).toBeCloseTo(0.003, 6);
|
||
});
|
||
it("keeps a group's own receipt price", () => {
|
||
const prices = new Map([["pasta", p("pasta", 0.005, "receipt")], ["spaghetti", p("spaghetti", 0.002, "receipt")]]);
|
||
expect(withGroupPrices(prices, new Map([["pasta", ["spaghetti"]]])).get("pasta")!.pricePerUnit).toBe(0.005);
|
||
});
|
||
});
|