import Foundation import 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))) }