170 lines
5.7 KiB
TypeScript
170 lines
5.7 KiB
TypeScript
/**
|
|
* TEST-ONLY unit tests for the `CacheCreator` Durable Object.
|
|
*
|
|
* Why this file exists
|
|
* --------------------
|
|
* The live API suite no longer waits on the DO (it reads the scaffold functions
|
|
* through `/api/test/cache/*`). This file keeps direct coverage of the DO's own
|
|
* logic — debounce, coalescing, global-vs-regional routing, alarm drain — without
|
|
* a real alarm delay, a workerd process, or any S3/D1 work.
|
|
*
|
|
* How
|
|
* ---
|
|
* - `CacheCreator` is a plain class: it is instantiated with a fake `state`
|
|
* (in-memory storage + alarm) and a fake `env`.
|
|
* - `initializeAllStores` and `ensureWorkerInit` are mocked, so only the DO's
|
|
* scheduling/routing is under test.
|
|
* - `jest.setSystemTime` drives the debounce guard deterministically.
|
|
*/
|
|
import { initializeAllStores } from '@/src/stores/store-initializer'
|
|
import { CacheCreator } from '@/src/jobs/cache-creator'
|
|
|
|
jest.mock('@/src/stores/store-initializer', () => ({
|
|
initializeAllStores: jest.fn(async () => undefined),
|
|
}))
|
|
jest.mock('@/src/lib/worker-init', () => ({
|
|
ensureWorkerInit: jest.fn(),
|
|
}))
|
|
|
|
const mockedInitializeAllStores = initializeAllStores as jest.MockedFunction<typeof initializeAllStores>
|
|
|
|
function makeState() {
|
|
const map = new Map<string, any>()
|
|
let alarm: number | null = null
|
|
return {
|
|
storage: {
|
|
get: async (key: string) => map.get(key),
|
|
put: async (key: string, value: any) => {
|
|
map.set(key, value)
|
|
},
|
|
delete: async (key: string) => map.delete(key),
|
|
deleteAll: async () => map.clear(),
|
|
setAlarm: async (when: number) => {
|
|
alarm = when
|
|
},
|
|
getAlarm: async () => alarm,
|
|
},
|
|
map,
|
|
getAlarm: () => alarm,
|
|
}
|
|
}
|
|
|
|
function makeCacheCreator(options: { delayMs?: string } = {}) {
|
|
const state = makeState()
|
|
const env = options.delayMs == null ? {} : { CACHE_ALARM_DELAY_MS: options.delayMs }
|
|
const instance = new CacheCreator(state as any, env as any)
|
|
return { state, instance }
|
|
}
|
|
|
|
beforeEach(() => {
|
|
jest.useFakeTimers()
|
|
jest.setSystemTime(new Date('2026-01-01T00:00:00.000Z'))
|
|
mockedInitializeAllStores.mockClear()
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.useRealTimers()
|
|
})
|
|
|
|
describe('CacheCreator.schedule', () => {
|
|
it('records a regional request, dedupes a burst, and schedules the alarm', async () => {
|
|
const { state, instance } = makeCacheCreator({ delayMs: '30000' })
|
|
|
|
await instance.schedule(1)
|
|
await instance.schedule(1)
|
|
await instance.schedule(2)
|
|
|
|
expect(state.map.get('pendingRegions')).toEqual([1, 2])
|
|
expect(state.map.get('allRegionsPending')).toBeUndefined()
|
|
expect(state.map.get('lastTrigger')).toBe(Date.now())
|
|
expect(state.getAlarm()).toBe(Date.now() + 30_000)
|
|
})
|
|
|
|
it('a global request wins and is never narrowed by a later regional request', async () => {
|
|
const { state, instance } = makeCacheCreator()
|
|
|
|
await instance.schedule(1)
|
|
await instance.schedule() // global
|
|
await instance.schedule(3) // must not narrow the global run
|
|
|
|
expect(state.map.get('allRegionsPending')).toBe(true)
|
|
expect(state.map.get('pendingRegions')).toBeUndefined()
|
|
})
|
|
|
|
it('honours CACHE_ALARM_DELAY_MS (0 fires immediately)', async () => {
|
|
const { state, instance } = makeCacheCreator({ delayMs: '0' })
|
|
await instance.schedule(1)
|
|
expect(state.getAlarm()).toBe(Date.now())
|
|
})
|
|
})
|
|
|
|
describe('CacheCreator.alarm', () => {
|
|
it('does nothing before the debounce window has elapsed', async () => {
|
|
const { instance } = makeCacheCreator({ delayMs: '30000' })
|
|
await instance.schedule(1)
|
|
|
|
await instance.alarm()
|
|
expect(mockedInitializeAllStores).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('drains the queued regions into one rebuild per region after the window', async () => {
|
|
const { state, instance } = makeCacheCreator({ delayMs: '30000' })
|
|
|
|
await instance.schedule(1)
|
|
await instance.schedule(1)
|
|
await instance.schedule(2)
|
|
|
|
jest.setSystemTime(Date.now() + 30_001)
|
|
await instance.alarm()
|
|
|
|
expect(mockedInitializeAllStores).toHaveBeenCalledTimes(2)
|
|
expect(mockedInitializeAllStores).toHaveBeenNthCalledWith(1, 1)
|
|
expect(mockedInitializeAllStores).toHaveBeenNthCalledWith(2, 2)
|
|
// drained
|
|
expect(state.map.get('pendingRegions')).toBeUndefined()
|
|
expect(state.map.get('allRegionsPending')).toBeUndefined()
|
|
})
|
|
|
|
it('a global request rebuilds every region (no region argument)', async () => {
|
|
const { instance } = makeCacheCreator({ delayMs: '30000' })
|
|
|
|
await instance.schedule()
|
|
jest.setSystemTime(Date.now() + 30_001)
|
|
await instance.alarm()
|
|
|
|
expect(mockedInitializeAllStores).toHaveBeenCalledTimes(1)
|
|
expect(mockedInitializeAllStores).toHaveBeenCalledWith()
|
|
})
|
|
|
|
it('an empty queue (no pending regions) means all regions', async () => {
|
|
const { state, instance } = makeCacheCreator({ delayMs: '30000' })
|
|
|
|
// Simulate a stale lastTrigger with no pending entries.
|
|
state.map.set('lastTrigger', Date.now() - 60_000)
|
|
await instance.alarm()
|
|
|
|
expect(mockedInitializeAllStores).toHaveBeenCalledTimes(1)
|
|
expect(mockedInitializeAllStores).toHaveBeenCalledWith()
|
|
})
|
|
})
|
|
|
|
describe('CacheCreator.fetch', () => {
|
|
it('POST /clear wipes the scheduling state', async () => {
|
|
const { state, instance } = makeCacheCreator()
|
|
await instance.schedule(1)
|
|
expect(state.map.size).toBeGreaterThan(0)
|
|
|
|
const res = await instance.fetch(new Request('https://cache-creator/clear', { method: 'POST' }))
|
|
expect(res.status).toBe(200)
|
|
expect(state.map.size).toBe(0)
|
|
})
|
|
|
|
it('POST /schedule?regionId=2 queues that region', async () => {
|
|
const { state, instance } = makeCacheCreator()
|
|
const res = await instance.fetch(
|
|
new Request('https://cache-creator/schedule?regionId=2', { method: 'POST' })
|
|
)
|
|
expect(res.status).toBe(200)
|
|
expect(state.map.get('pendingRegions')).toEqual([2])
|
|
})
|
|
})
|