95 lines
5.0 KiB
TypeScript
95 lines
5.0 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", () => ({}));
|
|
|
|
// Produktgruppen und eigene Zutaten (26.11.38): Pasta, Reis, Käse, Hülsenfrüchte
|
|
// sind Oberbegriffe mit Sorten. Eigene Zutaten sieht nur der eigene Haushalt,
|
|
// ein gemerktes Produkt gilt für alle Gerichte des Haushalts.
|
|
const url = process.env.TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!url)("ingredient groups", () => {
|
|
const client = new pg.Client({ connectionString: url });
|
|
const a = randomUUID();
|
|
const b = randomUUID();
|
|
let member = "";
|
|
const ing: Record<string, string> = {};
|
|
let ingredients: typeof import("@/lib/ingredients");
|
|
let dishes: typeof import("@/lib/dishes");
|
|
|
|
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 ('pasta','spaghetti','penne','gouda','ground_beef')")).rows) ing[r.key] = r.id;
|
|
ingredients = await import("@/lib/ingredients");
|
|
dishes = 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();
|
|
});
|
|
|
|
it("the seed knows the groups and their kinds", async () => {
|
|
const r = await client.query("select key from ingredients where parent_id = $1 order by key", [ing.pasta]);
|
|
expect(r.rows.map((x) => x.key)).toEqual(expect.arrayContaining(["spaghetti", "penne", "fusilli"]));
|
|
expect((await client.query("select is_group from ingredients where id = $1", [ing.pasta])).rows[0].is_group).toBe(true);
|
|
});
|
|
|
|
it("an own ingredient is seen only by its household and refuses duplicates and non-groups", async () => {
|
|
const name = `Gnocchi ${a.slice(0, 6)}`;
|
|
const own = await as(a, () => ingredients.createOwnIngredient(client as never, a, { name, unit: "g", parentId: ing.pasta!, category: "other" }));
|
|
expect(own).toMatchObject({ parentId: ing.pasta, own: true });
|
|
expect(own.category).toBe("pasta_rice");
|
|
const mine = await as(a, () => ingredients.allIngredients(client as never));
|
|
expect(mine.some((i) => i.id === own.id)).toBe(true);
|
|
const theirs = await as(b, () => ingredients.allIngredients(client as never));
|
|
expect(theirs.some((i) => i.id === own.id)).toBe(false);
|
|
await expect(as(a, () => ingredients.createOwnIngredient(client as never, a, { name: name.toUpperCase(), unit: "g", parentId: null, category: "other" }))).rejects.toThrow("exists");
|
|
await expect(as(a, () => ingredients.createOwnIngredient(client as never, a, { name: "Etwas", unit: "g", parentId: ing.spaghetti!, category: "other" }))).rejects.toThrow("group");
|
|
// Eine Gruppe anlegen darf ein Haushalt nicht, auch nicht an der Bibliothek vorbei.
|
|
await expect(
|
|
as(a, () => client.query("insert into ingredients (key, category, default_unit, names, household_id, is_group) values ($1, 'other', 'g', '{}', $2, true)", [`own_x${a.slice(0, 8)}`, a])),
|
|
).rejects.toThrow();
|
|
});
|
|
|
|
it("a remembered product is used by every other dish of the household, not by others", async () => {
|
|
const dish = (product: object | null) => ({
|
|
title: "Nudeln mit Soße",
|
|
portions: 4,
|
|
base: null,
|
|
tags: [],
|
|
effortMinutes: 20,
|
|
isTrial: false,
|
|
ingredients: [{ ingredientId: ing.spaghetti!, qty: 500, unit: "g" as const, product }],
|
|
steps: [{ text: "Nudeln kochen.", timerMinutes: null }],
|
|
});
|
|
await as(a, () => dishes.saveDish(client as never, a, member, dish({ name: "Barilla Spaghetti Nr. 5", pack: 500, cookMinutes: 9, remember: true }) as never));
|
|
const other = await as(a, () => dishes.saveDish(client as never, a, member, dish(null) as never));
|
|
const got = await as(a, () => dishes.getDish(client as never, other, "de"));
|
|
expect(got!.ingredients[0]!.product).toMatchObject({ name: "Barilla Spaghetti Nr. 5", fromHousehold: true, cookMinutes: 9 });
|
|
// Ein eigenes Produkt am Gericht schlägt das gemerkte.
|
|
const own = await as(a, () => dishes.saveDish(client as never, a, member, dish({ name: "De Cecco", pack: 500, cookMinutes: 11 }) as never));
|
|
expect((await as(a, () => dishes.getDish(client as never, own, "de")))!.ingredients[0]!.product).toMatchObject({ name: "De Cecco" });
|
|
expect((await as(a, () => dishes.getDish(client as never, own, "de")))!.ingredients[0]!.product!.fromHousehold).toBeFalsy();
|
|
expect((await as(b, () => client.query("select count(*)::int as n from household_products where ingredient_id = $1", [ing.spaghetti]))).rows[0].n).toBe(0);
|
|
});
|
|
});
|