import CottonKit import XCTest @testable import CottonUI final class NativeNotificationPreferencesTests: XCTestCase { private let stored = #"{"needs_import":false,"preferences":{"enabled":false,"categories":{"reminders":true,"events":false,"birthdays":true,"alarms":false},"askedAt":"2026-10-01T12:00:00Z"}}"# /// RULE: Legacy notification preferences are imported only when core asks for them, and delivery subsequently reads the native answer rather than keeping a second UserDefaults truth. func testImportHappensOnlyOnceAndDeliveryReadsTheNativeAnswer() async throws { let core = PreferenceReplyCore(reads: [#"{"needs_import":true}"#, stored, stored]) let legacy = InMemoryNotificationPrefsStore(NotificationPrefs(enabled: false, categories: [.reminders: true, .events: false, .birthdays: true, .alarms: false], askedAt: "2026-10-01T12:00:00Z")) let source = CoreNotificationPreferences(core: core, legacy: legacy) let first = try await source.read() let again = try await source.read() XCTAssertEqual(first, again) XCTAssertFalse(first.enabled) XCTAssertFalse(first.categories[.events] ?? true) XCTAssertEqual(first.askedAt, "2026-10-01T12:00:00Z") let calls = await core.calls let imports = calls.filter { $0.method == "device.preferences.import" } XCTAssertEqual(imports.count, 1, "An existing native preference file must never be overwritten by legacy values") let sent = try JSONDecoder().decode(JSONValue.self, from: Data(try XCTUnwrap(imports.first).request.utf8)) XCTAssertEqual(sent["preferences"]?["enabled"]?.bool, false) XCTAssertEqual(sent["preferences"]?["categories"]?["events"]?.bool, false) XCTAssertEqual(sent["preferences"]?.text("askedAt"), first.askedAt) } /// RULE: Existing native preferences always win over UserDefaults, including a disabled master switch; a page or launch never imports over them. func testExistingNativePreferencesAreNeverReplacedByLegacyDefaults() async throws { let core = PreferenceReplyCore(reads: [stored]) let source = CoreNotificationPreferences(core: core, legacy: InMemoryNotificationPrefsStore()) let prefs = try await source.read() XCTAssertFalse(prefs.enabled) let calls = await core.calls XCTAssertFalse(calls.contains { $0.method == "device.preferences.import" }) } /// RULE: A malformed native preference answer fails rather than enabling a missing switch by default or retrying an import that could overwrite the person's native choice. func testPartialNativePreferencesAreRefused() async throws { let core = PreferenceReplyCore(reads: [#"{"needs_import":false,"preferences":{"enabled":true,"categories":{"reminders":true}}}"#]) let source = CoreNotificationPreferences(core: core, legacy: InMemoryNotificationPrefsStore()) do { _ = try await source.read() XCTFail("A partial native answer became defaults") } catch {} let calls = await core.calls XCTAssertFalse(calls.contains { $0.method == "device.preferences.import" }) } /// RULE: OS prompt history is written through the core's own record of the ask (`device.notifications.asked`, /// 4d0123f128, as Android's), so launching or changing a notification switch cannot lose whether this device has /// already asked, and the ask carries only its time: it never rewrites a delivery switch. func testPromptHistoryWritesThroughNativePreferences() async throws { let core = PreferenceReplyCore(reads: [stored]) let source = CoreNotificationPreferences(core: core, legacy: InMemoryNotificationPrefsStore()) try await source.markAsked(at: "2026-10-13T12:00:00Z") let calls = await core.calls let asked = try XCTUnwrap(calls.first { $0.method == "device.notifications.asked" }) let sent = try JSONDecoder().decode(JSONValue.self, from: Data(asked.request.utf8)) XCTAssertEqual(sent.text("at"), "2026-10-13T12:00:00Z") XCTAssertNil(sent["notify"], "Prompt history must not rewrite delivery switches") XCTAssertFalse(calls.contains { $0.method == "device.preferences.set" }, "the ask is the core's own record, not a patch") } } private actor PreferenceReplyCore: CottonCalling { struct Call: Sendable { let method: String; let request: String } private var reads: [String] private(set) var calls: [Call] = [] init(reads: [String]) { self.reads = reads } func callRaw(_ method: String, requestJSON: String) async throws -> String { calls.append(Call(method: method, request: requestJSON)) let reply: String if method == "device.preferences.read" { guard !reads.isEmpty else { throw PreferenceReplyError.noReply } reply = reads.removeFirst() } else { reply = "{}" } return #"{"ok":true,"data":\#(reply)}"# } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {} func close() async {} } private enum PreferenceReplyError: Error { case noReply }