Files
lunch/tests/templates.test.ts
kai 3093dc3897
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 53s
release / tag and release (push) Successful in 2s
Seed: ingredients, starters, store layouts; browser tests (26.11.5) (#6)
2026-11-06 21:30:09 +00:00

108 lines
5.4 KiB
TypeScript

import { randomUUID } from "node:crypto";
import pg from "pg";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
// @ts-expect-error — reines JavaScript ohne Typen
import { seed, stableId } from "../scripts/seed.mjs";
import { seedHousehold, seedIngredient } from "./fixture";
vi.mock("server-only", () => ({}));
// Vorlagen übernehmen (lib/starter.ts, lib/stores.ts) und das Einspielen der
// Vorlagen (scripts/seed.mjs), gegen die Testdatenbank mit zwei Haushalten.
const url = process.env.TEST_DATABASE_URL;
describe.skipIf(!url)("templates", () => {
const client = new pg.Client({ connectionString: url });
const a = randomUUID();
const b = randomUUID();
const starter = `de_test_${randomUUID().slice(0, 8)}`;
let memberA = "";
let starterLib: typeof import("@/lib/starter");
let storesLib: typeof import("@/lib/stores");
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 () => {
await client.connect();
starterLib = await import("@/lib/starter");
storesLib = await import("@/lib/stores");
const ingredient = await seedIngredient(client);
memberA = (await seedHousehold(client, a, ingredient)).member;
await seedHousehold(client, b, ingredient);
// Eine Vorlage nur für diesen Test, unabhängig vom Inhalt von db/seed.
await client.query("begin");
await client.query("select set_config('app.maintenance', 'on', true)");
await client.query("insert into starter_dishes (key, locale, position, title, base, tags, effort_minutes) values ($1, 'de', 999, 'Testgericht', 'pasta', '{quick}', 20)", [starter]);
await client.query("insert into starter_dish_ingredients (starter_key, position, ingredient_id, qty_per_portion, unit) values ($1, 0, $2, 125, 'g')", [starter, ingredient]);
await client.query("insert into starter_dish_steps (starter_key, position, text, timer_seconds) values ($1, 0, 'Wasser aufsetzen und salzen.', 600)", [starter]);
await client.query("commit");
});
afterAll(async () => {
await client.query("begin");
await client.query("select set_config('app.maintenance', 'on', true)");
await client.query("delete from starter_dishes where key = $1", [starter]);
await client.query("commit");
await client.end();
});
it("stable ids are valid UUIDs and never change for a key", () => {
expect(stableId("ingredient", "ground_beef")).toBe(stableId("ingredient", "ground_beef"));
expect(stableId("ingredient", "ground_beef")).not.toBe(stableId("ingredient", "onion"));
expect(stableId("ingredient", "ground_beef")).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});
it("adopting a starter copies it into the household — once — and nowhere else", async () => {
const first = await as(a, () => starterLib.adoptStarterDish(client as never, a, memberA, starter));
expect(first.created).toBe(true);
const again = await as(a, () => starterLib.adoptStarterDish(client as never, a, memberA, starter));
expect(again).toEqual({ dishId: first.dishId, created: false });
const copy = await as(a, () =>
client.query(
`select d.title, d.base, d.tags, (select count(*)::int from dish_ingredients where dish_id = d.id) as ingredients,
(select timer_seconds from dish_steps where dish_id = d.id) as timer
from dishes d where d.id = $1`,
[first.dishId],
),
);
expect(copy.rows[0]).toEqual({ title: "Testgericht", base: "pasta", tags: ["quick"], ingredients: 1, timer: 600 });
const inB = await as(b, () => client.query("select count(*)::int as n from dishes where starter_key = $1", [starter]));
expect(inB.rows[0].n).toBe(0);
});
it("a chain becomes the household's own store with its aisles; the first is the main store", async () => {
const h = randomUUID();
const ingredient = await seedIngredient(client);
await seedHousehold(client, h, ingredient); // bringt schon einen Hauptladen mit
const store = await as(h, () => storesLib.createStoreFromChain(client as never, h, "lidl", { locale: "en" }));
const { rows } = await as(h, () =>
client.query("select s.name, s.is_default, (select array_agg(name order by position) from store_aisles where store_id = s.id) as aisles from stores s where s.id = $1", [store]),
);
expect(rows[0].name).toBe("Lidl");
expect(rows[0].is_default).toBe(false);
expect(rows[0].aisles[0]).toBe("Fruit & vegetables");
await expect(as(h, () => storesLib.createStoreFromChain(client as never, h, "unknown"))).rejects.toThrow(/unknown chain/);
});
it("seeding twice changes nothing and keeps ingredient ids", async () => {
const before = (await client.query("select count(*)::int as n from ingredients")).rows[0].n;
const first = await seed(client);
const second = await seed(client);
expect(second).toEqual(first);
expect((await client.query("select count(*)::int as n from ingredients")).rows[0].n).toBe(before);
const sample = (await client.query("select id, key from ingredients where key not like 'test_%' limit 5")).rows;
for (const r of sample) expect(r.id).toBe(stableId("ingredient", r.key));
});
});