feat(shopping): clearCheckedItems + Orphan-Cleanup
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m13s

This commit is contained in:
hsiegeln
2026-04-21 23:11:25 +02:00
parent 1889b0dea0
commit 974227590f
2 changed files with 115 additions and 3 deletions

View File

@@ -6,7 +6,8 @@ import {
removeRecipeFromCart,
listShoppingList,
setCartServings,
toggleCheck
toggleCheck,
clearCheckedItems
} from '../../src/lib/server/shopping/repository';
import type { Recipe } from '../../src/lib/types';
@@ -231,3 +232,56 @@ describe('toggleCheck', () => {
expect(rows[0].total_quantity).toBe(200);
});
});
describe('clearCheckedItems', () => {
it('removes recipes where ALL rows are checked', () => {
const db = openInMemoryForTest();
const a = insertRecipe(db, recipe({
title: 'A',
ingredients: [{ position: 1, quantity: 1, unit: 'Stk', name: 'Apfel', note: null, raw_text: '', section_heading: null }]
}));
const b = insertRecipe(db, recipe({
title: 'B',
ingredients: [
{ position: 1, quantity: 1, unit: 'Stk', name: 'Birne', note: null, raw_text: '', section_heading: null },
{ position: 2, quantity: 1, unit: 'Stk', name: 'Salz', note: null, raw_text: '', section_heading: null }
]
}));
addRecipeToCart(db, a, null);
addRecipeToCart(db, b, null);
toggleCheck(db, 'apfel', 'stk', true);
toggleCheck(db, 'birne', 'stk', true);
// Salz aus B noch nicht abgehakt → B bleibt, A fliegt
clearCheckedItems(db);
const snap = listShoppingList(db);
expect(snap.recipes.map((r) => r.recipe_id)).toEqual([b]);
// Birne-Check bleibt, weil B noch im Cart und Birne noch aktiv
const birneRow = snap.rows.find((r) => r.name_key === 'birne');
expect(birneRow?.checked).toBe(1);
});
it('purges orphan checks that no longer map to any cart recipe', () => {
const db = openInMemoryForTest();
const id = insertRecipe(db, recipe({
ingredients: [{ position: 1, quantity: 1, unit: 'Stk', name: 'Apfel', note: null, raw_text: '', section_heading: null }]
}));
addRecipeToCart(db, id, null);
toggleCheck(db, 'apfel', 'stk', true);
clearCheckedItems(db);
// Apfel-Check haengt jetzt an nichts mehr → muss aus der Tabelle raus sein
const row = db
.prepare('SELECT * FROM shopping_cart_check WHERE name_key = ?')
.get('apfel');
expect(row).toBeUndefined();
});
it('is a no-op when nothing is checked', () => {
const db = openInMemoryForTest();
const id = insertRecipe(db, recipe({
ingredients: [{ position: 1, quantity: 1, unit: 'Stk', name: 'Apfel', note: null, raw_text: '', section_heading: null }]
}));
addRecipeToCart(db, id, null);
clearCheckedItems(db);
expect(listShoppingList(db).recipes).toHaveLength(1);
});
});