/** * Decides whether the TUI should kick off a background `fetchAllFeeds` on * open. Pure: takes the latest fetch timestamp and the staleness threshold. * * Threshold semantics: * - `> 0` — auto-fetch when the latest fetch is older than the threshold, * OR when there has never been a fetch. * - `=== 0` — disabled (never auto-fetch). Lets users opt out via * `RSS_AUTO_FETCH_HOURS=0`. */ const DEFAULT_AUTO_FETCH_HOURS = 1; const HOUR_MS = 60 * 60 * 1000; export function shouldAutoFetch(latest: Date | null, thresholdMs: number, now: Date): boolean { if (!Number.isFinite(thresholdMs) || thresholdMs <= 0) return false; if (!latest) return true; return now.getTime() - latest.getTime() > thresholdMs; } export function resolveAutoFetchThresholdMs(envValue: string | undefined): number { const fallback = DEFAULT_AUTO_FETCH_HOURS * HOUR_MS; if (envValue === undefined || envValue === '') return fallback; const value = envValue.trim(); if (!/^\d+$/.test(value)) return fallback; const parsed = Number(value); const threshold = parsed * HOUR_MS; if (!Number.isSafeInteger(parsed) || !Number.isFinite(threshold)) return fallback; return threshold; }