← All repositories

crossmate

A collaborative crossword app for iPhone and iPad

git clone https://code.inqk.net/crossmate.git


Tests/Unit/Sync/PeerPresenceGraceTests.swift (1447B)

1import Foundation
2import Testing
3
4@testable import Crossmate
5
6@Suite("PeerPresence grace")
7struct 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(presenceUntil: 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(presenceUntil: 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(presenceUntil: lapsed, asOf: now))
25    }
26
27    @Test("no lease is absent")
28    func noLeaseIsAbsent() {
29        #expect(!PeerPresence.isPresent(presenceUntil: 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 presenceUntil exactly at the cutoff is not present (strictly greater wins).
36        #expect(!PeerPresence.isPresent(presenceUntil: PeerPresence.presenceCutoff(asOf: now), asOf: now))
37    }
38}