Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320import Testing
@testable import ParcelCore
@Test(arguments: Direction.allCases, [Layout.grid, .columns, .rows, .side])func focusUsesGeometryInCanonicalTrees(direction: Direction, layout: Layout) { let state = focused(4, in: populatedTree(8, layout: layout)) let number: Int? switch (layout, direction) { case (.columns, .left): number = 3 case (.columns, .right): number = 5 case (.rows, .up), (.side, .up): number = 3 case (.rows, .down), (.side, .down): number = 5 case (.side, .left), (.grid, .up): number = 1 case (.grid, .right): number = 5 case (.grid, .down): number = 7 default: number = nil } let expected = number.map(windowID) let result = command(.focus(direction), in: state) if let expected { #expect(result.0.focusedWindow == expected) #expect(result.1.contains(.focusWindow(expected))) } else { #expect(result.1 == [.report(.noNeighbor(direction))]) } #expect(frameWrites(result.1).isEmpty)}
@Test func focusLeftPixels() { let result = command(.focus(.left), in: focused(2, in: populated(4, layout: .grid))) #expect(result.0.focusedWindow == windowID(1))}
@Test func focusRightPixels() { let result = command(.focus(.right), in: focused(1, in: populated(4, layout: .grid))) #expect(result.0.focusedWindow == windowID(2))}
@Test func focusUpPixels() { let result = command(.focus(.up), in: focused(3, in: populated(4, layout: .grid))) #expect(result.0.focusedWindow == windowID(1))}
@Test func focusDownPixels() { let result = command(.focus(.down), in: focused(1, in: populated(4, layout: .grid))) #expect(result.0.focusedWindow == windowID(3))}
@Test func focusDoesNotWrapAtEdge() { let state = focused(1, in: populated(4, layout: .grid)) let result = command(.focus(.left), in: state) #expect(result.0 == state) #expect(result.1 == [.report(.noNeighbor(.left))])}
@Test func focusCanReachFloatingWindow() { let state = focused(1, in: populated(1)) let floating = reduce( state, .windowAppeared( managed(2, floating: true), workspace: primaryKey, frame: Rect(x: 1100, width: 100, height: 100)) ).0 let result = command(.focus(.right), in: focused(1, in: floating)) #expect(result.0.focusedWindow == windowID(2))}
@Test(arguments: [Direction.left, .right, .up, .down])func swapExchangesOnlyTwoWindowPositions(direction: Direction) { let number = direction == .left ? 2 : (direction == .up ? 3 : 1) let state = focused(number, in: populated(4, layout: .grid)) let target = state.neighbor(of: windowID(number), direction: direction) let result = command(.swap(direction), in: state) if let target { #expect(result.0.frames[windowID(number)] == state.frames[target]) #expect(result.0.frames[target] == state.frames[windowID(number)]) #expect(frameWrites(result.1).count == 2) } #expect(result.0.focusedWindow == windowID(number))}
@Test func swapAtEdgeReportsNoNeighbor() { let state = focused(1, in: populated()) let result = command(.swap(.left), in: state) #expect(result.0 == state) #expect(result.1 == [.report(.noNeighbor(.left))])}
@Test func swapExchangesVisibleMembersNotWholeStacks() { let state = focused(1, in: populated(4, cap: 2)) let result = command(.swap(.right), in: state) #expect(result.0.workspaces[primaryKey]?.tree?.stacks[0].windows == [windowID(4)]) #expect(result.0.workspaces[primaryKey]?.tree?.stacks[1].windows == [windowID(2), windowID(3), windowID(1)]) #expect(result.0.focusedWindow == windowID(1))}
@Test(arguments: Direction.allCases, Layout.allCases)func movePreservesMembershipAndFocus(direction: Direction, layout: Layout) { let state = focused(4, in: populated(8, layout: layout)) let result = command(.move(direction), in: state) #expect(Set(result.0.workspaces[primaryKey]?.order ?? []) == Set((1...8).map(windowID))) #expect( result.0.workspaces[primaryKey]?.tree?.tileCount == min(8, layout.capabilities(in: testScreen).maximumWindowCount)) #expect(result.0.focusedWindow == windowID(4)) for frame in result.0.frames.values { #expect(frame.minX >= 0 && frame.maxX <= 1200) #expect(frame.minY >= 0 && frame.maxY <= 800) }}
@Test func moveUpSplitsNeighborAboveIt() { let result = command(.move(.up), in: focused(3, in: populated(4, layout: .grid))) #expect(result.0.frames[windowID(3)] == Rect(width: 600, height: 200)) #expect(result.0.frames[windowID(1)] == Rect(y: 200, width: 600, height: 200)) #expect(result.0.workspaces[primaryKey]?.order == [windowID(3), windowID(1), windowID(2), windowID(4)])}
@Test func moveAtEdgeDoesNotChangeOrder() { let state = focused(1, in: populated()) let result = command(.move(.left), in: state) #expect(result.0 == state) #expect(result.1 == [.report(.noNeighbor(.left))])}
@Test func cycleLayoutFollowsStableOrder() { var state = populated(3, layout: .grid) for layout in [Layout.columns, .rows, .side, .grid] { let result = command(.cycleLayout, in: state) #expect(result.0.workspaces[primaryKey]?.layout == layout) #expect(result.1.contains(.announce(.layoutChanged(layout)))) state = result.0 }}
@Test(arguments: Layout.allCases)func setLayoutBalancesWithoutChangingMembership(layout: Layout) { let result = command(.setLayout(layout), in: focused(2, in: populated(4))) #expect(result.0.workspaces[primaryKey]?.layout == layout) #expect(result.0.workspaces[primaryKey]?.order == (1...4).map(windowID)) #expect(result.0.focusedWindow == windowID(2)) #expect(result.1.contains(.announce(.layoutChanged(layout))))}
@Test func setRatioUpdatesSideFrames() { let result = command(.setRatio(.threeFifths), in: populated(2, layout: .side)) #expect(result.0.frames[windowID(1)] == Rect(width: 720, height: 800)) #expect(result.0.frames[windowID(2)] == Rect(x: 720, width: 480, height: 800)) #expect(result.1.contains(.announce(.ratioChanged(.threeFifths))))}
@Test(arguments: Layout.allCases)func setRatioResizesMainWindowWithoutChangingLayout(layout: Layout) throws { let state = focused(1, in: populated(3, layout: layout)) let before = try #require(state.frames[windowID(1)]) let result = command(.setRatio(.twoThirds), in: state) let after = try #require(result.0.frames[windowID(1)]) #expect(after.size.width * after.size.height > before.size.width * before.size.height) #expect(result.0.workspaces[primaryKey]?.layout == layout) #expect(result.0.workspaces[primaryKey]?.order == state.workspaces[primaryKey]?.order) #expect(result.0.workspaces[primaryKey]?.openingOrder == state.workspaces[primaryKey]?.openingOrder) #expect(result.0.workspaces[primaryKey]?.visibleWindows == state.workspaces[primaryKey]?.visibleWindows) #expect(result.0.focusedWindow == windowID(1)) #expect(result.1.contains(.announce(.ratioChanged(.twoThirds)))) #expect(command(.setRatio(.twoThirds), in: result.0).0 == result.0)}
@Test func nudgeRatioMovesDivider() { let result = command(.nudgeRatio(0.1), in: populated(2, layout: .side)) #expect(result.0.workspaces[primaryKey]?.ratio == .threeFifths) #expect(result.0.frames[windowID(1)]?.size.width == 720) #expect( frameWrites(result.1) == [ WindowFrame(window: windowID(1), frame: Rect(width: 720, height: 800)), WindowFrame(window: windowID(2), frame: Rect(x: 720, width: 480, height: 800)), ])}
@Test(arguments: Layout.allCases)func nudgeRatioClampsBothBounds(layout: Layout) { let state = populated(2, layout: layout) #expect(command(.nudgeRatio(10), in: state).0.workspaces[primaryKey]?.ratio.value == 0.8) #expect(command(.nudgeRatio(-10), in: state).0.workspaces[primaryKey]?.ratio.value == 0.2)}
@Test(arguments: Layout.allCases, [Double.nan, .infinity, -.infinity])func nonFiniteNudgeIsReported(layout: Layout, delta: Double) { let state = populated(2, layout: layout) let result = command(.nudgeRatio(delta), in: state) #expect(result.0 == state) #expect(result.1 == [.report(.invalidRatio)])}
@Test func toggleFloatRemovesTileButRetainsMembership() { let result = command(.toggleFloat, in: focused(2, in: populated())) #expect(result.0.windows[windowID(2)]?.isFloating == true) #expect(result.0.workspaces[primaryKey]?.order == (1...3).map(windowID)) #expect(result.0.workspaces[primaryKey]?.tree?.windows == [windowID(1), windowID(3)]) #expect(result.0.frames[windowID(2)] == Rect(x: 180, y: 120, width: 840, height: 560)) #expect(result.1.contains(.announce(.floatingChanged(windowID(2), isFloating: true))))}
@Test func toggleFloatBackSplitsNewestTile() { let floating = command(.toggleFloat, in: focused(2, in: populated())).0 let result = command(.toggleFloat, in: floating) #expect(result.0.windows[windowID(2)]?.isFloating == false) #expect(result.0.workspaces[primaryKey]?.tree?.tileCount == 3) #expect(result.0.frames[windowID(2)] == Rect(x: 800, width: 400, height: 800))}
@Test func retileBalancesIncrementalSplitsAndRequestsReadbackConfirmation() { var state = populated(0) for number in 1...3 { state = reduce(state, .windowAppeared(managed(number), workspace: primaryKey, frame: Rect(width: 100, height: 100))) .0 } let result = command(.retile, in: state) #expect(result.0.frames[windowID(1)] == Rect(width: 400, height: 800)) #expect( result.1.contains( .confirmRetile( (1...3).map { WindowFrame(window: windowID($0), frame: result.0.frames[windowID($0)]!) })))}
@Test func cachedRetileRequestsRevalidationWithoutPlanningWritesOrGuessingMovement() { let result = command(.retile, in: populated()) #expect( result.1 == [ .confirmRetile( (1...3).map { WindowFrame(window: windowID($0), frame: result.0.frames[windowID($0)]!) }) ])}
@Test func stackJoinsDirectionalNeighborAndMakesFocusVisible() { let result = command(.stack(.right), in: focused(1, in: populated())) #expect(result.0.workspaces[primaryKey]?.tree?.tileCount == 2) #expect(result.0.frames[windowID(1)] == result.0.frames[windowID(2)]) #expect(result.0.visibleWindows.contains(windowID(1))) #expect(!result.0.visibleWindows.contains(windowID(2))) #expect(result.1.contains(.announce(.stackChanged(window: windowID(1), count: 2))))}
@Test func unstackCreatesNewTileWhenCapacityAllows() { let stacked = command(.stack(.right), in: focused(1, in: populated())).0 let result = command(.unstack, in: stacked) #expect(result.0.workspaces[primaryKey]?.tree?.tileCount == 3) #expect(result.0.focusedWindow == windowID(1)) #expect(result.1.contains(.announce(.stackChanged(window: windowID(1), count: 1))))}
@Test func unstackAtCapReportsInsteadOfLosingWindow() { let state = focused(3, in: populated(3, cap: 2)) let result = command(.unstack, in: state) #expect(result.0 == state) #expect(result.1 == [.report(.atWindowCap(2))])}
@Test func unstackSingletonIsNoOp() { let state = focused(2, in: populated()) let result = command(.unstack, in: state) #expect(result.0 == state) #expect(result.1.isEmpty)}
@Test func focusNextInStackCyclesAndWraps() { var state = focused(2, in: populated(4, cap: 2)) for number in [3, 4, 2] { let result = command(.focusNextInStack, in: state) #expect(result.0.focusedWindow == windowID(number)) #expect(result.1.contains(.focusWindow(windowID(number)))) #expect(frameWrites(result.1).isEmpty) state = result.0 }}
@Test func focusPreviousInStackCyclesAndWraps() { var state = focused(2, in: populated(4, cap: 2)) for number in [4, 3, 2] { state = command(.focusPreviousInStack, in: state).0 #expect(state.focusedWindow == windowID(number)) }}
@Test func stackCycleSingletonIsNoOp() { let state = focused(2, in: populated()) #expect(command(.focusNextInStack, in: state).1.isEmpty) #expect(command(.focusPreviousInStack, in: state).1.isEmpty)}
@Test func lowerCapMergesTilesAndPreservesFocus() { let result = command(.setWindowCap(2), in: focused(2, in: populated(5))) #expect(result.0.workspaces[primaryKey]?.tree?.tileCount == 2) #expect(result.0.workspaces[primaryKey]?.order == (1...5).map(windowID)) #expect(result.0.visibleWindows == [windowID(1), windowID(2)])}
@Test func raiseCapRefillsAutomaticStacks() { let result = command(.setWindowCap(3), in: focused(3, in: populated(3, cap: 2))) #expect(result.0.workspaces[primaryKey]?.windowCap == 3) #expect(result.0.workspaces[primaryKey]?.tree?.tileCount == 3) #expect(result.0.focusedWindow == windowID(3)) #expect(result.0.workspaces[primaryKey]?.openingOrder == (1...3).map(windowID))}
@Test func commandsWithoutFocusReportProblem() { let state = populated() let result = command(.toggleFloat, in: state) #expect(result.0 == state) #expect(result.1 == [.report(.noFocusedWindow)])}
@Test func commandsWithoutWorkspaceReportProblem() { #expect(command(.cycleLayout, in: State()).1 == [.report(.noActiveWorkspace)])}