import Testing @testable import ParcelCore extension CommandReviewRegressionTests { @Test func metadataRefreshCannotPutAnExplicitFloatBackIntoItsOldTile() throws { var original = focused(2, in: populated()) original.observedFrames = original.frames let window = windowID(2) let oldTile = try #require(original.frames[window]) let floating = command(.toggleFloat, in: original).0 let target = try #require(floating.frames[window]) var observed = try #require(original.windows[window]) let sheet = ManagedWindow(id: windowID(4), app: observed.app, role: "AXSheet", isFloating: true) let sheetFrame = Rect(x: 270, y: 140, width: 450, height: 220) let opened = reduce(floating, .windowAppeared(sheet, workspace: primaryKey, frame: sheetFrame)).0 let editingSheet = reduce(opened, .focusChanged(sheet.id)).0 observed.title = "Next browser page" observed.facts = WindowFacts(bundleID: observed.app.bundleID, hasSheet: true) let refreshed = reduce(editingSheet, .windowAppeared(observed, workspace: primaryKey, frame: oldTile)).0 #expect(refreshed.windows[window]?.isFloating == true) #expect(refreshed.frames[window] == target) #expect(refreshed.observedFrames[window] == oldTile) #expect(refreshed.focusedWindow == sheet.id) #expect(refreshed.frames[sheet.id] == sheetFrame) #expect(refreshed.workspaces[primaryKey]?.focus == window) let closed = reduce(refreshed, [.windowDisappeared(sheet.id), .focusChanged(window)]).0 #expect(closed.frames == floating.frames) #expect(closed.workspaces == floating.workspaces) let moved = Rect(x: 40, y: 50, width: 500, height: 400) let edited = reduce(closed, .windowFrameEdited(window, frame: moved)).0 #expect(edited.frames[window] == moved) } @Test(arguments: [Command.toggleFloat, .tileWindow(windowID(2))]) func explicitFloatCentersAndRebalancesThreeColumnsThenTilesBack(action: Command) throws { var original = focused(2, in: populated()) original.observedFrames = original.frames let floated = command(.toggleFloat, in: original) let target = try #require(floated.0.frames[windowID(2)]) #expect(target == Rect(x: 180, y: 120, width: 840, height: 560)) #expect(floated.0.observedFrames == original.observedFrames) #expect(floated.0.workspaces[primaryKey]?.tree?.tileCount == 2) #expect(floated.0.frames[windowID(1)] == Rect(width: 600, height: 800)) #expect(floated.0.frames[windowID(3)] == Rect(x: 600, width: 600, height: 800)) #expect(frameWrites(floated.1).contains(WindowFrame(window: windowID(2), frame: target))) #expect(floated.1.last == .raiseWindow(windowID(2))) let tiled = command(action, in: floated.0) #expect(tiled.0.windows[windowID(2)]?.isFloating == false) #expect(tiled.0.workspaces[primaryKey]?.tree?.tileCount == 3) #expect(tiled.0.frames[windowID(2)] == Rect(x: 800, width: 400, height: 800)) #expect(tiled.0.frames.values.allSatisfy { $0.size.width == 400 && $0.size.height == 800 }) #expect(tiled.0.workspaces[primaryKey]?.openingOrder == original.workspaces[primaryKey]?.openingOrder) } @Test(arguments: [Layout.grid, .columns, .rows, .side]) func explicitFloatAndTileUseAutomaticLayoutStrategy(layout: Layout) { let state = focused(2, in: populated(3, layout: layout, cap: Int.max)) let floating = command(.toggleFloat, in: state).0 #expect(floating.workspaces[primaryKey]?.tree?.tileCount == 2) #expect(floating.cannotFit.isEmpty) let tiled = command(.toggleFloat, in: floating).0 #expect(tiled.workspaces[primaryKey]?.layout == layout) #expect(tiled.workspaces[primaryKey]?.tree?.tileCount == 3) #expect(tiled.windows[windowID(2)]?.isFloating == false) #expect(tiled.cannotFit.isEmpty) } @Test func retiledFloatStillHonorsCompactRowCapacity() { let state = focused(2, in: populated(3, layout: .rows)) #expect(state.workspaces[primaryKey]?.effectiveWindowCap == 2) let floating = command(.toggleFloat, in: state).0 let tiled = command(.toggleFloat, in: floating).0 #expect(tiled.windows[windowID(2)]?.isFloating == false) #expect(tiled.workspaces[primaryKey]?.tree?.tileCount == 2) #expect(Set(tiled.workspaces[primaryKey]?.tree?.windows ?? []) == Set(state.windows.keys)) #expect(tiled.cannotFit.isEmpty) } @Test func explicitFloatUsesInsetUsableAreaAndKnownMinimum() { var state = focused(2, in: populated()) let screen = Rect(x: 100, y: 50, width: 1200, height: 800) state.displays[primaryDisplay] = Display(id: primaryDisplay, frame: screen) state.workspaces[primaryKey]?.reservedEdges = Insets(top: 50, left: 100, bottom: 30, right: 60) state.workspaces[primaryKey]?.gaps = Gaps(inner: 12, outer: Insets(20)) state.windows[windowID(2)]?.minimum = LearnedMinimumSize(size: Size(width: 800, height: 550)) let floated = command(.toggleFloat, in: state).0 #expect(floated.frames[windowID(2)] == Rect(x: 320, y: 185, width: 800, height: 550)) #expect(floated.windows[windowID(2)]?.managementReason == .user) } @Test func floatAndTileAllocateWideMinimumsWithoutUnnecessaryStacking() throws { var original = focused(2, in: populated()) original.windows[windowID(1)]?.minimum = LearnedMinimumSize(size: Size(width: 700, height: 100)) original.windows[windowID(2)]?.minimum = LearnedMinimumSize(size: Size(width: 200, height: 100)) original.windows[windowID(3)]?.minimum = LearnedMinimumSize(size: Size(width: 200, height: 100)) original.workspaces[primaryKey]?.tree = .split( axis: .horizontal, fraction: 0.6, first: .tile(WindowStack(windowID(1))), second: .split( axis: .horizontal, fraction: 0.5, first: .tile(WindowStack(windowID(2))), second: .tile(WindowStack(windowID(3))))) let floated = command(.toggleFloat, in: original).0 let first = try #require(floated.frames[windowID(1)]) let last = try #require(floated.frames[windowID(3)]) #expect(floated.workspaces[primaryKey]?.tree?.tileCount == 2) #expect(first.size.width >= 700 && last.size.width >= 200) #expect(first.minX == testScreen.minX && first.maxX == last.minX && last.maxX == testScreen.maxX) #expect(floated.cannotFit.isEmpty) let tiled = command(.toggleFloat, in: floated).0 #expect(tiled.workspaces[primaryKey]?.tree?.tileCount == 3) #expect((tiled.frames[windowID(1)]?.size.width ?? 0) >= 700) #expect(tiled.windows[windowID(2)]?.isFloating == false) #expect(tiled.cannotFit.isEmpty) } @Test func oversizedFloatKeepsItsTitleReachableAndRefusedTileIsAtomic() { var state = focused(1, in: populated(1)) state.windows[windowID(1)]?.minimum = LearnedMinimumSize(size: Size(width: 1300, height: 900)) let floated = command(.toggleFloat, in: state).0 #expect(floated.frames[windowID(1)] == Rect(width: 1300, height: 900)) #expect(floated.windows[windowID(1)]?.isFloating == true) let refused = command(.toggleFloat, in: floated) #expect(refused.0 == floated) #expect(refused.1.contains(.report(.spaceFull(windowID(1))))) #expect(!refused.1.contains(.announce(.floatingChanged(windowID(1), isFloating: true)))) } @Test(arguments: [Command.toggleFloat, .tileWindow(windowID(2))]) func explicitTileAtFloatCapacityPreservesGeometryAndUserOwnership(action: Command) { let floated = command(.toggleFloat, in: focused(2, in: populated(2))).0 var full = command(.setWindowCap(1), in: floated).0 full.workspaces[primaryKey]?.whenFull = .float let rejected = command(action, in: full) #expect(rejected.0 == full) #expect(rejected.1.contains(.report(.spaceFull(windowID(2))))) } @Test func explicitFloatOfStackMemberRetainsOtherMembersAndTileHonorsCap() { let original = focused(2, in: populated(3, cap: 1)) let floated = command(.toggleFloat, in: original).0 #expect(floated.workspaces[primaryKey]?.tree?.tileCount == 1) #expect(Set(floated.workspaces[primaryKey]?.tree?.windows ?? []) == Set([windowID(1), windowID(3)])) #expect(floated.frames[windowID(1)] == testScreen && floated.frames[windowID(3)] == testScreen) let tiled = command(.toggleFloat, in: floated).0 #expect(tiled.workspaces[primaryKey]?.tree?.tileCount == 1) #expect(Set(tiled.workspaces[primaryKey]?.tree?.windows ?? []) == Set(original.windows.keys)) #expect(tiled.workspaces[primaryKey]?.tree?.stacks.first?.visible == windowID(2)) } @Test func floatAndTileEachUndoInOneOperation() { let original = focused(2, in: populated()) let floating = command(.toggleFloat, in: original).0 let undoFloat = command(.undo, in: floating).0 #expect(undoFloat.frames == original.frames) #expect(undoFloat.workspaces[primaryKey]?.tree == original.workspaces[primaryKey]?.tree) #expect(undoFloat.windows[windowID(2)]?.isFloating == false) let tiled = command(.toggleFloat, in: floating).0 let undoTile = command(.undo, in: tiled).0 #expect(undoTile.frames == floating.frames) #expect(undoTile.workspaces[primaryKey]?.tree == floating.workspaces[primaryKey]?.tree) #expect(undoTile.windows[windowID(2)]?.isFloating == true) } @Test func floatingEndsFocusAndManualFrameGesturesKeepTheirOwnGeometry() { let original = focused(2, in: populated()) let expanded = command(.toggleFocus, in: original).0 let floated = command(.toggleFloat, in: expanded).0 #expect(floated.activeFocusModes.focusedWindow == nil) #expect(floated.frames[windowID(2)] == Rect(x: 180, y: 120, width: 840, height: 560)) let manual = Rect(x: 80, y: 90, width: 500, height: 350) let dragged = reduce( original, .gestureCommitted(.floatingFrame(window: windowID(2), workspace: primaryKey, frame: manual))) #expect(dragged.0.frames[windowID(2)] == manual) #expect(dragged.0.windows[windowID(2)]?.isFloating == true) } @Test(arguments: [Layout.tree, .bsp, .accordion]) func floatingDoesNotRebalanceManualTreeStrategies(layout: Layout) { var state = focused(3, in: populated(3, layout: layout)) state.workspaces[primaryKey]?.tree = .split( axis: .horizontal, fraction: 0.63, first: .tile(WindowStack(windowID(1))), second: .split( axis: .vertical, fraction: 0.4, first: .tile(WindowStack(windowID(2))), second: .tile(WindowStack(windowID(3))))) let expected = state.workspaces[primaryKey]?.tree?.removing(windowID(3)) let floated = command(.toggleFloat, in: state).0 #expect(floated.workspaces[primaryKey]?.tree == expected) } }