freshyo/apps/backend/tests/unit/slot-sku-cleanup.test.ts
2026-10-25 10:30:05 +05:30

35 lines
1.5 KiB
TypeScript

/**
* Unit tests for the slot skuIds retention window.
*
* `staleSlotsCleanup` used to walk a rolling "20 newest rows by id" window of
* `delivery_slot_info` and clear one row per slot creation. A burst of creations
* (test suites/imports) pushed that window through upcoming slots and wiped
* their `sku_ids`, which the customer app reads as "no delivery slot" -> every
* product out of stock. The cleanup is now time-based: only slots delivered more
* than 36 hours ago lose their sku list.
*/
import { SLOT_SKU_RETENTION_HOURS, slotSkuCleanupCutoff } from '@/src/dbService'
const NOW = new Date('2026-10-25T12:00:00.000Z')
const HOUR_MS = 60 * 60 * 1000
describe('slot sku retention', () => {
it('retains skuIds for 36 hours after delivery', () => {
expect(SLOT_SKU_RETENTION_HOURS).toBe(36)
expect(slotSkuCleanupCutoff(NOW).toISOString()).toBe('2026-10-24T00:00:00.000Z')
})
it('only cuts off slots delivered at/ before the cutoff', () => {
const cutoff = slotSkuCleanupCutoff(NOW)
const isCleared = (delivery: Date) => delivery.getTime() <= cutoff.getTime()
// Upcoming and recently delivered slots are never cleared.
expect(isCleared(new Date(NOW.getTime() + HOUR_MS))).toBe(false)
expect(isCleared(new Date(NOW.getTime() - 35 * HOUR_MS))).toBe(false)
expect(isCleared(new Date('2026-10-26T02:30:00.000Z'))).toBe(false)
// Slots at or beyond the 36h boundary are cleared.
expect(isCleared(cutoff)).toBe(true)
expect(isCleared(new Date(NOW.getTime() - 37 * HOUR_MS))).toBe(true)
})
})