116 lines
5.4 KiB
TypeScript
116 lines
5.4 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import pg from "pg";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import { seedHousehold } from "./fixture";
|
|
|
|
vi.mock("server-only", () => ({}));
|
|
|
|
// lib/plan.ts gegen die Testdatenbank: Vorschlag aus Wünschen und Pool,
|
|
// gesperrte Slots bleiben, Streichen und Reihenfolge, Freigabe.
|
|
const url = process.env.TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!url)("plan", () => {
|
|
const client = new pg.Client({ connectionString: url });
|
|
const h = randomUUID();
|
|
const other = randomUUID();
|
|
let kai = "";
|
|
let maike = "";
|
|
const d: Record<string, string> = {};
|
|
let plan: typeof import("@/lib/plan");
|
|
let rounds: typeof import("@/lib/rounds");
|
|
let wishes: typeof import("@/lib/wishes");
|
|
let db: typeof import("@/lib/db");
|
|
|
|
const as = <T>(fn: (c: pg.PoolClient) => Promise<T>, household = h) => db.withHousehold(household, fn);
|
|
const round = () => as(async (c) => (await rounds.currentRound(c))!);
|
|
const slots = async () => as(async (c) => plan.planSlots(c, (await rounds.currentRound(c))!.id));
|
|
|
|
beforeAll(async () => {
|
|
process.env.DATABASE_URL = url;
|
|
await client.connect();
|
|
const ingredient = (await client.query("select id from ingredients limit 1")).rows[0].id;
|
|
kai = (await seedHousehold(client, h, ingredient)).member; // offene Runde, 4 Slots, ein gekochter Slot, ein Wunsch
|
|
await seedHousehold(client, other, ingredient);
|
|
plan = await import("@/lib/plan");
|
|
rounds = await import("@/lib/rounds");
|
|
wishes = await import("@/lib/wishes");
|
|
db = await import("@/lib/db");
|
|
await as(async (c) => {
|
|
maike = (await c.query("insert into members (household_id, display_name, role) values ($1, 'Maike', 'member') returning id", [h])).rows[0].id;
|
|
for (const t of ["Curry", "Pizza", "Suppe", "Fisch", "Auflauf"]) {
|
|
d[t] = (await c.query("insert into dishes (household_id, title) values ($1, $2) returning id", [h, t])).rows[0].id;
|
|
}
|
|
// Frische Runde ohne die Slots aus der Fixture.
|
|
await c.query("update rounds set status = 'done'");
|
|
await rounds.openRound(c, h, kai, { shoppingDate: "2026-11-14", slotsCount: 3, defaultPortions: 4 });
|
|
await wishes.addWish(c, h, maike, d.Curry!, { wantsToCook: true });
|
|
await wishes.addWish(c, h, kai, d.Curry!);
|
|
await wishes.addWish(c, h, kai, d.Pizza!, { veto: true });
|
|
});
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db.pool.end();
|
|
await client.end();
|
|
});
|
|
|
|
it("suggests from wishes first, with cook and reason, and leaves out vetoes", async () => {
|
|
await as(async (c) => plan.makeSuggestion(c, h, (await rounds.currentRound(c))!));
|
|
const s = await slots();
|
|
expect(s).toHaveLength(3);
|
|
expect(s.map((x) => x.position)).toEqual([1, 2, 3]);
|
|
const curry = s.find((x) => x.dishId === d.Curry)!;
|
|
expect(curry.reason).toEqual({ k: "wish", n: expect.arrayContaining(["Kai", "Maike"]) });
|
|
expect(curry.cookName).toBe("Maike");
|
|
expect(curry.portions).toBe(4);
|
|
expect(s.map((x) => x.dishId)).not.toContain(d.Pizza);
|
|
});
|
|
|
|
it("keeps locked slots when suggesting again", async () => {
|
|
const s = await slots();
|
|
const lockedOne = s.find((x) => x.dishId !== d.Curry)!;
|
|
await as(async (c) => plan.updateSlot(c, (await rounds.currentRound(c))!.id, lockedOne.id, { locked: true, portions: 6 }));
|
|
await as(async (c) => plan.makeSuggestion(c, h, (await rounds.currentRound(c))!));
|
|
const again = await slots();
|
|
expect(again).toHaveLength(3);
|
|
expect(again.find((x) => x.id === lockedOne.id)).toMatchObject({ locked: true, portions: 6, position: lockedOne.position });
|
|
expect(new Set(again.map((x) => x.dishId)).size).toBe(3);
|
|
});
|
|
|
|
it("removes a slot, renumbers, and reorders only with the round's own slots", async () => {
|
|
const before = await slots();
|
|
const r = await round();
|
|
expect(await as((c) => plan.removeSlot(c, r, before[0]!.id))).toBe(true);
|
|
const after = await slots();
|
|
expect(after.map((x) => x.position)).toEqual([1, 2]);
|
|
expect((await round()).slotsCount).toBe(2);
|
|
const reversed = [...after].reverse().map((x) => x.id);
|
|
expect(await as((c) => plan.renumber(c, r.id, reversed))).toBe(true);
|
|
expect((await slots()).map((x) => x.id)).toEqual(reversed);
|
|
expect(await as((c) => plan.renumber(c, r.id, [reversed[0]!, randomUUID()]))).toBe(false);
|
|
});
|
|
|
|
it("adds a dish as an extra slot and replaces a dish by hand", async () => {
|
|
const r = await round();
|
|
await as((c) => plan.addSlot(c, h, r, d.Auflauf!));
|
|
const s = await slots();
|
|
expect(s.at(-1)).toMatchObject({ dishId: d.Auflauf, position: 3, reason: { k: "manual" } });
|
|
expect((await round()).slotsCount).toBe(3);
|
|
await as((c) => plan.updateSlot(c, r.id, s[0]!.id, { dishId: d.Fisch! }));
|
|
expect((await slots())[0]).toMatchObject({ dishId: d.Fisch, reason: { k: "manual" } });
|
|
});
|
|
|
|
it("approves once, from open only, and closes wishing", async () => {
|
|
const r = await round();
|
|
expect(await as((c) => plan.approve(c, r, kai))).toBe(true);
|
|
expect((await round()).status).toBe("planned");
|
|
expect(await as((c) => plan.approve(c, r, kai))).toBe(false);
|
|
await expect(as((c) => wishes.addWish(c, h, maike, d.Suppe!))).rejects.toMatchObject({ reason: "noRound" });
|
|
});
|
|
|
|
it("another household sees none of it", async () => {
|
|
const theirs = await as(async (c) => (await c.query("select count(*)::int as n from slots where dish_id = any($1)", [Object.values(d)])).rows[0].n, other);
|
|
expect(theirs).toBe(0);
|
|
});
|
|
});
|