const assert = require('node:assert'); const db = require('./db'); const { parseQuantityString, inferCategory } = require('./barcode'); console.log('๐Ÿงช Running Fridger Backend Unit Tests...'); // 1. Test quantity parsing console.log('1. Testing quantity parsing...'); assert.strictEqual(parseQuantityString('250 g').quantity, 250); assert.strictEqual(parseQuantityString('250 g').unit, 'g'); assert.strictEqual(parseQuantityString('1.5 kg').quantity, 1500); assert.strictEqual(parseQuantityString('1.5 kg').unit, 'g'); assert.strictEqual(parseQuantityString('500ml').quantity, 500); assert.strictEqual(parseQuantityString('500ml').unit, 'ml'); assert.strictEqual(parseQuantityString('6 x 100g').quantity, 600); assert.strictEqual(parseQuantityString('4 Stk').quantity, 4); assert.strictEqual(parseQuantityString('4 Stk').unit, 'pcs'); console.log('โœ“ Quantity parsing tests passed!'); // 2. Test category inference console.log('2. Testing category inference...'); assert.strictEqual(inferCategory([], 'Frische Vollmilch 3.5%'), 'Dairy & Cheese'); assert.strictEqual(inferCategory([], 'Gouda Kรคse in Scheiben'), 'Dairy & Cheese'); assert.strictEqual(inferCategory([], 'Rinderhackfleisch'), 'Meat & Fish'); assert.strictEqual(inferCategory([], 'Bio Tomaten'), 'Produce'); assert.strictEqual(inferCategory([], 'Apfelsaft 100%'), 'Beverages'); assert.strictEqual(inferCategory([], 'Senf mittelscharf'), 'Sauces & Condiments'); console.log('โœ“ Category inference tests passed!'); // 3. Test Database CRUD & Quantity adjustments console.log('3. Testing Database operations...'); const newItem = db.createItem({ name: 'Test Yogurt', brand: 'TestBrand', barcode: '999999999999', category: 'Dairy & Cheese', location: 'Fridge - Top Shelf', quantity: 500, initial_quantity: 500, unit: 'g', expiry_date: '2026-10-20', notes: 'Test note' }); assert(newItem.id, 'Item should have an ID'); assert.strictEqual(newItem.name, 'Test Yogurt'); assert.strictEqual(newItem.quantity, 500); assert.strictEqual(newItem.unit, 'g'); // Test Whole Packets: addPacket & removePacket console.log('Testing packet addition (+1 whole packet of 500g)...'); const testPack = db.createItem({ name: 'Test Milk Pack', quantity: 1000, initial_quantity: 1000, unit: 'ml' }); const packAdded = db.addPacket(testPack.id, 1); assert.strictEqual(packAdded.quantity, 2000, 'Adding 1 packet should add 1000ml'); console.log('Testing packet removal (-1 whole packet)...'); const packRemoved = db.removePacket(testPack.id, 1); assert.strictEqual(packRemoved.quantity, 1000, 'Removing 1 packet should leave 1000ml'); db.deleteItem(testPack.id); console.log('โœ“ Whole packet tests passed!'); // Adjust grams: e.g. consumed 150g -> remaining 350g console.log('Testing partial consumption (-150g)...'); const adjusted = db.adjustQuantity(newItem.id, { delta: -150 }); assert.strictEqual(adjusted.quantity, 350); assert.strictEqual(adjusted.is_opened, 1, 'Should automatically mark as opened when grams reduced'); // Slider or direct set: e.g. set remaining to 100g console.log('Testing absolute set (100g remaining)...'); const updatedAbs = db.adjustQuantity(newItem.id, { newQuantity: 100 }); assert.strictEqual(updatedAbs.quantity, 100); // Consume remaining console.log('Testing consume all...'); const finished = db.adjustQuantity(newItem.id, { newQuantity: 0 }); assert.strictEqual(finished.quantity, 0); // Delete test item const deleted = db.deleteItem(newItem.id); assert.strictEqual(deleted, true); assert.strictEqual(db.getItemById(newItem.id), undefined); // Test stats const stats = db.getStats(); assert(typeof stats.total === 'number'); assert(typeof stats.fresh === 'number'); console.log('โœ“ All database operations passed!'); console.log('๐ŸŽ‰ All backend tests completed successfully!');