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", () => ({})); vi.mock("next/headers", () => ({ cookies: async () => ({ get: () => undefined, has: () => false }) })); // lib/kitchen.ts: Was zeigt das Küchendisplay unter „Als Nächstes“? Die noch // nicht gekochten Gerichte der laufenden Runde in Reihenfolge — nichts, solange // die Runde noch gewünscht wird. const url = process.env.TEST_DATABASE_URL; describe.skipIf(!url)("kitchen display", () => { const client = new pg.Client({ connectionString: url }); const h = randomUUID(); let roundId = ""; let kitchen: typeof import("@/lib/kitchen"); let db: typeof import("@/lib/db"); const as = (fn: (c: pg.PoolClient) => Promise) => db.withHousehold(h, fn); beforeAll(async () => { process.env.DATABASE_URL = url; await client.connect(); const ingredient = (await client.query("select id from ingredients limit 1")).rows[0].id; await seedHousehold(client, h, ingredient); kitchen = await import("@/lib/kitchen"); db = await import("@/lib/db"); await as(async (c) => { await c.query("update rounds set status = 'done'"); roundId = (await c.query("insert into rounds (household_id, slots_count, default_portions, status) values ($1, 3, 4, 'open') returning id", [h])).rows[0].id; for (const [i, t] of ["Pizza", "Curry", "Suppe"].entries()) { const d = (await c.query("insert into dishes (household_id, title) values ($1, $2) returning id", [h, t])).rows[0].id; await c.query("insert into slots (household_id, round_id, position, dish_id, portions) values ($1, $2, $3, $4, $5)", [h, roundId, i + 1, d, 3 + i]); } }); }); afterAll(async () => { await client.end(); await db.pool.end(); }); it("shows nothing while the round is still being wished", async () => { expect(await as(kitchen.upcoming)).toEqual([]); }); it("lists the uncooked dishes of a planned round in order, with their portions", async () => { await as((c) => c.query("update rounds set status = 'shopping' where id = $1", [roundId])); expect((await as(kitchen.upcoming)).map((d) => [d.title, d.portions])).toEqual([["Pizza", 3], ["Curry", 4], ["Suppe", 5]]); await as((c) => c.query("update slots set cooked_at = now() where position = 1 and round_id = $1", [roundId])); expect((await as(kitchen.upcoming)).map((d) => d.title)).toEqual(["Curry", "Suppe"]); }); it("the overview carries every dish of the pool", async () => { const { dishes } = await as(kitchen.kitchenOverview); expect(dishes.map((d) => d.title)).toEqual(expect.arrayContaining(["Curry", "Pizza", "Suppe"])); }); });