127 lines
7.2 KiB
TypeScript
127 lines
7.2 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", () => ({}));
|
|
|
|
// Löffel und Gebinde (26.11.39): Das Gericht merkt sich das eingegebene Maß, die
|
|
// Menge bleibt in der Basiseinheit. Gebindegröße: Produkt am Gericht, sonst
|
|
// Haushalt, sonst Vorgabe der Zutat — und kein Haushalt sieht die des anderen.
|
|
const url = process.env.TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!url)("kitchen units", () => {
|
|
const client = new pg.Client({ connectionString: url });
|
|
const a = randomUUID();
|
|
const b = randomUUID();
|
|
let member = "";
|
|
const ing: Record<string, string> = {};
|
|
let dishes: typeof import("@/lib/dishes");
|
|
let ingredients: typeof import("@/lib/ingredients");
|
|
let db: typeof import("@/lib/db");
|
|
|
|
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 ('olive_oil','chopped_tomatoes','onion','ground_beef')")).rows) ing[r.key] = r.id;
|
|
dishes = await import("@/lib/dishes");
|
|
ingredients = await import("@/lib/ingredients");
|
|
db = await import("@/lib/db");
|
|
member = (await seedHousehold(client, a, ing.ground_beef!)).member;
|
|
await seedHousehold(client, b, ing.ground_beef!);
|
|
// Das Fixture setzt eine Haushaltsgröße für Dosentomaten; hier gilt erst die Vorgabe.
|
|
for (const h of [a, b]) await db.withHousehold(h, (c) => c.query("delete from household_pack_sizes"));
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db.pool.end();
|
|
await client.end();
|
|
});
|
|
|
|
const dish = (list: { ingredientId: string; qty: number; unit: string; size?: number; product?: object | null }[], portions = 4) =>
|
|
({ title: "Tomatensoße", portions, base: null, tags: [], effortMinutes: 20, isTrial: false, ingredients: list, steps: [] }) as never;
|
|
|
|
it("keeps the typed spoon and can; amounts stay in base units", async () => {
|
|
const got = await db.withHousehold(a, async (c) => {
|
|
const id = await dishes.saveDish(c, a, member, dish([
|
|
{ ingredientId: ing.olive_oil!, qty: 2, unit: "tbsp" },
|
|
{ ingredientId: ing.chopped_tomatoes!, qty: 1, unit: "can" },
|
|
]));
|
|
return dishes.getDish(c, id, "de");
|
|
});
|
|
expect(got!.ingredients[0]).toMatchObject({ displayUnit: "tbsp", unit: "ml", qtyPerPortion: 7.5 });
|
|
expect(got!.ingredients[1]).toMatchObject({ displayUnit: "can", unit: "g", qtyPerPortion: 100, packKind: "can", packSize: 400 });
|
|
});
|
|
|
|
it("refuses a can for an ingredient that has none", async () => {
|
|
await expect(db.withHousehold(a, (c) => dishes.saveDish(c, a, member, dish([{ ingredientId: ing.onion!, qty: 1, unit: "can" }])))).rejects.toThrow();
|
|
});
|
|
|
|
it("household size beats the default, a product pack beats both; other households keep the default", async () => {
|
|
expect(await db.withHousehold(a, (c) => ingredients.setPackSize(c, a, ing.chopped_tomatoes!, 500))).toBe(true);
|
|
expect(await db.withHousehold(a, (c) => ingredients.setPackSize(c, a, ing.onion!, 500))).toBe(false);
|
|
const [house, product] = await db.withHousehold(a, async (c) => {
|
|
const one = await dishes.saveDish(c, a, member, dish([{ ingredientId: ing.chopped_tomatoes!, qty: 1, unit: "can" }]));
|
|
const two = await dishes.saveDish(c, a, member, dish([{ ingredientId: ing.chopped_tomatoes!, qty: 1, unit: "can", product: { name: "Mutti", pack: 280 } }]));
|
|
return [await dishes.getDish(c, one, "de"), await dishes.getDish(c, two, "de")];
|
|
});
|
|
expect(house!.ingredients[0]).toMatchObject({ qtyPerPortion: 125, packSize: 500 });
|
|
expect(product!.ingredients[0]).toMatchObject({ qtyPerPortion: 70, packSize: 280 });
|
|
const theirs = await db.withHousehold(b, (c) => ingredients.allIngredients(c));
|
|
expect(theirs.find((i) => i.id === ing.chopped_tomatoes)!.packSize).toBe(400);
|
|
});
|
|
|
|
it("any ingredient takes a pack once its size is known; the size is remembered (26.11.49)", async () => {
|
|
// Hackfleisch hat kein Gebinde: ohne Größe geht „1 Packung“ nicht …
|
|
await expect(db.withHousehold(a, (c) => dishes.saveDish(c, a, member, dish([{ ingredientId: ing.ground_beef!, qty: 1, unit: "pack" }])))).rejects.toThrow();
|
|
// … mit Größe schon, und der Haushalt merkt sie sich für das nächste Gericht.
|
|
const [first, second] = await db.withHousehold(a, async (c) => {
|
|
const one = await dishes.saveDish(c, a, member, dish([{ ingredientId: ing.ground_beef!, qty: 1, unit: "pack", size: 500 }], 2));
|
|
const two = await dishes.saveDish(c, a, member, dish([{ ingredientId: ing.ground_beef!, qty: 2, unit: "pack" }]));
|
|
return [await dishes.getDish(c, one, "de"), await dishes.getDish(c, two, "de")];
|
|
});
|
|
expect(first).toMatchObject({ recipePortions: 2 });
|
|
expect(first!.ingredients[0]).toMatchObject({ displayUnit: "pack", qtyPerPortion: 250, packSize: 500 });
|
|
expect(second!.ingredients[0]).toMatchObject({ displayUnit: "pack", qtyPerPortion: 250, packSize: 500 });
|
|
// Der andere Haushalt kennt die Größe nicht.
|
|
const theirs = await db.withHousehold(b, (c) => ingredients.allIngredients(c));
|
|
expect(theirs.find((i) => i.id === ing.ground_beef)!.sizes).toBeUndefined();
|
|
});
|
|
|
|
it("slices of a piece ingredient count as one piece each; a typed piece of it is no kitchen unit", async () => {
|
|
const got = await db.withHousehold(a, async (c) => {
|
|
const id = await dishes.saveDish(c, a, member, dish([
|
|
{ ingredientId: ing.onion!, qty: 2, unit: "slice" },
|
|
{ ingredientId: ing.onion!, qty: 3, unit: "piece" },
|
|
]));
|
|
return dishes.getDish(c, id, "de");
|
|
});
|
|
expect(got!.ingredients[0]).toMatchObject({ displayUnit: "slice", unit: "piece", qtyPerPortion: 0.5 });
|
|
expect(got!.ingredients[1]).toMatchObject({ displayUnit: null, unit: "piece", qtyPerPortion: 0.75 });
|
|
});
|
|
|
|
it("stock cubes in millilitres and yoghurt in millilitres (26.11.50)", async () => {
|
|
const [stock, yoghurt] = (await client.query("select id from ingredients where key in ('stock_cube', 'yogurt') order by key")).rows.map((r) => r.id as string);
|
|
const got = await db.withHousehold(a, async (c) => {
|
|
const id = await dishes.saveDish(c, a, member, dish([
|
|
{ ingredientId: stock!, qty: 750, unit: "ml", size: 500 },
|
|
{ ingredientId: yoghurt!, qty: 200, unit: "ml" },
|
|
], 2));
|
|
return dishes.getDish(c, id, "de");
|
|
});
|
|
expect(got!.ingredients[0]).toMatchObject({ displayUnit: "ml", unit: "piece", qtyPerPortion: 0.75, packSize: 500 });
|
|
expect(got!.ingredients[1]).toMatchObject({ displayUnit: "ml", unit: "g", qtyPerPortion: 100 });
|
|
const mine = await db.withHousehold(a, (c) => ingredients.allIngredients(c));
|
|
expect(mine.find((i) => i.id === stock)!.sizes).toMatchObject({ ml: 500 });
|
|
});
|
|
|
|
it("an old dish without a typed unit shows spoons where they fit", async () => {
|
|
const got = await db.withHousehold(a, async (c) => {
|
|
const id = await dishes.saveDish(c, a, member, dish([{ ingredientId: ing.olive_oil!, qty: 30, unit: "ml" }]));
|
|
await c.query("update dish_ingredients set display_unit = null where dish_id = $1", [id]);
|
|
return dishes.getDish(c, id, "de");
|
|
});
|
|
expect(got!.ingredients[0]!.displayUnit).toBe("tbsp");
|
|
});
|
|
});
|