import ParcelConfig import ParcelCore import ParcelEngine import Testing @MainActor struct WindowControlEngineTests { @Test(arguments: [ Command.swap(.right), .focusNextWindow, .focusPreviousWindow, .focusNextInStack, .focusPreviousInStack, ]) func focusedCommandsUseConfirmedPhysicalFocusInsteadOfThePreviousModelSelection(command: Command) async throws { var configuration = engineConfiguration() if command == .focusNextInStack || command == .focusPreviousInStack { configuration.layout.global.windowCap = 1 } let harness = try EngineHarness(configuration: configuration) try await harness.start() try harness.add([1, 2, 3]) let first = WindowID(rawValue: 1) let second = WindowID(rawValue: 2) let third = WindowID(rawValue: 3) harness.engine.send(.focusChanged(third)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.focusedWindow == third) harness.world.commandWindow = engineWindow(1) let before = harness.engine.snapshot.state.frames let operation = Task { try await harness.engine.perform(command) } for _ in 0..<30 { await Task.yield() try harness.scheduler.runReady() } #expect(try await operation.value.isAccepted) try harness.finishMotion() if command == .swap(.right) { #expect(harness.engine.snapshot.state.focusedWindow == first) #expect(harness.world.frames[first] == before[second]) #expect(harness.world.frames[second] == before[first]) #expect(harness.world.frames[third] == before[third]) } else { let forward = command == .focusNextWindow || command == .focusNextInStack #expect(harness.engine.snapshot.state.focusedWindow == (forward ? second : third)) #expect(harness.world.frames == before) } await harness.engine.stop() } @Test(arguments: [1, 2]) func makePrimaryUsesNativeFocusBeforeItsNotificationArrives(nativeID: UInt32) async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2, 3]) try harness.finishMotion() let selected = WindowID(rawValue: nativeID) let stale = WindowID(rawValue: 3) harness.engine.send(.focusChanged(stale)) try harness.scheduler.runReady() let before = harness.engine.snapshot.state.frames harness.world.commandWindow = engineWindow(nativeID) let operation = Task { try await harness.engine.perform(.makePrimary) } for _ in 0..<30 { await Task.yield() try harness.scheduler.runReady() } #expect(try await operation.value.isAccepted) try harness.finishMotion() let state = harness.engine.snapshot.state #expect(state.focusedWindow == selected) #expect(state.workspaces[engineWorkspace]?.primaryWindow == selected) #expect(harness.world.frames == before) #expect(harness.world.frames[stale] == before[stale]) await harness.engine.stop() } @Test func makePrimarySurfacesNativeFocusFailureWithoutPromotingTheStaleSelection() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2, 3]) try harness.finishMotion() let before = harness.engine.snapshot.state harness.world.commandWindowError = EngineWindowCommandError.noFocusedWindow await #expect(throws: EngineWindowCommandError.noFocusedWindow) { try await harness.engine.perform(.makePrimary) } #expect(harness.engine.snapshot.state == before) #expect(harness.world.frames == before.frames) await harness.engine.stop() } @Test func configuredHoverFocusUpdatesCoreAndIPCState() async throws { var configuration = engineConfiguration() configuration.mouse.focusFollowsMouse = true let harness = try EngineHarness(configuration: configuration) try await harness.start() #expect(harness.engine.snapshot.state.hoverFocusEnabled) configuration.mouse.focusFollowsMouse = false try harness.engine.applyConfiguration(configuration) try harness.scheduler.runReady() #expect(!harness.engine.snapshot.state.hoverFocusEnabled) harness.engine.send(.commandIssued(.toggleHoverFocus)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.hoverFocusEnabled) let config = try await EngineIPCHandler(engine: harness.engine).query(.config) guard case .object(let values) = config else { Issue.record("Expected config object") return } #expect(values["hover_focus"] == .bool(true)) await harness.engine.stop() } @Test func targetSpecificCommandMovesRequestedRealWorldFrame() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2, 3]) try harness.finishMotion() let before = harness.engine.snapshot.state.frames let task = Task { try await harness.engine.perform(.makePrimary, targeting: WindowID(rawValue: 2)) } for _ in 0..<30 { await Task.yield() try harness.scheduler.runReady() } #expect(try await task.value.isAccepted) try harness.finishMotion() #expect(harness.world.frames == before) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.primaryWindow == WindowID(rawValue: 2)) #expect(harness.engine.snapshot.state.focusedWindow == WindowID(rawValue: 2)) await harness.engine.stop() } @Test func invalidProgrammaticMouseTimingFailsLoudly() { var configuration = engineConfiguration() configuration.mouse.typingCooldown = .nan #expect(throws: EngineError.self) { try EngineHarness(configuration: configuration) } } @Test func disconnectedDisplayCannotReceiveHoveredWindow() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1]) let before = harness.engine.snapshot.state #expect(!harness.engine.moveWindow(WindowID(rawValue: 1), to: DisplayID(rawValue: "unknown"))) #expect(harness.engine.snapshot.state == before) await harness.engine.stop() } }