78 lines
3.1 KiB
JavaScript
78 lines
3.1 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');
|
|
|
|
// 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!');
|