feat(parser): add ingredient parser
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
42
tests/unit/ingredient.test.ts
Normal file
42
tests/unit/ingredient.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseIngredient } from '../../src/lib/server/parsers/ingredient';
|
||||
|
||||
describe('parseIngredient', () => {
|
||||
it.each([
|
||||
['200 g Mehl', { quantity: 200, unit: 'g', name: 'Mehl' }],
|
||||
['1 kg Kartoffeln', { quantity: 1, unit: 'kg', name: 'Kartoffeln' }],
|
||||
['500 ml Milch', { quantity: 500, unit: 'ml', name: 'Milch' }],
|
||||
['1 TL Salz', { quantity: 1, unit: 'TL', name: 'Salz' }],
|
||||
['2 EL Olivenöl', { quantity: 2, unit: 'EL', name: 'Olivenöl' }],
|
||||
['3 Eier', { quantity: 3, unit: null, name: 'Eier' }],
|
||||
['1/2 Zitrone', { quantity: 0.5, unit: null, name: 'Zitrone' }],
|
||||
['1,5 l Wasser', { quantity: 1.5, unit: 'l', name: 'Wasser' }]
|
||||
] as const)('parses %s', (input, expected) => {
|
||||
const parsed = parseIngredient(input);
|
||||
expect(parsed.quantity).toBe(expected.quantity);
|
||||
expect(parsed.unit).toBe(expected.unit);
|
||||
expect(parsed.name).toBe(expected.name);
|
||||
expect(parsed.raw_text).toBe(input);
|
||||
});
|
||||
|
||||
it('handles notes', () => {
|
||||
const p = parseIngredient('200 g Mehl (Type 550)');
|
||||
expect(p.quantity).toBe(200);
|
||||
expect(p.name).toBe('Mehl');
|
||||
expect(p.note).toBe('Type 550');
|
||||
});
|
||||
|
||||
it('falls back to raw_text when unparsable', () => {
|
||||
const p = parseIngredient('etwas frischer Pfeffer');
|
||||
expect(p.quantity).toBeNull();
|
||||
expect(p.unit).toBeNull();
|
||||
expect(p.name).toBe('etwas frischer Pfeffer');
|
||||
expect(p.raw_text).toBe('etwas frischer Pfeffer');
|
||||
});
|
||||
|
||||
it('handles ranges by taking the lower bound', () => {
|
||||
const p = parseIngredient('2-3 Tomaten');
|
||||
expect(p.quantity).toBe(2);
|
||||
expect(p.name).toBe('Tomaten');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user