131 lines
5.7 KiB
TypeScript
131 lines
5.7 KiB
TypeScript
import { existsSync, readFileSync } from "node:fs";
|
|
import { describe, expect, it } from "vitest";
|
|
import { BASES, CATEGORIES, TAGS, UNITS } from "@/lib/categories";
|
|
import { LOCALES } from "@/lib/locale";
|
|
|
|
// Prüft die Seed-Daten in db/seed/ (Arbeitspaket 3), bevor sie per Migration in
|
|
// die Datenbank gehen: Zutaten in vier Sprachen, Startergerichte DE und EN,
|
|
// Gangfolgen der Ketten. Jede Regel hier ist eine, die sonst in der
|
|
// Einkaufsliste oder beim Hochrechnen falsch herauskäme.
|
|
|
|
type Ingredient = {
|
|
key: string;
|
|
category: string;
|
|
unit: string;
|
|
freshness: number;
|
|
names: Record<string, string>;
|
|
synonyms: string[];
|
|
packs: number[];
|
|
pantry: boolean;
|
|
};
|
|
type Starter = {
|
|
key: string;
|
|
locale: string;
|
|
title: string;
|
|
base: string;
|
|
tags: string[];
|
|
effort_minutes: number;
|
|
ingredients: { key: string; qty: number; unit: string; note?: string; staple?: boolean }[];
|
|
steps: { text: string; timer_seconds?: number }[];
|
|
};
|
|
type Chain = { key: string; name: string; aisles: { names: Record<string, string>; categories: string[] }[] };
|
|
|
|
const read = <T>(file: string): T => JSON.parse(readFileSync(`db/seed/${file}`, "utf8")) as T;
|
|
const has = (file: string) => existsSync(`db/seed/${file}`);
|
|
|
|
describe.skipIf(!has("ingredients.json"))("seed: ingredients", () => {
|
|
const list = has("ingredients.json") ? read<Ingredient[]>("ingredients.json") : [];
|
|
|
|
it("has around 300 ingredients with unique keys", () => {
|
|
expect(list.length).toBeGreaterThanOrEqual(280);
|
|
expect(new Set(list.map((i) => i.key)).size).toBe(list.length);
|
|
for (const i of list) expect(i.key).toMatch(/^[a-z0-9_]+$/);
|
|
});
|
|
|
|
it("every ingredient has category, base unit, freshness and names in all four languages", () => {
|
|
for (const i of list) {
|
|
expect([i.key, CATEGORIES.includes(i.category as never)]).toEqual([i.key, true]);
|
|
expect([i.key, UNITS.includes(i.unit as never)]).toEqual([i.key, true]);
|
|
expect([i.key, [1, 2, 3].includes(i.freshness)]).toEqual([i.key, true]);
|
|
for (const l of LOCALES) expect([i.key, l, (i.names[l] ?? "").trim().length > 0]).toEqual([i.key, l, true]);
|
|
expect(Array.isArray(i.synonyms)).toBe(true);
|
|
expect(typeof i.pantry).toBe("boolean");
|
|
for (const p of i.packs) expect([i.key, p > 0 && p <= 10000]).toEqual([i.key, true]);
|
|
}
|
|
});
|
|
|
|
it("names are unique per language, so matching is never ambiguous", () => {
|
|
for (const l of LOCALES) {
|
|
const names = list.map((i) => i.names[l]!.toLowerCase());
|
|
const dupes = names.filter((n, idx) => names.indexOf(n) !== idx);
|
|
expect([l, dupes]).toEqual([l, []]);
|
|
}
|
|
});
|
|
|
|
it("covers every category", () => {
|
|
for (const c of CATEGORIES) expect([c, list.some((i) => i.category === c)]).toEqual([c, true]);
|
|
});
|
|
});
|
|
|
|
describe.each(["de", "en"])("seed: starter pool %s", (locale) => {
|
|
const file = `starters.${locale}.json`;
|
|
const ingredients = has("ingredients.json") ? read<Ingredient[]>("ingredients.json") : [];
|
|
const byKey = new Map(ingredients.map((i) => [i.key, i]));
|
|
const starters = has(file) ? read<Starter[]>(file) : [];
|
|
|
|
it.skipIf(!has(file))("has 15 family classics with unique keys", () => {
|
|
expect(starters).toHaveLength(15);
|
|
expect(new Set(starters.map((s) => s.key)).size).toBe(15);
|
|
for (const s of starters) {
|
|
expect(s.key).toMatch(new RegExp(`^${locale}_[a-z0-9_]+$`));
|
|
expect(s.locale).toBe(locale);
|
|
}
|
|
});
|
|
|
|
it.skipIf(!has(file))("each starter is complete and uses known ingredients in their base unit", () => {
|
|
for (const s of starters) {
|
|
expect([s.key, s.title.trim().length > 0]).toEqual([s.key, true]);
|
|
expect([s.key, BASES.includes(s.base as never)]).toEqual([s.key, true]);
|
|
for (const t of s.tags) expect([s.key, t, TAGS.includes(t as never)]).toEqual([s.key, t, true]);
|
|
expect([s.key, s.effort_minutes >= 5 && s.effort_minutes <= 240]).toEqual([s.key, true]);
|
|
expect([s.key, s.ingredients.length >= 3, s.steps.length >= 3]).toEqual([s.key, true, true]);
|
|
const seen = new Set<string>();
|
|
for (const i of s.ingredients) {
|
|
const known = byKey.get(i.key);
|
|
expect([s.key, i.key, Boolean(known)]).toEqual([s.key, i.key, true]);
|
|
expect([s.key, i.key, i.unit]).toEqual([s.key, i.key, known?.unit]);
|
|
expect([s.key, i.key, seen.has(i.key)]).toEqual([s.key, i.key, false]);
|
|
seen.add(i.key);
|
|
// Pro Portion: plausible Mengen in Basiseinheiten.
|
|
const max = i.unit === "piece" ? 6 : 600;
|
|
expect([s.key, i.key, i.qty > 0 && i.qty <= max]).toEqual([s.key, i.key, true]);
|
|
}
|
|
for (const step of s.steps) {
|
|
expect([s.key, step.text.trim().length >= 10]).toEqual([s.key, true]);
|
|
if (step.timer_seconds !== undefined) expect([s.key, step.timer_seconds > 0]).toEqual([s.key, true]);
|
|
}
|
|
if (s.tags.includes("vegetarian") || s.tags.includes("vegan")) {
|
|
for (const i of s.ingredients) {
|
|
expect([s.key, i.key, ["meat", "poultry", "sausage", "fish"].includes(byKey.get(i.key)!.category)]).toEqual([s.key, i.key, false]);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
describe.skipIf(!has("chains.json"))("seed: store chains", () => {
|
|
const chains = has("chains.json") ? read<Chain[]>("chains.json") : [];
|
|
|
|
it("has REWE, Edeka, Lidl, Aldi and a generic order", () => {
|
|
expect(chains.map((c) => c.key).sort()).toEqual(["aldi", "edeka", "generic", "lidl", "rewe"]);
|
|
});
|
|
|
|
it("every chain places every category in exactly one aisle, named in four languages", () => {
|
|
for (const c of chains) {
|
|
const placed = c.aisles.flatMap((a) => a.categories);
|
|
expect([c.key, [...placed].sort()]).toEqual([c.key, [...CATEGORIES].sort()]);
|
|
for (const a of c.aisles) for (const l of LOCALES) expect([c.key, l, (a.names[l] ?? "").length > 0]).toEqual([c.key, l, true]);
|
|
}
|
|
});
|
|
});
|