import ParcelConfig import ParcelCore import Testing @testable import ParcelEngine @MainActor struct RuleLayoutAcceptanceTests { @Test(arguments: [false, true]) func systemSettingsDefaultAndExplicitFloatRuleSurviveReload(startsWithFloatRule: Bool) async throws { let app = AppID(pid: 101, bundleID: "com.apple.systempreferences") var config = engineConfiguration() let floatRule = RuleConfiguration(bundleID: app.bundleID, action: .float) config.rules = startsWithFloatRule ? [floatRule] : [] let harness = try EngineHarness(configuration: config) try await harness.start() var window = engineWindow(1, app: app) window.facts = WindowFacts( bundleID: app.bundleID, activationPolicy: .regular, role: "AXWindow", subrole: "AXStandardWindow", isResizable: true, minimumSize: Size(width: 668, height: 400)) harness.world.appear(window, frame: Rect(x: 40, y: 60, width: 668, height: 600)) try harness.scheduler.runReady() for usesFloatRule in [startsWithFloatRule, !startsWithFloatRule, startsWithFloatRule] { config.rules = usesFloatRule ? [floatRule] : [] try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() let reason: ClassificationReason = usesFloatRule ? .rule(index: 0, bundleID: app.bundleID) : .standardWindow #expect(harness.engine.classifications[window.id]?.reason == reason) #expect(harness.engine.snapshot.state.windows[window.id]?.isFloating == usesFloatRule) let tiled = harness.engine.snapshot.state.workspaces[engineWorkspace]?.tree?.windows.contains(window.id) ?? false #expect(tiled == !usesFloatRule) } await harness.engine.stop() } @Test(arguments: ConfigLayout.allCases) func everyConfiguredRuleLayoutReachesTheDestinationWorkspace(layout: ConfigLayout) async throws { var config = engineConfiguration() config.rules = [RuleConfiguration(bundleID: engineApp.bundleID, layout: layout)] let harness = try EngineHarness(configuration: config) try await harness.start() try harness.add([1, 2]) let workspace = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) #expect(workspace.layout.rawValue == layout.rawValue) #expect(harness.engine.classifications[WindowID(rawValue: 1)]?.layout?.rawValue == layout.rawValue) #expect(workspace.order == [WindowID(rawValue: 1), WindowID(rawValue: 2)]) #expect(harness.engine.snapshot.state.frames.count == 2) await harness.engine.stop() } @Test func ruleLayoutTargetsOnlyTheAssignedDisplayAndKeepsWindowOrder() async throws { var config = engineConfiguration() config.rules = [ RuleConfiguration(bundleID: engineApp.bundleID, display: secondEngineDisplay.rawValue, layout: .rows) ] let harness = try EngineHarness(configuration: config, initialState: engineInitialState(twoDisplays: true)) try await harness.start() harness.world.appear(engineWindow(1, app: secondEngineApp)) try harness.scheduler.runReady() try harness.add([2, 3]) let destination = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .columns) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.order == [WindowID(rawValue: 1)]) #expect(harness.engine.snapshot.state.workspaces[destination]?.layout == .rows) #expect( harness.engine.snapshot.state.workspaces[destination]?.order == [ WindowID(rawValue: 2), WindowID(rawValue: 3), ]) #expect(harness.engine.snapshot.state.workspace(containing: WindowID(rawValue: 2)) == destination) await harness.engine.stop() } @Test func changedRuleLayoutReclassifiesExistingWindowsOnReload() async throws { var config = engineConfiguration() config.rules = [RuleConfiguration(bundleID: engineApp.bundleID, layout: .rows)] let harness = try EngineHarness(configuration: config) try await harness.start() try harness.add([1, 2]) let before = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) let frames = harness.engine.snapshot.state.frames #expect(before.layout == .rows) config.rules = [RuleConfiguration(bundleID: engineApp.bundleID, layout: .columns)] try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() let after = try #require(harness.engine.snapshot.state.workspaces[engineWorkspace]) #expect(after.layout == .columns) #expect(after.order == before.order) #expect(harness.engine.snapshot.state.frames != frames) #expect(harness.engine.classifications[WindowID(rawValue: 1)]?.layout == .columns) await harness.engine.stop() } @Test func laterMatchingLayoutWinsAndUnmatchedRuleCannotChangeLayout() async throws { var config = engineConfiguration() config.rules = [ RuleConfiguration(bundleID: engineApp.bundleID, layout: .side), RuleConfiguration(bundleID: engineApp.bundleID, layout: .rows), RuleConfiguration(bundleID: "test.unmatched", layout: .grid), ] let harness = try EngineHarness(configuration: config) try await harness.start() try harness.add([1]) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .rows) #expect(harness.engine.classifications[WindowID(rawValue: 1)]?.layout == .rows) await harness.engine.stop() } @Test func disabledDefaultReloadTilesTheExistingExcludedWindowAndCanRestoreItsDefault() async throws { let app = AppID(pid: 101, bundleID: "com.apple.calculator") var config = engineConfiguration() let harness = try EngineHarness(configuration: config) try await harness.start() try harness.add([1], app: app) let id = WindowID(rawValue: 1) #expect(harness.engine.snapshot.state.windows[id]?.isFloating == true) #expect(harness.engine.classifications[id]?.reason == .defaultFloatApp(bundleID: app.bundleID)) config.classification.disabledDefaultFloatApps.insert(app.bundleID) try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[id]?.isFloating == false) #expect(harness.engine.classifications[id]?.reason == .standardWindow) config.classification.disabledDefaultFloatApps.remove(app.bundleID) try harness.engine.applyConfiguration(config) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[id]?.isFloating == true) #expect(harness.engine.classifications[id]?.reason == .defaultFloatApp(bundleID: app.bundleID)) await harness.engine.stop() } @Test func explicitAppTileRuleOverridesTheDefaultFloatListThroughTheEngine() async throws { let app = AppID(pid: 101, bundleID: "com.apple.calculator") var config = engineConfiguration() config.rules = [RuleConfiguration(bundleID: app.bundleID, action: .tile, layout: .side)] let harness = try EngineHarness(configuration: config) try await harness.start() try harness.add([1], app: app) #expect(harness.engine.snapshot.state.windows[WindowID(rawValue: 1)]?.isFloating == false) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .side) #expect( harness.engine.classifications[WindowID(rawValue: 1)]?.reason == .rule(index: 0, bundleID: app.bundleID)) await harness.engine.stop() } @Test func fullscreenIgnoredWindowCannotAssignALayoutOrANativeSpace() async throws { var config = engineConfiguration() config.rules = [RuleConfiguration(bundleID: engineApp.bundleID, action: .tile, layout: .rows, space: 2)] let harness = try EngineHarness(configuration: config) try await harness.start() let window = ManagedWindow( id: WindowID(rawValue: 1), app: engineApp, facts: WindowFacts(bundleID: engineApp.bundleID, role: "AXWindow", isFullScreen: true)) harness.world.appear(window) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[window.id] == nil) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .columns) #expect(harness.engine.classifications[window.id]?.reason == .fullScreen) #expect(harness.engine.classifications[window.id]?.space == nil) #expect(harness.engine.classifications[window.id]?.layout == nil) #expect(harness.world.writes.isEmpty) await harness.engine.stop() } }