Repository Public License
1import CoreGraphics2import Foundation3import NavigatorCore4import Testing5@testable import OrbitEngine67private let window = WindowID(), spaceID = SpaceID()8private let epoch = Date(timeIntervalSince1970: 10_000)910private func tab(_ order: Int, pinned: Bool = false, parent: TabID? = nil, group: TabGroupID? = nil,11 active: TimeInterval = 0, closed: Bool = false, title: String = "") -> Tab {12 Tab(windowID: window, spaceID: spaceID, parentID: parent, groupID: group, title: title, isPinned: pinned, order: order,13 createdAt: epoch, lastActiveAt: epoch.addingTimeInterval(active), closedAt: closed ? epoch : nil)14}1516@Suite struct TabLayoutTests {17 @Test func pinnedTabsComeFirstThenOrder() {18 let a = tab(2), b = tab(0), p = tab(5, pinned: true), q = tab(1, pinned: true), gone = tab(0, closed: true)19 #expect(TabLayout.ordered([a, b, p, q, gone]).map(\.id) == [q.id, p.id, b.id, a.id])20 }2122 @Test func equalOrdersFallBackToIdSoTheListNeverFlickers() {23 let a = tab(0), b = tab(0)24 let expected = [min(a.id, b.id), max(a.id, b.id)]25 #expect(TabLayout.ordered([a, b]).map(\.id) == expected)26 #expect(TabLayout.ordered([b, a]).map(\.id) == expected)27 }2829 @Test func treeIndentsChildrenUnderTheirParent() {30 let root = tab(0), child = tab(1, parent: root.id), grandchild = tab(2, parent: child.id)31 let other = tab(3), late = tab(4, parent: root.id)32 let rows = TabLayout.tree([late, other, grandchild, child, root])33 #expect(rows.map(\.tab.id) == [root.id, child.id, grandchild.id, late.id, other.id])34 #expect(rows.map(\.depth) == [0, 1, 2, 1, 0])35 #expect(rows[0].childCount == 2)36 }3738 @Test func orphansAndCyclesBecomeRoots() {39 let orphan = tab(0, parent: TabID())40 var a = tab(1), b = tab(2)41 a.parentID = b.id; b.parentID = a.id42 var selfParent = tab(3); selfParent.parentID = selfParent.id43 let rows = TabLayout.tree([orphan, a, b, selfParent])44 #expect(Set(rows.map(\.tab.id)) == [orphan.id, a.id, b.id, selfParent.id])45 #expect(rows.count == 4)46 #expect(rows.first?.depth == 0)47 }4849 @Test func depthIsCapped() {50 var tabs = [tab(0)]51 for index in 1...12 { tabs.append(tab(index, parent: tabs[index - 1].id)) }52 #expect(TabLayout.tree(tabs).map(\.depth).max() == TabLayout.maximumDepth)53 }5455 @Test func collapsedSubtreesAreHidden() {56 let root = tab(0), child = tab(1, parent: root.id), grandchild = tab(2, parent: child.id), next = tab(3)57 let rows = TabLayout.tree([root, child, grandchild, next], collapsed: [root.id])58 #expect(rows.map(\.tab.id) == [root.id, next.id])59 let partial = TabLayout.tree([root, child, grandchild, next], collapsed: [child.id])60 #expect(partial.map(\.tab.id) == [root.id, child.id, next.id])61 }6263 @Test func sectionsGroupAndCollapse() {64 let work = TabGroup(spaceID: spaceID, name: "Work", order: 1), play = TabGroup(spaceID: spaceID, name: "Play", isCollapsed: true, order: 0)65 let empty = TabGroup(spaceID: spaceID, name: "Empty", order: 2)66 let pinned = tab(0, pinned: true), w1 = tab(1, group: work.id), w2 = tab(2, parent: w1.id, group: work.id)67 let p1 = tab(3, group: play.id), p2 = tab(4, group: play.id), loose = tab(5), stale = tab(6, group: TabGroupID())68 let sections = TabLayout.sections(tabs: [loose, p2, p1, w2, w1, pinned, stale], groups: [work, play, empty], selected: p2.id)69 #expect(sections.map(\.id) == ["pinned", "group-\(play.id.string)", "group-\(work.id.string)", "group-\(empty.id.string)", "ungrouped"])70 #expect(sections[1].rows.map(\.tab.id) == [p2.id]) // the selected tab of a collapsed group stays visible71 #expect(sections[1].hiddenCount == 1)72 #expect(sections[2].rows.map(\.depth) == [0, 1])73 #expect(sections[3].rows.isEmpty)74 #expect(sections[4].rows.map(\.tab.id) == [loose.id, stale.id]) // a tab whose group vanished is ungrouped75 #expect(TabLayout.flatOrder(tabs: [loose, p2, p1, w2, w1, pinned, stale], groups: [work, play, empty], selected: nil)76 == [pinned.id, w1.id, w2.id, loose.id, stale.id])77 }7879 @Test func pinnedTabsIgnoreGroupsAndTree() {80 let group = TabGroup(spaceID: spaceID, name: "G")81 let parent = tab(0), pinnedChild = tab(1, pinned: true, parent: parent.id, group: group.id)82 let sections = TabLayout.sections(tabs: [parent, pinnedChild], groups: [group])83 #expect(sections[0].kind == .pinned && sections[0].rows[0].depth == 0)84 #expect(sections[1].rows.isEmpty)85 }8687 // MARK: Horizontal overflow8889 private let metrics = TabStripMetrics(pinnedWidth: 40, minimumTabWidth: 100, maximumTabWidth: 200, spacing: 0, overflowButtonWidth: 30)9091 @Test func allTabsFitAndShareTheWidth() {92 let ids = (0..<4).map { _ in TabID() }93 let wide = TabLayout.strip(order: ids, pinned: [], selected: nil, width: 1_000, metrics: metrics)94 #expect(wide.visible == ids && !wide.hasOverflow && wide.tabWidth == 200)95 let tight = TabLayout.strip(order: ids, pinned: [ids[0]], selected: nil, width: 400, metrics: metrics)96 #expect(tight.visible == ids && tight.tabWidth == 120)97 let exact = TabLayout.strip(order: ids, pinned: [], selected: nil, width: 400, metrics: metrics)98 #expect(exact.visible.count == 4 && exact.tabWidth == 100)99 }100101 @Test func overflowKeepsOrderAndTheSelectedTabVisible() {102 let ids = (0..<8).map { _ in TabID() }103 let layout = TabLayout.strip(order: ids, pinned: [ids[0]], selected: ids[6], width: 400, metrics: metrics)104 // 400 − 30 (button) = 370: 40 pinned + 3 × 100.105 #expect(layout.visible == [ids[0], ids[1], ids[2], ids[6]])106 #expect(layout.overflow == [ids[3], ids[4], ids[5], ids[7]])107 #expect(layout.tabWidth == 110)108 #expect(Set(layout.visible).union(layout.overflow) == Set(ids))109 let unselected = TabLayout.strip(order: ids, pinned: [ids[0]], selected: nil, width: 400, metrics: metrics)110 #expect(unselected.visible == Array(ids.prefix(4)))111 }112113 @Test func spacingCountsAgainstTheWidth() {114 let spaced = TabStripMetrics(pinnedWidth: 40, minimumTabWidth: 100, maximumTabWidth: 200, spacing: 10, overflowButtonWidth: 30)115 let ids = (0..<5).map { _ in TabID() }116 let layout = TabLayout.strip(order: ids, pinned: [], selected: nil, width: 400, metrics: spaced)117 // 400 − 30 − 10 = 360: three tabs need 320, four need 430.118 #expect(layout.visible.count == 3 && layout.overflow.count == 2)119 }120121 @Test func tooManyPinnedTabsOverflowToo() {122 let ids = (0..<12).map { _ in TabID() }123 let layout = TabLayout.strip(order: ids, pinned: Set(ids.prefix(10)), selected: ids[9], width: 200, metrics: metrics)124 #expect(layout.visible.count == 4)125 #expect(layout.visible.last == ids[9])126 #expect(layout.overflow.count == 8)127 #expect(TabLayout.strip(order: ids, pinned: [], selected: nil, width: 0, metrics: metrics).visible.isEmpty)128 }129130 @Test func gridFramesKeepTheAspectRatio() {131 let frames = TabLayout.gridFrames(count: 7, in: CGSize(width: 1_000, height: 600), minimumCellWidth: 300, aspectRatio: 2, spacing: 20)132 #expect(frames.count == 7)133 #expect(frames[0].width == 320 && frames[0].height == 160)134 #expect(frames[3].minY == 180 && frames[6].minY == 360)135 let single = TabLayout.gridFrames(count: 1, in: CGSize(width: 1_000, height: 600))136 #expect(single[0].width == 1_000) // never more columns than tabs137 }138139 @Test func orbitSlotsPutPinnedTabsOnRingZero() {140 let ids = (0..<30).map { _ in TabID() }141 let pinned = Set(ids.prefix(3))142 let slots = TabLayout.orbitSlots(order: ids, pinned: pinned)143 #expect(pinned.allSatisfy { slots[$0]?.ring == 0 })144 #expect(ids.dropFirst(3).allSatisfy { (slots[$0]?.ring ?? 0) >= 1 })145 #expect(Set(slots.values).count == 30)146 let many = (0..<500).map { _ in TabID() }147 #expect(TabLayout.orbitSlots(order: many, pinned: []).count == OrbitalLayout.totalCapacity())148 }149150 @Test(arguments: TabLayoutMode.allCases)151 func everyModeHasAPresentation(mode: TabLayoutMode) {152 let old = tab(0, active: 5), recent = tab(1, active: 50), pinned = tab(0, pinned: true, active: 1)153 let presentation = TabLayout.presentation(mode: mode, tabs: [old, recent, pinned], groups: [], selected: nil,154 size: CGSize(width: 900, height: 600))155 switch presentation {156 case .vertical(let sections):157 #expect(mode == .vertical && sections.count == 2)158 case .horizontal(let strip, let sections):159 #expect(mode == .horizontal && strip.visible == [pinned.id, old.id, recent.id] && sections.count == 2)160 case .compact(let current, let switcher, let count):161 #expect(mode == .compact && current == recent.id && switcher == [recent.id, old.id, pinned.id] && count == 3)162 case .grid(let order, let frames):163 #expect(mode == .grid && order == [pinned.id, old.id, recent.id] && frames.count == 3)164 case .orbit(let slots, let order):165 #expect(mode == .orbit && slots.count == 3 && order.first == pinned.id)166 }167 }168169 @Test func compactModeKeepsAValidSelection() {170 let a = tab(0, active: 1), b = tab(1, active: 2)171 guard case .compact(let current, _, _) = TabLayout.presentation(mode: .compact, tabs: [a, b], groups: [], selected: a.id, size: .zero)172 else { Issue.record("wrong presentation"); return }173 #expect(current == a.id)174 guard case .compact(let fallback, _, _) = TabLayout.presentation(mode: .compact, tabs: [a, b], groups: [], selected: TabID(), size: .zero)175 else { Issue.record("wrong presentation"); return }176 #expect(fallback == b.id)177 }178179 // MARK: Tab operations180181 @Test func duplicateIsAFreshUnpinnedCopy() {182 var original = tab(3, pinned: true, group: TabGroupID(), title: "Doc")183 original.url = URL(string: "https://example.com/a")184 original.sessionState = Data([1, 2])185 let copy = TabOperations.duplicate(original, now: epoch.addingTimeInterval(9))186 #expect(copy.id != original.id && copy.url == original.url && copy.sessionState == original.sessionState)187 #expect(!copy.isPinned && copy.groupID == nil && copy.lifecycle == .dormant && copy.createdAt == epoch.addingTimeInterval(9))188 }189190 @Test func newTabsOpenAfterTheParentsFamily() {191 let parent = tab(0), child = tab(1, parent: parent.id), grandchild = tab(2, parent: child.id), other = tab(3)192 let ordered = [parent, child, grandchild, other]193 #expect(TabOperations.insertionIndex(for: .after(parent.id), in: ordered) == 3)194 #expect(TabOperations.insertionIndex(for: .after(other.id), in: ordered) == 4)195 #expect(TabOperations.insertionIndex(for: .after(TabID()), in: ordered) == 4)196 #expect(TabOperations.insertionIndex(for: .end, in: ordered) == 4)197 }198199 @Test func selectionAfterClosing() {200 let ids = (0..<3).map { _ in TabID() }201 #expect(TabOperations.selectionAfterClosing(ids[1], selected: ids[1], order: ids) == ids[2])202 #expect(TabOperations.selectionAfterClosing(ids[2], selected: ids[2], order: ids) == ids[1])203 #expect(TabOperations.selectionAfterClosing(ids[1], selected: ids[0], order: ids) == ids[0])204 #expect(TabOperations.selectionAfterClosing(ids[0], selected: ids[0], order: [ids[0]]) == nil)205 #expect(TabOperations.inserting(ids[2], into: ids, at: 0) == [ids[2], ids[0], ids[1]])206 #expect(TabOperations.inserting(ids[0], into: ids, at: 99) == [ids[1], ids[2], ids[0]])207 }208209 @Test func namesAreCleaned() {210 #expect(TabOperations.cleanedName(" Work\n\tStuff ") == "Work Stuff")211 #expect(TabOperations.cleanedName(" \n\u{0} ") == nil)212 #expect(TabOperations.cleanedName(String(repeating: "x", count: 200))?.count == 60)213 }214}215