Files
biblical-guide.com/__tests__/lib/cache-manager.test.ts
2025-11-12 08:00:39 +00:00

210 lines
6.3 KiB
TypeScript

import { initDatabase, cacheChapter, getCachedChapter, clearExpiredCache } from '@/lib/cache-manager'
import { BibleChapter } from '@/types'
// Mock IndexedDB for testing
const mockIndexedDB = (() => {
let stores: Record<string, Record<string, any>> = {}
let dbVersion = 0
return {
open: (name: string, version: number) => {
const request: any = {
result: null,
error: null,
onsuccess: null,
onerror: null,
onupgradeneeded: null,
}
setTimeout(() => {
if (version > dbVersion) {
dbVersion = version
const upgradeEvent: any = {
target: {
result: {
objectStoreNames: {
contains: (name: string) => !!stores[name]
},
createObjectStore: (storeName: string, options: any) => {
stores[storeName] = {}
return {
createIndex: () => {}
}
}
}
}
}
request.onupgradeneeded?.(upgradeEvent)
}
request.result = {
transaction: (storeNames: string[], mode: string) => {
const storeName = storeNames[0]
return {
objectStore: (name: string) => {
if (!stores[name]) stores[name] = {}
return {
get: (key: string) => {
const req: any = {
result: stores[name][key],
onsuccess: null,
onerror: null
}
setTimeout(() => req.onsuccess?.(), 0)
return req
},
put: (value: any) => {
const key = value.chapterId
stores[name][key] = value
const req: any = {
onsuccess: null,
onerror: null
}
setTimeout(() => req.onsuccess?.(), 0)
return req
},
count: () => {
const req: any = {
result: Object.keys(stores[name]).length,
onsuccess: null
}
setTimeout(() => req.onsuccess?.(), 0)
return req
},
openCursor: () => {
const keys = Object.keys(stores[name])
let index = 0
const req: any = {
result: null,
onsuccess: null
}
setTimeout(() => {
if (index < keys.length) {
req.result = {
value: stores[name][keys[index]],
delete: () => {
delete stores[name][keys[index]]
},
continue: () => {
index++
setTimeout(() => {
if (index < keys.length) {
req.result = {
value: stores[name][keys[index]],
delete: () => {
delete stores[name][keys[index]]
},
continue: req.result.continue
}
} else {
req.result = null
}
req.onsuccess?.({ target: req })
}, 0)
}
}
}
req.onsuccess?.({ target: req })
}, 0)
return req
},
index: (indexName: string) => {
return {
openCursor: (range?: any) => {
const req: any = {
result: null,
onsuccess: null
}
setTimeout(() => req.onsuccess?.({ target: req }), 0)
return req
}
}
}
}
}
}
}
}
request.onsuccess?.()
}, 0)
return request
}
}
})()
// Setup mock for tests
beforeAll(() => {
;(global as any).indexedDB = mockIndexedDB
})
describe('cache-manager', () => {
const mockChapter: BibleChapter = {
id: '1-1',
bookId: 1,
bookName: 'Genesis',
chapter: 1,
verses: [
{
id: 'v1',
chapterId: '1-1',
verseNum: 1,
text: 'In the beginning God created the heaven and the earth.',
version: 'KJV',
chapter: {
chapterNum: 1,
book: {
name: 'Genesis'
}
}
}
]
}
describe('initDatabase', () => {
it('initializes the database successfully', async () => {
const db = await initDatabase()
expect(db).toBeDefined()
expect(db.transaction).toBeDefined()
})
})
describe('cacheChapter', () => {
it('caches a chapter successfully', async () => {
await cacheChapter(mockChapter)
// If no error thrown, test passes
expect(true).toBe(true)
})
it('creates cache entry with expiration', async () => {
await cacheChapter(mockChapter)
const cached = await getCachedChapter('1-1')
expect(cached).toBeDefined()
expect(cached?.id).toBe('1-1')
})
})
describe('getCachedChapter', () => {
it('returns cached chapter if not expired', async () => {
await cacheChapter(mockChapter)
const result = await getCachedChapter('1-1')
expect(result).not.toBeNull()
expect(result?.bookName).toBe('Genesis')
expect(result?.chapter).toBe(1)
})
it('returns null for non-existent chapter', async () => {
const result = await getCachedChapter('999-999')
expect(result).toBeNull()
})
})
describe('clearExpiredCache', () => {
it('runs without error', async () => {
await clearExpiredCache()
// If no error thrown, test passes
expect(true).toBe(true)
})
})
})