Files
Fridger/test.js
T
me0nline 7b24efc977
Build and Push Fridger Container / Build & Push Fridger Image (push) Successful in 1m20s
feat(freezer): remove expiration tracking for freezer compatibility and clean stock management
2026-10-10 23:51:57 +02:00

97 lines
3.8 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
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.full === 'number');
assert(typeof stats.opened === 'number');
console.log('✓ All database operations passed!');
console.log('🎉 All backend tests completed successfully!');