Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266import Foundationimport Testing
@testable import ParcelCore
private func snapState(floating: Bool = false) -> State { let display = Display(id: primaryDisplay, frame: Rect(width: 1200, height: 800)) let window = managed(1, floating: floating) var state = State(displays: [display], activeSpaces: [primaryDisplay: primarySpace]) state = reduce( state, .windowAppeared(window, workspace: primaryKey, frame: Rect(x: 100, y: 100, width: 300, height: 200)) ).0 return state}
@Test func snapFloatsAndCyclesThenRestores() throws { var state = snapState() let original = try #require(state.frames[windowID(1)]) for width in [600.0, 800, 400, 600] { let (next, effects) = reduce(state, .commandIssued(.snap(.leftHalf))) #expect(next.windows[windowID(1)]?.isFloating == true) #expect(next.workspaces[primaryKey]?.tree == nil) #expect(next.frames[windowID(1)] == Rect(width: width, height: 800)) #expect( effects.contains(.setFrames([WindowFrame(window: windowID(1), frame: Rect(width: width, height: 800))]))) state = next } let (restored, effects) = reduce(state, .commandIssued(.snap(.restore))) #expect(restored.frames[windowID(1)] == original) #expect(restored.windows[windowID(1)]?.isFloating == true) #expect(effects.contains(.setFrames([WindowFrame(window: windowID(1), frame: original)]))) #expect(reduce(restored, .commandIssued(.snap(.restore))).1.contains(.report(.noSnapHistory))) try roundTrip(state)}
@Test func snapCycleResetsOnDifferentAction() { var state = snapState(floating: true) state = reduce(state, .commandIssued(.snap(.leftHalf))).0 state = reduce(state, .commandIssued(.snap(.rightHalf))).0 state = reduce(state, .commandIssued(.snap(.leftHalf))).0 #expect(state.frames[windowID(1)] == Rect(width: 600, height: 800))}
@Test(arguments: [false, true])func snapFrameObservationsPreserveCycleAndOriginalRestore(resized: Bool) throws { var state = snapState(floating: true) let original = try #require(state.frames[windowID(1)]) state = reduce(state, .commandIssued(.snap(.leftHalf))).0 let history = state.snapHistory let observed = Rect(x: 30, y: 40, width: 595, height: 800) let event: Event = resized ? .windowResizedByUser(windowID(1), frame: observed) : .windowMovedByUser(windowID(1), frame: observed) let (moved, effects) = reduce(state, event) #expect(moved.frames[windowID(1)] == observed) #expect(moved.snapHistory == history) #expect(!effects.contains(where: { if case .setFrames = $0 { true } else { false } })) state = reduce(moved, .commandIssued(.snap(.leftHalf))).0 #expect(state.frames[windowID(1)] == Rect(width: 800, height: 800)) state = reduce(state, .commandIssued(.snap(.restore))).0 #expect(state.frames[windowID(1)] == original)}
@Test func snapMoveAndRestoreAcrossDisplays() { let second = DisplayID(rawValue: "second") let secondSpace = SpaceID(rawValue: 2) var state = snapState(floating: true) state = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: Rect(width: 1200, height: 800)), Display(id: second, frame: Rect(x: 1200, y: 100, width: 900, height: 600)), ]) ).0 state = reduce(state, .activeSpaceChanged(display: second, space: secondSpace)).0 let original = state.frames[windowID(1)] state = reduce(state, .commandIssued(.snap(.nextDisplay))).0 #expect(state.workspace(containing: windowID(1))?.display == second) #expect(state.frames[windowID(1)] == Rect(x: 1267, y: 167, width: 300, height: 200)) state = reduce(state, .commandIssued(.snap(.restore))).0 #expect(state.workspace(containing: windowID(1)) == primaryKey) #expect(state.frames[windowID(1)] == original)}
@Test func snapBatchCyclesAndRestores() throws { let initial = snapState() let original = try #require(initial.frames[windowID(1)]) let (cycled, _) = reduce( initial, [ .commandIssued(.snap(.leftHalf)), .commandIssued(.snap(.leftHalf)), ]) #expect(cycled.frames[windowID(1)] == Rect(width: 800, height: 800)) let (restored, _) = reduce(cycled, [.commandIssued(.snap(.restore))]) #expect(restored.frames[windowID(1)] == original)}
@Test func snapResetsInvalidCycleHistory() throws { var state = snapState(floating: true) state = reduce(state, .commandIssued(.snap(.leftHalf))).0 state.snapHistory?[windowID(1)]?.cycle = Int.max let next = reduce(state, .commandIssued(.snap(.leftHalf))).0 #expect(next.frames[windowID(1)] == Rect(width: 600, height: 800))}
@Test func snapHistoryDecodesOlderState() throws { let encoded = try JSONEncoder().encode(snapState()) #expect(!String(decoding: encoded, as: UTF8.self).contains("snapHistory")) let decoded = try JSONDecoder().decode(State.self, from: encoded) #expect(decoded.snapHistory == nil)}
@Test func snapHistoryWithoutRecordedDisplayDecodesAndRestores() throws { var state = snapState(floating: true) let original = try #require(state.frames[windowID(1)]) state = reduce(state, .commandIssued(.snap(.leftHalf))).0 state.snapHistory?[windowID(1)]?.previousDisplayFrame = nil state.undoHistory = nil let encoded = try JSONEncoder().encode(state) #expect(!String(decoding: encoded, as: UTF8.self).contains("previousDisplayFrame")) let decoded = try JSONDecoder().decode(State.self, from: encoded) #expect(decoded.snapHistory?[windowID(1)]?.previousDisplayFrame == nil) #expect(reduce(decoded, .commandIssued(.snap(.restore))).0.frames[windowID(1)] == original)}
@Test func snapHistoryRecordsDisplayFrameAndRoundTrips() throws { let state = reduce(snapState(floating: true), .commandIssued(.snap(.leftHalf))).0 #expect(state.snapHistory?[windowID(1)]?.previousDisplayFrame == Rect(width: 1200, height: 800)) try roundTrip(state)}
@Test func snapUnavailableDisplayDoesNotFloat() { let state = snapState() let (next, effects) = reduce(state, .commandIssued(.snap(.previousDisplay))) #expect(next.windows[windowID(1)]?.isFloating == false) #expect(effects.contains(.report(.noOtherDisplay)))}
@Test(arguments: [false, true])func snapRestoreAfterUserMoveOrResize(resized: Bool) throws { var state = snapState(floating: true) let original = try #require(state.frames[windowID(1)]) state = reduce(state, .commandIssued(.snap(.rightHalf))).0 let moved = Rect(x: 450, y: 50, width: 500, height: 300) state = reduce( state, resized ? .windowResizedByUser(windowID(1), frame: moved) : .windowMovedByUser(windowID(1), frame: moved) ).0 let (restored, effects) = reduce(state, .commandIssued(.snap(.restore))) #expect(restored.frames[windowID(1)] == original) #expect(restored.snapHistory?[windowID(1)] == nil) #expect(!effects.contains(.report(.noSnapHistory)))}
@Test(arguments: [ (Rect(x: -1600, y: -900, width: 1200, height: 800), Rect(x: -1500, y: -800, width: 300, height: 200)), (Rect(x: -1000, y: -700, width: 250, height: 180), Rect(x: -1000, y: -700, width: 250, height: 180)),])func snapRestoreUsesRecordedDisplayOffsetAndCurrentVisibleFrame(displayFrame: Rect, expected: Rect) { var state = snapState(floating: true) state = reduce(state, .commandIssued(.snap(.leftHalf))).0 state = reduce(state, .displaySetChanged([Display(id: primaryDisplay, frame: displayFrame)])).0 let (restored, effects) = reduce(state, .commandIssued(.snap(.restore))) #expect(restored.workspace(containing: windowID(1)) == primaryKey) #expect(restored.frames[windowID(1)] == expected) #expect(effects.contains(.setFrames([WindowFrame(window: windowID(1), frame: expected)])))}
@Test func snapRestoreFallsBackToCurrentDisplayWhenOriginalIsRemoved() { let second = DisplayID(rawValue: "second") let secondSpace = SpaceID(rawValue: 2) var state = snapState(floating: true) state = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: Rect(width: 1200, height: 800)), Display(id: second, frame: Rect(x: 1200, y: 100, width: 900, height: 600)), ]) ).0 state = reduce(state, .activeSpaceChanged(display: second, space: secondSpace)).0 state = reduce(state, .commandIssued(.snap(.nextDisplay))).0 state = reduce( state, .displaySetChanged([Display(id: second, frame: Rect(x: -900, y: -500, width: 450, height: 300))]) ).0 let (restored, effects) = reduce(state, .commandIssued(.snap(.restore))) #expect(restored.workspace(containing: windowID(1)) == WorkspaceKey(display: second, space: secondSpace)) #expect(restored.frames[windowID(1)] == Rect(x: -800, y: -400, width: 300, height: 200)) #expect(restored.snapHistory?[windowID(1)] == nil) #expect(!effects.contains(.report(.unavailableDisplay(primaryDisplay))))}
@Test func snapRestoreUsesOriginalWorkspaceConfigurationAfterDisplayMove() { let second = DisplayID(rawValue: "second") let secondSpace = SpaceID(rawValue: 2) var state = snapState(floating: true) state = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: Rect(width: 1200, height: 800)), Display(id: second, frame: Rect(x: 1200, y: 100, width: 900, height: 600)), ]) ).0 state = reduce(state, .activeSpaceChanged(display: second, space: secondSpace)).0 state = reduce(state, .commandIssued(.snap(.nextDisplay))).0 state.workspaces[primaryKey]?.gaps = Gaps(outer: Insets(10)) state.workspaces[primaryKey]?.reservedEdges = Insets(top: 200, left: 250, bottom: 100, right: 150) let restored = reduce(state, .commandIssued(.snap(.restore))).0 #expect(restored.workspace(containing: windowID(1)) == primaryKey) #expect(restored.frames[windowID(1)] == Rect(x: 260, y: 210, width: 300, height: 200))}
@Test func snapInvalidRestoreDoesNotConsumeHistory() { var state = reduce(snapState(floating: true), .commandIssued(.snap(.leftHalf))).0 state.snapHistory?[windowID(1)]?.previousFrame.origin.xCoordinate = .infinity let (next, effects) = reduce(state, .commandIssued(.snap(.restore))) #expect(next == state) #expect(effects.contains(.report(.invalidGeometry)))}
@Test func snapMoveUsesConfiguredDestinationInsets() { let second = DisplayID(rawValue: "second") let secondSpace = SpaceID(rawValue: 2) let secondKey = WorkspaceKey(display: second, space: secondSpace) var state = snapState(floating: true) state = reduce( state, .displaySetChanged([ Display(id: primaryDisplay, frame: Rect(width: 1200, height: 800)), Display(id: second, frame: Rect(x: 1200, y: 100, width: 900, height: 600)), ]) ).0 state = reduce(state, .activeSpaceChanged(display: second, space: secondSpace)).0 state.workspaces[secondKey]?.gaps = Gaps(inner: 20, outer: Insets(10)) state.workspaces[secondKey]?.reservedEdges = Insets(top: 30, left: 40, bottom: 50, right: 60) state = reduce(state, .commandIssued(.snap(.nextDisplay))).0 #expect(state.frames[windowID(1)] == Rect(x: 1303, y: 190, width: 300, height: 200)) #expect(state.workspace(containing: windowID(1)) == secondKey) #expect(state.workspaces[secondKey]?.gaps == Gaps(inner: 20, outer: Insets(10)))}
@Test func snapMissingDestinationWorkspaceDoesNotUseDefaults() { let second = DisplayID(rawValue: "second") let secondSpace = SpaceID(rawValue: 2) let secondKey = WorkspaceKey(display: second, space: secondSpace) var state = snapState() state.displays[second] = Display(id: second, frame: Rect(x: 1200, width: 900, height: 600)) state.activeSpaces[second] = secondSpace let original = state.frames[windowID(1)] let (next, effects) = reduce(state, .commandIssued(.snap(.nextDisplay))) #expect(next.frames[windowID(1)] == original) #expect(next.windows[windowID(1)]?.isFloating == false) #expect(next.snapHistory?[windowID(1)] == nil) #expect(next.workspaces[secondKey] == nil) #expect(effects.contains(.report(.invalidGeometry)))}