// Several databases on one device, Slice 2 (P10): the surfaces that act on a // row from a database the window is not showing — the reminders this device // schedules — act in the row's own database. // Synthetic ids, names and instants throughout; no wall clock. import CottonKit import Foundation import XCTest @testable import CottonUI @MainActor final class ReadsAcrossDatabasesTests: XCTestCase { // ── Reminders ──────────────────────────────────────────────────────── /// RULE: Each held database's reminders are reconciled against its own scheduled ones only, so a pass for one database never cancels another's, and each reminder's tap names its database. /// /// Why: iOS holds one queue for the whole app and the crate's reconcile /// cancels every Cotton identifier it does not want. Handing one /// database's pass the whole queue would cancel every other database's /// reminders on every pass — and a reminder that cannot say its database /// would open a record of the same id in whichever database is shown. func testReminderReconcileForOnePlaceKeepsTheOthers() async throws { let ownTodo = "cotton:todo:t-1:20261008T0900" let core = DesiredByDatabase([ "": [ownTodo], // The same todo id at the same minute in the project database. "db-garden": [ownTodo, "cotton:event:e-1:20261008T1000"], ]) let centre = RecordingCentre(pending: ["someone-elses-alarm"]) let pass = ReminderPass(core: core, centre: centre) _ = await pass.run(timeZone: "Europe/London", prefs: .defaults, permission: .granted, others: ["db-garden"]) XCTAssertEqual(centre.held.count, 4, "the foreign one, the personal todo and the garden's two") XCTAssertTrue(centre.held.contains("someone-elses-alarm")) XCTAssertTrue(centre.held.contains(ownTodo), "the personal database's identifiers are the crate's own, unchanged") // A second pass changes nothing: no database's pass cancels another's. let again = await pass.run(timeZone: "Europe/London", prefs: .defaults, permission: .granted, others: ["db-garden"]) XCTAssertTrue(again.noop) XCTAssertEqual(centre.cancelled.flatMap { $0 }, []) // The garden's event moves: only the garden's reminder is replaced. await core.want("db-garden", [ownTodo, "cotton:event:e-1:20261008T1100"]) _ = await pass.run(timeZone: "Europe/London", prefs: .defaults, permission: .granted, others: ["db-garden"]) XCTAssertEqual(centre.cancelled.flatMap { $0 }.count, 1) XCTAssertFalse(centre.cancelled.flatMap { $0 }.contains(ownTodo)) XCTAssertEqual(centre.held.count, 4) // Each reminder's tap opens in its own database. let scheduled = centre.scheduled.flatMap { $0 } let gardenRows = scheduled.filter { $0.identifier != ownTodo } XCTAssertEqual(gardenRows.count, 3) for row in gardenRows { let tap = NotificationTap(userInfo: NotificationTaps.value(of: row.userInfo), identifier: row.identifier) XCTAssertEqual(NotificationTaps.link(tap)?.database, "db-garden") } let own = try XCTUnwrap(scheduled.first { $0.identifier == ownTodo }) XCTAssertNil(NotificationTaps.link(NotificationTap(userInfo: NotificationTaps.value(of: own.userInfo)))?.database) // The garden is left: its reminders go, the personal one stays. _ = await pass.run(timeZone: "Europe/London", prefs: .defaults, permission: .granted, others: []) XCTAssertEqual(Set(centre.held), ["someone-elses-alarm", ownTodo]) } } /// The crate's `reminders.reconcile`, per database: the desired set the /// request's `database` names (the personal one when it names none), diffed /// as `cotton_core::rules::reminders::reconcile` diffs it — schedule what /// is wanted and not held, cancel the `cotton:` identifiers held and not /// wanted, leave every other identifier alone. private actor DesiredByDatabase: CottonCalling { private var desired: [String: [String]] init(_ desired: [String: [String]]) { self.desired = desired } func want(_ database: String, _ identifiers: [String]) { desired[database] = identifiers } func callRaw(_ method: String, requestJSON: String) async throws -> String { let request = try JSONSerialization.jsonObject(with: Data(requestJSON.utf8)) as? [String: Any] ?? [:] let database = request["database"] as? String ?? "" let held = ((request["args"] as? [String: Any])?["held"] as? [String]) ?? [] let wanted = desired[database] ?? [] let schedule = wanted.filter { !held.contains($0) }.map { identifier -> [String: Any] in let parts = identifier.split(separator: ":").map(String.init) return [ "identifier": identifier, "kind": parts[1], "recordId": parts[2], "at": "2026-10-08T09:00:00.000Z", "title": "Due", ] } let cancel = held.filter { $0.hasPrefix("cotton:") && !wanted.contains($0) }.sorted() let answer: [String: Any] = [ "ok": true, "data": ["schedule": schedule, "cancel": cancel, "noop": schedule.isEmpty && cancel.isEmpty], ] return String(decoding: try JSONSerialization.data(withJSONObject: answer), as: UTF8.self) } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {} func close() async {} }