Files
Scarriffle dccf8671b2 Add 'CalendarrKit/' from commit '9ea1a8705d608b1aa476c0b35685c57466bb0c50'
git-subtree-dir: CalendarrKit
git-subtree-mainline: 8bc32faa7f
git-subtree-split: 9ea1a8705d
2026-10-19 05:34:15 +02:00

77 lines
3.4 KiB
Swift

import Foundation
import Testing
@testable import CalendarrCore
struct WatchPushPolicyTests {
private static let now = Date(timeIntervalSince1970: 1_789_000_000)
private static func event(_ id: String, startOffset: TimeInterval,
duration: TimeInterval = 3600) -> SnapshotEvent {
SnapshotEvent(id: id, title: id,
start: now.addingTimeInterval(startOffset),
end: now.addingTimeInterval(startOffset + duration),
isAllDay: false, colorHex: "#4285f4", location: "", calendarKey: "local:1")
}
private static func snapshot(_ events: [SnapshotEvent]) -> CalendarrSnapshot {
CalendarrSnapshot(writtenAt: now,
coverageStart: now.addingTimeInterval(-86400),
coverageEnd: now.addingTimeInterval(14 * 86400),
isLoggedIn: true, writerVersion: "1.0", events: events,
theme: SnapshotTheme(today: "#1", text: "#2", background: "#3",
line: "#4", primary: "#5", accent: "#6"),
language: "de")
}
@Test("The next event is the first one that has not ended yet")
func picksFirstUnfinished() {
let snap = Self.snapshot([Self.event("past", startOffset: -7200),
Self.event("running", startOffset: -600),
Self.event("later", startOffset: 3600)])
#expect(WatchPushPolicy.nextEventIdentity(in: snap, at: Self.now)?.id == "running")
}
@Test("An empty calendar has no next event")
func emptyHasNoIdentity() {
#expect(WatchPushPolicy.nextEventIdentity(in: Self.snapshot([]), at: Self.now) == nil)
}
@Test("Events are considered in start order regardless of input order")
func sortsBeforePicking() {
let snap = Self.snapshot([Self.event("late", startOffset: 7200),
Self.event("soon", startOffset: 1800)])
#expect(WatchPushPolicy.nextEventIdentity(in: snap, at: Self.now)?.id == "soon")
}
@Test("An unchanged next event does not spend a push")
func unchangedDoesNotPush() {
let identity = NextEventIdentity(id: "a", start: Self.now)
#expect(WatchPushPolicy.shouldPushComplicationUpdate(previous: identity, current: identity) == false)
}
@Test("A different event, a moved event, or a first event all spend a push")
func changesPush() {
let a = NextEventIdentity(id: "a", start: Self.now)
let b = NextEventIdentity(id: "b", start: Self.now)
let moved = NextEventIdentity(id: "a", start: Self.now.addingTimeInterval(600))
#expect(WatchPushPolicy.shouldPushComplicationUpdate(previous: a, current: b))
#expect(WatchPushPolicy.shouldPushComplicationUpdate(previous: a, current: moved))
#expect(WatchPushPolicy.shouldPushComplicationUpdate(previous: nil, current: a))
}
@Test("Losing the last event spends a push, so the watch stops showing it")
func clearingPushes() {
let a = NextEventIdentity(id: "a", start: Self.now)
#expect(WatchPushPolicy.shouldPushComplicationUpdate(previous: a, current: nil))
}
@Test("Two empty calendars in a row do not push")
func stayingEmptyDoesNotPush() {
#expect(WatchPushPolicy.shouldPushComplicationUpdate(previous: nil, current: nil) == false)
}
}