import Testing @testable import ParcelCore struct WorkspaceCommandTests { let left = DisplayID(rawValue: "left") let right = DisplayID(rawValue: "right") let space = SpaceID(rawValue: 7) func scene() -> State { let displays = [ Display(id: left, frame: Rect(width: 1440, height: 900)), Display(id: right, frame: Rect(x: 1440, y: 0, width: 1440, height: 900)), ] var state = State(displays: displays, activeSpaces: [left: space, right: space]) for index in 1...6 { let id = WindowID(rawValue: UInt32(index)) let window = ManagedWindow(id: id, app: AppID(pid: 42, bundleID: "test.fixture")) state = reduce( state, .windowAppeared( window, workspace: WorkspaceKey(display: index <= 3 ? left : right, space: space), frame: Rect(width: 300, height: 200)) ).0 } return reduce(state, .focusChanged(WindowID(rawValue: 1))).0 } @Test(arguments: [ Command.setWindowCap(2), .setLayout(.rows), .setRatio(.threeFifths), .resetSizes, .retile, ]) func targetsOneDisplayWithoutStealingFocus(command: Command) throws { var before = scene() let key = WorkspaceKey(display: right, space: space) if case .setRatio = command { before = reduce(before, .workspaceCommandIssued(.setLayout(.side), workspace: key)).0 } let (after, effects) = reduce(before, .workspaceCommandIssued(command, workspace: key)) #expect(after.activeDisplay == left) #expect(after.focusedWindow == WindowID(rawValue: 1)) #expect(after.activeSpaces == before.activeSpaces) #expect( after.workspaces[WorkspaceKey(display: left, space: space)] == before.workspaces[WorkspaceKey(display: left, space: space)]) #expect(!effects.contains(.report(.unsupported))) if command == .setWindowCap(2) { #expect( after.workspaces[key]?.tree?.stacks.map(\.windows) == [ [WindowID(rawValue: 4)], [WindowID(rawValue: 5), WindowID(rawValue: 6)], ]) } if case .setRatio(let ratio) = command { #expect(after.workspaces[key]?.layout == .side) #expect(after.workspaces[key]?.ratio == ratio) } if command == .retile { #expect( effects.contains( .confirmRetile( (4...6).map { WindowFrame( window: WindowID(rawValue: UInt32($0)), frame: after.frames[WindowID(rawValue: UInt32($0))]!) }))) } } @Test func targetedLayoutRecordsUndoForItsWindowsWithoutChangingTheOtherDisplay() { let before = scene() let key = WorkspaceKey(display: right, space: space) let (edited, _) = reduce(before, .workspaceCommandIssued(.setLayout(.rows), workspace: key)) #expect(edited.activeDisplay == before.activeDisplay) #expect(edited.focusedWindow == before.focusedWindow) #expect(edited.undoHistory?.count(for: WindowID(rawValue: 4)) == 1) #expect(edited.undoHistory?.count(for: WindowID(rawValue: 1)) ?? 0 == 0) let focused = reduce(edited, .focusChanged(WindowID(rawValue: 4))).0 let (restored, effects) = reduce(focused, .commandIssued(.undo)) #expect(restored.workspaces[key]?.tree == before.workspaces[key]?.tree) #expect(restored.workspaces[key]?.layout == before.workspaces[key]?.layout) let other = WorkspaceKey(display: left, space: space) #expect(restored.workspaces[other] == before.workspaces[other]) #expect(effects.contains(.announce(.undone(WindowID(rawValue: 4))))) } @Test func scopedRetileReportsOnlyItsOwnChangesInABatch() { let key = WorkspaceKey(display: right, space: space) let (_, effects) = reduce( scene(), [ .workspaceCommandIssued(.setLayout(.columns), workspace: key), .workspaceCommandIssued(.retile, workspace: key), ]) #expect(effects[1].contains(.confirmRetile([]))) #expect(!effects[0].contains(.confirmRetile([]))) } @Test func repeatedScopedRetilesCountEachWriteOnce() { let key = WorkspaceKey(display: right, space: space) let event = Event.workspaceCommandIssued(.retile, workspace: key) let (after, effects) = reduce(scene(), [event, event]) #expect( effects[0].contains( .confirmRetile( (4...6).map { WindowFrame( window: WindowID(rawValue: UInt32($0)), frame: after.frames[WindowID(rawValue: UInt32($0))]! ) }))) #expect(effects[1].contains(.confirmRetile([]))) } @Test func staleSpaceAndDisconnectedDisplayDoNothing() { let state = scene() for key in [ WorkspaceKey(display: right, space: SpaceID(rawValue: 9)), WorkspaceKey(display: DisplayID(rawValue: "gone"), space: space), ] { let (after, effects) = reduce(state, .workspaceCommandIssued(.setWindowCap(2), workspace: key)) #expect(after == state) #expect(effects.count == 1) #expect( effects.first == .report(key.display == right ? .noActiveWorkspace : .unavailableDisplay(key.display))) } } @Test(arguments: [Command.close, .toggleFullscreen, .toggleFloat, .focus(.right), .enterMode("test")]) func rejectsWindowAndGlobalCommands(command: Command) { let state = scene() let (after, effects) = reduce( state, .workspaceCommandIssued(command, workspace: WorkspaceKey(display: right, space: space))) #expect(after == state) #expect(effects == [.report(.unsupported)]) } @Test func targetLayoutEndsOnlyThatSpacesFocus() { var state = scene() let first = WorkspaceKey(display: left, space: space) let second = WorkspaceKey(display: right, space: space) state.focusModes = [ first: FocusModes(focusedWindow: WindowID(rawValue: 1)), second: FocusModes(focusedWindow: WindowID(rawValue: 4)), ] let (after, _) = reduce(state, .workspaceCommandIssued(.setLayout(.columns), workspace: second)) #expect(after.modes(for: first).focusedWindow == WindowID(rawValue: 1)) #expect(after.modes(for: second).focusedWindow == nil) } @Test(arguments: [Command.toggleFocus, .toggleNoPeek, .toggleCleanSpace]) func disablingTargetModesDoesNotNeedLocalFocus(command: Command) { var state = scene() let key = WorkspaceKey(display: right, space: space) state.focusModes = [key: FocusModes(focusedWindow: WindowID(rawValue: 4), noPeek: true, cleanSpace: true)] let (after, effects) = reduce(state, .workspaceCommandIssued(command, workspace: key)) #expect(!effects.contains(.report(.noFocusedWindow))) #expect(after.activeDisplay == left) if command == .toggleFocus { #expect(after.modes(for: key).focusedWindow == nil) } if command == .toggleNoPeek { #expect(!after.modes(for: key).noPeek) } if command == .toggleCleanSpace { #expect(!after.modes(for: key).cleanSpace) } } @Test func explicitWorkspaceDoesNotRequireAnActiveDisplay() { var state = scene() state.activeDisplay = nil let key = WorkspaceKey(display: right, space: space) let (after, effects) = reduce(state, .workspaceCommandIssued(.setLayout(.rows), workspace: key)) #expect(after.activeDisplay == nil) #expect(after.workspaces[key]?.layout == .rows) #expect(!effects.contains(.report(.noActiveWorkspace))) } @Test(arguments: [Command.toggleFocus, .toggleNoPeek, .toggleCleanSpace]) func rejectsModeOnAnotherDisplaysWindow(command: Command) { let state = scene() let key = WorkspaceKey(display: right, space: space) let (after, effects) = reduce(state, .workspaceCommandIssued(command, workspace: key)) #expect(after.modes(for: key).focusedWindow == nil) #expect(effects.contains(.report(.noFocusedWindow))) #expect(after.focusedWindow == state.focusedWindow) } }