freshyo/apps/backend/tests/api/cache-consistency.test.ts
2026-09-26 17:50:33 +05:30

141 lines
4.7 KiB
TypeScript

/**
* Cross-file cache invariants — the relationships the app relies on when it
* merges these files at runtime.
*
* Two of these can legitimately lag: `slots.json` is rewritten synchronously on
* a slot mutation while `products.json` only rebuilds ~30s later (CACHE_CREATOR),
* and `essential-consts.json` embeds the versions that were current when it was
* written. So the version-sensitive checks re-read the current versions and poll.
*/
import {
availabilityCacheUrl,
essentialConsts,
essentialConstsCacheUrl,
fetchCacheJson,
pollUntil,
productsCacheUrl,
slotsCacheUrl,
storesCacheUrl,
storeDetailCacheUrl,
} from '../helpers/cache'
interface ProductsCache {
products: { id: number }[]
count: number
}
interface AvailabilityCache {
availability: { id: number }[]
count: number
}
interface SlotsCache {
slots: { id: number; products: { id: number }[] }[]
}
interface StoresCache {
stores: { id: number; productCount: number }[]
}
interface StoreDetailCache {
store: { id: number }
products: unknown[]
}
interface EssentialConstsCache {
cacheVersion: number
slotsVersionNum: number
availabilityVersionNum: number
}
describe('cache consistency', () => {
it('every availability entry maps to a product in products.json', async () => {
// availability.json is bumped synchronously by stock toggles, so it can lead
// products.json (which waits for the rebuild) — poll for them to converge.
await pollUntil<number[]>(
'every availability id to appear in products.json',
async () => {
const consts = await essentialConsts()
const [products, availability] = await Promise.all([
fetchCacheJson<ProductsCache>(productsCacheUrl(consts, consts.cacheVersion)),
fetchCacheJson<AvailabilityCache>(
availabilityCacheUrl(consts, consts.availabilityVersionNum)
),
])
const productIds = new Set(products.products.map((p) => p.id))
const orphans = availability.availability
.map((a) => a.id)
.filter((id) => !productIds.has(id))
return orphans.length === 0 ? [] : null
},
{ timeoutMs: 120_000, intervalMs: 5_000 }
)
})
it('availability count matches the number of entries', async () => {
const consts = await essentialConsts()
const availability = await fetchCacheJson<AvailabilityCache>(
availabilityCacheUrl(consts, consts.availabilityVersionNum)
)
expect(availability.count).toBe(availability.availability.length)
})
it('every product referenced by slots.json exists in products.json', async () => {
// slots.json is synchronous; products.json catches up via the rebuild, so poll.
await pollUntil<number[]>(
'every product id referenced by slots.json to appear in products.json',
async () => {
const consts = await essentialConsts()
const [products, slots] = await Promise.all([
fetchCacheJson<ProductsCache>(productsCacheUrl(consts, consts.cacheVersion)),
fetchCacheJson<SlotsCache>(slotsCacheUrl(consts, consts.slotsVersionNum)),
])
const productIds = new Set(products.products.map((p) => p.id))
const referenced = new Set<number>()
slots.slots.forEach((slot) => slot.products.forEach((p) => referenced.add(p.id)))
const orphans = [...referenced].filter((id) => !productIds.has(id))
return orphans.length === 0 ? [] : null
},
{ timeoutMs: 120_000, intervalMs: 5_000 }
)
})
it('essential-consts.json is self-consistent with the version in its path', async () => {
const consts = await essentialConsts()
const cache = await fetchCacheJson<EssentialConstsCache>(
essentialConstsCacheUrl(consts, consts.cacheVersion)
)
// The file lives at v-<cacheVersion>, so it must report that same version.
expect(cache.cacheVersion).toBe(consts.cacheVersion)
// The embedded slots/availability versions are a snapshot at write time and
// may lag the live ones (slots are bumped independently) — assert shape only.
expect(typeof cache.slotsVersionNum).toBe('number')
expect(typeof cache.availabilityVersionNum).toBe('number')
})
it('stores.json productCount matches each store detail file', async () => {
const consts = await essentialConsts()
const stores = await fetchCacheJson<StoresCache>(
storesCacheUrl(consts, consts.cacheVersion)
)
expect(stores.stores.length).toBeGreaterThan(0)
const store = stores.stores[0]
const detail = await fetchCacheJson<StoreDetailCache>(
storeDetailCacheUrl(consts, consts.cacheVersion, store.id)
)
expect(detail.store.id).toBe(store.id)
expect(detail.products).toHaveLength(store.productCount)
})
})