import ParcelConfig import ParcelCore import ParcelIPC import ParcelPlatform import Testing @testable import ParcelEngine @MainActor struct FocusModeEngineTests { @Test func configuredFocusMovesAndRestoresFramesThroughWriter() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) let window = WindowID(rawValue: 2) let original = harness.engine.snapshot.state.frames[window] let handler = EngineIPCHandler(engine: harness.engine) let task = Task { @MainActor in try await handler.run(command: "focus-toggle", args: []) } for _ in 0..<100 where harness.scheduler.readyCount == 0 { await Task.yield() } try harness.scheduler.runReady() guard case .object(let response) = try await task.value else { Issue.record("Expected mode command response") await harness.engine.stop() return } #expect(response["accepted"] == .bool(true)) try harness.finishMotion() #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == window) #expect(harness.world.writes(for: window).last?.frame == engineScreen) #expect(harness.announcer.announcements.contains(.focusModeChanged(.focus, enabled: true))) harness.world.appear(engineWindow(3), workspace: engineWorkspace) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == nil) #expect(harness.world.writes(for: window).last?.frame != engineScreen) #expect(harness.engine.snapshot.state.frames[window] != original) await harness.engine.stop() } @Test(arguments: [ConfigCommand.toggleNoPeek, .toggleCleanSpace]) func privacyCommandsReachIPCAndSnapshot(command: ConfigCommand) async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1]) let handler = EngineIPCHandler(engine: harness.engine) let task = Task { @MainActor in try await handler.run(command: command.rawValue, args: []) } for _ in 0..<100 where harness.scheduler.readyCount == 0 { await Task.yield() } try harness.scheduler.runReady() guard case .object(let response) = try await task.value else { Issue.record("Expected mode command response") await harness.engine.stop() return } #expect(response["accepted"] == .bool(true)) #expect(harness.engine.snapshot.state.activeFocusModes.noPeek == (command == .toggleNoPeek)) #expect(harness.engine.snapshot.state.activeFocusModes.cleanSpace == (command == .toggleCleanSpace)) guard case .array(let workspaces) = try await handler.query(.workspaces), case .object(let workspace) = try #require(workspaces.first), case .object(let modes) = workspace["focus_modes"] else { Issue.record("Expected per-Space mode query") await harness.engine.stop() return } #expect(modes["noPeek"] == .bool(command == .toggleNoPeek)) #expect(modes["cleanSpace"] == .bool(command == .toggleCleanSpace)) await harness.engine.stop() } @Test func nativeClickPreservesFocusUntilAUserFrameChanges() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) harness.engine.send(.commandIssued(.toggleFocus)) try harness.scheduler.runReady() let window = try #require(harness.engine.snapshot.state.activeFocusModes.focusedWindow) harness.world.emit(.pointerButton(isDown: true)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == window) harness.world.emit(.pointerButton(isDown: false)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == window) harness.world.emit(.pointerButton(isDown: true)) harness.world.userFrame(window, frame: Rect(x: 20, y: 10, width: 1200, height: 800)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == nil) harness.world.emit(.pointerButton(isDown: false)) try harness.scheduler.runReady() await harness.engine.stop() } @Test func mouseClickPreservesFocusAndDragEndsItBeforeFrameObservation() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) harness.engine.send(.commandIssued(.toggleFocus)) try harness.scheduler.runReady() let window = try #require(harness.engine.snapshot.state.activeFocusModes.focusedWindow) let start = DragSample(kind: .down, location: .init(x: 700, y: 40)) let target = DragWindow(pid: engineApp.pid, windowNumber: window.rawValue) harness.engine.handleMouse(start, windowAtStart: target) #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == window) harness.engine.handleMouse(DragSample(kind: .up, location: start.location)) #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == window) harness.engine.handleMouse(start, windowAtStart: target) harness.engine.handleMouse(DragSample(kind: .dragged, location: .init(x: 750, y: 40))) #expect(harness.engine.snapshot.state.activeFocusModes.focusedWindow == nil) harness.engine.handleMouse(DragSample(kind: .up, location: .init(x: 750, y: 40))) await harness.engine.stop() } @Test func unmanagedFocusClearsManagedFocusWithoutTurningOffPrivacyModes() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) let lastFocus = harness.engine.snapshot.focusedWindow harness.engine.send(.commandIssued(.toggleNoPeek)) harness.engine.send(.commandIssued(.toggleCleanSpace)) try harness.scheduler.runReady() harness.world.emit(.observed(.focusChanged(WindowID(rawValue: 999)))) try harness.scheduler.runReady() #expect(harness.engine.snapshot.focusedWindow == nil) #expect(harness.engine.snapshot.state.activeFocusModes.noPeek) #expect(harness.engine.snapshot.state.activeFocusModes.cleanSpace) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.focus == lastFocus) await harness.engine.stop() } @Test func ignoredSameAppFocusClearsOnlyGlobalFocus() async throws { var configuration = engineConfiguration() configuration.rules = [RuleConfiguration(titleContains: "Ignored", action: .ignore)] let harness = try EngineHarness(configuration: configuration) try await harness.start() try harness.add([1]) harness.engine.send(.commandIssued(.toggleNoPeek)) try harness.scheduler.runReady() var ignored = engineWindow(3) ignored.title = "Ignored" harness.world.appear(ignored, workspace: engineWorkspace) harness.world.emit(.observed(.focusChanged(ignored.id))) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[ignored.id] == nil) #expect(harness.engine.snapshot.focusedWindow == nil) #expect(harness.engine.snapshot.state.activeFocusModes.noPeek) #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.focus == WindowID(rawValue: 1)) await harness.engine.stop() } @Test func newlyAppearedFocusInTheSameBatchIsNotMistakenForUnmanaged() async throws { let harness = try EngineHarness() try await harness.start() harness.world.appear(engineWindow(1), workspace: engineWorkspace) harness.world.emit(.observed(.focusChanged(WindowID(rawValue: 1)))) try harness.scheduler.runReady() #expect(harness.engine.snapshot.focusedWindow == WindowID(rawValue: 1)) await harness.engine.stop() } @Test(arguments: [Double.nan, .infinity, -0.1, 0, 0.09, 0.91, 1]) func engineRejectsInvalidPrivacyConfiguration(level: Double) { var configuration = engineConfiguration() configuration.focusModes.noPeekLevel = level #expect(throws: EngineError.invalidConfiguration("Invalid No peek level")) { try EngineHarness(configuration: configuration) } } @Test func configurationQueryIncludesPrivacySettings() async throws { var configuration = engineConfiguration() configuration.focusModes = FocusModeConfiguration(noPeekLevel: 0.8, noPeekBlur: false, cleanSpaceColor: .mist) let harness = try EngineHarness(configuration: configuration) try await harness.start() let handler = EngineIPCHandler(engine: harness.engine) guard case .object(let config) = try await handler.query(.config), case .object(let modes) = config["focus-modes"] else { Issue.record("Expected privacy configuration") await harness.engine.stop() return } #expect(modes["no-peek-level"] == .number(0.8)) #expect(modes["no-peek-blur"] == .bool(false)) #expect(modes["clean-space-color"] == .string("mist")) #expect(config["focus_modes"] == nil) #expect(modes.keys.allSatisfy { !$0.contains("_") }) await harness.engine.stop() } }