Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281import Foundationimport Testing
@testable import ParcelCore
private func partitionContaining(_ tree: TileTree?, _ first: WindowID, _ second: WindowID) -> TileTree? { guard let tree else { return nil } switch tree { case .tile: return nil case .split(_, _, let left, let right), .partition(_, _, let left, let right): if left.windows.contains(first) && right.windows.contains(second) { return tree } return partitionContaining(left, first, second) ?? partitionContaining(right, first, second) }}
@Test func manualSplitTargetsPreviousFocusAndConsumesPendingAxis() throws { var state = focused(1, in: populated(3, layout: .tree)) state = command(.split(.vertical), in: state).0 #expect(state.workspaces[primaryKey]?.pendingSplitAxis == .vertical) try roundTrip(state.workspaces[primaryKey]) state = reduce(state, .windowAppeared(managed(4), workspace: primaryKey, frame: Rect())).0 let node = try #require(partitionContaining(state.workspaces[primaryKey]?.tree, windowID(1), windowID(4))) guard case .partition(let axis, _, let first, let second) = node else { Issue.record("Expected a new partition") return } #expect(axis == .vertical) #expect(first.windows == [windowID(1)]) #expect(second.windows == [windowID(4)]) #expect(state.workspaces[primaryKey]?.pendingSplitAxis == nil) #expect(state.workspaces[primaryKey]?.order == state.workspaces[primaryKey]?.tree?.windows) #expect(state.focusedWindow == windowID(4))}
@Test func pendingSplitSurvivesFloatingAndDuplicateObservations() { var state = focused(1, in: populated(2, layout: .tree)) state = command(.split(.vertical), in: state).0 state = reduce(state, .windowAppeared(managed(1), workspace: primaryKey, frame: Rect())).0 state = reduce(state, .windowAppeared(managed(3, floating: true), workspace: primaryKey, frame: Rect())).0 #expect(state.workspaces[primaryKey]?.pendingSplitAxis == .vertical) state = reduce(state, .windowAppeared(managed(4), workspace: primaryKey, frame: Rect())).0 #expect(state.workspaces[primaryKey]?.pendingSplitAxis == nil)}
@Test func pendingSplitWaitsForAnActualPartition() throws { var state = focused(1, in: populated(2, layout: .tree, cap: 2)) state = command(.split(.vertical), in: state).0 state = reduce(state, .windowAppeared(managed(3), workspace: primaryKey, frame: Rect())).0 #expect(state.workspaces[primaryKey]?.pendingSplitAxis == .vertical) state = command(.setWindowCap(4), in: state).0 #expect(state.workspaces[primaryKey]?.tree?.tileCount == 3) #expect(state.workspaces[primaryKey]?.pendingSplitAxis == .vertical) state = reduce(state, .windowAppeared(managed(4), workspace: primaryKey, frame: Rect())).0 let node = try #require(partitionContaining(state.workspaces[primaryKey]?.tree, windowID(3), windowID(4))) guard case .partition(let axis, _, _, _) = node else { Issue.record("Expected a partition") return } #expect(axis == .vertical) #expect(state.workspaces[primaryKey]?.pendingSplitAxis == nil)}
@Test func bspSplitsFocusedTilesActualLongSide() throws { var state = focused(1, in: populated(2, layout: .bsp)) #expect(state.frames[windowID(1)] == Rect(width: 600, height: 800)) state = reduce(state, .windowAppeared(managed(3), workspace: primaryKey, frame: Rect())).0 let node = try #require(partitionContaining(state.workspaces[primaryKey]?.tree, windowID(1), windowID(3))) guard case .partition(let axis, _, let first, let second) = node else { Issue.record("Expected a new partition") return } #expect(axis == .vertical) #expect(first.windows == [windowID(1)]) #expect(second.windows == [windowID(3)]) #expect(state.frames[windowID(2)] == Rect(x: 600, width: 600, height: 800))}
@Test func bspUsesRoundedDisplayedDimensionsForSplitAxis() throws { var space = workspace(1, layout: .bsp) let screen = Rect(width: 500.1, height: 500.4) space.adapt(to: screen) space.insert(windowID(2), floating: false, in: screen, windows: [:]) let node = try #require(partitionContaining(space.tree, windowID(1), windowID(2))) guard case .partition(let axis, _, _, _) = node else { Issue.record("Expected a new partition") return } #expect(axis == .horizontal)}
@Test(arguments: [Layout.tree, .bsp])func treeInsertionAtCapUsesExistingStackBehavior(layout: Layout) { var state = focused(1, in: populated(2, layout: layout, cap: 2)) state = reduce(state, .windowAppeared(managed(3), workspace: primaryKey, frame: Rect())).0 #expect(state.workspaces[primaryKey]?.tree?.stacks.map(\.windows) == [[windowID(1), windowID(3)], [windowID(2)]]) #expect(state.focusedWindow == windowID(3))}
@Test func joinMovesAnEntireStackBesideNeighbor() throws { var state = focused(3, in: populated(3, layout: .tree, cap: 3)) state = reduce(state, .windowDropped(source: windowID(3), target: windowID(2), stacking: true)).0 state = command(.setWindowCap(4), in: state).0 let original = try #require(state.workspaces[primaryKey]?.tree) #expect(original.stacks.last?.windows == [windowID(2), windowID(3)]) state = command(.join(.left), in: state).0 let updated = try #require(state.workspaces[primaryKey]?.tree) #expect(updated.stacks.map(\.windows) == [[windowID(2), windowID(3)], [windowID(1)]]) #expect(state.workspaces[primaryKey]?.order == updated.windows) #expect(state.focusedWindow == windowID(3)) #expect(state.workspaces[primaryKey]?.focus == windowID(3)) #expect(state.visibleWindows.contains(windowID(3))) #expect(command(.join(.left), in: state).1 == [.report(.noNeighbor(.left))])}
@Test(arguments: [Layout.tree, .bsp, .accordion])func treeRetileAndCapChangesPreserveSurvivingTopology(layout: Layout) throws { var state = focused(2, in: populated(5, layout: layout)) state = command(.rotate, in: state).0 let original = try #require(state.workspaces[primaryKey]?.tree) state = command(.retile, in: state).0 #expect(state.workspaces[primaryKey]?.tree == original) state = command(.setWindowCap(5), in: state).0 #expect(state.workspaces[primaryKey]?.tree == original) state = command(.setWindowCap(3), in: state).0 let limited = try #require(state.workspaces[primaryKey]?.tree) #expect(limited.tileCount == 3) #expect(Set(limited.windows) == Set(original.windows)) #expect(state.focusedWindow == windowID(2)) #expect(limited.stacks.contains { $0.visible == windowID(2) }) state = command(.setWindowCap(8), in: state).0 #expect(state.workspaces[primaryKey]?.tree == original)}
@Test(arguments: [Layout.tree, .bsp, .accordion], Direction.allCases)func directionalFocusAndSwapUseTreeGeometry(layout: Layout, direction: Direction) throws { var initial = populated(4, layout: layout) initial.workspaces[primaryKey]?.tree = TileTree.balanced((1...4).map { WindowStack(windowID($0)) }, layout: .grid) initial = reduce(initial, .relayout).0 let source = direction == .left || direction == .up ? 4 : 1 initial = focused(source, in: initial) let neighbors = initial.visibleWindows.filter { $0 != windowID(source) }.compactMap { id -> (WindowID, DirectionalDistance)? in guard let frame = initial.frames[id], let sourceFrame = initial.frames[windowID(source)], let distance = direction.distance(from: sourceFrame, to: frame) else { return nil } return (id, distance) }.sorted { $0.1 == $1.1 ? $0.0 < $1.0 : $0.1 < $1.1 } let target = try #require(neighbors.first?.0) let focusedState = command(.focus(direction), in: initial).0 #expect(focusedState.focusedWindow == target) let swapped = command(.swap(direction), in: initial).0 let before = try #require(initial.workspaces[primaryKey]?.tree) let after = try #require(swapped.workspaces[primaryKey]?.tree) #expect( after.windows == before.windows.map { $0 == windowID(source) ? target : ($0 == target ? windowID(source) : $0) } ) #expect(swapped.focusedWindow == windowID(source)) #expect(Set(swapped.frames.keys) == Set(initial.frames.keys))}
@Test func transformationsUseTreeOperationsAndSynchronizeOrder() throws { var state = focused(2, in: populated(4, layout: .tree)) for operation in [Command.balance, .rotate, .mirror(.horizontal), .mirror(.vertical)] { let before = try #require(state.workspaces[primaryKey]?.tree) let expected: TileTree switch operation { case .balance: expected = before.balancedSplits() case .rotate: expected = before.rotatedClockwise() case .mirror(let axis): expected = before.mirrored(along: axis) default: return } state = command(operation, in: state).0 #expect(state.workspaces[primaryKey]?.tree == expected) #expect(state.workspaces[primaryKey]?.order == expected.windows) #expect(state.focusedWindow == windowID(2)) }}
@Test func treeOrderSynchronizationPreservesFloatingWindowSlots() throws { var state = populated(3, layout: .tree) state = reduce(state, .windowAppeared(managed(4, floating: true), workspace: primaryKey, frame: Rect())).0 state = reduce(state, .windowAppeared(managed(5), workspace: primaryKey, frame: Rect())).0 let before = try #require(state.workspaces[primaryKey]?.order) let floatingIndex = try #require(before.firstIndex(of: windowID(4))) for operation in [Command.rotate, .mirror(.horizontal), .mirror(.vertical), .balance] { state = command(operation, in: state).0 let space = try #require(state.workspaces[primaryKey]) #expect(space.order[floatingIndex] == windowID(4)) #expect(space.order.filter { $0 != windowID(4) } == space.tree?.windows) #expect(Set(space.order) == Set(before)) }}
@Test func accordionShowsSliversAndFocusChangesLargestTile() throws { var state = populated(4, layout: .accordion) state.workspaces[primaryKey]?.gaps = Gaps(inner: 10) for number in 1...4 { state = focused(number, in: state) let focusFrame = try #require(state.frames[windowID(number)]) for other in 1...4 { let frame = try #require(state.frames[windowID(other)]) #expect(frame.size.width > 0 && frame.size.height > 0) #expect(frame.minX >= 0 && frame.maxX <= testScreen.maxX) if other != number { #expect(focusFrame.size.width > frame.size.width) } } let ordered = state.workspaces[primaryKey]?.tree?.visibleWindows ?? [] for (left, right) in zip(ordered, ordered.dropFirst()) { let leftFrame = try #require(state.frames[left]) let rightFrame = try #require(state.frames[right]) #expect(leftFrame.maxX + 10 <= rightFrame.minX) } } #expect(LayoutEngine.edges(try #require(state.workspaces[primaryKey]), in: testScreen).isEmpty)}
@Test func accordionRespectsFeasibleMinimumsAndReportsImpossibleOnes() throws { var state = focused(1, in: populated(3, layout: .accordion)) for number in 1...3 { state = reduce( state, .minimumSizeChanged(windowID(number), minimum: LearnedMinimumSize(size: Size(width: 100, height: 200))) ).0 } #expect(state.cannotFit.isEmpty) for frame in state.frames.values { #expect(frame.size.width >= 100 && frame.size.height >= 200) } let impossible = reduce( state, .minimumSizeChanged(windowID(2), minimum: LearnedMinimumSize(size: Size(width: 1500, height: 200)))) state = impossible.0 #expect(state.cannotFit.isEmpty) #expect(state.windows[windowID(2)]?.isFloating == true) #expect(state.windows[windowID(2)]?.managementReason == .whenFull) #expect(impossible.1.contains(.report(.spaceFull(windowID(2))))) #expect(state.frames[windowID(2)]?.size.width == 1500) for (window, frame) in state.frames where state.windows[window]?.isFloating == false { #expect(frame.minX >= 0 && frame.maxX <= 1200) #expect(frame.minY >= 0 && frame.maxY <= 800) }}
@Test func bindingModeWorksWithoutWindowsAndRoundTrips() throws { var state = State() let entered = command(.enterMode("resize"), in: state) #expect(entered.0.activeMode == "resize") #expect(entered.1 == [.announce(.modeChanged("resize"))]) try roundTrip(entered.0) try roundTrip(entered.1) state = command(.enterMode(nil), in: entered.0).0 #expect(state.activeMode == nil) #expect(command(.enterMode(nil), in: state).1 == [.announce(.modeChanged(nil))])}
@Test func oldWorkspaceAndStateDecodeWithoutNewOptionalProperties() throws { let oldWorkspace = workspace(2) let workspaceData = try JSONEncoder().encode(oldWorkspace) #expect(!String(decoding: workspaceData, as: UTF8.self).contains("pendingSplitAxis")) #expect(try JSONDecoder().decode(Workspace.self, from: workspaceData) == oldWorkspace) let oldState = populated(2) let stateData = try JSONEncoder().encode(oldState) #expect(!String(decoding: stateData, as: UTF8.self).contains("activeMode")) #expect(try JSONDecoder().decode(State.self, from: stateData) == oldState)}
@Test func treeCommandsAndModesReplayDeterministically() throws { let initial = focused(1, in: populated(3, layout: .tree)) let events: [Event] = [ .commandIssued(.enterMode("resize")), .commandIssued(.split(.vertical)), .windowAppeared(managed(4), workspace: primaryKey, frame: Rect()), .commandIssued(.join(.left)), .commandIssued(.balance), .commandIssued(.rotate), .commandIssued(.mirror(.horizontal)), .commandIssued(.setLayout(.accordion)), .focusChanged(windowID(2)), .commandIssued(.enterMode(nil)), ] let encoded = try EventStream.encode(events) #expect(try EventStream.decode(encoded) == events) let replay = try Replay.run(jsonLines: encoded, initial: initial) #expect(replay == Replay.run(events, initial: initial)) #expect(replay == (try Replay.run(jsonLines: encoded, initial: initial))) #expect(replay.state.activeMode == nil) #expect(replay.effects.first == [.announce(.modeChanged("resize"))]) try roundTrip(replay)}