Files
lunch/tests/suggest.test.ts
kai e6a6386541
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m5s
release / tag and release (push) Successful in 2s
26.11.30: Mein Gericht (#31)
2026-11-07 10:22:47 +00:00

145 lines
6.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { freshnessOf, spreadBases, suggest, type Candidate, type WishInput } from "@/lib/suggest";
const now = Date.parse("2026-11-06T12:00:00Z");
const daysAgo = (n: number) => new Date(now - n * 86_400_000).toISOString();
const dish = (id: string, over: Partial<Candidate> = {}): Candidate => ({
id, title: id, base: null, isTrial: false, shareUp: null, lastCooked: null, freshness: 0, ...over,
});
const wish = (dishId: string, memberName: string, over: Partial<WishInput> = {}): WishInput => ({
dishId, memberId: memberName.toLowerCase(), memberName, isVeto: false, wantsToCook: false, ...over,
});
describe("suggest", () => {
it("puts everyone's own dish first, and no veto removes it", () => {
const plan = suggest({
slots: 3,
candidates: [dish("pommes"), dish("b"), dish("c"), dish("d")],
wishes: [
wish("b", "Kai"),
wish("b", "Maike"),
wish("pommes", "Ben", { isMine: true }),
wish("pommes", "Lea", { isVeto: true }), // Geschwister
],
previousRound: new Set(["pommes"]),
now,
});
expect(plan.map((p) => p.dishId)).toContain("pommes");
expect(plan.find((p) => p.dishId === "pommes")!.reason).toEqual({ k: "mine", n: ["Ben"] });
expect(plan.map((p) => p.dishId)).toContain("b");
});
it("with too few slots, whoever went without last round comes first", () => {
const wishes = [wish("x", "Kai", { isMine: true }), wish("y", "Ben", { isMine: true }), wish("z", "Lea", { isMine: true })];
const run = (owed: string[]) =>
suggest({ slots: 2, candidates: [dish("x"), dish("y"), dish("z")], wishes, previousRound: new Set(), owed: new Set(owed), now }).map((p) => p.dishId).sort();
expect(run([])).toEqual(["x", "y"]); // in Reihenfolge des Markierens
expect(run(["lea"])).toEqual(["x", "z"]); // Lea ging leer aus, diesmal zuerst
});
it("an own dish already locked in the plan is not planned twice", () => {
const plan = suggest({ slots: 2, candidates: [dish("a"), dish("b"), dish("c")], wishes: [wish("a", "Ben", { isMine: true })], previousRound: new Set(), keep: ["a"], now });
expect(plan.map((p) => p.dishId)).not.toContain("a");
expect(plan).toHaveLength(1);
});
it("puts wishes first, favourites before single wishes, and names who wished", () => {
const plan = suggest({
slots: 3,
candidates: [dish("a"), dish("b"), dish("c"), dish("d")],
wishes: [wish("a", "Ben"), wish("b", "Kai"), wish("b", "Maike", { wantsToCook: true })],
previousRound: new Set(),
now,
});
expect(plan.map((p) => p.dishId).slice(0, 2)).toEqual(["b", "a"]);
expect(plan[0]!.reason).toEqual({ k: "wish", n: ["Kai", "Maike"] });
expect(plan[0]!.cookMemberId).toBe("maike");
expect(plan).toHaveLength(3);
});
it("respects vetoes and leaves out last round's dishes when filling", () => {
const plan = suggest({
slots: 3,
candidates: [dish("a"), dish("b"), dish("c"), dish("d"), dish("e", { isTrial: true })],
wishes: [wish("a", "Kai"), wish("a", "Ben", { isVeto: true })],
previousRound: new Set(["b"]),
now,
});
const ids = plan.map((p) => p.dishId);
expect(ids).not.toContain("a");
expect(ids).not.toContain("b");
expect(ids).not.toContain("e"); // „Ausprobieren“ nur über einen Wunsch
expect(ids.sort()).toEqual(["c", "d"]);
});
it("fills with liked dishes not had for a while, and says why", () => {
const plan = suggest({
slots: 2,
candidates: [
dish("recent", { shareUp: 1, lastCooked: daysAgo(3) }),
dish("loved", { shareUp: 1, lastCooked: daysAgo(40) }),
dish("meh", { shareUp: 0.2, lastCooked: daysAgo(80) }),
dish("never"),
],
wishes: [],
previousRound: new Set(),
now,
});
expect(plan.map((p) => p.dishId)).toEqual(["loved", "never"]);
expect(plan[0]!.reason).toEqual({ k: "popular" });
expect(plan[1]!.reason).toEqual({ k: "longAgo" });
});
it("orders fresh dishes first and keeps the same base apart", () => {
const plan = suggest({
slots: 4,
candidates: [
dish("pasta1", { base: "pasta" }),
dish("pasta2", { base: "pasta" }),
dish("fish", { base: "rice", freshness: 3 }),
dish("veg", { base: "potato", freshness: 2 }),
],
wishes: [wish("pasta1", "Kai"), wish("pasta2", "Ben"), wish("fish", "Maike"), wish("veg", "Ben")],
previousRound: new Set(),
now,
});
const ids = plan.map((p) => p.dishId);
expect(ids.slice(0, 2)).toEqual(["fish", "veg"]);
expect(ids.slice(2)).toEqual(["pasta1", "pasta2"].filter(Boolean)); // keine Alternative mehr: bleibt
const spread = spreadBases(["pasta", "pasta", "rice"], (b) => b);
expect(spread).toEqual(["pasta", "rice", "pasta"]);
});
it("does not exceed the slots and skips dishes already locked", () => {
const plan = suggest({ slots: 3, candidates: [dish("a"), dish("b"), dish("c")], wishes: [wish("a", "Kai")], previousRound: new Set(), keep: ["a"], now });
expect(plan.map((p) => p.dishId).sort()).toEqual(["b", "c"]);
expect(suggest({ slots: 1, candidates: [dish("a")], wishes: [], previousRound: new Set(), keep: ["a"], now })).toEqual([]);
});
it("reads freshness from ingredient categories", () => {
expect(freshnessOf(["pasta_rice", "fish"])).toBe(3);
expect(freshnessOf(["vegetables", "canned"])).toBe(2);
expect(freshnessOf(["canned", "spices"])).toBe(0);
});
});
describe("Kosten im Vorschlag", () => {
const c = (id: string, cls: "cheap" | "normal" | "expensive", shareUp = 0.8) => ({
id, title: id, base: null, isTrial: false, shareUp, lastCooked: null, freshness: 0, priceClass: cls,
});
it("nimmt höchstens ein teures Gericht je vier Slots", () => {
const out = suggest({ slots: 4, candidates: [c("a", "expensive", 1), c("b", "expensive", 1), c("c", "normal", 0.5), c("d", "normal", 0.5), c("e", "cheap", 0.5)], wishes: [], previousRound: new Set() });
expect(out.filter((p) => ["a", "b"].includes(p.dishId))).toHaveLength(1);
});
it("streicht teure Wünsche nie", () => {
const wishes = ["a", "b"].map((id) => ({ dishId: id, memberId: "m", memberName: "Mia", isVeto: false, wantsToCook: false }));
const out = suggest({ slots: 4, candidates: [c("a", "expensive"), c("b", "expensive"), c("x", "expensive", 1), c("d", "normal")], wishes, previousRound: new Set() });
expect(out.map((p) => p.dishId)).toEqual(expect.arrayContaining(["a", "b"]));
expect(out.map((p) => p.dishId)).not.toContain("x");
});
it("zieht bei gleicher Beliebtheit Günstiges vor und nennt es", () => {
const out = suggest({ slots: 1, candidates: [c("n", "normal", 0.6), c("g", "cheap", 0.6)], wishes: [], previousRound: new Set(), now: Date.parse("2026-11-07") });
expect(out[0]).toMatchObject({ dishId: "g" });
});
});