// The scheduler applies the crate's plan, and computes nothing of its own. // // its BEHAVIOUR cases (permission gating, the master-switch wipe, other // features' notifications left alone, keeps going after one schedule failure) // and not its cache-reading ones: there is no query cache in this app, and the // pass asks the replica through the crate instead, which is strictly simpler // and is the same answer. // // The load-bearing assertion in this file is the FIRST one: what iOS holds goes // down verbatim and the two lists come back applied as given. Every "which // reminders change" decision is the crate's, because two implementations of // that would eventually disagree — and a disagreement there is a person whose // reminder fires twice or not at all. import CottonKit import XCTest @testable import CottonUI @MainActor final class ReminderPassTests: XCTestCase { private func plan( schedule: [(String, String)] = [], cancel: [String] = [], noop: Bool = false ) -> String { let rows = schedule.map { identifier, recordId in """ {"identifier":"\(identifier)","kind":"todo","recordId":"\(recordId)",\ "at":"2026-09-11T09:00:00Z","title":"Milk","body":"Due now"} """ } let cancels = cancel.map { "\"\($0)\"" }.joined(separator: ",") return """ {"schedule":[\(rows.joined(separator: ","))],"cancel":[\(cancels)],\ "noop":\(noop),"timeZone":"Asia/Kolkata","now":"2026-09-10T12:00:00Z"} """ } private func pass( core: ScriptedCore, centre: RecordingCentre ) -> ReminderPass { ReminderPass(core: core, centre: centre) } // ── The contract with the crate ─────────────────────────────────────── func testItSendsWhatIOSHoldsVerbatimAndAppliesExactlyWhatCameBack() async throws { let centre = RecordingCentre(pending: [ "cotton:todo:t1:20260911T0900", // A FOREIGN identifier — another app's, or a future Cotton // feature's. It goes down untouched: `is_cotton_identifier` lives // inside the crate's `reconcile`, and that is the only place the // decision should exist. "someone-elses-alarm", ]) let core = ScriptedCore([ CottonMethod.remindersReconcile: .ok( plan( schedule: [("cotton:todo:t2:20260912T0900", "t2")], cancel: ["cotton:todo:t1:20260911T0900"] ) ) ]) let outcome = await pass(core: core, centre: centre) .run(timeZone: "Asia/Kolkata", prefs: .defaults, permission: .granted) let calls = await core.calls XCTAssertEqual(calls.count, 1) // Decoded, not matched as a substring: key order and slash escaping are // `JSONEncoder`'s business, and neither is the behaviour under test. let sent = try XCTUnwrap( try? JSONDecoder().decode(JSONValue.self, from: Data(calls[0].requestJSON.utf8)) ) let args = try XCTUnwrap(sent["args"], "the two `reminders.*` methods read `request[\"args\"]`") XCTAssertEqual( args["held"]?.strings?.contains("someone-elses-alarm"), true, "a foreign identifier was filtered on this side; the crate owns that rule" ) XCTAssertEqual(args["timeZone"]?.string, "Asia/Kolkata") // Applied as GIVEN: nothing added, nothing filtered, nothing re-ordered. XCTAssertEqual(centre.cancelled, [["cotton:todo:t1:20260911T0900"]]) XCTAssertEqual(centre.scheduled.map { $0.map(\.identifier) }, [["cotton:todo:t2:20260912T0900"]]) XCTAssertEqual(outcome, ReminderPassOutcome(noop: false, cancelled: 1, scheduled: 1)) // The foreign one is still standing. XCTAssertTrue(centre.held.contains("someone-elses-alarm")) } func testARefusalIsCarriedRatherThanSwallowed() async { let centre = RecordingCentre(pending: ["cotton:todo:t1:20260911T0900"]) let core = ScriptedCore([ CottonMethod.remindersReconcile: .refuse( code: "bad_request", message: "`timeZone` is the IANA zone" ) ]) let outcome = await pass(core: core, centre: centre) .run(timeZone: "Nowhere/Atall", prefs: .defaults, permission: .granted) XCTAssertEqual(outcome.refusal, "bad_request") // And NOTHING was cancelled. A pass that could not be decided must not // read as "nothing should exist" and wipe the queue. XCTAssertEqual(centre.cancelled, []) XCTAssertEqual(centre.held, ["cotton:todo:t1:20260911T0900"]) } // ── Permission is the master switch, and a revocation is a WIPE ─────── func testWithoutPermissionThePassAsksForAnEmptySetRatherThanSkipping() async { // The defect this protects against: a port that short-circuits on "no // permission, nothing to do" leaves stale reminders scheduled for ever. let centre = RecordingCentre(pending: ["cotton:todo:t1:20260911T0900"]) let core = ScriptedCore([ CottonMethod.remindersReconcile: .ok( plan(cancel: ["cotton:todo:t1:20260911T0900"]) ) ]) let outcome = await pass(core: core, centre: centre) .run(timeZone: "Asia/Kolkata", prefs: .defaults, permission: .denied) let calls = await core.calls XCTAssertEqual(calls.count, 1, "the pass must still run") XCTAssertTrue( calls[0].requestJSON.contains("\"enabled\":false"), "the OS's no must reach the engine as the master switch being off" ) XCTAssertEqual(outcome.cancelled, 1) XCTAssertTrue(centre.held.isEmpty) } // ── Through the model ───────────────────────────────────────────────── }