import Foundation import Testing @testable import ParcelCore let secondaryDisplay = DisplayID(rawValue: "00000000-0000-0000-0000-000000000002") let secondarySpace = SpaceID(rawValue: 2) let secondaryKey = WorkspaceKey(display: secondaryDisplay, space: secondarySpace) func multipleDisplays(_ direction: Direction = .right, destinationCap: Int = 8) -> State { let frame: Rect switch direction { case .left: frame = Rect(x: -1200, width: 1200, height: 800) case .right: frame = Rect(x: 1200, width: 1200, height: 800) case .up: frame = Rect(y: -800, width: 1200, height: 800) case .down: frame = Rect(y: 800, width: 1200, height: 800) } let other = Workspace( key: secondaryKey, layout: .columns, windowCap: destinationCap, order: [windowID(3), windowID(4)]) let state = State( displays: [Display(id: primaryDisplay, frame: testScreen), Display(id: secondaryDisplay, frame: frame)], activeSpaces: [primaryDisplay: primarySpace, secondaryDisplay: secondarySpace], workspaces: [primaryKey: workspace(2, layout: .columns), secondaryKey: other], windows: Dictionary(uniqueKeysWithValues: (1...4).map { (windowID($0), managed($0)) }) ) return focused(1, in: command(.retile, in: state).0) } @Test func visibilityKeepsFloatingWindowsWithTheirWorkspace() { var state = command(.toggleFloat, in: focused(1, in: multipleDisplays())).0 state = command(.toggleFloat, in: focused(3, in: state)).0 #expect(state.visibleWindows == [windowID(2), windowID(1), windowID(4), windowID(3)]) state.activeSpaces[secondaryDisplay] = SpaceID(rawValue: 99) #expect(state.visibleWindows == [windowID(2), windowID(1)]) } @Test(arguments: Direction.allCases) func focusCrossesDisplaysByGeometry(direction: Direction) { let state = focused(direction == .right ? 2 : 1, in: multipleDisplays(direction)) let result = command(.focus(direction), in: state) #expect(result.0.focusedWindow == (direction == .left ? windowID(4) : windowID(3))) #expect(result.0.activeDisplay == secondaryDisplay) #expect(frameWrites(result.1).isEmpty) } @Test(arguments: Direction.allCases) func moveToDisplayUsesGlobalGeometry(direction: Direction) { let state = multipleDisplays(direction) let result = command(.moveToDisplay(direction), in: state) #expect(result.0.workspace(containing: windowID(1)) == secondaryKey) #expect(result.0.workspaces[primaryKey]?.order == [windowID(2)]) #expect(result.0.workspaces[secondaryKey]?.order == [windowID(3), windowID(4), windowID(1)]) #expect(result.0.focusedWindow == windowID(1)) #expect(result.0.activeDisplay == secondaryDisplay) #expect(result.1.contains(.announce(.movedToDisplay(windowID(1), secondaryDisplay)))) if let frame = result.0.frames[windowID(1)], let screen = result.0.displays[secondaryDisplay]?.frame { #expect(frame.minX >= screen.minX && frame.maxX <= screen.maxX) #expect(frame.minY >= screen.minY && frame.maxY <= screen.maxY) } } @Test func moveToFullDisplayStacksWithoutChangingSettings() { let state = multipleDisplays(destinationCap: 2) let result = command(.moveToDisplay(.right), in: state) #expect(result.0.workspaces[secondaryKey]?.tree?.tileCount == 2) #expect(result.0.frames[windowID(1)] == result.0.frames[windowID(4)]) #expect(result.0.workspaces[secondaryKey]?.layout == .columns) #expect(result.0.workspaces[secondaryKey]?.windowCap == 2) } @Test func moveToMissingDisplayReportsNoNeighbor() { let state = focused(1, in: populated()) let result = command(.moveToDisplay(.right), in: state) #expect(result.0 == state) #expect(result.1 == [.report(.noNeighbor(.right))]) } @Test(arguments: Direction.allCases) func swapAcrossFullDisplaysKeepsEachWindowCountWhenDestinationDoesNotStack(direction: Direction) { let source = windowID(direction == .right ? 2 : 1) let target = windowID(direction == .left ? 4 : 3) var state = focused(Int(source.rawValue), in: multipleDisplays(direction, destinationCap: 2)) state.workspaces[secondaryKey]?.whenFull = .float let result = command(.swap(direction), in: state) #expect(result.0.workspace(containing: source) == secondaryKey) #expect(result.0.workspace(containing: target) == primaryKey) for key in [primaryKey, secondaryKey] { #expect(result.0.workspaces[key]?.order.count == state.workspaces[key]?.order.count) #expect(result.0.workspaces[key]?.tree?.tileCount == 2) #expect(result.0.workspaces[key]?.layout == state.workspaces[key]?.layout) #expect(result.0.workspaces[key]?.whenFull == state.workspaces[key]?.whenFull) } #expect(result.0.frames[source] == state.frames[target]) #expect(result.0.frames[target] == state.frames[source]) #expect(result.0.focusedWindow == source) #expect(result.0.activeDisplay == secondaryDisplay) } @Test(arguments: WhenDisplayFull.allCases) func edgeSwapKeepsHiddenStackMembersOnTheirOriginalDisplay(policy: WhenDisplayFull) { var state = multipleDisplays(destinationCap: 2) state = command(.setWindowCap(2), in: state).0 state = reduce(focused(2, in: state), .windowAppeared(managed(5), workspace: primaryKey, frame: Rect())).0 state = command(.stack(.left), in: focused(4, in: state)).0 state = command(.setWindowCap(1), in: state).0 state = focused(2, in: state) state.whenDisplayFull = policy state.workspaces[secondaryKey]?.whenFull = .float let result = command(.swap(.right), in: state) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.workspace(containing: windowID(5)) == primaryKey) #expect(result.0.workspace(containing: windowID(3)) == secondaryKey) #expect(result.0.workspace(containing: windowID(4)) == (policy == .swap ? primaryKey : secondaryKey)) #expect(result.0.workspaces[primaryKey]?.order.count == (policy == .swap ? 3 : 2)) #expect(result.0.workspaces[secondaryKey]?.order.count == (policy == .swap ? 2 : 3)) #expect(result.0.workspaces[primaryKey]?.visibleWindows.count == 2) #expect(result.0.workspaces[secondaryKey]?.visibleWindows == [windowID(2)]) #expect(result.0.focusedWindow == windowID(2)) } @Test(arguments: Layout.allCases, Layout.allCases) func fullDisplaySwapPreservesIndependentLayoutSettings(sourceLayout: Layout, destinationLayout: Layout) throws { var state = multipleDisplays(destinationCap: 2) state.workspaces[secondaryKey]?.whenFull = .float state = command(.setLayout(sourceLayout), in: state).0 state = command(.setRatio(.threeFifths), in: state).0 state = command(.setLayout(destinationLayout), in: focused(3, in: state)).0 state = command(.setRatio(.twoThirds), in: state).0 state = focused(2, in: state) let result = command(.swap(.right), in: state) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) for key in [primaryKey, secondaryKey] { let before = try #require(state.workspaces[key]) let after = try #require(result.0.workspaces[key]) #expect(after.layout == before.layout) #expect(after.ratio == before.ratio) #expect(after.gaps == before.gaps) #expect(after.reservedEdges == before.reservedEdges) #expect(after.windowCap == before.windowCap) #expect(after.order.count == before.order.count) #expect(after.visibleWindows.count == before.visibleWindows.count) } } @Test(arguments: WhenDisplayFull.allCases) func displayFullPolicyRoundTripsAndLegacySnapshotsDefaultToSwap(policy: WhenDisplayFull) throws { var state = multipleDisplays(destinationCap: 2) #expect(state.whenDisplayFull == .swap) #expect(State(whenDisplayFull: policy).whenDisplayFull == policy) state.whenDisplayFull = policy let data = try JSONEncoder().encode(state) let decoded = try JSONDecoder().decode(State.self, from: data) #expect(decoded == state) #expect(decoded.whenDisplayFull == policy) #expect( command(.swap(.right), in: focused(2, in: decoded)).0 == command(.swap(.right), in: focused(2, in: state)).0) if policy == .swap { #expect(!String(decoding: data, as: UTF8.self).contains("\"whenDisplayFull\"")) var explicit = state explicit.whenDisplayFull = .stack let encoded = try JSONEncoder().encode(explicit) let json = String(decoding: encoded, as: UTF8.self).replacingOccurrences( of: "\"whenDisplayFull\":\"stack\"", with: "\"whenDisplayFull\":\"swap\"") #expect(try JSONDecoder().decode(State.self, from: Data(json.utf8)) == state) } else { let invalid = String(decoding: data, as: UTF8.self).replacingOccurrences( of: "\"whenDisplayFull\":\"stack\"", with: "\"whenDisplayFull\":\"invalid\"") #expect(throws: DecodingError.self) { try JSONDecoder().decode(State.self, from: Data(invalid.utf8)) } } } @Test func edgeSwapReachesAFullStaggeredDisplay() { var state = multipleDisplays(destinationCap: 2) state.workspaces[secondaryKey]?.whenFull = .float state = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: testScreen), Display(id: secondaryDisplay, frame: Rect(x: 300, y: 800, width: 700, height: 800)), ]) ).0 state = focused(2, in: state) let result = command(.swap(.right), in: state) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.workspace(containing: windowID(3)) == primaryKey) } @Test(arguments: Direction.allCases, [(WhenDisplayFull.swap, WhenFull.stack), (.stack, .float), (.stack, .stack)]) func edgeSwapStacksOnAFullDisplayWhenEitherPolicyRequestsIt(direction: Direction, policies: (WhenDisplayFull, WhenFull)) { let source = windowID(direction == .right ? 2 : 1) let target = windowID(direction == .left ? 4 : 3) var state = focused(Int(source.rawValue), in: multipleDisplays(direction, destinationCap: 2)) let (policy, overflow) = policies state.whenDisplayFull = policy state.workspaces[secondaryKey]?.whenFull = overflow let result = command(.swap(direction), in: state) #expect(result.0.workspace(containing: source) == secondaryKey) #expect(result.0.workspace(containing: target) == secondaryKey) #expect(result.0.workspaces[primaryKey]?.order.count == 1) #expect(result.0.workspaces[secondaryKey]?.order.count == 3) #expect(result.0.workspaces[secondaryKey]?.tree?.tileCount == 2) #expect(result.0.frames[source] == result.0.frames[target]) #expect(!result.0.visibleWindows.contains(target)) #expect(result.0.windows[source]?.isFloating == false) #expect(result.0.workspaces[secondaryKey]?.layout == .columns) #expect(result.0.workspaces[secondaryKey]?.whenFull == overflow) #expect(result.0.focusedWindow == source) #expect(result.1.contains(.announce(.movedToDisplay(source, secondaryDisplay)))) } @Test(arguments: Direction.allCases) func sourceStackPreferenceDoesNotOverrideADestinationThatExchanges(direction: Direction) { let source = windowID(direction == .right ? 2 : 1) let target = windowID(direction == .left ? 4 : 3) var state = focused(Int(source.rawValue), in: multipleDisplays(direction, destinationCap: 2)) state.workspaces[primaryKey]?.whenFull = .stack state.workspaces[secondaryKey]?.whenFull = .float state.whenDisplayFull = .swap let result = command(.swap(direction), in: state) #expect(result.0.workspace(containing: source) == secondaryKey) #expect(result.0.workspace(containing: target) == primaryKey) #expect(result.0.workspaces[primaryKey]?.order.count == 2) #expect(result.0.workspaces[secondaryKey]?.order.count == 2) } @Test(arguments: Direction.allCases, WhenDisplayFull.allCases) func edgeSwapMovesToEmptyAdjacentDisplay(direction: Direction, policy: WhenDisplayFull) throws { var state = multipleDisplays(direction) state.whenDisplayFull = policy state = reduce(state, .windowDisappeared(windowID(3))).0 state = reduce(state, .windowDisappeared(windowID(4))).0 state = focused(direction == .right ? 2 : 1, in: state) let window = try #require(state.focusedWindow) let result = command(.swap(direction), in: state) #expect(result.0.workspace(containing: window) == secondaryKey) #expect(result.0.focusedWindow == window) #expect(result.0.activeDisplay == secondaryDisplay) #expect(result.1.contains(.announce(.movedToDisplay(window, secondaryDisplay)))) } @Test(arguments: WhenDisplayFull.allCases) func edgeSwapMovesToDisplayWithRoomWithoutDisplacingWindows(policy: WhenDisplayFull) { var state = focused(2, in: multipleDisplays()) state.whenDisplayFull = policy let result = command(.swap(.right), in: state) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.workspace(containing: windowID(3)) == secondaryKey) #expect(result.0.workspaces[secondaryKey]?.tree?.tileCount == 3) #expect(result.0.focusedWindow == windowID(2)) } @Test func edgeSwapDoesNotSkipAnEmptyDisplayForAFartherWindow() { let thirdDisplay = DisplayID(rawValue: "00000000-0000-0000-0000-000000000003") let thirdKey = WorkspaceKey(display: thirdDisplay, space: secondarySpace) var state = multipleDisplays() state = reduce(state, .windowDisappeared(windowID(3))).0 state = reduce(state, .windowDisappeared(windowID(4))).0 state.displays[thirdDisplay] = Display(id: thirdDisplay, frame: Rect(x: 2400, width: 1200, height: 800)) state.activeSpaces[thirdDisplay] = secondarySpace state.workspaces[thirdKey] = Workspace(key: thirdKey, layout: .columns, order: [windowID(3)]) state.windows[windowID(3)] = managed(3) state = focused(2, in: command(.retile, in: state).0) let result = command(.swap(.right), in: state) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.workspace(containing: windowID(3)) == thirdKey) } @Test(arguments: WhenDisplayFull.allCases) func swapPrefersNeighborOnTheSameDisplay(policy: WhenDisplayFull) { var state = focused(1, in: multipleDisplays(destinationCap: 2)) state.whenDisplayFull = policy let result = command(.swap(.right), in: state) #expect(result.0.workspace(containing: windowID(1)) == primaryKey) #expect(result.0.workspace(containing: windowID(2)) == primaryKey) #expect(result.0.frames[windowID(1)] == state.frames[windowID(2)]) #expect(result.0.focusedWindow == windowID(1)) } @Test func moveAcrossDisplaysSplitsDirectionalTarget() { let state = focused(2, in: multipleDisplays()) let result = command(.move(.right), in: state) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.frames[windowID(2)] == Rect(x: 1500, width: 300, height: 800)) #expect(result.0.frames[windowID(3)] == Rect(x: 1200, width: 300, height: 800)) } @Test func stackAcrossDisplaysJoinsTargetTile() { let result = command(.stack(.right), in: focused(2, in: multipleDisplays())) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.frames[windowID(2)] == result.0.frames[windowID(3)]) #expect(result.0.workspaces[secondaryKey]?.tree?.tileCount == 2) } @Test func floatingMoveToDisplayCentersAndWritesOnce() { let floated = command(.toggleFloat, in: multipleDisplays()).0 let state = reduce(floated, .windowResizedByUser(windowID(1), frame: Rect(width: 300, height: 200))).0 let result = command(.moveToDisplay(.right), in: state) #expect(result.0.frames[windowID(1)] == Rect(x: 1650, y: 300, width: 300, height: 200)) #expect(frameWrites(result.1).filter { $0.window == windowID(1) }.count == 1) #expect(result.0.windows[windowID(1)]?.isFloating == true) } @Test func workspacesKeepIndependentLayoutsAndRatios() { let state = multipleDisplays() let first = command(.setLayout(.side), in: state).0 let ratio = command(.setRatio(.threeFifths), in: first).0 let second = command(.setLayout(.rows), in: focused(3, in: ratio)).0 #expect(second.workspaces[primaryKey]?.layout == .side) #expect(second.workspaces[primaryKey]?.ratio == .threeFifths) #expect(second.workspaces[secondaryKey]?.layout == .rows) #expect(second.workspaces[secondaryKey]?.ratio == .half) } @Test func spaceSwitchRetainsOldWorkspaceAndCreatesNewOne() { let state = focused(2, in: populated()) let result = reduce(state, .activeSpaceChanged(display: primaryDisplay, space: secondarySpace)) let key = WorkspaceKey(display: primaryDisplay, space: secondarySpace) #expect(result.0.workspaces[primaryKey] == state.workspaces[primaryKey]) #expect(result.0.workspaces[key]?.order.isEmpty == true) #expect(result.0.focusedWindow == nil) #expect(result.0.visibleWindows.isEmpty) #expect(frameWrites(result.1).isEmpty) } @Test func returningToSpaceRestoresFocusAndSettings() { let initial = command(.setRatio(.twoThirds), in: focused(2, in: populated())).0 let away = reduce(initial, .activeSpaceChanged(display: primaryDisplay, space: secondarySpace)).0 let result = reduce(away, .activeSpaceChanged(display: primaryDisplay, space: primarySpace)) #expect(result.0.workspaces[primaryKey] == initial.workspaces[primaryKey]) #expect(result.0.focusedWindow == windowID(2)) #expect(result.1.contains(.focusWindow(windowID(2)))) #expect(frameWrites(result.1).isEmpty) } @Test func inactiveSpaceAppearanceDoesNotWriteOrFocus() { let state = populated() let key = WorkspaceKey(display: primaryDisplay, space: secondarySpace) let result = reduce(state, .windowAppeared(managed(4), workspace: key, frame: Rect(width: 100, height: 100))) #expect(result.0.focusedWindow == state.focusedWindow) #expect(result.0.workspaces[key]?.order == [windowID(4)]) #expect(result.1.isEmpty) } @Test func activatingPopulatedSpaceWritesItsFrames() { let key = WorkspaceKey(display: primaryDisplay, space: secondarySpace) let state = reduce(populated(), .windowAppeared(managed(4), workspace: key, frame: Rect(width: 100, height: 100))).0 let result = reduce(state, .activeSpaceChanged(display: primaryDisplay, space: secondarySpace)) #expect(frameWrites(result.1) == [WindowFrame(window: windowID(4), frame: testScreen)]) #expect(result.0.focusedWindow == windowID(4)) #expect(result.0.visibleWindows == [windowID(4)]) } @Test func inactiveSpaceFocusNotificationIsIgnored() { let initial = focused(2, in: populated()) let state = reduce(initial, .activeSpaceChanged(display: primaryDisplay, space: secondarySpace)).0 let result = reduce(state, .focusChanged(windowID(1))) #expect(result.0 == state) #expect(result.1.isEmpty) } @Test func spaceChangeOnOtherDisplayDoesNotStealFocus() { let state = multipleDisplays() let result = reduce(state, .activeSpaceChanged(display: secondaryDisplay, space: SpaceID(rawValue: 3))) #expect(result.0.focusedWindow == state.focusedWindow) #expect(result.0.activeDisplay == primaryDisplay) } @Test func missingDisplaySpaceChangeIsReported() { let state = populated() let result = reduce(state, .activeSpaceChanged(display: secondaryDisplay, space: secondarySpace)) #expect(result.0 == state) #expect(result.1 == [.report(.unavailableDisplay(secondaryDisplay))]) } @Test func unchangedDisplaySetDoesNotWrite() { let state = multipleDisplays() let result = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: testScreen), state.displays[secondaryDisplay] ?? Display(id: secondaryDisplay, frame: Rect()), ])) #expect(result.0 == state) #expect(result.1.isEmpty) } @Test func displayResizeDiffsOnlyAffectedDisplay() { let state = multipleDisplays() let result = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: Rect(width: 1000, height: 800)), state.displays[secondaryDisplay] ?? Display(id: secondaryDisplay, frame: Rect()), ])) #expect(frameWrites(result.1).map(\.window) == [windowID(1), windowID(2)]) #expect(result.0.frames[windowID(3)] == state.frames[windowID(3)]) } @Test func displayDisconnectMigratesActiveWindowsAndRetainsSettings() { let state = multipleDisplays() let result = reduce( state, .displaySetChanged([state.displays[secondaryDisplay] ?? Display(id: secondaryDisplay, frame: Rect())])) #expect(result.0.workspace(containing: windowID(1)) == secondaryKey) #expect(result.0.workspace(containing: windowID(2)) == secondaryKey) #expect(result.0.workspaces[primaryKey]?.order.isEmpty == true) #expect(result.0.workspaces[primaryKey]?.layout == .columns) #expect(result.0.focusedWindow == windowID(1)) } @Test func reconnectRestoresEmptyWorkspaceSettings() { let state = command(.setLayout(.side), in: multipleDisplays()).0 let disconnected = reduce( state, .displaySetChanged([state.displays[secondaryDisplay] ?? Display(id: secondaryDisplay, frame: Rect())]) ).0 let result = reduce( disconnected, .displaySetChanged([ Display(id: primaryDisplay, frame: testScreen), state.displays[secondaryDisplay] ?? Display(id: secondaryDisplay, frame: Rect()), ])) #expect(result.0.workspaces[primaryKey]?.layout == .side) #expect(result.0.workspaces[primaryKey]?.order.isEmpty == true) } @Test func noDisplaysRetainsWindowsWithoutFrameWrites() { let state = populated() let result = reduce(state, .displaySetChanged([])) #expect(result.0.windows == state.windows) #expect(result.0.frames == state.frames) #expect(result.0.workspaces == state.workspaces) #expect(result.0.focusedWindow == nil) #expect(result.1.isEmpty) } @Test func duplicateDisplayIDsAreRejected() { let state = populated() let display = Display(id: primaryDisplay, frame: testScreen) let result = reduce(state, .displaySetChanged([display, display])) #expect(result.0 == state) #expect(result.1 == [.report(.invalidGeometry)]) } @Test func invalidDisplayGeometryIsRejected() { let result = reduce( populated(), .displaySetChanged([Display(id: primaryDisplay, frame: Rect(x: .nan, width: 10, height: 10))])) #expect(result.1 == [.report(.invalidGeometry)]) } @Test func reappearingWindowOnDifferentWorkspaceTransfersMembership() { let state = multipleDisplays() let result = reduce( state, .windowAppeared(managed(1), workspace: secondaryKey, frame: Rect(x: 1200, width: 300, height: 300))) #expect(result.0.workspace(containing: windowID(1)) == secondaryKey) #expect(result.0.workspaces[primaryKey]?.order == [windowID(2)]) #expect(result.0.workspaces[secondaryKey]?.order.filter { $0 == windowID(1) }.count == 1) }