/** * centralSlotStore.setSlotsData — the rebuild of productSlotsMap on every slots * or availability refresh. * * The cart reconciler and the out-of-stock rules both read this map, so a stale * or partial rebuild silently changes what the user can buy. store staleness * beyond the map itself (query-level races) is listed in tests/KNOWN-GAPS.md. */ import { useCentralSlotStore } from '@/src/store/centralSlotStore' type AvailabilityEntry = { id: number isOutOfStock?: boolean isFlashAvailable?: boolean isSuspended?: boolean } const slot = (id: number, productIds: number[]) => ({ id, deliveryTime: new Date(Date.now() + 3 * 3600_000).toISOString(), freezeTime: new Date(Date.now() + 2 * 3600_000).toISOString(), products: productIds.map((productId) => ({ id: productId, images: [] })), }) const product = (id: number) => ({ id }) const reset = () => useCentralSlotStore.setState({ slots: [], productSlotsMap: {}, isSlotsLoaded: false, refetchSlots: null, setSlotsData: useCentralSlotStore.getState().setSlotsData, setRefetchSlots: useCentralSlotStore.getState().setRefetchSlots, }) const setup = ( slots: unknown[], productAvailability: unknown[], availability: AvailabilityEntry[] ) => useCentralSlotStore.getState().setSlotsData(slots as never, productAvailability as never, availability as never) const map = () => useCentralSlotStore.getState().productSlotsMap describe('centralSlotStore.setSlotsData', () => { beforeEach(reset) it('creates an entry for every product in productAvailability', () => { setup([], [product(1), product(2)], []) expect(Object.keys(map()).sort()).toEqual(['1', '2']) expect(map()[1].slots).toEqual([]) expect(map()[1].isOutOfStock).toBe(false) }) it('merges the availability flags onto each product', () => { setup([], [product(1), product(2), product(3)], [ { id: 1, isOutOfStock: true }, { id: 2, isFlashAvailable: true }, { id: 3, isSuspended: true }, ]) expect(map()[1]).toMatchObject({ isOutOfStock: true, isFlashAvailable: false, isSuspended: false }) expect(map()[2]).toMatchObject({ isOutOfStock: false, isFlashAvailable: true, isSuspended: false }) expect(map()[3]).toMatchObject({ isOutOfStock: false, isFlashAvailable: false, isSuspended: true }) }) it('defaults every flag to false when the product has no availability row', () => { setup([], [product(1)], []) expect(map()[1]).toEqual({ slots: [], isOutOfStock: false, isFlashAvailable: false, isSuspended: false }) }) it('attaches each slot to every product it carries', () => { setup([slot(1849, [1, 2]), slot(1847, [1])], [product(1), product(2)], []) expect(map()[1].slots.map((s: { id: number }) => s.id)).toEqual([1849, 1847]) expect(map()[2].slots.map((s: { id: number }) => s.id)).toEqual([1849]) }) it('ignores a slot product that is not in productAvailability', () => { setup([slot(1849, [1, 99])], [product(1)], []) expect(Object.keys(map())).toEqual(['1']) expect(map()[1].slots).toHaveLength(1) }) it('marks the slots loaded so the cart reconciler can run', () => { expect(useCentralSlotStore.getState().isSlotsLoaded).toBe(false) setup([], [product(1)], []) expect(useCentralSlotStore.getState().isSlotsLoaded).toBe(true) }) /** * A refresh rebuilds the map from scratch rather than merging, so a product * that drops out of the cache disappears entirely — the cart reconciler and * the out-of-stock checks then see it as unknown. */ it('replaces the map wholesale on a refresh, dropping vanished products', () => { setup([slot(1849, [1, 2])], [product(1), product(2)], []) expect(Object.keys(map()).sort()).toEqual(['1', '2']) setup([slot(1850, [1])], [product(1)], []) expect(Object.keys(map())).toEqual(['1']) expect(map()[1].slots.map((s: { id: number }) => s.id)).toEqual([1850]) }) it('keeps the availability flags when the same product is refreshed', () => { setup([], [product(1)], [{ id: 1, isOutOfStock: true }]) expect(map()[1].isOutOfStock).toBe(true) setup([], [product(1)], []) // The availability row is gone, so the flag resets. expect(map()[1].isOutOfStock).toBe(false) }) })