import Foundation import ParcelCore import Testing struct WindowControlTests { let display = DisplayID(rawValue: "controls") var key: WorkspaceKey { WorkspaceKey(display: display, space: SpaceID(rawValue: 1)) } func id(_ number: UInt32) -> WindowID { WindowID(rawValue: number) } func populated() -> State { var state = State( displays: [Display(id: display, frame: Rect(width: 1200, height: 800))], activeSpaces: [display: key.space]) for number in 1...3 { let window = ManagedWindow(id: id(UInt32(number)), app: AppID(pid: 42, bundleID: "org.example.fixture")) state = reduce(state, .windowAppeared(window, workspace: key, frame: Rect())).0 } return state } @Test func controlsActOnCapturedWindowNotCurrentFocus() { let state = populated() let (primary, effects) = reduce(state, .commandForWindow(id(2), .makePrimary)) #expect(primary.workspaces[key]?.order.first == id(2)) #expect(primary.focusedWindow == id(2)) #expect(effects.contains(.announce(.primaryChanged(id(2))))) let floating = reduce(state, .commandForWindow(id(1), .toggleFloat)).0 #expect(floating.windows[id(1)]?.isFloating == true) #expect(floating.windows[id(3)]?.isFloating == false) } @Test(arguments: [Command.focusNextInStack, .focusPreviousInStack]) func capturedStackCycleRaisesOnlyTheDestination(command: Command) throws { var state = reduce(populated(), .commandIssued(.setWindowCap(1))).0 state = reduce(state, .focusChanged(id(2))).0 let stack = try #require(state.workspaces[key]?.tree?.stacks.first) let index = try #require(stack.windows.firstIndex(of: stack.visible)) let offset = command == .focusNextInStack ? 1 : stack.windows.count - 1 let destination = stack.windows[(index + offset) % stack.windows.count] let result = reduce(state, .commandForWindow(id(1), command, workspace: key)) #expect(result.0.focusedWindow == destination) #expect(result.0.workspaces[key]?.tree?.stacks.first?.visible == destination) #expect(result.1.filter { if case .focusWindow = $0 { true } else { false } } == [.focusWindow(destination)]) } @Test func focusControlIsTargetSpecificAndTogglesOff() { let focused = reduce(populated(), .commandForWindow(id(2), .toggleFocus)).0 #expect(focused.modes(for: key).focusedWindow == id(2)) let restored = reduce(focused, .commandForWindow(id(2), .toggleFocus)).0 #expect(restored.modes(for: key).focusedWindow == nil) } @Test func staleTargetAndUnsupportedCommandDoNotMutateState() { let state = populated() #expect(reduce(state, .commandForWindow(id(9), .makePrimary)).0 == state) #expect(reduce(state, .commandForWindow(id(2), .setWindowCap(2))).1 == [.report(.unsupported)]) } @Test func capturedSpaceCannotRetargetAWindowCommand() { let state = populated() let captured = WorkspaceKey(display: display, space: SpaceID(rawValue: 2)) let (result, effects) = reduce(state, .commandForWindow(id(2), .makePrimary, workspace: captured)) #expect(result == state) #expect(effects == [.report(.noActiveWorkspace)]) } @Test func hoverToggleAndSettledFocusHaveSemanticEffects() { let state = populated() let (enabled, effects) = reduce(state, .commandIssued(.toggleHoverFocus)) #expect(enabled.hoverFocusEnabled) #expect(effects.contains(.announce(.hoverFocusChanged(true)))) let (focused, focusEffects) = reduce(enabled, .hoverFocusSettled(id(1), workspace: key)) #expect(focused.focusedWindow == id(1)) #expect(focusEffects.contains(.focusWindow(id(1)))) #expect(!reduce(enabled, .commandIssued(.toggleHoverFocus)).0.hoverFocusEnabled) } @Test func hoverFocusRejectsDisabledUnknownAndInactiveTargets() { let state = populated() #expect(reduce(state, .hoverFocusSettled(id(1), workspace: key)).0 == state) let enabled = reduce(state, .hoverFocusConfigured(true)).0 let hidden = WorkspaceKey(display: display, space: SpaceID(rawValue: 2)) #expect(reduce(enabled, .hoverFocusSettled(id(1), workspace: hidden)).0 == enabled) #expect(reduce(enabled, .hoverFocusSettled(id(9), workspace: key)).0 == enabled) } @Test func floatingReclassificationEndsFocusInsteadOfLeavingAnInvisibleModeOn() throws { let focused = reduce(populated(), .commandForWindow(id(1), .toggleFocus)).0 var floating = try #require(focused.windows[id(1)]) floating.isFloating = true let frame = try #require(focused.frames[id(1)]) let (result, effects) = reduce(focused, .windowAppeared(floating, workspace: key, frame: frame)) #expect(result.modes(for: key).focusedWindow == nil) #expect(effects.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test func invalidPhysicalDisplayBoundsAreRejectedBeforeAdapting() { let state = populated() for physical in [Rect(width: 100, height: 100), Rect(x: .nan, width: 1200, height: 800)] { let replacement = Display(id: display, frame: Rect(width: 1200, height: 800), physicalFrame: physical) let (result, effects) = reduce(state, .displaySetChanged([replacement])) #expect(result == state) #expect(effects == [.report(.invalidGeometry)]) } } @Test func oldStatePayloadDefaultsHoverFocusOff() throws { let original = State() let data = try JSONEncoder().encode(original) let decoded = try JSONDecoder().decode(State.self, from: data) #expect(!decoded.hoverFocusEnabled) var canonical = original canonical.hoverFocusEnabled = false #expect(canonical == original) let enabled = reduce(original, .hoverFocusConfigured(true)).0 #expect(try JSONDecoder().decode(State.self, from: JSONEncoder().encode(enabled)).hoverFocusEnabled) } }