SPB Git

Repository Public

LayoutNodeTests.swift 5.5 KB 119 lines swift

1import Testing2import Foundation3import CoreGraphics4@testable import WorkspaceCore56@Suite struct LayoutNodeTests {7    @Test func splitLeafCreatesTwoChildren() {8        let a = PaneID(), b = PaneID()9        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b)10        #expect(tree.paneIDs == [a, b])11        #expect(tree.isWellFormed)12        #expect(tree.depth == 1)13    }1415    @Test func repeatedSplitSameAxisStaysFlat() {16        let a = PaneID(), b = PaneID(), c = PaneID()17        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .horizontal, newPane: c)18        #expect(tree.depth == 1)19        #expect(tree.paneIDs == [a, b, c])20        if case .split(_, _, _, let f) = tree { #expect(f == [0.5, 0.25, 0.25]) } else { Issue.record("expected split") }21    }2223    @Test func perpendicularSplitNests() {24        let a = PaneID(), b = PaneID(), c = PaneID()25        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .vertical, newPane: c)26        #expect(tree.depth == 2)27        #expect(tree.isWellFormed)28    }2930    @Test func removingCollapsesSingleChildSplit() {31        let a = PaneID(), b = PaneID(), c = PaneID()32        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .vertical, newPane: c)33        let without = tree.removing(c)34        #expect(without?.paneIDs == [a, b])35        #expect(without?.depth == 1)36        #expect(tree.removing(a)?.removing(b)?.removing(c) == nil)37        #expect(LayoutNode.leaf(a).removing(a) == nil)38    }3940    @Test func removingRenormalisesFractions() {41        let a = PaneID(), b = PaneID(), c = PaneID()42        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .horizontal, newPane: c)43        guard case .split(_, _, _, let f) = tree.removing(a)! else { Issue.record("expected split"); return }44        #expect(abs(f.reduce(0, +) - 1) < 0.0001)45        #expect(f == [0.5, 0.5])46    }4748    @Test func swapKeepsShape() {49        let a = PaneID(), b = PaneID(), c = PaneID()50        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .vertical, newPane: c)51        let swapped = tree.swapping(a, c)52        #expect(swapped.paneIDs == [c, b, a])53        #expect(swapped.depth == tree.depth)54    }5556    @Test func moveNextTo() {57        let a = PaneID(), b = PaneID(), c = PaneID()58        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .horizontal, newPane: c)59        let moved = tree.moving(c, nextTo: a, axis: .horizontal, after: false)60        #expect(moved.paneIDs == [c, a, b])61        #expect(moved.isWellFormed)62    }6364    @Test func resizeClampsToMinimum() {65        let a = PaneID(), b = PaneID()66        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b)67        guard case .split(let sid, _, _, _) = tree else { Issue.record("split"); return }68        let resized = tree.resizing(split: sid, dividerIndex: 0, delta: 0.9)69        guard case .split(_, _, _, let f) = resized else { Issue.record("split"); return }70        #expect(f[0] == 1 - LayoutNode.minimumFraction)71        #expect(abs(f.reduce(0, +) - 1) < 0.0001)72    }7374    @Test func framesAndNeighbors() {75        let a = PaneID(), b = PaneID(), c = PaneID()76        // a | (b / c)77        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .vertical, newPane: c)78        let frames = tree.frames(in: CGRect(x: 0, y: 0, width: 200, height: 100))79        #expect(frames[a] == CGRect(x: 0, y: 0, width: 100, height: 100))80        #expect(frames[b] == CGRect(x: 100, y: 0, width: 100, height: 50))81        #expect(frames[c] == CGRect(x: 100, y: 50, width: 100, height: 50))82        #expect(tree.neighbor(of: a, direction: .right) == b)83        #expect(tree.neighbor(of: b, direction: .down) == c)84        #expect(tree.neighbor(of: c, direction: .left) == a)85        #expect(tree.neighbor(of: a, direction: .left) == nil)86        #expect(tree.nextPane(after: c) == a)87        #expect(tree.nextPane(after: a, reverse: true) == c)88    }8990    @Test func dividersAccountForThickness() {91        let a = PaneID(), b = PaneID()92        let tree = LayoutNode.leaf(a).splitting(a, axis: .vertical, newPane: b)93        let dividers = tree.dividers(in: CGRect(x: 0, y: 0, width: 100, height: 104), dividerThickness: 4)94        #expect(dividers.count == 1)95        #expect(dividers[0].frame == CGRect(x: 0, y: 50, width: 100, height: 4))96        #expect(dividers[0].splitExtent == 100)97    }9899    @Test func codableRoundTrip() throws {100        let a = PaneID(), b = PaneID(), c = PaneID()101        let tree = LayoutNode.leaf(a).splitting(a, axis: .horizontal, newPane: b).splitting(b, axis: .vertical, newPane: c)102        let data = try JSONEncoder().encode(tree)103        let decoded = try JSONDecoder().decode(LayoutNode.self, from: data)104        #expect(decoded == tree)105    }106107    @Test func workspaceConsistency() {108        var ws = WorkspaceState.single()109        #expect(ws.isConsistent)110        let pane = PaneState()111        ws.windows[0].tabs[0].root = ws.windows[0].tabs[0].root.splitting(ws.windows[0].tabs[0].root.firstPane!, axis: .horizontal, newPane: pane.id)112        #expect(!ws.isConsistent) // pane record missing113        ws.panes[pane.id] = pane114        #expect(ws.isConsistent)115        #expect(ws.location(ofPane: pane.id)?.window == ws.windows[0].id)116        #expect(ws.referencedSessions.count == 2)117    }118}119
Files main
Menu

Theme