PeerPresenceGraceTests.swift (1405B)
1 import Foundation 2 import Testing 3 4 @testable import Crossmate 5 6 @Suite("PeerPresence grace") 7 struct PeerPresenceGraceTests { 8 private let now = Date(timeIntervalSince1970: 10_000) 9 10 @Test("a live (future) lease is present") 11 func futureLeaseIsPresent() { 12 #expect(PeerPresence.isPresent(readAt: now.addingTimeInterval(600), asOf: now)) 13 } 14 15 @Test("a lease lapsed within the grace still counts as present") 16 func recentlyLapsedLeaseIsPresent() { 17 let lapsed = now.addingTimeInterval(-(PeerPresence.presenceGrace - 5)) 18 #expect(PeerPresence.isPresent(readAt: lapsed, asOf: now)) 19 } 20 21 @Test("a lease lapsed past the grace is absent") 22 func longLapsedLeaseIsAbsent() { 23 let lapsed = now.addingTimeInterval(-(PeerPresence.presenceGrace + 5)) 24 #expect(!PeerPresence.isPresent(readAt: lapsed, asOf: now)) 25 } 26 27 @Test("no lease is absent") 28 func noLeaseIsAbsent() { 29 #expect(!PeerPresence.isPresent(readAt: nil, asOf: now)) 30 } 31 32 @Test("the cutoff is the grace before now and agrees with isPresent") 33 func cutoffMatchesGrace() { 34 #expect(PeerPresence.presenceCutoff(asOf: now) == now.addingTimeInterval(-PeerPresence.presenceGrace)) 35 // A readAt exactly at the cutoff is not present (strictly greater wins). 36 #expect(!PeerPresence.isPresent(readAt: PeerPresence.presenceCutoff(asOf: now), asOf: now)) 37 } 38 }