59 lines
3.1 KiB
TypeScript
59 lines
3.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { aggregate, choosePack, withPacks, type PlanIngredient } from "@/lib/aggregate";
|
||
|
||
const ing = (id: string, over: Partial<PlanIngredient> = {}): PlanIngredient => ({
|
||
ingredientId: id, name: id, unit: "g", qtyPerPortion: 100, category: "vegetables", packs: [], pantry: false, ...over,
|
||
});
|
||
const aisles = [
|
||
{ position: 1, name: "Obst & Gemüse", categories: ["vegetables", "fruit"] },
|
||
{ position: 3, name: "Fleisch", categories: ["meat"] },
|
||
{ position: 2, name: "Brot", categories: ["bakery"] },
|
||
];
|
||
|
||
describe("aggregate", () => {
|
||
it("sums the same ingredient over all slots and remembers the dishes", () => {
|
||
const list = aggregate(
|
||
[
|
||
{ dishTitle: "Bolognese", portions: 4, ingredients: [ing("hack", { category: "meat", qtyPerPortion: 120 }), ing("zwiebel", { unit: "piece", qtyPerPortion: 0.25 })] },
|
||
{ dishTitle: "Chili", portions: 2, ingredients: [ing("hack", { category: "meat", qtyPerPortion: 150 }), ing("zwiebel", { unit: "piece", qtyPerPortion: 0.5 })] },
|
||
],
|
||
aisles,
|
||
);
|
||
const hack = list.find((l) => l.ingredientId === "hack")!;
|
||
expect(hack.needed).toBe(780);
|
||
expect(hack.dishes).toEqual(["Bolognese", "Chili"]);
|
||
const zwiebel = list.find((l) => l.ingredientId === "zwiebel")!;
|
||
expect(zwiebel.needed).toBe(2);
|
||
expect(zwiebel.qty).toBe(2);
|
||
});
|
||
|
||
it("rounds pieces up — half an onion is not for sale", () => {
|
||
const [l] = aggregate([{ dishTitle: "Suppe", portions: 3, ingredients: [ing("ei", { unit: "piece", qtyPerPortion: 0.5 })] }], aisles);
|
||
expect(l!.qty).toBe(2);
|
||
});
|
||
|
||
it("sorts by the aisles of the main store, unknown categories last", () => {
|
||
const list = aggregate(
|
||
[{ dishTitle: "X", portions: 1, ingredients: [ing("salz", { category: "spices" }), ing("hack", { category: "meat" }), ing("brot", { category: "bakery" }), ing("apfel", { category: "fruit" })] }],
|
||
aisles,
|
||
);
|
||
expect(list.map((l) => l.ingredientId)).toEqual(["apfel", "brot", "hack", "salz"]);
|
||
expect(list.at(-1)!.aislePosition).toBeNull();
|
||
});
|
||
|
||
it("buys in packs: the smallest pack that covers it, or several", () => {
|
||
expect(choosePack(480, [250, 500, 1000])).toEqual({ count: 1, size: 500 });
|
||
expect(choosePack(1200, [250, 500])).toEqual({ count: 5, size: 250 }); // weniger Rest als 3 × 500
|
||
expect(choosePack(4000, [250, 500])).toEqual({ count: 8, size: 500 }); // nie mehr als sechs kleine Packungen
|
||
expect(choosePack(700, [400])).toEqual({ count: 2, size: 400 });
|
||
expect(choosePack(100, [])).toBeNull();
|
||
const [l] = withPacks(aggregate([{ dishTitle: "X", portions: 4, ingredients: [ing("hack", { qtyPerPortion: 120 })] }], aisles), () => [500, 1000]);
|
||
expect(l).toMatchObject({ needed: 480, qty: 500, pack: { count: 1, size: 500 } });
|
||
});
|
||
|
||
it("keeps pantry staples marked for the pantry check", () => {
|
||
const list = aggregate([{ dishTitle: "X", portions: 2, ingredients: [ing("oel", { unit: "ml", qtyPerPortion: 15, pantry: true, category: "oil_vinegar" })] }], aisles);
|
||
expect(list[0]).toMatchObject({ pantry: true, needed: 30 });
|
||
});
|
||
});
|