|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { mergeIngredients } from './ingredientNormalizer.js'; |
| 3 | + |
| 4 | +const dish = (name, ingredients) => ({ dish_name: name, ingredients }); |
| 5 | + |
| 6 | +describe('mergeIngredients — exact-name merging only', () => { |
| 7 | + it('does NOT merge 干辣椒 and 辣椒 (different names)', () => { |
| 8 | + const out = mergeIngredients([ |
| 9 | + dish('A', [{ name: '干辣椒', amount: '40', unit: 'g', category: '香料' }]), |
| 10 | + dish('B', [{ name: '辣椒', amount: '100', unit: 'g', category: '蔬菜' }]), |
| 11 | + ]); |
| 12 | + const names = out.map(i => i.name); |
| 13 | + expect(names).toContain('干辣椒'); |
| 14 | + expect(names).toContain('辣椒'); |
| 15 | + expect(out).toHaveLength(2); |
| 16 | + }); |
| 17 | + |
| 18 | + it('does NOT merge synonyms like 西红柿 and 番茄 — keeps original names verbatim', () => { |
| 19 | + const out = mergeIngredients([ |
| 20 | + dish('A', [{ name: '西红柿', amount: '2', unit: '个', category: '蔬菜' }]), |
| 21 | + dish('B', [{ name: '番茄', amount: '1', unit: '个', category: '蔬菜' }]), |
| 22 | + ]); |
| 23 | + const names = out.map(i => i.name); |
| 24 | + expect(names).toContain('西红柿'); |
| 25 | + expect(names).toContain('番茄'); |
| 26 | + expect(out).toHaveLength(2); |
| 27 | + }); |
| 28 | + |
| 29 | + it('merges and sums ingredients whose names are exactly identical', () => { |
| 30 | + const out = mergeIngredients([ |
| 31 | + dish('A', [{ name: '辣椒', amount: '40', unit: 'g', category: '蔬菜' }]), |
| 32 | + dish('B', [{ name: '辣椒', amount: '100', unit: 'g', category: '蔬菜' }]), |
| 33 | + ]); |
| 34 | + expect(out).toHaveLength(1); |
| 35 | + expect(out[0].name).toBe('辣椒'); |
| 36 | + expect(out[0].amount).toContain('140'); |
| 37 | + expect(out[0].sources).toEqual(['A', 'B']); |
| 38 | + }); |
| 39 | + |
| 40 | + it('treats names differing only by whitespace as the same (trim)', () => { |
| 41 | + const out = mergeIngredients([ |
| 42 | + dish('A', [{ name: ' 盐 ', amount: '2', unit: 'g', category: '调料' }]), |
| 43 | + dish('B', [{ name: '盐', amount: '3', unit: 'g', category: '调料' }]), |
| 44 | + ]); |
| 45 | + expect(out).toHaveLength(1); |
| 46 | + expect(out[0].name).toBe('盐'); |
| 47 | + }); |
| 48 | +}); |
0 commit comments