From 59b232c5fc3d7b5ac6b549ce6acfcf82631443bf Mon Sep 17 00:00:00 2001 From: hsiegeln <37154749+hsiegeln@users.noreply.github.com> Date: Wed, 22 Apr 2026 16:44:18 +0200 Subject: [PATCH] docs(plan): Implementation-Plan fuer Einkaufsliste-Konsolidierung MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7 Tasks (TDD, atomic commits): 1. unitFamily + Unit-Tests 2. consolidate + Tests fuer alle Edge-Cases 3. formatQuantity auf toLocaleString('de-DE', ...) 4. Migration 015 — Check-Keys auf Family 5. listShoppingList konsolidiert via TS-side grouping 6. clearCheckedItems + toggleCheck auf Family-Key 7. E2E-Smoke im Dev-Deployment Co-Authored-By: Claude Opus 4.7 (1M context) --- ...2026-04-22-einkaufsliste-konsolidierung.md | 887 ++++++++++++++++++ 1 file changed, 887 insertions(+) create mode 100644 docs/superpowers/plans/2026-04-22-einkaufsliste-konsolidierung.md diff --git a/docs/superpowers/plans/2026-04-22-einkaufsliste-konsolidierung.md b/docs/superpowers/plans/2026-04-22-einkaufsliste-konsolidierung.md new file mode 100644 index 0000000..08c2c43 --- /dev/null +++ b/docs/superpowers/plans/2026-04-22-einkaufsliste-konsolidierung.md @@ -0,0 +1,887 @@ +# Einkaufsliste Mengen-Konsolidierung Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Verschiedene Unit-Varianten derselben Zutat (500 g + 1 kg Kartoffeln) in der Einkaufsliste zu einer Zeile konsolidieren (→ 1,5 kg). Scope: g↔kg, ml↔l. + +**Architecture:** Zwei reine TS-Utilities (`unitFamily`, `consolidate`) kapseln die Logik. `listShoppingList()` lässt SQL weiterhin pro (name, unit) aggregieren, bündelt die Zeilen dann in TS pro `(name, unitFamily)` und konsolidiert. Migration 015 macht `shopping_cart_check.unit_key` zum Family-Key, damit Abhaks nicht verloren gehen wenn Display-Unit zwischen g und kg wechselt. `formatQuantity` wechselt app-weit auf `toLocaleString('de-DE')` (Komma als Dezimaltrennzeichen). + +**Tech Stack:** SvelteKit, better-sqlite3, Vitest. Keine neuen Deps. + +--- + +## File Structure + +**Create:** +- `src/lib/server/shopping/unit-consolidation.ts` — `unitFamily()` + `consolidate()` +- `src/lib/server/db/migrations/015_shopping_check_family.sql` — Family-Key-Migration +- `tests/unit/unit-consolidation.test.ts` — Unit-Tests + +**Modify:** +- `src/lib/quantity-format.ts` — `toLocaleString('de-DE', …)` statt Punkt +- `tests/unit/quantity-format.test.ts` — Erwartungen auf Komma anpassen +- `src/lib/server/shopping/repository.ts` — `listShoppingList`, `toggleCheck`, `clearCheckedItems` auf Family-Key umstellen +- `tests/integration/shopping-repository.test.ts` — neue Describe-Blöcke für Konsolidierung + +--- + +### Task 1: Unit-Family-Utility + +**Files:** +- Create: `src/lib/server/unit-consolidation.ts` +- Test: `tests/unit/unit-consolidation.test.ts` + +Hinweis: Datei bewusst in `src/lib/server/` (nicht in `shopping/`), weil `unitFamily` auch vom Migration-Code referenziert wird — eine Ebene höher ist intuitiver. + +- [ ] **Step 1: Write failing tests for `unitFamily`** + +Create `tests/unit/unit-consolidation.test.ts`: + +```typescript +import { describe, it, expect } from 'vitest'; +import { unitFamily } from '../../src/lib/server/unit-consolidation'; + +describe('unitFamily', () => { + it('maps g and kg to weight', () => { + expect(unitFamily('g')).toBe('weight'); + expect(unitFamily('kg')).toBe('weight'); + }); + it('maps ml and l to volume', () => { + expect(unitFamily('ml')).toBe('volume'); + expect(unitFamily('l')).toBe('volume'); + }); + it('lowercases and trims unknown units', () => { + expect(unitFamily(' Bund ')).toBe('bund'); + expect(unitFamily('TL')).toBe('tl'); + expect(unitFamily('Stück')).toBe('stück'); + }); + it('is case-insensitive for weight/volume', () => { + expect(unitFamily('Kg')).toBe('weight'); + expect(unitFamily('ML')).toBe('volume'); + }); + it('returns empty string for null/undefined/empty', () => { + expect(unitFamily(null)).toBe(''); + expect(unitFamily(undefined)).toBe(''); + expect(unitFamily('')).toBe(''); + expect(unitFamily(' ')).toBe(''); + }); +}); +``` + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `npm test -- tests/unit/unit-consolidation.test.ts` +Expected: All fail with "Cannot find module …/unit-consolidation". + +- [ ] **Step 3: Implement `unitFamily`** + +Create `src/lib/server/unit-consolidation.ts`: + +```typescript +export type UnitFamily = 'weight' | 'volume' | string; + +const WEIGHT_UNITS = new Set(['g', 'kg']); +const VOLUME_UNITS = new Set(['ml', 'l']); + +export function unitFamily(unit: string | null | undefined): UnitFamily { + const u = (unit ?? '').trim().toLowerCase(); + if (WEIGHT_UNITS.has(u)) return 'weight'; + if (VOLUME_UNITS.has(u)) return 'volume'; + return u; +} +``` + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `npm test -- tests/unit/unit-consolidation.test.ts` +Expected: 5 tests pass. + +- [ ] **Step 5: Commit** + +```bash +git add src/lib/server/unit-consolidation.ts tests/unit/unit-consolidation.test.ts +git commit -m "feat(shopping): unitFamily-Utility fuer Konsolidierung" +``` + +--- + +### Task 2: Consolidate-Funktion + +**Files:** +- Modify: `src/lib/server/unit-consolidation.ts` +- Modify: `tests/unit/unit-consolidation.test.ts` + +- [ ] **Step 1: Append failing tests for `consolidate` to the existing test file** + +Append to `tests/unit/unit-consolidation.test.ts` (after the `unitFamily` describe): + +```typescript +import { consolidate } from '../../src/lib/server/unit-consolidation'; + +describe('consolidate', () => { + it('kombiniert 500 g + 1 kg zu 1,5 kg', () => { + const out = consolidate([ + { quantity: 500, unit: 'g' }, + { quantity: 1, unit: 'kg' } + ]); + expect(out).toEqual({ quantity: 1.5, unit: 'kg' }); + }); + + it('bleibt bei g wenn Summe < 1 kg', () => { + const out = consolidate([ + { quantity: 200, unit: 'g' }, + { quantity: 300, unit: 'g' } + ]); + expect(out).toEqual({ quantity: 500, unit: 'g' }); + }); + + it('kombiniert ml + l analog (400 ml + 0,5 l → 900 ml)', () => { + const out = consolidate([ + { quantity: 400, unit: 'ml' }, + { quantity: 0.5, unit: 'l' } + ]); + expect(out).toEqual({ quantity: 900, unit: 'ml' }); + }); + + it('promoted zu l ab 1000 ml (0,5 l + 0,8 l → 1,3 l)', () => { + const out = consolidate([ + { quantity: 0.5, unit: 'l' }, + { quantity: 0.8, unit: 'l' } + ]); + expect(out).toEqual({ quantity: 1.3, unit: 'l' }); + }); + + it('summiert gleiche nicht-family-units (2 Bund + 1 Bund → 3 Bund)', () => { + const out = consolidate([ + { quantity: 2, unit: 'Bund' }, + { quantity: 1, unit: 'Bund' } + ]); + expect(out).toEqual({ quantity: 3, unit: 'Bund' }); + }); + + it('behandelt quantity=null als 0', () => { + const out = consolidate([ + { quantity: null, unit: 'TL' }, + { quantity: 1, unit: 'TL' } + ]); + expect(out).toEqual({ quantity: 1, unit: 'TL' }); + }); + + it('gibt null zurueck wenn alle quantities null sind', () => { + const out = consolidate([ + { quantity: null, unit: 'Prise' }, + { quantity: null, unit: 'Prise' } + ]); + expect(out).toEqual({ quantity: null, unit: 'Prise' }); + }); + + it('rundet Float-Artefakte auf 2 Dezimalen (0,1 + 0,2 kg → 0,3 kg)', () => { + const out = consolidate([ + { quantity: 0.1, unit: 'kg' }, + { quantity: 0.2, unit: 'kg' } + ]); + // 0.1 + 0.2 in kg = 0.3 kg, in g = 300 → promoted? 300 < 1000 → 300 g + expect(out).toEqual({ quantity: 300, unit: 'g' }); + }); + + it('nimmt unit vom ersten Eintrag bei unbekannter family', () => { + const out = consolidate([{ quantity: 5, unit: 'Stück' }]); + expect(out).toEqual({ quantity: 5, unit: 'Stück' }); + }); +}); +``` + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `npm test -- tests/unit/unit-consolidation.test.ts` +Expected: Fail with "consolidate is not a function" or similar (9 new tests fail). + +- [ ] **Step 3: Implement `consolidate`** + +Append to `src/lib/server/unit-consolidation.ts`: + +```typescript +export interface QuantityInUnit { + quantity: number | null; + unit: string | null; +} + +function round2(n: number): number { + return Math.round(n * 100) / 100; +} + +/** + * Konsolidiert mehrere {quantity, unit}-Eintraege derselben Unit-Family + * zu einer gemeinsamen Menge + Display-Unit. + * + * - Gewicht (g, kg): summiert in g, promoted bei >=1000 g auf kg. + * - Volumen (ml, l): summiert in ml, promoted bei >=1000 ml auf l. + * - Andere: summiert quantity ohne Umrechnung, Display-Unit vom ersten + * Eintrag. + * + * quantity=null wird als 0 behandelt. Wenn ALLE quantities null sind, + * ist die Gesamtmenge ebenfalls null. + */ +export function consolidate(rows: QuantityInUnit[]): QuantityInUnit { + if (rows.length === 0) return { quantity: null, unit: null }; + + const family = unitFamily(rows[0].unit); + const firstUnit = rows[0].unit; + + const allNull = rows.every((r) => r.quantity === null); + + if (family === 'weight') { + if (allNull) return { quantity: null, unit: firstUnit }; + const grams = rows.reduce((sum, r) => { + const q = r.quantity ?? 0; + return sum + (unitFamily(r.unit) === 'weight' && r.unit?.toLowerCase().trim() === 'kg' ? q * 1000 : q); + }, 0); + if (grams >= 1000) return { quantity: round2(grams / 1000), unit: 'kg' }; + return { quantity: round2(grams), unit: 'g' }; + } + + if (family === 'volume') { + if (allNull) return { quantity: null, unit: firstUnit }; + const ml = rows.reduce((sum, r) => { + const q = r.quantity ?? 0; + return sum + (r.unit?.toLowerCase().trim() === 'l' ? q * 1000 : q); + }, 0); + if (ml >= 1000) return { quantity: round2(ml / 1000), unit: 'l' }; + return { quantity: round2(ml), unit: 'ml' }; + } + + // Non-family: summiere quantity direkt + if (allNull) return { quantity: null, unit: firstUnit }; + const sum = rows.reduce((acc, r) => acc + (r.quantity ?? 0), 0); + return { quantity: round2(sum), unit: firstUnit }; +} +``` + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `npm test -- tests/unit/unit-consolidation.test.ts` +Expected: 14 tests pass (5 from Task 1 + 9 new). + +- [ ] **Step 5: Commit** + +```bash +git add src/lib/server/unit-consolidation.ts tests/unit/unit-consolidation.test.ts +git commit -m "feat(shopping): consolidate() fuer g/kg + ml/l Summierung" +``` + +--- + +### Task 3: formatQuantity auf deutsches Locale + +**Files:** +- Modify: `src/lib/quantity-format.ts` +- Modify: `tests/unit/quantity-format.test.ts` + +- [ ] **Step 1: Update tests to expect comma decimal** + +Open `tests/unit/quantity-format.test.ts`. Jede Erwartung mit Dezimalpunkt auf Komma ändern, z. B.: + +```typescript +// vorher: expect(formatQuantity(0.333)).toBe('0.33'); +// nachher: +expect(formatQuantity(0.333)).toBe('0,33'); +``` + +Betroffene Assertions (aus dem bestehenden Test-File): +- `formatQuantity(0.333)` → `'0,33'` +- `formatQuantity(0.5)` → `'0,5'` +- `formatQuantity(1.25)` → `'1,25'` + +Ganze Zahlen (`formatQuantity(3)` → `'3'`) und null (`''`) bleiben gleich. + +- [ ] **Step 2: Run tests to verify they fail with current implementation** + +Run: `npm test -- tests/unit/quantity-format.test.ts` +Expected: Tests with decimal values fail (`'0.33'` received, `'0,33'` expected). + +- [ ] **Step 3: Rewrite `formatQuantity` mit toLocaleString** + +Replace contents of `src/lib/quantity-format.ts`: + +```typescript +export function formatQuantity(q: number | null): string { + if (q === null || q === undefined) return ''; + return q.toLocaleString('de-DE', { + maximumFractionDigits: 2, + useGrouping: false + }); +} +``` + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `npm test -- tests/unit/quantity-format.test.ts` +Expected: Alle 5 Tests grün. + +- [ ] **Step 5: Run full suite to catch app-wide regressions** + +Run: `npm test` +Expected: Alle Tests grün. Falls andere Tests (z. B. Rezept-Detail-Rendering) Erwartungen auf `'.'` haben und fehlschlagen, Assertions dort auf Komma anpassen und in denselben Commit nehmen. + +- [ ] **Step 6: Commit** + +```bash +git add src/lib/quantity-format.ts tests/unit/quantity-format.test.ts +git commit -m "feat(format): formatQuantity app-weit auf de-DE Komma-Dezimal" +``` + +--- + +### Task 4: Migration 015 — Check-Keys auf Family + +**Files:** +- Create: `src/lib/server/db/migrations/015_shopping_check_family.sql` + +Hinweis: Migrations werden via `import.meta.glob('./migrations/*.sql', {eager, query:'?raw'})` gebundelt (siehe CLAUDE.md) — kein Dockerfile-Copy nötig. + +- [ ] **Step 1: Write the migration** + +Create `src/lib/server/db/migrations/015_shopping_check_family.sql`: + +```sql +-- Konsolidierung: unit_key in shopping_cart_check wird zum Family-Key, damit +-- Abhaks stabil bleiben wenn Display-Unit zwischen g und kg wechselt. +-- g/kg → 'weight', ml/l → 'volume', Rest bleibt unveraendert. +UPDATE shopping_cart_check SET unit_key = 'weight' WHERE LOWER(TRIM(unit_key)) IN ('g', 'kg'); +UPDATE shopping_cart_check SET unit_key = 'volume' WHERE LOWER(TRIM(unit_key)) IN ('ml', 'l'); + +-- Nach Relabeling koennen Duplikate entstehen (zwei Zeilen mit 'weight' pro +-- name_key). Juengsten Eintrag behalten. +DELETE FROM shopping_cart_check +WHERE rowid NOT IN ( + SELECT MAX(rowid) + FROM shopping_cart_check + GROUP BY name_key, unit_key +); +``` + +- [ ] **Step 2: Verify migration runs (smoke test via any integration test)** + +Run: `npm test -- tests/integration/shopping-repository.test.ts` +Expected: Alle bestehenden Tests grün (Migration läuft beim `openInMemoryForTest()`, bricht nichts weil Tabelle beim ersten Lauf leer ist). + +- [ ] **Step 3: Commit** + +```bash +git add src/lib/server/db/migrations/015_shopping_check_family.sql +git commit -m "feat(shopping): Migration 015 — Check-Keys auf Unit-Family" +``` + +--- + +### Task 5: listShoppingList mit Family-Konsolidierung + +**Files:** +- Modify: `src/lib/server/shopping/repository.ts:70-107` +- Modify: `tests/integration/shopping-repository.test.ts` + +- [ ] **Step 1: Write failing integration test** + +Append to `tests/integration/shopping-repository.test.ts` (z. B. nach dem vorhandenen `addRecipeToCart`-Block, ein eigener Describe-Block): + +```typescript +describe('listShoppingList — Konsolidierung ueber Einheiten', () => { + it('fasst 500 g + 1 kg Kartoffeln zu 1,5 kg zusammen', () => { + const db = openInMemoryForTest(); + const a = insertRecipe( + db, + recipe({ + title: 'Kartoffelsuppe', + servings_default: 4, + ingredients: [ + { position: 1, name: 'Kartoffeln', quantity: 500, unit: 'g', note: null, raw_text: null } + ] + }) + ); + const b = insertRecipe( + db, + recipe({ + title: 'Kartoffelpuffer', + servings_default: 4, + ingredients: [ + { position: 1, name: 'Kartoffeln', quantity: 1, unit: 'kg', note: null, raw_text: null } + ] + }) + ); + addRecipeToCart(db, a, null); + addRecipeToCart(db, b, null); + + const snap = listShoppingList(db); + const kartoffeln = snap.rows.filter((r) => r.display_name.toLowerCase() === 'kartoffeln'); + expect(kartoffeln).toHaveLength(1); + expect(kartoffeln[0].total_quantity).toBe(1.5); + expect(kartoffeln[0].display_unit).toBe('kg'); + }); + + it('kombiniert ml + l korrekt (400 ml + 0,5 l → 900 ml)', () => { + const db = openInMemoryForTest(); + const a = insertRecipe( + db, + recipe({ + title: 'R1', + servings_default: 4, + ingredients: [{ position: 1, name: 'Milch', quantity: 400, unit: 'ml', note: null, raw_text: null }] + }) + ); + const b = insertRecipe( + db, + recipe({ + title: 'R2', + servings_default: 4, + ingredients: [{ position: 1, name: 'Milch', quantity: 0.5, unit: 'l', note: null, raw_text: null }] + }) + ); + addRecipeToCart(db, a, null); + addRecipeToCart(db, b, null); + + const milch = listShoppingList(db).rows.filter((r) => r.display_name.toLowerCase() === 'milch'); + expect(milch).toHaveLength(1); + expect(milch[0].total_quantity).toBe(900); + expect(milch[0].display_unit).toBe('ml'); + }); + + it('laesst inkompatible Families getrennt (5 Stueck Eier + 500 g Eier = 2 Zeilen)', () => { + const db = openInMemoryForTest(); + const a = insertRecipe( + db, + recipe({ + title: 'R1', + servings_default: 4, + ingredients: [{ position: 1, name: 'Eier', quantity: 5, unit: 'Stück', note: null, raw_text: null }] + }) + ); + const b = insertRecipe( + db, + recipe({ + title: 'R2', + servings_default: 4, + ingredients: [{ position: 1, name: 'Eier', quantity: 500, unit: 'g', note: null, raw_text: null }] + }) + ); + addRecipeToCart(db, a, null); + addRecipeToCart(db, b, null); + + const eier = listShoppingList(db).rows.filter((r) => r.display_name.toLowerCase() === 'eier'); + expect(eier).toHaveLength(2); + }); + + it('summiert gleiche Unit-Family ohne Konversion (2 Bund + 1 Bund → 3 Bund)', () => { + const db = openInMemoryForTest(); + const a = insertRecipe( + db, + recipe({ + title: 'R1', + servings_default: 4, + ingredients: [{ position: 1, name: 'Petersilie', quantity: 2, unit: 'Bund', note: null, raw_text: null }] + }) + ); + const b = insertRecipe( + db, + recipe({ + title: 'R2', + servings_default: 4, + ingredients: [{ position: 1, name: 'Petersilie', quantity: 1, unit: 'Bund', note: null, raw_text: null }] + }) + ); + addRecipeToCart(db, a, null); + addRecipeToCart(db, b, null); + + const petersilie = listShoppingList(db).rows.filter((r) => r.display_name.toLowerCase() === 'petersilie'); + expect(petersilie).toHaveLength(1); + expect(petersilie[0].total_quantity).toBe(3); + expect(petersilie[0].display_unit?.toLowerCase()).toBe('bund'); + }); +}); +``` + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `npm test -- tests/integration/shopping-repository.test.ts` +Expected: 4 neue Tests fail (Konsolidierung existiert noch nicht). + +- [ ] **Step 3: Rewrite `listShoppingList` to use TS-side consolidation** + +Replace the `listShoppingList` body in `src/lib/server/shopping/repository.ts` (Zeilen 70-107): + +```typescript +import { consolidate, unitFamily } from '../unit-consolidation'; + +// (oben im File unter den bestehenden Imports einfuegen) + +export function listShoppingList( + db: Database.Database +): ShoppingListSnapshot { + const recipes = db + .prepare( + `SELECT cr.recipe_id, r.title, r.image_path, cr.servings, + COALESCE(r.servings_default, cr.servings) AS servings_default + FROM shopping_cart_recipe cr + JOIN recipe r ON r.id = cr.recipe_id + ORDER BY cr.added_at ASC` + ) + .all() as ShoppingCartRecipe[]; + + // SQL aggregiert weiterhin pro (name, raw-unit). Die family-Gruppierung + // + Konsolidierung macht TypeScript, damit SQL lesbar bleibt und die + // Logik Unit-testbar ist. + type RawRow = { + name_key: string; + unit_key: string; + display_name: string; + display_unit: string | null; + total_quantity: number | null; + from_recipes: string; + }; + + const raw = db + .prepare( + `SELECT + LOWER(TRIM(i.name)) AS name_key, + LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key, + MIN(i.name) AS display_name, + MIN(i.unit) AS display_unit, + SUM(i.quantity * cr.servings * 1.0 / NULLIF(COALESCE(r.servings_default, cr.servings), 0)) AS total_quantity, + GROUP_CONCAT(DISTINCT r.title) AS from_recipes + FROM shopping_cart_recipe cr + JOIN recipe r ON r.id = cr.recipe_id + JOIN ingredient i ON i.recipe_id = r.id + GROUP BY name_key, unit_key` + ) + .all() as RawRow[]; + + // Check-Keys einmalig vorladen + const checkedSet = new Set( + ( + db + .prepare('SELECT name_key, unit_key FROM shopping_cart_check') + .all() as { name_key: string; unit_key: string }[] + ).map((c) => `${c.name_key}|${c.unit_key}`) + ); + + // Gruppieren nach (name_key, unitFamily(unit_key)) + const grouped = new Map(); + for (const r of raw) { + const familyKey = unitFamily(r.unit_key); + const key = `${r.name_key}|${familyKey}`; + const arr = grouped.get(key) ?? []; + arr.push(r); + grouped.set(key, arr); + } + + const rows: ShoppingListRow[] = []; + for (const [key, members] of grouped) { + const [nameKey, familyKey] = key.split('|'); + const consolidated = consolidate( + members.map((m) => ({ quantity: m.total_quantity, unit: m.display_unit })) + ); + // display_name: ersten nehmen (alle Member haben dasselbe name_key) + const displayName = members[0].display_name; + // from_recipes: alle unique Titel aus den Members kombinieren + const allRecipes = new Set(); + for (const m of members) { + for (const t of m.from_recipes.split(',')) allRecipes.add(t); + } + rows.push({ + name_key: nameKey, + unit_key: familyKey, // wichtig: family-key, matched mit checked-Lookup + display_name: displayName, + display_unit: consolidated.unit, + total_quantity: consolidated.quantity, + from_recipes: [...allRecipes].join(','), + checked: checkedSet.has(`${nameKey}|${familyKey}`) ? 1 : 0 + }); + } + + // Sort wie bisher: erst unchecked, dann alphabetisch by display_name + rows.sort((a, b) => { + if (a.checked !== b.checked) return a.checked - b.checked; + return a.display_name.localeCompare(b.display_name, 'de', { sensitivity: 'base' }); + }); + + const uncheckedCount = rows.reduce((n, r) => n + (r.checked ? 0 : 1), 0); + return { recipes, rows, uncheckedCount }; +} +``` + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `npm test -- tests/integration/shopping-repository.test.ts` +Expected: Alle Tests grün (bestehende + 4 neue). + +- [ ] **Step 5: Commit** + +```bash +git add src/lib/server/shopping/repository.ts tests/integration/shopping-repository.test.ts +git commit -m "feat(shopping): listShoppingList konsolidiert g/kg + ml/l" +``` + +--- + +### Task 6: toggleCheck + clearCheckedItems auf Family-Key + +**Files:** +- Modify: `src/lib/server/shopping/repository.ts:109-188` +- Modify: `tests/integration/shopping-repository.test.ts` + +- [ ] **Step 1: Write failing integration tests** + +Append to `tests/integration/shopping-repository.test.ts`: + +```typescript +describe('toggleCheck — stabil ueber Unit-Family', () => { + it('haekchen bleibt erhalten wenn Gesamtmenge von kg auf g faellt', () => { + const db = openInMemoryForTest(); + const a = insertRecipe( + db, + recipe({ + title: 'R1', + servings_default: 4, + ingredients: [{ position: 1, name: 'Kartoffeln', quantity: 500, unit: 'g', note: null, raw_text: null }] + }) + ); + const b = insertRecipe( + db, + recipe({ + title: 'R2', + servings_default: 4, + ingredients: [{ position: 1, name: 'Kartoffeln', quantity: 1, unit: 'kg', note: null, raw_text: null }] + }) + ); + addRecipeToCart(db, a, null); + addRecipeToCart(db, b, null); + + // Abhaken der konsolidierten 1,5-kg-Zeile via family-key + const before = listShoppingList(db).rows[0]; + toggleCheck(db, before.name_key, before.unit_key, true); + expect(listShoppingList(db).rows[0].checked).toBe(1); + + // Ein Rezept rausnehmen → nur noch 500 g, display wechselt auf g + removeRecipeFromCart(db, b); + const after = listShoppingList(db).rows[0]; + expect(after.display_unit).toBe('g'); + expect(after.total_quantity).toBe(500); + // Haekchen bleibt: unit_key ist weiterhin 'weight' + expect(after.checked).toBe(1); + }); +}); +``` + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `npm test -- tests/integration/shopping-repository.test.ts` +Expected: Der neue Test failt, weil `toggleCheck` noch mit `unit_key` als raw unit arbeitet — der Check wird mit `'weight'` geschrieben, ABER das Schreiben selbst könnte fälschlich doch durchgehen (toggleCheck macht ja nur INSERT mit dem gegebenen Key). Der Failure entsteht beim zweiten `listShoppingList()`: der Lookup-Key matched noch nicht mit dem gespeicherten Check. + +Tatsächlich: Mit Task 5 schreibt `toggleCheck(db, name, 'weight', true)` einen Eintrag `(kartoffeln, 'weight')` in `shopping_cart_check`. `listShoppingList` liest den Check mit dem Family-Key — also passt. Der Test müsste grün sein _wenn_ toggleCheck unverändert funktioniert. + +Hmm — let me re-check. `toggleCheck(db, nameKey, unitKey, checked)` nimmt einfach den String, den der Caller übergibt, und speichert. Das ist agnostisch. Also wenn die UI `row.unit_key` durchreicht (was ja jetzt 'weight' ist), funktioniert das. Kein Code-Change nötig in toggleCheck. + +`clearCheckedItems` hingegen vergleicht Check-Keys mit der Ingredient-Tabelle via `LOWER(TRIM(COALESCE(i.unit, '')))` — das ist aber der RAW unit, nicht der Family-Key. Hier ist der Fix nötig. + +→ Step 2 wird daher beide Facetten prüfen: (1) toggleCheck/round-trip funktioniert bereits (Test grün), (2) clearCheckedItems dedupliziert korrekt. + +Ich füge daher einen expliziten clearCheckedItems-Test hinzu: + +Append weitere Test-Case in denselben Block: + +```typescript +it('clearCheckedItems respektiert family-key beim Orphan-Cleanup', () => { + const db = openInMemoryForTest(); + const a = insertRecipe( + db, + recipe({ + title: 'R1', + servings_default: 4, + ingredients: [ + { position: 1, name: 'Kartoffeln', quantity: 500, unit: 'g', note: null, raw_text: null }, + { position: 2, name: 'Salz', quantity: 1, unit: 'Prise', note: null, raw_text: null } + ] + }) + ); + addRecipeToCart(db, a, null); + const rows = listShoppingList(db).rows; + // Alle abhaken + for (const r of rows) toggleCheck(db, r.name_key, r.unit_key, true); + clearCheckedItems(db); + // Das Rezept sollte raus sein + expect(listShoppingList(db).recipes).toHaveLength(0); + // Check-Tabelle sollte leer sein (keine Orphans) + const remaining = (db.prepare('SELECT COUNT(*) AS c FROM shopping_cart_check').get() as { c: number }).c; + expect(remaining).toBe(0); +}); +``` + +Run: `npm test -- tests/integration/shopping-repository.test.ts` +Expected: Der clearCheckedItems-Test könnte failen weil der Orphan-Cleanup mit raw-unit vergleicht — der Check hat 'weight', das Ingredient hat 'g', Key-Match schlägt fehl, Check bleibt als Orphan. + +- [ ] **Step 3: Fix `clearCheckedItems` to use family-key for orphan comparison** + +In `src/lib/server/shopping/repository.ts`, in `clearCheckedItems` den Orphan-Cleanup-Block: + +Ersetzen (aktuell Zeilen 163-185): + +```typescript + // Orphan-Checks raeumen: alle Check-Keys, die jetzt in KEINEM Cart-Rezept + // mehr vorkommen. + const activeKeys = db + .prepare( + `SELECT DISTINCT + LOWER(TRIM(i.name)) AS name_key, + LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key + FROM shopping_cart_recipe cr + JOIN ingredient i ON i.recipe_id = cr.recipe_id` + ) + .all() as { name_key: string; unit_key: string }[]; + const activeSet = new Set(activeKeys.map((k) => `${k.name_key} ${k.unit_key}`)); + const allChecks = db + .prepare('SELECT name_key, unit_key FROM shopping_cart_check') + .all() as { name_key: string; unit_key: string }[]; + const del = db.prepare( + 'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?' + ); + for (const c of allChecks) { + if (!activeSet.has(`${c.name_key} ${c.unit_key}`)) { + del.run(c.name_key, c.unit_key); + } + } +``` + +durch: + +```typescript + // Orphan-Checks raeumen: Active-Keys nach (name_key, unitFamily(raw-unit)) + // bauen, damit Checks mit family-key korrekt gematcht werden. + const activeRaw = db + .prepare( + `SELECT DISTINCT + LOWER(TRIM(i.name)) AS name_key, + LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key + FROM shopping_cart_recipe cr + JOIN ingredient i ON i.recipe_id = cr.recipe_id` + ) + .all() as { name_key: string; unit_key: string }[]; + const activeSet = new Set( + activeRaw.map((k) => `${k.name_key}|${unitFamily(k.unit_key)}`) + ); + const allChecks = db + .prepare('SELECT name_key, unit_key FROM shopping_cart_check') + .all() as { name_key: string; unit_key: string }[]; + const del = db.prepare( + 'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?' + ); + for (const c of allChecks) { + if (!activeSet.has(`${c.name_key}|${c.unit_key}`)) { + del.run(c.name_key, c.unit_key); + } + } +``` + +Analog den oberen Block in `clearCheckedItems` (perRecipe-Gruppierung, Zeilen 132-146), der `unit_key` mit `LOWER(TRIM(i.unit))` matched — da wird pro recipe_id gezählt, ob alle Zeilen abgehakt sind. Der Count-Vergleich mit `shopping_cart_check` erfolgt auch hier via unit_key. Anpassen: + +Ersetzen (aktuell Zeilen 132-147): + +```typescript + const allRows = db + .prepare( + `SELECT + cr.recipe_id, + LOWER(TRIM(i.name)) AS name_key, + LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key, + EXISTS( + SELECT 1 FROM shopping_cart_check c + WHERE c.name_key = LOWER(TRIM(i.name)) + AND c.unit_key = LOWER(TRIM(COALESCE(i.unit, ''))) + ) AS checked + FROM shopping_cart_recipe cr + JOIN ingredient i ON i.recipe_id = cr.recipe_id` + ) + .all() as { recipe_id: number; name_key: string; unit_key: string; checked: 0 | 1 }[]; +``` + +durch: + +```typescript + // Rohe (name, unit)-Zeilen holen, checked-Status per Family-Key-Lookup + // in JS entscheiden (SQL-CASE-Duplikation vermeiden). + const allRowsRaw = db + .prepare( + `SELECT + cr.recipe_id, + LOWER(TRIM(i.name)) AS name_key, + LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key + FROM shopping_cart_recipe cr + JOIN ingredient i ON i.recipe_id = cr.recipe_id` + ) + .all() as { recipe_id: number; name_key: string; unit_key: string }[]; + + const checkSet = new Set( + ( + db + .prepare('SELECT name_key, unit_key FROM shopping_cart_check') + .all() as { name_key: string; unit_key: string }[] + ).map((c) => `${c.name_key}|${c.unit_key}`) + ); + + const allRows = allRowsRaw.map((r) => ({ + recipe_id: r.recipe_id, + name_key: r.name_key, + unit_key: r.unit_key, + checked: checkSet.has(`${r.name_key}|${unitFamily(r.unit_key)}`) ? (1 as const) : (0 as const) + })); +``` + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `npm test -- tests/integration/shopping-repository.test.ts` +Expected: Alle Tests grün. + +- [ ] **Step 5: Run full suite + typecheck** + +Run: `npm test && npm run check` +Expected: +- Tests: alle grün +- svelte-check: `0 ERRORS 0 WARNINGS` + +- [ ] **Step 6: Commit** + +```bash +git add src/lib/server/shopping/repository.ts tests/integration/shopping-repository.test.ts +git commit -m "feat(shopping): clearCheckedItems auf Family-Key umgestellt" +``` + +--- + +### Task 7: End-to-End-Smoketest im Dev-Deployment + +**Files:** keine + +- [ ] **Step 1: Push und warten auf CI-Deploy** + +```bash +git push +``` + +CI baut arm64-Image, deployt nach dev. ~5 Min. + +- [ ] **Step 2: Manuell auf `https://kochwas-dev.siegeln.net/shopping-list` prüfen** + +Check-Liste: +- Zwei Rezepte mit 500 g + 1 kg gleicher Zutat in den Warenkorb → eine Zeile mit "1,5 kg". +- 400 ml + 0,5 l → "900 ml". +- Komma-Darstellung in Rezept-Detail überall ok (keine Regressionen). +- Abhaken + Rezept rausnehmen → Haken bleibt. + +Wenn alle grün: Feature ist done. Kein separater Commit nötig. + +--- + +## Self-Review Checklist + +- [x] Spec-Coverage: Alle Sektionen abgedeckt (Unit-Konsolidierung → Task 1+2, Migration → Task 4, Formatter → Task 3, listShoppingList-Integration → Task 5, Check-Stabilität → Task 6). +- [x] Keine Placeholder: alle Tests und Implementierungen vollständig ausgeschrieben. +- [x] Type-Konsistenz: `QuantityInUnit`, `ShoppingListRow` einheitlich referenziert. `unit_key` bleibt derselbe Feldname, semantisch jetzt Family-Key. +- [x] Scope: eine einzelne Phase, atomic commits, TDD.