Files
lunch/tests/cooking.test.ts
kai 0692914666
release / which version (push) Successful in 1s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m0s
release / tag and release (push) Successful in 3s
Cooking mode with timers and wake lock, ratings and thank-yous (26.11.13) (#14)
2026-11-07 05:06:07 +00:00

82 lines
4.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/cooking.ts gegen die Testdatenbank: Fertig, Ende der Runde, Bewertung,
// Dankeschön (nicht an sich selbst), offene Bewertungen, Rückmeldung an den Koch.
const url = process.env.TEST_DATABASE_URL;
describe.skipIf(!url)("cooking", () => {
const client = new pg.Client({ connectionString: url });
const h = randomUUID();
let kai = "";
let ben = "";
let roundId = "";
const slots: string[] = [];
let cooking: typeof import("@/lib/cooking");
let db: typeof import("@/lib/db");
const as = <T>(fn: (c: pg.PoolClient) => Promise<T>) => 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;
kai = (await seedHousehold(client, h, ingredient)).member;
cooking = await import("@/lib/cooking");
db = await import("@/lib/db");
await as(async (c) => {
await c.query("update rounds set status = 'done'");
ben = (await c.query("insert into members (household_id, display_name, role) values ($1, 'Ben', 'child') returning id", [h])).rows[0].id;
roundId = (await c.query("insert into rounds (household_id, slots_count, default_portions, status) values ($1, 2, 4, 'shopping') returning id", [h])).rows[0].id;
for (const [i, t] of ["Curry", "Pizza"].entries()) {
const d = (await c.query("insert into dishes (household_id, title) values ($1, $2) returning id", [h, t])).rows[0].id;
slots.push((await c.query("insert into slots (household_id, round_id, position, dish_id, portions) values ($1, $2, $3, $4, 4) returning id", [h, roundId, i + 1, d])).rows[0].id);
}
});
});
afterAll(async () => {
await db.pool.end();
await client.end();
});
it("marks a slot cooked once, makes the finisher the cook, and ends the round with the last slot", async () => {
expect(await as((c) => cooking.markCooked(c, slots[0]!, kai))).toBe(true);
expect(await as((c) => cooking.markCooked(c, slots[0]!, kai))).toBe(false);
const status = () => as(async (c) => (await c.query("select status from rounds where id = $1", [roundId])).rows[0].status);
expect(await status()).toBe("shopping");
expect(await as((c) => cooking.markCooked(c, slots[1]!, kai))).toBe(true);
expect(await status()).toBe("done");
expect(await as(async (c) => (await c.query("select cook_member_id from slots where id = $1", [slots[0]])).rows[0].cook_member_id)).toBe(kai);
});
it("rates once per person (a second rating replaces it) and lists what is left to rate", async () => {
const mine = async () => (await as((c) => cooking.pendingRatings(c, ben))).map((r) => r.slotId).filter((id) => slots.includes(id));
expect((await mine()).sort()).toEqual([...slots].sort()); // dazu kommt der gekochte Slot aus der Fixture
await as((c) => cooking.rate(c, h, slots[0]!, ben, false, "mehr Soße"));
await as((c) => cooking.rate(c, h, slots[0]!, ben, true, " doch lecker "));
const r = await as(async (c) => (await c.query("select thumbs_up, comment from ratings where slot_id = $1", [slots[0]])).rows);
expect(r).toEqual([{ thumbs_up: true, comment: "doch lecker" }]);
expect(await mine()).toEqual([slots[1]]);
});
it("thanks the cook once, never oneself, and the cook sees it", async () => {
await as((c) => cooking.thank(c, h, slots[0]!, ben));
await as((c) => cooking.thank(c, h, slots[0]!, ben));
await expect(as((c) => cooking.thank(c, h, slots[0]!, kai))).rejects.toThrow("ownDish");
const thanks = await as((c) => cooking.thanksForCook(c, kai));
expect(thanks.find((x) => x.slotId === slots[0])).toMatchObject({ kudos: 1, up: 1, down: 0, comments: ["doch lecker"] });
});
it("refuses ratings for what has not been cooked", async () => {
const open = await as(async (c) => {
const d = (await c.query("select dish_id from slots where id = $1", [slots[0]])).rows[0].dish_id;
return (await c.query("insert into slots (household_id, round_id, position, dish_id, portions) values ($1, $2, 3, $3, 2) returning id", [h, roundId, d])).rows[0].id;
});
await expect(as((c) => cooking.rate(c, h, open, ben, true, null))).rejects.toThrow("notCooked");
});
});