Files
AeroFetch/test/ytdlpPolicy.test.ts
T
debont80 81dd0ce742 fix(main): R9 — guard the yt-dlp update throttle against a backward clock
shouldAutoCheckYtdlp compared (now - lastCheck) >= interval; a backward system-
clock correction leaves lastCheck in the future so the difference is negative and
the daily check never comes due again. Treat a future lastCheck as skewed and run
the check. The renderer scheduled-item promoter is inherently fire-once (promotion
flips the item out of 'saved'), so it can't double-fire on a backward jump — a
forward jump firing early is documented and accepted as minor.

Unit-tested. typecheck + 263 tests + eslint + prettier green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 09:19:00 -04:00

42 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { shouldAutoCheckYtdlp, AUTO_UPDATE_INTERVAL_MS } from '../src/main/ytdlpPolicy'
const NOW = 1_782_435_469_486 // arbitrary fixed "now"
describe('shouldAutoCheckYtdlp', () => {
it('never checks when auto-update is disabled', () => {
expect(shouldAutoCheckYtdlp(false, 0, NOW)).toBe(false)
// …even if a full interval has elapsed.
expect(shouldAutoCheckYtdlp(false, NOW - AUTO_UPDATE_INTERVAL_MS * 2, NOW)).toBe(false)
})
it('checks on first run (lastCheck is 0 / never)', () => {
expect(shouldAutoCheckYtdlp(true, 0, NOW)).toBe(true)
})
it('skips while inside the throttle window', () => {
// Checked one minute ago → far inside the 24h window.
expect(shouldAutoCheckYtdlp(true, NOW - 60_000, NOW)).toBe(false)
// Exactly one ms short of the interval still skips.
expect(shouldAutoCheckYtdlp(true, NOW - (AUTO_UPDATE_INTERVAL_MS - 1), NOW)).toBe(false)
})
it('checks once the interval has elapsed (boundary inclusive)', () => {
expect(shouldAutoCheckYtdlp(true, NOW - AUTO_UPDATE_INTERVAL_MS, NOW)).toBe(true)
expect(shouldAutoCheckYtdlp(true, NOW - AUTO_UPDATE_INTERVAL_MS * 3, NOW)).toBe(true)
})
it('honours a custom interval', () => {
const hour = 60 * 60 * 1000
expect(shouldAutoCheckYtdlp(true, NOW - 30 * 60_000, NOW, hour)).toBe(false)
expect(shouldAutoCheckYtdlp(true, NOW - hour, NOW, hour)).toBe(true)
})
it('checks when the clock ran backward (lastCheck is in the future, R9)', () => {
// A backward clock correction leaves lastCheck > now → (now - lastCheck) is
// negative; without the guard the check would be skipped forever.
expect(shouldAutoCheckYtdlp(true, NOW + AUTO_UPDATE_INTERVAL_MS, NOW)).toBe(true)
expect(shouldAutoCheckYtdlp(true, NOW + 1, NOW)).toBe(true)
})
})