import type { ListItem } from '@superlist/shared' import { describe, expect, it } from 'vitest' import { splitItems } from './items' function item(overrides: Partial): ListItem { return { id: 'item-1', listId: 'list-1', name: 'Leche', 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, } } const NOW = new Date(2026, 9, 15, 18, 0) describe('splitItems', () => { it('keeps pending items untouched, in API order', () => { const first = item({ id: 'a', order: 1 }) const second = item({ id: 'b', order: 2 }) const { pending, boughtToday } = splitItems([first, second], NOW) expect(pending).toEqual([first, second]) expect(boughtToday).toEqual([]) }) it('moves items bought today to boughtToday, most recent first', () => { const boughtEarlier = item({ id: 'a', completed: true, boughtAt: new Date(2026, 9, 15, 9, 0).toISOString(), }) const boughtJustNow = item({ id: 'b', completed: true, boughtAt: new Date(2026, 9, 15, 17, 30).toISOString(), }) const { pending, boughtToday } = splitItems([boughtEarlier, boughtJustNow], NOW) expect(pending).toEqual([]) expect(boughtToday.map((i) => i.id)).toEqual(['b', 'a']) }) it('hides items bought on a previous local day', () => { const boughtYesterday = item({ completed: true, boughtAt: new Date(2026, 9, 14, 23, 30).toISOString(), }) const { pending, boughtToday } = splitItems([boughtYesterday], NOW) expect(pending).toEqual([]) expect(boughtToday).toEqual([]) }) it('compares in the local timezone, not in UTC', () => { // 23:30 local on Oct 15th already belongs to Oct 16th in UTC for // UTC-negative timezones; the item must still count as bought today. const boughtLateAtNight = item({ completed: true, boughtAt: new Date(2026, 9, 15, 23, 30).toISOString(), }) const lateNightNow = new Date(2026, 9, 15, 23, 45) const { boughtToday } = splitItems([boughtLateAtNight], lateNightNow) expect(boughtToday).toHaveLength(1) }) it('hides completed items without boughtAt', () => { const legacy = item({ completed: true, boughtAt: null }) const { pending, boughtToday } = splitItems([legacy], NOW) expect(pending).toEqual([]) expect(boughtToday).toEqual([]) }) })