/** * Cache regeneration for the writes the existing cache suites do not cover: * price updates (availability, synchronous), slot updates (slots.json, * synchronous) and banner creation (products.json, eventual via CACHE_CREATOR). * * The write paths differ: toggleOutOfStock/updateProductPrices/slot mutations * rewrite their cache file inline, while product/store/banner/tag changes go * through scheduleStoreInitialization (a Durable Object that rebuilds ~30s * later). See apps/backend/src/lib/cloud_cache.ts. */ import { apiClient } from '../helpers/api' import { availabilityCacheUrl, essentialConsts, fetchCacheJson, pollUntil, slotsCacheUrl, type EssentialConsts, } from '../helpers/cache' import { createFutureSlot, createStoreWithSku } from '../helpers/fixtures' import { staffSession } from '../helpers/sessions' interface AvailabilityCache { availability: { id: number; price: number }[] count: number } interface SlotsCache { slots: { id: number; deliveryTime: string; freezeTime: string }[] } describe('cache invalidation', () => { let admin: ReturnType beforeAll(async () => { const staff = await staffSession() admin = apiClient(staff.token) }) it('refreshes availability.json synchronously on updateProductPrices', async () => { const store = await createStoreWithSku({ price: 199 }) const before = await essentialConsts() await admin.admin.product.updateProductPrices.mutate({ updates: [{ productId: store.productId, price: 250 }], }) const after = await essentialConsts() expect(after.availabilityVersionNum).toBeGreaterThan(before.availabilityVersionNum) const cache = await fetchCacheJson( availabilityCacheUrl(after, after.availabilityVersionNum) ) // availability.json is keyed by SKU id. const entry = cache.availability.find((a) => a.id === store.skuId) expect(entry).toBeDefined() expect(entry!.price).toBe(250) }) it('refreshes slots.json synchronously on updateSlot', async () => { const store = await createStoreWithSku() const slot = await createFutureSlot([store.skuId]) const newDelivery = new Date(Date.now() + 9 * 3600_000).toISOString() const newFreeze = new Date(Date.now() + 8 * 3600_000).toISOString() const before = await essentialConsts() await admin.admin.slots.updateSlot.mutate({ id: slot.slotId, deliveryTime: newDelivery, freezeTime: newFreeze, isActive: true, skuIds: [store.skuId], }) const after = await essentialConsts() expect(after.slotsVersionNum).toBeGreaterThan(before.slotsVersionNum) const cache = await fetchCacheJson( slotsCacheUrl(after, after.slotsVersionNum) ) const found = cache.slots.find((s) => s.id === slot.slotId) expect(found).toBeDefined() expect(new Date(found!.deliveryTime).toISOString()).toBe(newDelivery) }) it('rebuilds the versioned cache eventually after creating a banner', async () => { const before = await essentialConsts() await admin.admin.banner.createBanner.mutate({ name: `API banner ${Date.now()}`, imageUrl: 'https://assets.freshyo.in/api-cache/test-banner.png', }) const advanced = await pollUntil( `cacheVersion to advance past ${before.cacheVersion} after a banner create`, async () => { const now = await essentialConsts() return now.cacheVersion > before.cacheVersion ? now : null }, // The CACHE_CREATOR Durable Object rebuilds ~30s after the write, but can // lag when the suite has queued many writes — mirror cache-products' 180s. { timeoutMs: 180_000, intervalMs: 5_000 } ) expect(advanced.cacheVersion).toBeGreaterThan(before.cacheVersion) }, 210_000) })