100 lines
5.0 KiB
TypeScript
100 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", () => ({}));
|
|
|
|
// lib/shopping.ts gegen die Testdatenbank: Liste aus dem Plan, Packungen,
|
|
// Gänge, Vorrats-Check, Abhaken, Abgleich nach Planänderung.
|
|
const url = process.env.TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!url)("shopping", () => {
|
|
const client = new pg.Client({ connectionString: url });
|
|
const h = randomUUID();
|
|
let kai = "";
|
|
const ing: Record<string, string> = {};
|
|
let shopping: typeof import("@/lib/shopping");
|
|
let rounds: typeof import("@/lib/rounds");
|
|
let stores: typeof import("@/lib/stores");
|
|
let db: typeof import("@/lib/db");
|
|
let bolo = "";
|
|
let chili = "";
|
|
|
|
const as = <T>(fn: (c: pg.PoolClient) => Promise<T>) => db.withHousehold(h, fn);
|
|
const round = () => as(async (c) => (await rounds.currentRound(c))!);
|
|
const items = async () => {
|
|
const r = await round();
|
|
return as(async (c) => {
|
|
await shopping.syncList(c, h, r, "de");
|
|
return shopping.listItems(c, r.id);
|
|
});
|
|
};
|
|
|
|
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','onion','olive_oil','spaghetti')")).rows) ing[r.key] = r.id;
|
|
kai = (await seedHousehold(client, h, ing.ground_beef!)).member;
|
|
shopping = await import("@/lib/shopping");
|
|
rounds = await import("@/lib/rounds");
|
|
stores = await import("@/lib/stores");
|
|
db = await import("@/lib/db");
|
|
await as(async (c) => {
|
|
await c.query("update rounds set status = 'done'");
|
|
await c.query("update stores set is_default = false");
|
|
await stores.createStoreFromChain(c, h, "rewe", { locale: "de" });
|
|
await c.query("update stores set is_default = (chain = 'rewe' and name = 'REWE' and id = (select id from stores where chain = 'rewe' order by created_at desc limit 1))");
|
|
const add = async (title: string, list: [string, number, string][]) => {
|
|
const id = (await c.query("insert into dishes (household_id, title) values ($1, $2) returning id", [h, title])).rows[0].id as string;
|
|
for (const [key, q, unit] of list) {
|
|
await c.query("insert into dish_ingredients (household_id, dish_id, ingredient_id, qty_per_portion, unit) values ($1, $2, $3, $4, $5)", [h, id, ing[key], q, unit]);
|
|
}
|
|
return id;
|
|
};
|
|
bolo = await add("Bolognese", [["ground_beef", 120, "g"], ["onion", 0.25, "piece"], ["spaghetti", 125, "g"], ["olive_oil", 10, "ml"]]);
|
|
chili = await add("Chili", [["ground_beef", 100, "g"], ["onion", 0.5, "piece"]]);
|
|
const id = await rounds.openRound(c, h, kai, { shoppingDate: null, slotsCount: 2, defaultPortions: 4 });
|
|
await c.query("insert into slots (household_id, round_id, position, dish_id, portions) values ($1, $2, 1, $3, 4), ($1, $2, 2, $4, 2)", [h, id, bolo, chili]);
|
|
await c.query("update rounds set status = 'planned' where id = $1", [id]);
|
|
});
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db.pool.end();
|
|
await client.end();
|
|
});
|
|
|
|
it("sums the plan into packs, sorted by the store's aisles", async () => {
|
|
const list = await items();
|
|
const beef = list.find((i) => i.ingredientId === ing.ground_beef)!;
|
|
expect(beef).toMatchObject({ needed: 680, packCount: 1, packSize: 1000, label: "Rinderhackfleisch", dishes: ["Bolognese", "Chili"] });
|
|
expect(list.find((i) => i.ingredientId === ing.onion)).toMatchObject({ needed: 2, qty: 2, unit: "piece" });
|
|
const positions = list.map((i) => i.aislePosition ?? 999);
|
|
expect([...positions].sort((a, b) => a - b)).toEqual(positions);
|
|
});
|
|
|
|
it("asks about pantry staples instead of listing them", async () => {
|
|
const list = await items();
|
|
const oil = list.find((i) => i.ingredientId === ing.olive_oil)!;
|
|
expect(oil.pantryState).toBe("ask");
|
|
const r = await round();
|
|
await as((c) => shopping.answerPantry(c, r.id, oil.id, false));
|
|
expect((await items()).find((i) => i.id === oil.id)!.pantryState).toBe("need"); // bleibt beim Abgleich
|
|
});
|
|
|
|
it("ticking moves the round to shopping and survives a plan change; free text stays", async () => {
|
|
const r = await round();
|
|
const beef = (await items()).find((i) => i.ingredientId === ing.ground_beef)!;
|
|
await as((c) => shopping.setChecked(c, r, beef.id, kai, true));
|
|
expect((await round()).status).toBe("shopping");
|
|
await as((c) => shopping.addManual(c, h, r.id, "Klopapier"));
|
|
// Chili fällt aus dem Plan: Zwiebelmenge sinkt, Hack bleibt (schon abgehakt).
|
|
await as((c) => c.query("delete from slots where dish_id = $1", [chili]));
|
|
const list = await items();
|
|
expect(list.find((i) => i.ingredientId === ing.onion)).toMatchObject({ needed: 1 });
|
|
expect(list.find((i) => i.ingredientId === ing.ground_beef)).toMatchObject({ checkedBy: "Kai" });
|
|
expect(list.find((i) => i.label === "Klopapier")).toMatchObject({ manual: true, aislePosition: null });
|
|
});
|
|
});
|