import { describe, expect, it } from 'vitest' import { backgroundSyncReducer, initialBackgroundSyncState, planSync, } from '../src/lib/sync' describe('background-sync decision', () => { it('syncs a fresh foregrounded state', () => { expect(planSync(initialBackgroundSyncState, 1_000)).toBe(true) }) it('does not sync again while a sync is already in flight', () => { const syncing = backgroundSyncReducer(initialBackgroundSyncState, { type: 'syncStarted', at: 1_000 }) expect(planSync(syncing, 1_100)).toBe(false) }) it('respects the interval gate after a recent attempt, but a forced pull bypasses it', () => { let s = backgroundSyncReducer(initialBackgroundSyncState, { type: 'syncStarted', at: 1_000 }) s = backgroundSyncReducer(s, { type: 'syncSucceeded', at: 1_050 }) // 1 minute later — inside the 5-minute interval → no auto sync… expect(planSync(s, 1_050 + 60_000)).toBe(false) // …but a manual pull forces it. expect(planSync(s, 1_050 + 60_000, true)).toBe(true) // 5+ minutes later the interval gate opens. expect(planSync(s, 1_050 + 6 * 60_000)).toBe(true) }) it('does not auto-sync while backgrounded, but a forced sync still runs', () => { const bg = backgroundSyncReducer(initialBackgroundSyncState, { type: 'background' }) expect(planSync(bg, 10_000)).toBe(false) expect(planSync(bg, 10_000, true)).toBe(true) }) })