import AppKit import Foundation import ParcelConfig import ParcelCore import ParcelEngine import ParcelUI import Testing @testable import ParcelApp extension AppKitTestSuite { @Suite(.timeLimit(.minutes(1))) @MainActor struct AppRuleControlsTests { init() { _ = NSApplication.shared } @Test(arguments: [ "[[rules]]\nbundle-id='test.app'\ntitle-regex='^Document$'\nrole='AXWindow'\naction='float' # authored\n", "rules=[{bundle-id='other',action='tile'}, {bundle-id='test.app',title-regex='^Document$',role='AXWindow',action='float'}] # authored\n", ]) func duplicateAlwaysFloatHasNoUndoAndLeavesAuthoredRuleUntouched(_ source: String) async throws { try await withSettings(source) { settings, url in var applies = 0 let controls = AppRuleControls( settings: settings, engine: { nil }, apply: { applies += 1 }, failure: { Issue.record($0) }) defer { controls.stop() } let window = ManagedWindow( id: WindowID(rawValue: 7), app: AppID(pid: 123, bundleID: "test.app"), title: "Document", role: "AXWindow", facts: WindowFacts(bundleID: "test.app", role: "AXWindow", title: "Document")) try await controls.alwaysFloat(window) #expect(applies == 0) #expect(!settings.canUndoLastWindowRule) await #expect(throws: (any Error).self) { try await controls.undoLastRule() } #expect(try String(contentsOf: url, encoding: .utf8) == source) } } @Test func emptyRoleAlwaysFloatPersistsAndUndoAppliesOnlyTheInsertedRule() async throws { let source = "[[rules]]\nbundle-id='authored'\naction='tile' # authored\n" try await withSettings(source) { settings, url in var applies = 0 let controls = AppRuleControls( settings: settings, engine: { nil }, apply: { applies += 1 }, failure: { Issue.record($0) }) defer { controls.stop() } let window = ManagedWindow( id: WindowID(rawValue: 7), app: AppID(pid: 123, bundleID: "test.app"), title: "Document", role: "", facts: WindowFacts(bundleID: "test.app", role: "", title: "Document")) try await controls.alwaysFloat(window) #expect( settings.configuration.rules == [ RuleConfiguration(bundleID: "authored", action: .tile), RuleConfiguration(bundleID: "test.app", titleRegex: "^Document$", action: .float), ]) #expect(applies == 1 && settings.canUndoLastWindowRule) try await controls.undoLastRule() #expect(applies == 2 && !settings.canUndoLastWindowRule) #expect(try String(contentsOf: url, encoding: .utf8) == source) } } @Test func missingBundleSurfacesAnActionableFailureWithoutSavingOrApplying() async throws { try await withSettings("# authored\n") { settings, url in var applies = 0 let controls = AppRuleControls( settings: settings, engine: { nil }, apply: { applies += 1 }, failure: { Issue.record($0) }) defer { controls.stop() } let window = ManagedWindow( id: WindowID(rawValue: 7), app: AppID(pid: 123, bundleID: ""), title: "Document", role: "", facts: WindowFacts(bundleID: "", role: "", title: "Document")) do { try await controls.alwaysFloat(window) Issue.record("Missing bundle unexpectedly accepted") } catch { #expect(error is SettingsFailure) #expect(!error.localizedDescription.isEmpty) } #expect(applies == 0 && settings.configuration.rules.isEmpty) let saved = try String(contentsOf: url, encoding: .utf8) #expect(saved == "# authored\n") } } @Test func appSwitchHasNoTargetForTheManagerOrAnUnknownApplication() async throws { try await withSettings("# authored\n") { settings, _ in let controls = AppRuleControls( settings: settings, engine: { nil }, apply: {}, failure: { Issue.record($0) }) defer { controls.stop() } #expect(controls.menuApplication(nil) == nil) #expect(controls.menuApplication(NSRunningApplication.current) == nil) } } @Test func appSwitchTogglesTheCurrentRuleRatherThanAStaleMenuSnapshot() async throws { try await withSettings("[[rules]]\nbundle-id='test.app'\naction='float'\n") { settings, _ in var configuration = settings.configuration configuration.motion.enabled = false let engine = try ParcelEngine( world: RecordingWorld(), clock: SystemEngineClock(), scheduler: RunLoopEngineScheduler(), displayLink: { _ in throw AppFailure(message: "Unexpected display link") }, announcer: AppAnnouncer(onModeChange: {}), log: AppEngineLog(), configuration: configuration, trace: nil, reduceMotionSource: nil) try await engine.start() var applied = false let controls = AppRuleControls( settings: settings, engine: { engine }, apply: { applied = true }, failure: { Issue.record($0) }) defer { controls.stop() } controls.toggleApplication( MenuApplication(bundleIdentifier: "test.app", name: "Fixture", isTiled: true)) do { let deadline = ContinuousClock.now + .seconds(2) while !applied { guard ContinuousClock.now < deadline else { throw AppFailure(message: "App switch did not apply") } await Task.yield() } #expect(WindowRuleEditing.tilesApp("test.app", configuration: settings.configuration)) } catch { await engine.stop() throw error } await engine.stop() } } private func withSettings( _ source: String, _ body: @MainActor (SettingsModel, URL) async throws -> Void ) async throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } let url = directory.appendingPathComponent("parcel.toml") try Data(source.utf8).write(to: url) let settings = SettingsModel(store: ConfigStore(fileURL: url)) await settings.start() do { try await body(settings, url) } catch { await settings.stop() throw error } await settings.stop() } } }