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

59 lines
1.8 KiB
TypeScript

/**
* availability.json — regenerated SYNCHRONOUSLY by
* admin.product.toggleOutOfStock (it calls createAvailabilityCacheFile()),
* so the file can be read immediately after the mutation.
*/
import { apiClient } from '../helpers/api'
import {
availabilityCacheUrl,
essentialConsts,
fetchCacheJson,
} from '../helpers/cache'
import { createStoreWithSku, type StoreWithSku } from '../helpers/fixtures'
import { staffSession } from '../helpers/sessions'
interface AvailabilityCache {
availability: { id: number; isOutOfStock: boolean }[]
count: number
}
describe('availability.json cache', () => {
let store: StoreWithSku
beforeAll(async () => {
store = await createStoreWithSku()
})
it('reflects an out-of-stock toggle for the new SKU', async () => {
const staff = await staffSession()
const admin = apiClient(staff.token)
const before = await essentialConsts()
const toggled = await admin.admin.product.toggleOutOfStock.mutate({
id: store.productId,
})
expect(toggled.isOutOfStock).toBe(true)
const after = await essentialConsts()
expect(after.availabilityVersionNum).toBeGreaterThan(before.availabilityVersionNum)
const cache = await fetchCacheJson<AvailabilityCache>(
availabilityCacheUrl(after, after.availabilityVersionNum)
)
// availability.json is keyed by SKU id (not product id).
const entry = cache.availability.find((a) => a.id === store.skuId)
expect(entry).toBeDefined()
expect(entry!.isOutOfStock).toBe(true)
})
it('has a count matching the number of entries', async () => {
const current = await essentialConsts()
const cache = await fetchCacheJson<AvailabilityCache>(
availabilityCacheUrl(current, current.availabilityVersionNum)
)
expect(cache.count).toBe(cache.availability.length)
})
})