122 lines
5.1 KiB
TypeScript
122 lines
5.1 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/dishes.ts gegen die Testdatenbank: Mengen werden pro Portion in der
|
|
// Basiseinheit gespeichert, Unpassendes abgewiesen, Bearbeiten ersetzt,
|
|
// Entfernen archiviert — und kein Haushalt sieht die Gerichte des anderen.
|
|
const url = process.env.TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!url)("dishes", () => {
|
|
const client = new pg.Client({ connectionString: url });
|
|
const a = randomUUID();
|
|
const b = randomUUID();
|
|
let member = "";
|
|
let lib: typeof import("@/lib/dishes");
|
|
const ing: Record<string, string> = {};
|
|
|
|
async function as<T>(h: string, fn: () => Promise<T>): Promise<T> {
|
|
await client.query("begin");
|
|
try {
|
|
await client.query("select set_config('app.household_id', $1, true)", [h]);
|
|
const r = await fn();
|
|
await client.query("commit");
|
|
return r;
|
|
} catch (e) {
|
|
await client.query("rollback");
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
process.env.DATABASE_URL = url;
|
|
await client.connect();
|
|
for (const r of (await client.query("select id, key from ingredients where key in ('ground_beef','olive_oil','egg')")).rows) ing[r.key] = r.id;
|
|
lib = await import("@/lib/dishes");
|
|
member = (await seedHousehold(client, a, ing.ground_beef!)).member;
|
|
await seedHousehold(client, b, ing.ground_beef!);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
(await import("@/lib/db")).pool.end();
|
|
await client.end();
|
|
});
|
|
|
|
const input = (over: Partial<import("@/lib/dishes").DishInput> = {}) => ({
|
|
title: "Hack-Pfanne",
|
|
portions: 4,
|
|
base: null,
|
|
tags: [],
|
|
effortMinutes: 20,
|
|
isTrial: false,
|
|
ingredients: [
|
|
{ ingredientId: ing.ground_beef!, qty: 0.5, unit: "kg" as const },
|
|
{ ingredientId: ing.olive_oil!, qty: 2, unit: "tbsp" as const },
|
|
{ ingredientId: ing.egg!, qty: 2, unit: "piece" as const },
|
|
],
|
|
steps: [{ text: "Alles anbraten.", timerMinutes: 10 }],
|
|
...over,
|
|
});
|
|
|
|
it("stores amounts per portion in the base unit", async () => {
|
|
const id = await as(a, () => lib.saveDish(client as never, a, member, input()));
|
|
const dish = await as(a, () => lib.getDish(client as never, id, "de"));
|
|
expect(dish!.ingredients.map((i) => [i.unit, i.qtyPerPortion])).toEqual([
|
|
["g", 125],
|
|
["ml", 7.5],
|
|
["piece", 0.5],
|
|
]);
|
|
expect(dish!.steps).toEqual([{ section: "cook", text: "Alles anbraten.", timerSeconds: 600, ingredientIds: [] }]);
|
|
});
|
|
|
|
it("keeps the product of an ingredient; its cooking time becomes the timer of the step that cooks it", async () => {
|
|
const id = await as(a, () =>
|
|
lib.saveDish(
|
|
client as never,
|
|
a,
|
|
member,
|
|
input({
|
|
ingredients: [
|
|
{ ingredientId: ing.ground_beef!, qty: 0.5, unit: "kg", product: { name: "Bio-Rinderhack", pack: 0.4, cookMinutes: 8 } },
|
|
{ ingredientId: ing.egg!, qty: 2, unit: "piece" },
|
|
],
|
|
steps: [{ text: "Hackfleisch anbraten.", timerMinutes: 10 }, { text: "Eier dazugeben." }],
|
|
}),
|
|
),
|
|
);
|
|
const dish = await as(a, () => lib.getDish(client as never, id, "de"));
|
|
expect(dish!.ingredients[0]!.product).toEqual({ name: "Bio-Rinderhack", pack: 400, cookMinutes: 8 }); // 0,4 kg → 400 g
|
|
expect(dish!.ingredients[1]!.product).toBeNull();
|
|
expect(dish!.steps[0]).toMatchObject({ timerSeconds: 480, timerFrom: "Bio-Rinderhack" });
|
|
expect(dish!.steps[1]!.timerSeconds).toBeNull();
|
|
});
|
|
|
|
it("refuses an amount that does not fit the ingredient", async () => {
|
|
await expect(
|
|
as(a, () => lib.saveDish(client as never, a, member, input({ ingredients: [{ ingredientId: ing.egg!, qty: 200, unit: "g" }] }))),
|
|
).rejects.toBeInstanceOf(lib.InvalidAmount);
|
|
});
|
|
|
|
it("editing replaces ingredients and steps; archiving hides and restoring brings back", async () => {
|
|
const id = await as(a, () => lib.saveDish(client as never, a, member, input()));
|
|
await as(a, () => lib.saveDish(client as never, a, member, input({ title: "Neu", ingredients: [], steps: [] }), id));
|
|
const edited = await as(a, () => lib.getDish(client as never, id, "de"));
|
|
expect([edited!.title, edited!.ingredients.length, edited!.steps.length]).toEqual(["Neu", 0, 0]);
|
|
await as(a, () => lib.setArchived(client as never, id, true));
|
|
expect(await as(a, () => lib.getDish(client as never, id, "de"))).toBeNull();
|
|
expect((await as(a, () => lib.listDishes(client as never))).some((d) => d.id === id)).toBe(false);
|
|
await as(a, () => lib.setArchived(client as never, id, false));
|
|
expect((await as(a, () => lib.getDish(client as never, id, "de")))!.title).toBe("Neu");
|
|
});
|
|
|
|
it("another household can neither read nor change the dish", async () => {
|
|
const id = await as(a, () => lib.saveDish(client as never, a, member, input()));
|
|
expect(await as(b, () => lib.getDish(client as never, id, "de"))).toBeNull();
|
|
expect(await as(b, () => lib.setArchived(client as never, id, true))).toBe(false);
|
|
await expect(as(b, () => lib.saveDish(client as never, b, member, input(), id))).rejects.toThrow();
|
|
});
|
|
});
|