import { expect, it } from 'vitest'; import type { LocalDelivery } from '../../src/db'; import { activeDeliveries, deliveriesForDate, deliveryDateKey, toDateKey, valueOf, weekDates } from '../../src/analytics'; function delivery(overrides: Partial = {}): LocalDelivery { return { id: crypto.randomUUID(), customerId: crypto.randomUUID(), addressId: crypto.randomUUID(), deliveryTypeId: crypto.randomUUID(), deliveryTypeVersion: 1, shipmentUnitHundredths: 100, unitPriceAgorot: 1000, totalPriceAgorot: 1000, deliveryNumber: 1001, scheduledDate: '2026-09-06', deliverySlot: 'afternoon', specificTime: null, deliveryWindow: null, notes: '', status: 'created', version: 1, createdAt: '2026-09-04T10:00:00.000Z', updatedAt: null, cancelledAt: null, cancellationReason: null, deletedAt: null, syncState: 'synced', ...overrides, }; } it('builds an Israeli Sunday-to-Saturday week without timezone drift', () => { const days = weekDates(new Date(2026, 8, 9, 23)); expect(days.map(toDateKey)).toEqual(['2026-09-06', '2026-09-07', '2026-09-08', '2026-09-09', '2026-09-10', '2026-09-11', '2026-09-12']); }); it('uses the planned date and excludes cancelled deliveries from value totals', () => { const legacy = delivery({ scheduledDate: null }); const active = delivery(); const cancelled = delivery({ status: 'cancelled', totalPriceAgorot: 5000 }); expect(deliveryDateKey(legacy)).toBe('2026-09-04'); expect(deliveriesForDate([legacy, active, cancelled], '2026-09-06')).toHaveLength(2); expect(activeDeliveries([active, cancelled])).toEqual([active]); expect(valueOf([active, cancelled])).toBe(1000); });