Files
biblical-guide.com/__tests__/lib/highlight-sync-manager.test.ts
2025-11-12 07:50:28 +00:00

107 lines
2.6 KiB
TypeScript

import { HighlightSyncManager } from '@/lib/highlight-sync-manager'
import { BibleHighlight } from '@/types'
describe('HighlightSyncManager', () => {
let manager: HighlightSyncManager
beforeEach(() => {
manager = new HighlightSyncManager()
})
it('should add highlight to sync queue', async () => {
const highlight: BibleHighlight = {
id: 'h-1',
verseId: 'v-1',
color: 'yellow',
createdAt: Date.now(),
updatedAt: Date.now(),
syncStatus: 'pending'
}
await manager.queueHighlight(highlight)
const pending = await manager.getPendingSyncItems()
expect(pending.length).toBe(1)
expect(pending[0].id).toBe('h-1')
})
it('should mark highlight as syncing', async () => {
const highlight: BibleHighlight = {
id: 'h-1',
verseId: 'v-1',
color: 'yellow',
createdAt: Date.now(),
updatedAt: Date.now(),
syncStatus: 'pending'
}
await manager.queueHighlight(highlight)
await manager.markSyncing(['h-1'])
const syncing = await manager.getSyncingItems()
expect(syncing.length).toBe(1)
})
it('should mark highlight as synced', async () => {
const highlight: BibleHighlight = {
id: 'h-1',
verseId: 'v-1',
color: 'yellow',
createdAt: Date.now(),
updatedAt: Date.now(),
syncStatus: 'pending'
}
await manager.queueHighlight(highlight)
await manager.markSynced(['h-1'])
const pending = await manager.getPendingSyncItems()
expect(pending.length).toBe(0)
})
it('should retry sync on error', async () => {
const highlight: BibleHighlight = {
id: 'h-1',
verseId: 'v-1',
color: 'yellow',
createdAt: Date.now(),
updatedAt: Date.now(),
syncStatus: 'pending'
}
await manager.queueHighlight(highlight)
await manager.markError(['h-1'], 'Network error')
await manager.markSyncing(['h-1'])
const syncing = await manager.getSyncingItems()
expect(syncing.length).toBe(1)
})
it('should perform sync and mark items as synced', async () => {
const highlight: BibleHighlight = {
id: 'h-1',
verseId: 'v-1',
color: 'yellow',
createdAt: Date.now(),
updatedAt: Date.now(),
syncStatus: 'pending'
}
await manager.queueHighlight(highlight)
await manager.init()
// Mock fetch
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({ synced: 1, errors: [] })
})
) as jest.Mock
const result = await manager.performSync()
expect(result.synced).toBe(1)
expect(result.errors).toBe(0)
})
})