import type { ListItem } from '@superlist/shared' import { describe, expect, it } from 'vitest' import { findDuplicate, mergeBatch, mergeInto } from './merge' import { parseItemInput } from './parse-item' function item(overrides: Partial): ListItem { return { id: 'item-1', listId: 'list-1', name: 'Jamón', completed: false, boughtAt: null, quantity: null, notes: null, order: 0, createdAt: '2026-10-15T10:00:00.000Z', updatedAt: '2026-10-15T10:00:00.000Z', ...overrides, } } describe('findDuplicate', () => { it('matches pending items ignoring case, accents and a final "s"', () => { const jamon = item({}) const pascualinas = item({ id: 'item-2', name: 'Pascualinas' }) expect(findDuplicate([jamon, pascualinas], 'jamon')).toBe(jamon) expect(findDuplicate([jamon, pascualinas], 'Pascualina')).toBe(pascualinas) expect(findDuplicate([jamon], 'Queso')).toBeUndefined() }) it('ignores items already bought', () => { expect(findDuplicate([item({ completed: true })], 'Jamón')).toBeUndefined() }) }) describe('mergeInto', () => { it('sums quantities and appends new notes', () => { expect( mergeInto({ quantity: '400 g', notes: 'Feteado' }, { quantity: '200 g', notes: 'Cocido' }), ).toEqual({ quantity: '600 g', notes: 'Feteado · Cocido' }) }) it('leaves out what does not change', () => { expect( mergeInto({ quantity: '400 g', notes: 'Feteado' }, { quantity: null, notes: 'feteado' }), ).toEqual({}) }) }) describe('mergeBatch', () => { it('folds repeated items of one input', () => { const merged = mergeBatch(parseItemInput('pan, leche, 2 panes')) expect(merged.map(({ name, quantity }) => ({ name, quantity }))).toEqual([ { name: 'Pan', quantity: '3' }, { name: 'Leche', quantity: null }, ]) }) })