Files
me0nline 81b80d3bc5
Build and Push Fridger Container / Build & Push Fridger Image (push) Successful in 1m16s
feat: automatically remove items from stock when empty (quantity <= 0)
2026-10-10 23:59:10 +02:00

107 lines
4.4 KiB
JavaScript

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 -> should automatically remove from stock
console.log('Testing consume all (auto-remove on empty)...');
const finished = db.adjustQuantity(newItem.id, { newQuantity: 0 });
assert.strictEqual(finished.quantity, 0);
assert.strictEqual(finished.deleted, true, 'Result should indicate item was deleted');
assert.strictEqual(db.getItemById(newItem.id), undefined, 'Item should be automatically removed from DB when empty');
// Test packet removal to 0 -> should automatically remove from stock
console.log('Testing packet removal to 0 (auto-remove on empty)...');
const autoRemovePack = db.createItem({
name: 'Test Packet Auto-Remove',
quantity: 250,
initial_quantity: 250,
unit: 'g'
});
const packFinished = db.removePacket(autoRemovePack.id, 1);
assert.strictEqual(packFinished.quantity, 0);
assert.strictEqual(packFinished.deleted, true);
assert.strictEqual(db.getItemById(autoRemovePack.id), undefined, 'Packet should be automatically removed when 0 left');
// Test stats
const stats = db.getStats();
assert(typeof stats.total === 'number');
assert(typeof stats.full === 'number');
assert(typeof stats.opened === 'number');
console.log('✓ All database operations passed!');
console.log('🎉 All backend tests completed successfully!');