Files
SubTracker/apps/api/tests/unit/worker-lite-cache.test.ts
SmileQWQ dba4be9986 refactor: remove worker KV state and D1 transaction-heavy paths
Move worker state from KV to D1 or memory.
Clear affected page queries after Wallos import and avoid D1 transaction-heavy Prisma paths.
2026-04-23 21:33:34 +08:00

41 lines
1.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
describe('worker-lite memory cache', () => {
beforeEach(() => {
vi.resetModules()
})
it('reuses in-memory values within ttl and inflight window', async () => {
const { withWorkerLiteCache } = await import('../../src/services/worker-lite-cache.service')
const loader = vi.fn(async () => 'cached-value')
const [first, second] = await Promise.all([
withWorkerLiteCache('settings', 'app-settings', loader, 30),
withWorkerLiteCache('settings', 'app-settings', loader, 30)
])
expect(first).toBe('cached-value')
expect(second).toBe('cached-value')
expect(loader).toHaveBeenCalledTimes(1)
const third = await withWorkerLiteCache('settings', 'app-settings', loader, 30)
expect(third).toBe('cached-value')
expect(loader).toHaveBeenCalledTimes(1)
})
it('clears current-isolate entries on namespace invalidation', async () => {
const { invalidateWorkerLiteCache, withWorkerLiteCache } = await import('../../src/services/worker-lite-cache.service')
let version = 0
const loader = vi.fn(async () => ({ version: ++version }))
const first = await withWorkerLiteCache('subscriptions', 'all', loader, 30)
expect(first).toEqual({ version: 1 })
await invalidateWorkerLiteCache(['subscriptions'])
const second = await withWorkerLiteCache('subscriptions', 'all', loader, 30)
expect(second).toEqual({ version: 2 })
expect(loader).toHaveBeenCalledTimes(2)
})
})