Files
lunch/tests/suggest.test.ts
T
kai 6f0097e0e6
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 3s
release / app (push) Successful in 56s
release / tag and release (push) Successful in 2s
Plan: rule-based suggestion, drag to reorder, lock, replace, approve (26.11.11) (#12)
2026-11-07 04:33:10 +00:00

93 lines
3.9 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 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);
});
});