import CottonKit import XCTest @testable import CottonUI @MainActor final class RecordPlatformActionTests: XCTestCase { func testPhotoShareCarriesTheFetchedFileToThePlatform() throws { let answer = try JSONDecoder().decode(CommandOutcome.self, from: Data( #"{"command":"share","answer":{"platform_action":"share_file","path":"/tmp/synthetic-photo.png"}}"#.utf8)) let change = try answer.change(table: "photo", record: "synthetic") XCTAssertEqual(change.opened, .platform(.shareFile(URL(fileURLWithPath: "/tmp/synthetic-photo.png")))) XCTAssertFalse(change.undoable) } /// RULE: A native command requesting an OS effect carries that pending effect to the platform, rather than claiming that permission was granted by Rust. func testCommandAnswerCarriesThePendingPlatformEffect() throws { let answer = try JSONDecoder().decode(CommandOutcome.self, from: Data(#"{"command":"allow","kind":"method","answer":{"platform_action":"request_notifications"}}"#.utf8)) let change = try answer.change(table: "example", record: "example-row") XCTAssertEqual(change.opened, .platform(.requestNotifications)) XCTAssertNil(change.said) XCTAssertFalse(change.undoable) } /// RULE: An unknown platform effect is refused rather than silently reported as a successful command; only the closed shipped vocabulary reaches OS APIs. func testUnknownPlatformEffectIsRefused() throws { XCTAssertThrowsError(try JSONDecoder().decode(CommandOutcome.self, from: Data( #"{"command":"example","answer":{"platform_action":"invented_effect"}}"#.utf8))) } /// RULE: A notification request spends the OS prompt only while undetermined; a repeated action never asks again once permission has an answer. func testNotificationRequestUsesTheExistingAuthorityOnce() async { let authority = ScriptedNotificationSettingsAuthority([.undetermined, .granted]) await RecordPlatformEffects.perform(.requestNotifications, authority: authority) await RecordPlatformEffects.perform(.requestNotifications, authority: authority) XCTAssertEqual(authority.timesAsked, 1) XCTAssertEqual(authority.timesOpenedSettings, 0) } /// RULE: Opening notification settings is an OS effect alone and never spends the notification prompt. func testOpeningNotificationSettingsDoesNotRequestPermission() async { let authority = ScriptedNotificationSettingsAuthority([.denied]) await RecordPlatformEffects.perform(.openNotificationSettings, authority: authority) XCTAssertEqual(authority.timesAsked, 0) XCTAssertEqual(authority.timesOpenedSettings, 1) } }