113 lines
5.8 KiB
TypeScript
113 lines
5.8 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/wishes.ts und lib/rounds.ts gegen die Testdatenbank: Jetons und Vetos
|
|
// zählen auf dem Server, auch wenn zwei Tipps gleichzeitig ankommen.
|
|
const url = process.env.TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!url)("wishes", () => {
|
|
const client = new pg.Client({ connectionString: url });
|
|
const h = randomUUID();
|
|
let kai = "";
|
|
let maike = "";
|
|
let bolognese = "";
|
|
const dishes: string[] = [];
|
|
let wishes: typeof import("@/lib/wishes");
|
|
let rounds: typeof import("@/lib/rounds");
|
|
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;
|
|
const seeded = await seedHousehold(client, h, ingredient); // legt eine offene Runde und einen Wunsch an
|
|
kai = seeded.member;
|
|
bolognese = seeded.dish;
|
|
wishes = await import("@/lib/wishes");
|
|
rounds = await import("@/lib/rounds");
|
|
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 [title, trial] of [["Curry", false], ["Pizza", false], ["Suppe", false], ["Neues Ding", true]] as const) {
|
|
dishes.push((await c.query("insert into dishes (household_id, title, is_trial) values ($1, $2, $3) returning id", [h, title, trial])).rows[0].id);
|
|
}
|
|
});
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db.pool.end();
|
|
await client.end();
|
|
});
|
|
|
|
const round = () => as((c) => rounds.currentRound(c));
|
|
const budget = async (m: string) => as(async (c) => wishes.budget(c, (await rounds.currentRound(c))!.id, m));
|
|
|
|
it("counts tokens and refuses a wish beyond them", async () => {
|
|
expect(await budget(kai)).toEqual({ tokens: 2, tokensLeft: 1, vetos: 1, vetosLeft: 1 });
|
|
await as((c) => wishes.addWish(c, h, kai, dishes[0]!));
|
|
expect((await budget(kai)).tokensLeft).toBe(0);
|
|
await expect(as((c) => wishes.addWish(c, h, kai, dishes[1]!))).rejects.toMatchObject({ reason: "noTokens" });
|
|
});
|
|
|
|
it("a veto needs a free counter-suggestion from the pool, which costs no token", async () => {
|
|
const veto = (alt?: string) => as((c) => wishes.addWish(c, h, kai, dishes[1]!, { veto: true, alternativeDishId: alt }));
|
|
await expect(veto()).rejects.toMatchObject({ reason: "alternative" }); // ohne Gegenvorschlag
|
|
await expect(veto(dishes[0])).rejects.toMatchObject({ reason: "alternative" }); // schon gewünscht
|
|
await expect(veto(dishes[3])).rejects.toMatchObject({ reason: "alternative" }); // nur „Ausprobieren“
|
|
await expect(veto(dishes[1])).rejects.toMatchObject({ reason: "alternative" }); // das Gericht selbst
|
|
await veto(dishes[2]);
|
|
expect(await budget(kai)).toMatchObject({ tokensLeft: 0, vetosLeft: 0 });
|
|
const entries = await as(async (c) => wishes.roundWishes(c, (await rounds.currentRound(c))!.id));
|
|
expect(entries.find((e) => e.dishId === dishes[2])).toMatchObject({ memberId: kai, isVeto: false, viaVeto: true });
|
|
|
|
// Den Gegenvorschlag zurücknehmen beendet auch das Veto — und umgekehrt.
|
|
expect(await as((c) => wishes.removeWish(c, kai, dishes[2]!))).toBe(true);
|
|
expect((await budget(kai)).vetosLeft).toBe(1);
|
|
await veto(dishes[2]);
|
|
expect(await as((c) => wishes.removeWish(c, kai, dishes[1]!))).toBe(true);
|
|
const after = await as(async (c) => wishes.roundWishes(c, (await rounds.currentRound(c))!.id));
|
|
expect(after.some((e) => e.dishId === dishes[2])).toBe(false);
|
|
await veto(dishes[2]);
|
|
});
|
|
|
|
it("allows one veto, and taking a wish back frees its token", async () => {
|
|
await expect(as((c) => wishes.addWish(c, h, kai, bolognese, { veto: true, alternativeDishId: dishes[0] }))).rejects.toMatchObject({ reason: "noVetos" });
|
|
expect(await as((c) => wishes.removeWish(c, kai, dishes[0]!))).toBe(true);
|
|
expect((await budget(kai)).tokensLeft).toBe(1);
|
|
await expect(as((c) => wishes.addWish(c, h, kai, bolognese))).rejects.toMatchObject({ reason: "already" });
|
|
});
|
|
|
|
it("marks a dish wished by two people as favourite, and keeps note and cook", async () => {
|
|
await as((c) => wishes.addWish(c, h, maike, bolognese, { note: " Oma kommt ", wantsToCook: true }));
|
|
const entries = await as(async (c) => wishes.roundWishes(c, (await rounds.currentRound(c))!.id));
|
|
expect(wishes.favorites(entries).has(bolognese)).toBe(true);
|
|
expect(entries.find((e) => e.memberId === maike)).toMatchObject({ note: "Oma kommt", wantsToCook: true, memberName: "Maike" });
|
|
});
|
|
|
|
it("counts correctly when taps arrive at the same time", async () => {
|
|
await as((c) => c.query("delete from wishes where member_id = $1", [maike]));
|
|
const results = await Promise.allSettled(dishes.slice(0, 3).map((d) => as((c) => wishes.addWish(c, h, maike, d))));
|
|
expect(results.filter((r) => r.status === "fulfilled")).toHaveLength(2);
|
|
expect((await budget(maike)).tokensLeft).toBe(0);
|
|
});
|
|
|
|
it("the joker only draws from dishes to try", async () => {
|
|
expect(await as((c) => wishes.jokerDish(c, kai))).toMatchObject({ id: dishes[3], title: "Neues Ding" });
|
|
await as((c) => wishes.addWish(c, h, kai, dishes[3]!));
|
|
expect(await as((c) => wishes.jokerDish(c, kai))).toBeNull();
|
|
});
|
|
|
|
it("there is only one running round", async () => {
|
|
expect((await round())?.status).toBe("open");
|
|
await expect(
|
|
as((c) => rounds.openRound(c, h, kai, { shoppingDate: "2026-11-14", slotsCount: 5, defaultPortions: 4 })),
|
|
).rejects.toBeInstanceOf(rounds.RoundExists);
|
|
});
|
|
});
|