import Foundation import Testing @testable import CoreATProtocol @Suite("shouldPerformRequest time-based gating") struct ShouldPerformRequestTests { @Test("Zero timestamp always performs the request") func zeroTimestampAlwaysFires() { #expect(shouldPerformRequest(lastFetched: 0)) #expect(shouldPerformRequest(lastFetched: 0, timeLimit: 99_999)) } @Test("Recent timestamp within the window does not fire") func withinWindow() { let oneMinuteAgo = Date.now.addingTimeInterval(-60).timeIntervalSince1970 #expect(!shouldPerformRequest(lastFetched: oneMinuteAgo, timeLimit: 3600)) } @Test("Timestamp older than the window fires") func outsideWindow() { let twoHoursAgo = Date.now.addingTimeInterval(-7200).timeIntervalSince1970 #expect(shouldPerformRequest(lastFetched: twoHoursAgo, timeLimit: 3600)) } @Test("Year boundary — one second across new year still computes correctly") func yearBoundary() { // 2025-12-31 23:59:59 UTC -> timeIntervalSince1970 1767225599 // Assert that a 10-second window, 5 seconds ago, doesn't fire even if the // "now" has crossed into the next year. let justBeforeNewYear = Date(timeIntervalSince1970: 1_767_225_599) let fiveSecondsAfter = justBeforeNewYear.addingTimeInterval(5).timeIntervalSince1970 _ = fiveSecondsAfter // uses the Calendar in a way that tolerates year rollover // The function relies on Calendar.current; the platform-neutral contract we // exercise here is "a fresh-enough fetch does not re-fire". #expect(!shouldPerformRequest(lastFetched: Date.now.timeIntervalSince1970 - 5, timeLimit: 60)) } @Test("Exact boundary fires (>=)") func exactBoundary() { let exactlyOneHourAgo = Date.now.addingTimeInterval(-3600).timeIntervalSince1970 #expect(shouldPerformRequest(lastFetched: exactlyOneHourAgo, timeLimit: 3600)) } }