feat(shopping): consolidate() fuer g/kg + ml/l Summierung

Implementiert consolidate() in unit-consolidation.ts: summiert Mengen
innerhalb einer Unit-Family (Gewicht g/kg, Volumen ml/l) mit automatischer
Promotion ab Schwellwert; nicht-family-units werden direkt summiert.
quantity=null wird als 0 behandelt; alle-null ergibt null-Ergebnis.
9 neue Tests, alle 14 Tests gruen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-04-22 16:50:24 +02:00
parent 29f0245ce0
commit c6a549699a
2 changed files with 128 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { unitFamily } from '../../src/lib/server/unit-consolidation';
import { unitFamily, consolidate } from '../../src/lib/server/unit-consolidation';
describe('unitFamily', () => {
it('maps g and kg to weight', () => {
@@ -26,3 +26,75 @@ describe('unitFamily', () => {
expect(unitFamily(' ')).toBe('');
});
});
describe('consolidate', () => {
it('kombiniert 500 g + 1 kg zu 1,5 kg', () => {
const out = consolidate([
{ quantity: 500, unit: 'g' },
{ quantity: 1, unit: 'kg' }
]);
expect(out).toEqual({ quantity: 1.5, unit: 'kg' });
});
it('bleibt bei g wenn Summe < 1 kg', () => {
const out = consolidate([
{ quantity: 200, unit: 'g' },
{ quantity: 300, unit: 'g' }
]);
expect(out).toEqual({ quantity: 500, unit: 'g' });
});
it('kombiniert ml + l analog (400 ml + 0,5 l → 900 ml)', () => {
const out = consolidate([
{ quantity: 400, unit: 'ml' },
{ quantity: 0.5, unit: 'l' }
]);
expect(out).toEqual({ quantity: 900, unit: 'ml' });
});
it('promoted zu l ab 1000 ml (0,5 l + 0,8 l → 1,3 l)', () => {
const out = consolidate([
{ quantity: 0.5, unit: 'l' },
{ quantity: 0.8, unit: 'l' }
]);
expect(out).toEqual({ quantity: 1.3, unit: 'l' });
});
it('summiert gleiche nicht-family-units (2 Bund + 1 Bund → 3 Bund)', () => {
const out = consolidate([
{ quantity: 2, unit: 'Bund' },
{ quantity: 1, unit: 'Bund' }
]);
expect(out).toEqual({ quantity: 3, unit: 'Bund' });
});
it('behandelt quantity=null als 0', () => {
const out = consolidate([
{ quantity: null, unit: 'TL' },
{ quantity: 1, unit: 'TL' }
]);
expect(out).toEqual({ quantity: 1, unit: 'TL' });
});
it('gibt null zurueck wenn alle quantities null sind', () => {
const out = consolidate([
{ quantity: null, unit: 'Prise' },
{ quantity: null, unit: 'Prise' }
]);
expect(out).toEqual({ quantity: null, unit: 'Prise' });
});
it('rundet Float-Artefakte auf 2 Dezimalen (0,1 + 0,2 kg → 0,3 kg)', () => {
const out = consolidate([
{ quantity: 0.1, unit: 'kg' },
{ quantity: 0.2, unit: 'kg' }
]);
// 0.1 + 0.2 in kg = 0.3 kg, in g = 300 → promoted? 300 < 1000 → 300 g
expect(out).toEqual({ quantity: 300, unit: 'g' });
});
it('nimmt unit vom ersten Eintrag bei unbekannter family', () => {
const out = consolidate([{ quantity: 5, unit: 'Stück' }]);
expect(out).toEqual({ quantity: 5, unit: 'Stück' });
});
});