Cotton code
104 lines · 3.3 KB · SwiftRaw
// The three seams a notification test drives, doubled.//// Everything below stands in for something only a device has — the notification// centre's pending queue, the OS permission, and the vendor identifier. They// live in one file because all three test files use all three, and a double// declared twice is a double that eventually disagrees with itself.import CottonKitimport Foundation/// The notification centre, remembering what it was told.////// `@MainActor` rather than a lock: `NSLock` is unavailable from an async/// context, every test in this family already runs on the main actor, and a/// main-actor class is `Sendable` — which is what the protocol asks for. The/// pass hops to reach it, exactly as it would hop to reach `UNUserNotification/// Center` on a real device.@MainActorfinal class RecordingCentre: ReminderCentre {    private var pending: [String]    private(set) var cancelled: [[String]] = []    private(set) var scheduled: [[DesiredReminder]] = []    /// Identifiers `schedule` refuses, so a test can prove one failure does not    /// stop the rest.    var refuses: Set<String> = []    /// Every call, in order — the assertion for "cancel before schedule".    private(set) var order: [String] = []    init(pending: [String] = []) {        self.pending = pending    }    func heldIdentifiers() async -> [String] {        order.append("held")        return pending    }    func cancel(_ identifiers: [String]) async {        order.append("cancel")        cancelled.append(identifiers)        pending.removeAll { identifiers.contains($0) }    }    func schedule(_ reminders: [DesiredReminder]) async -> Int {        order.append("schedule")        scheduled.append(reminders)        var landed = 0        for reminder in reminders where !refuses.contains(reminder.identifier) {            pending.append(reminder.identifier)            landed += 1        }        return landed    }    var held: [String] { pending }}/// The OS permission, scripted. `@MainActor` for `RecordingCentre`'s reason.@MainActorfinal class ScriptedAuthority: NotificationAuthority {    private var state: NotificationPermission    /// What `ask()` answers. Defaults to whatever `read()` says, so a test that    /// does not care need not name one.    private var answers: NotificationPermission?    private(set) var asked = 0    private(set) var registeredForRemote = 0    init(state: NotificationPermission, answers: NotificationPermission? = nil) {        self.state = state        self.answers = answers    }    func read() async -> NotificationPermission {        state    }    func ask() async -> NotificationPermission {        asked += 1        if let answers { state = answers }        return state    }    func registerForRemoteNotifications() async {        registeredForRemote += 1    }}/// What the phone knows about itself, named.////// `@MainActor` with the protocol — see `DeviceFactsSource`; the vendor/// identifier is UIKit's and its only caller is a `@MainActor` model.@MainActorstruct ScriptedFacts: DeviceFactsSource {    var facts: DeviceFacts? = DeviceFacts(        deviceId: "vendor-1",        bundleId: AppFlavor.current.phoneAppID,        appVersion: "1.0.0",        build: "200",        locale: "en_GB",        timezone: "Asia/Kolkata"    )    func read() -> DeviceFacts? { facts }}