Repository Public License
1import CoreGraphics2import Foundation3import NavigatorCore4import Persistence5import Testing6@testable import OrbitEngine78@Suite struct OrbitServiceTests {9 private func fixture() async throws -> (NavigatorDatabase, OrbitService, OrbitBootstrap, TestClock) {10 let database = try await NavigatorDatabase.inMemory()11 let clock = TestClock()12 let service = OrbitService(database: database, now: { clock.now })13 return (database, service, try await service.bootstrap(), clock)14 }1516 // MARK: Profiles and spaces1718 @Test func bootstrapCreatesADefaultProfileAndSpaceOnce() async throws {19 let (database, service, first, _) = try await fixture()20 #expect(first.createdProfile && first.createdSpace)21 #expect(first.profile.name == "Personal" && first.space.name == "Home" && first.profile.kind == .standard)22 #expect(first.space.ring == 0 && abs(first.space.angle + .pi / 2) < 1e-12)23 let second = try await service.bootstrap(profileName: "Other", spaceName: "Other")24 #expect(!second.createdProfile && !second.createdSpace && second.profile == first.profile && second.space.id == first.space.id)25 #expect(try await database.profiles.all().count == 1)26 // A profile left without any space gets one back.27 try await database.spaces.delete(first.space.id)28 let repaired = try await service.bootstrap(spaceName: "Again")29 #expect(!repaired.createdProfile && repaired.createdSpace && repaired.space.name == "Again")30 }3132 @Test func newSpacesNeverOverlapOnTheOrbit() async throws {33 let (_, service, boot, _) = try await fixture()34 for index in 0..<25 { try await service.createSpace(profile: boot.profile.id, name: "Space \(index)") }35 let spaces = try await service.spaces(profile: boot.profile.id)36 #expect(spaces.count == 26)37 #expect(OrbitalLayout.overlaps(in: spaces).isEmpty)38 #expect(spaces.map(\.order) == Array(0..<26))39 #expect(spaces.map(\.ring).max()! >= 1)40 }4142 @Test func theOrbitCanFillUp() async throws {43 let database = try await NavigatorDatabase.inMemory()44 let service = OrbitService(database: database, metrics: OrbitalMetrics(firstRingRadius: 60, maximumRing: 0))45 let boot = try await service.bootstrap()46 var created = 147 await #expect(throws: OrbitError.orbitFull) {48 while created < 100 { try await service.createSpace(profile: boot.profile.id, name: "S"); created += 1 }49 }50 // Halving gaps stops before the nominal capacity; it never exceeds it and never overlaps.51 #expect(created >= 2 && created <= OrbitalMetrics(firstRingRadius: 60, maximumRing: 0).capacity(ofRing: 0))52 #expect(OrbitalLayout.overlaps(in: try await service.spaces(), metrics: OrbitalMetrics(firstRingRadius: 60, maximumRing: 0)).isEmpty)53 }5455 @Test func spacesCanBeRenamedRecolouredAndMoved() async throws {56 let (_, service, boot, clock) = try await fixture()57 clock.advance(60)58 let renamed = try await service.renameSpace(boot.space.id, to: " Research\n")59 #expect(renamed.name == "Research" && renamed.updatedAt == clock.now && renamed.createdAt < renamed.updatedAt)60 await #expect(throws: OrbitError.invalidName) { try await service.renameSpace(boot.space.id, to: " ") }61 #expect(try await service.recolourSpace(boot.space.id, tint: .ember).tint == .ember)62 #expect(try await service.setSymbol("orix.comet", forSpace: boot.space.id).symbol == "orix.comet")63 #expect(try await service.moveSpace(boot.space.id, to: OrbitSlot(ring: 99, angle: 7)).ring == OrbitalMetrics.standard.maximumRing)64 await #expect(throws: OrbitError.spaceNotFound) { try await service.renameSpace(SpaceID(), to: "x") }65 }6667 @Test func droppingASatelliteStoresWhereItLands() async throws {68 let (_, service, boot, _) = try await fixture()69 let other = try await service.createSpace(profile: boot.profile.id, name: "Other")70 let viewport = OrbitalViewport(size: CGSize(width: 1_000, height: 800))71 // Drop exactly on the first space: the satellite is pushed next to it.72 let target = OrbitalLayout.position(ring: boot.space.ring, angle: boot.space.angle, viewport: viewport)73 let (moved, result) = try await service.dropSpace(other.id, at: target, viewport: viewport)74 #expect(result.displaced && moved.ring == 0)75 #expect(OrbitalLayout.overlaps(in: try await service.spaces(profile: boot.profile.id)).isEmpty)76 }7778 @Test func reorderAndTidy() async throws {79 let (_, service, boot, _) = try await fixture()80 let b = try await service.createSpace(profile: boot.profile.id, name: "B")81 let c = try await service.createSpace(profile: boot.profile.id, name: "C")82 try await service.reorderSpaces([c.id, boot.space.id, b.id])83 #expect(OrbitalLayout.listOrder(try await service.spaces()).map(\.name) == ["C", "Home", "B"])84 let tidy = try await service.tidySpaces(profile: boot.profile.id)85 #expect(tidy.map(\.name) == ["C", "Home", "B"])86 #expect(tidy.allSatisfy { $0.ring == 0 })87 #expect(abs(OrbitalLayout.angularDistance(tidy[0].angle, tidy[1].angle) - 2 * .pi / 3) < 1e-9)88 }8990 @Test func archivingKeepsOneActiveSpaceAndTheWindows() async throws {91 let (database, service, boot, _) = try await fixture()92 await #expect(throws: OrbitError.lastSpace) { try await service.archiveSpace(boot.space.id) }93 await #expect(throws: OrbitError.lastSpace) { try await service.deleteSpace(boot.space.id) }94 let second = try await service.createSpace(profile: boot.profile.id, name: "Second")95 let window = try await service.openWindow(space: boot.space.id)96 try await service.openTab(in: window.id, url: URL(string: "https://example.com"))97 try await service.archiveSpace(boot.space.id)98 #expect(try await service.spaces().map(\.id) == [second.id])99 #expect(try await service.spaces(includeArchived: true).count == 2)100 #expect(try await database.tabs.tabs(window: window.id).count == 1)101 await #expect(throws: OrbitError.lastSpace) { try await service.archiveSpace(second.id) }102103 // Its old position is taken while it is away: it comes back somewhere free.104 try await service.moveSpace(second.id, to: OrbitSlot(ring: boot.space.ring, angle: boot.space.angle))105 let back = try await service.unarchiveSpace(boot.space.id)106 #expect(!back.isArchived)107 #expect(OrbitalLayout.overlaps(in: try await service.spaces()).isEmpty)108109 try await service.deleteSpace(second.id)110 #expect(try await service.spaces(includeArchived: true).map(\.id) == [boot.space.id])111 }112113 @Test func profilesAreCreatedWithASpaceAndTheLastOneCannotBeDeleted() async throws {114 let (database, service, boot, _) = try await fixture()115 await #expect(throws: OrbitError.lastProfile) { _ = try await service.deleteProfile(boot.profile.id) }116 let (work, workSpace) = try await service.createProfile(name: "Work", tint: .forest)117 #expect(work.dataStoreIdentifier != boot.profile.dataStoreIdentifier && workSpace.profileID == work.id && workSpace.tint == .forest)118 #expect(try await service.renameProfile(work.id, to: "Client").name == "Client")119 #expect(try await service.recolourProfile(work.id, tint: .rose).tint == .rose)120 try await database.bookmarks.save(Bookmark(profileID: work.id, title: "B", url: URL(string: "https://example.com")))121 let deletion = try await service.deleteProfile(work.id)122 #expect(deletion.dataStoreIdentifier == work.dataStoreIdentifier && deletion.report.deleted[.bookmarks] == 1)123 #expect(try await service.profiles().map(\.id) == [boot.profile.id])124 #expect(try await database.spaces.space(workSpace.id) == nil)125 await #expect(throws: OrbitError.profileNotFound) { try await service.renameProfile(work.id, to: "x") }126 }127128 // MARK: Tabs129130 @Test func tabsOpenAtTheEndOrAfterTheirParentsFamily() async throws {131 let (database, service, boot, _) = try await fixture()132 let window = try await service.openWindow(space: boot.space.id)133 let a = try await service.openTab(in: window.id, url: URL(string: "https://a.example"))134 let b = try await service.openTab(in: window.id, url: URL(string: "https://b.example"))135 let a1 = try await service.openTab(in: window.id, url: nil, parent: a.id, position: .after(a.id), select: false)136 let a2 = try await service.openTab(in: window.id, url: nil, parent: a.id, position: .after(a.id), select: false)137 #expect(try await database.tabs.tabs(window: window.id).map(\.id) == [a.id, a1.id, a2.id, b.id])138 #expect(try await database.windows.window(window.id)?.selectedTabID == b.id)139 #expect(a1.parentID == a.id && a.lifecycle == .live)140 await #expect(throws: OrbitError.windowNotFound) { try await service.openTab(in: WindowID(), url: nil) }141 }142143 @Test func pinningMovesATabToTheFlatFavouritesRow() async throws {144 let (database, service, boot, _) = try await fixture()145 let window = try await service.openWindow(space: boot.space.id)146 let a = try await service.openTab(in: window.id, url: nil), b = try await service.openTab(in: window.id, url: nil, parent: a.id)147 let c = try await service.openTab(in: window.id, url: nil)148 let group = try await service.createGroup(space: boot.space.id, name: "G", tabs: [b.id])149 let pinned = try await service.setPinned(b.id, true)150 #expect(pinned.isPinned && pinned.groupID == nil && pinned.parentID == nil)151 try await service.setPinned(c.id, true)152 #expect(try await database.tabs.tabs(window: window.id).map(\.id) == [b.id, c.id, a.id])153 #expect(try await database.tabs.tabs(group: group.id).isEmpty)154 try await service.setPinned(b.id, false)155 #expect(try await database.tabs.tabs(window: window.id).map(\.id) == [c.id, b.id, a.id])156 await #expect(throws: OrbitError.differentSpaces) { try await service.addTabs([c.id], toGroup: group.id) } // pinned157 }158159 @Test func duplicatesOpenRightAfterTheOriginal() async throws {160 let (database, service, boot, _) = try await fixture()161 let window = try await service.openWindow(space: boot.space.id)162 let a = try await service.openTab(in: window.id, url: URL(string: "https://a.example"), title: "A")163 let b = try await service.openTab(in: window.id, url: nil)164 let copy = try await service.duplicateTab(a.id)165 #expect(try await database.tabs.tabs(window: window.id).map(\.id) == [a.id, copy.id, b.id])166 #expect(copy.url == a.url && copy.title == "A" && copy.lifecycle == .dormant)167 try await service.setPinned(a.id, true)168 let fromPinned = try await service.duplicateTab(a.id)169 #expect(try await database.tabs.tabs(window: window.id).map(\.id) == [a.id, fromPinned.id, copy.id, b.id])170 }171172 @Test func closingATabFixesSelectionSplitAndTree() async throws {173 let (database, service, boot, _) = try await fixture()174 var window = try await service.openWindow(space: boot.space.id)175 let a = try await service.openTab(in: window.id, url: nil), b = try await service.openTab(in: window.id, url: nil, parent: a.id)176 let c = try await service.openTab(in: window.id, url: nil, parent: b.id)177 window = try #require(try await database.windows.window(window.id))178 window.split = SplitLayout(panes: [a.id, b.id, c.id], focusedPane: 1)179 window.selectedTabID = b.id180 try await database.windows.save(window)181182 let first = try await service.closeTab(b.id)183 #expect(first.window.split?.panes == [a.id, c.id] && !first.windowIsEmpty)184 #expect(first.window.selectedTabID == c.id)185 #expect(try await database.tabs.tab(c.id)?.parentID == a.id) // adopted by the grandparent186187 let second = try await service.closeTab(c.id)188 #expect(second.window.split == nil && second.window.selectedTabID == a.id)189 let last = try await service.closeTab(a.id)190 #expect(last.windowIsEmpty && last.window.selectedTabID == nil)191 await #expect(throws: OrbitError.tabNotFound) { try await service.closeTab(a.id) }192 }193194 @Test func closedTabsAndWindowsCanBeReopened() async throws {195 let (database, service, boot, clock) = try await fixture()196 let window = try await service.openWindow(space: boot.space.id)197 let a = try await service.openTab(in: window.id, url: URL(string: "https://a.example"), title: "A")198 let b = try await service.openTab(in: window.id, url: nil, title: "B")199 try await service.closeTab(a.id)200 clock.advance(10)201 try await service.closeTab(b.id)202 #expect(try await service.recentlyClosed().tabs.map(\.title) == ["B", "A"])203 #expect(try await service.reopenClosedTab().id == b.id)204 #expect(try await service.reopenClosedTab(space: boot.space.id).id == a.id)205 await #expect(throws: OrbitError.nothingToReopen) { try await service.reopenClosedTab() }206 await #expect(throws: OrbitError.nothingToReopen) { try await service.reopenClosedTab(a.id) } // it is open207208 // The original window is gone: the tab reopens in another window of the space, or a new one.209 try await service.closeTab(a.id)210 try await service.closeWindow(window.id)211 let reopened = try await service.reopenClosedTab(a.id)212 #expect(reopened.windowID != window.id && reopened.lifecycle == .dormant)213 #expect(try await database.windows.openWindows(space: boot.space.id).map(\.id) == [reopened.windowID])214215 let closed = try await service.reopenClosedWindow()216 #expect(closed.window.id == window.id && closed.tabs.map(\.id) == [b.id])217 await #expect(throws: OrbitError.nothingToReopen) { _ = try await service.reopenClosedWindow() }218 await #expect(throws: OrbitError.windowNotFound) { _ = try await service.reopenClosedWindow(WindowID()) }219 }220221 @Test func movingTabsBetweenWindowsAndSpaces() async throws {222 let (database, service, boot, _) = try await fixture()223 let first = try await service.openWindow(space: boot.space.id), second = try await service.openWindow(space: boot.space.id)224 let a = try await service.openTab(in: first.id, url: nil), b = try await service.openTab(in: first.id, url: nil, parent: a.id)225 let c = try await service.openTab(in: second.id, url: nil), d = try await service.openTab(in: second.id, url: nil)226 let group = try await service.createGroup(space: boot.space.id, name: "G", tabs: [a.id])227228 let moved = try await service.moveTab(a.id, toWindow: second.id, index: 1)229 #expect(!moved.crossedProfile && moved.tab.groupID == group.id && moved.tab.order == 1) // same space: the group follows230 #expect(try await database.tabs.tabs(window: second.id).map(\.id) == [c.id, a.id, d.id])231 #expect(try await database.tabs.tab(b.id)?.parentID == nil)232 #expect(try await database.windows.window(first.id)?.selectedTabID == b.id)233234 // Reordering inside the same window.235 try await service.moveTab(d.id, toWindow: second.id, index: 0)236 #expect(try await database.tabs.tabs(window: second.id).map(\.id) == [d.id, c.id, a.id])237238 // Another space of the same profile: the group is left behind, the history is kept.239 try await database.session.apply([.setSessionState(id: a.id, state: Data([1, 2, 3]))])240 let other = try await service.createSpace(profile: boot.profile.id, name: "Other")241 let toSpace = try await service.moveTab(a.id, toSpace: other.id)242 #expect(toSpace.createdWindow != nil && toSpace.createdWindow?.selectedTabID == a.id)243 #expect(toSpace.tab.spaceID == other.id && toSpace.tab.groupID == nil && toSpace.tab.sessionState == Data([1, 2, 3]))244 let again = try await service.moveTab(c.id, toSpace: other.id)245 #expect(again.createdWindow == nil && again.tab.windowID == toSpace.tab.windowID)246 }247248 @Test func crossingAProfileDropsTheWebKitSessionState() async throws {249 let (database, service, boot, _) = try await fixture()250 let window = try await service.openWindow(space: boot.space.id)251 let tab = try await service.openTab(in: window.id, url: URL(string: "https://bank.example/account"))252 try await database.session.apply([.setSessionState(id: tab.id, state: Data([1, 2, 3]))])253 let (_, workSpace) = try await service.createProfile(name: "Work")254 let moved = try await service.moveTab(tab.id, toSpace: workSpace.id)255 #expect(moved.crossedProfile && moved.tab.sessionState == nil && moved.tab.lifecycle == .dormant)256 #expect(try await database.tabs.tab(tab.id)?.sessionState == nil)257 #expect(try await database.tabs.tab(tab.id)?.url == tab.url)258259 // Moving a whole window across profiles does the same to every tab.260 let another = try await service.openTab(in: window.id, url: nil)261 try await database.session.apply([.setSessionState(id: another.id, state: Data([4]))])262 let movedWindow = try await service.moveWindow(window.id, toSpace: workSpace.id)263 #expect(movedWindow.spaceID == workSpace.id)264 #expect(try await database.tabs.tab(another.id)?.sessionState == nil)265 #expect(try await database.tabs.tab(another.id)?.spaceID == workSpace.id)266 }267268 // MARK: Groups and search269270 @Test func groupsLifecycle() async throws {271 let (database, service, boot, _) = try await fixture()272 let window = try await service.openWindow(space: boot.space.id)273 let a = try await service.openTab(in: window.id, url: nil), b = try await service.openTab(in: window.id, url: nil)274 let c = try await service.openTab(in: window.id, url: nil)275 let group = try await service.createGroup(space: boot.space.id, name: " Reading ", tint: .ocean, tabs: [a.id])276 let second = try await service.createGroup(space: boot.space.id, name: "Later")277 #expect(group.name == "Reading" && second.order == 1)278 try await service.addTabs([b.id, c.id], toGroup: group.id)279 #expect(try await database.tabs.tabs(group: group.id).count == 3)280 try await service.ungroupTabs([a.id])281 #expect(try await service.renameGroup(group.id, to: "Read").name == "Read")282 #expect(try await service.recolourGroup(group.id, tint: .solar).tint == .solar)283 #expect(try await service.setGroupCollapsed(group.id, true).isCollapsed)284 try await service.reorderGroups([second.id, group.id])285 #expect(try await database.tabGroups.groups(space: boot.space.id).map(\.id) == [second.id, group.id])286287 try await service.deleteGroup(second.id)288 try await service.deleteGroup(group.id, closingTabs: true)289 #expect(try await database.tabs.tabs(window: window.id).map(\.id) == [a.id])290 #expect(try await service.recentlyClosed().tabs.count == 2)291 await #expect(throws: OrbitError.groupNotFound) { try await service.deleteGroup(group.id) }292293 let other = try await service.createSpace(profile: boot.profile.id, name: "Other")294 await #expect(throws: OrbitError.differentSpaces) { try await service.createGroup(space: other.id, name: "X", tabs: [a.id]) }295 #expect(try await database.tabGroups.groups(space: other.id).isEmpty) // the failed creation left nothing behind296 }297298 @Test func searchCoversSpacesAndOpenTabs() async throws {299 let (_, service, boot, _) = try await fixture()300 let research = try await service.createSpace(profile: boot.profile.id, name: "Research")301 let archived = try await service.createSpace(profile: boot.profile.id, name: "Research archive")302 let window = try await service.openWindow(space: research.id)303 let paper = try await service.openTab(in: window.id, url: URL(string: "https://arxiv.org/abs/1"), title: "Orbital research")304 let closed = try await service.openTab(in: window.id, url: nil, title: "Research closed")305 try await service.closeTab(closed.id)306 try await service.archiveSpace(archived.id)307 let results = try await service.search("research")308 #expect(results.map(\.id) == ["space-\(research.id.string)", "tab-\(paper.id.string)"])309 #expect(try await service.search("research", includeArchived: true).count == 3)310 let (other, _) = try await service.createProfile(name: "Work")311 #expect(try await service.search("research", profile: other.id).isEmpty)312 }313314 @Test func ghostEntitiesAreRefused() async throws {315 let (database, service, boot, _) = try await fixture()316 let ghostProfile = ProfileID(), ghostSpace = SpaceID(), ghostWindow = WindowID()317 database.ghosts.declare(profile: ghostProfile, spaces: [ghostSpace], windows: [ghostWindow])318 let before = try await rowCount(database)319 await #expect(throws: OrbitError.ghostNotPersisted) { try await service.createSpace(profile: ghostProfile, name: "x") }320 await #expect(throws: OrbitError.ghostNotPersisted) { try await service.renameSpace(ghostSpace, to: "x") }321 await #expect(throws: OrbitError.ghostNotPersisted) { try await service.openWindow(space: ghostSpace) }322 await #expect(throws: OrbitError.ghostNotPersisted) { try await service.openTab(in: ghostWindow, url: nil) }323 await #expect(throws: OrbitError.ghostNotPersisted) { _ = try await service.deleteProfile(ghostProfile) }324 let window = try await service.openWindow(space: boot.space.id)325 let tab = try await service.openTab(in: window.id, url: nil)326 await #expect(throws: OrbitError.ghostNotPersisted) { try await service.moveTab(tab.id, toWindow: ghostWindow) }327 await #expect(throws: OrbitError.ghostNotPersisted) { try await service.moveTab(tab.id, toSpace: ghostSpace) }328 #expect(try await rowCount(database) == before + 2) // only the normal window and tab329 }330}331332@Suite struct BlinkCoordinatorTests {333 private func fixture() async throws -> (NavigatorDatabase, BlinkCoordinator, [Tab], TestClock) {334 let session = try await SessionFixture.make(windowCount: 1)335 let clock = TestClock()336 var tabs: [Tab] = []337 for index in 0..<5 {338 var tab = session.tab(0, order: index)339 tab.lifecycle = .live340 tab.url = URL(string: "https://site\(index).example/page")341 tab.lastActiveAt = clock.now.addingTimeInterval(-Double(index + 1) * 1_000)342 tabs.append(tab)343 }344 try await session.database.tabs.save(tabs)345 let coordinator = BlinkCoordinator(database: session.database, automaticBudget: BlinkBudget(maximumLiveWebViews: 3), now: { clock.now })346 return (session.database, coordinator, tabs, clock)347 }348349 @Test func theBudgetIsAutomaticUntilTheUserSetsOne() async throws {350 let (_, coordinator, _, _) = try await fixture()351 #expect(try await coordinator.budget() == BlinkBudget(maximumLiveWebViews: 3))352 try await coordinator.setMaximumLiveWebViews(7)353 try await coordinator.setIdleLimit(seconds: 7_200)354 #expect(try await coordinator.budget() == BlinkBudget(maximumLiveWebViews: 7, idleLimit: 7_200))355 try await coordinator.setMaximumLiveWebViews(nil)356 try await coordinator.setIdleLimit(seconds: -4)357 #expect(try await coordinator.budget() == BlinkBudget(maximumLiveWebViews: 3))358 }359360 @Test func signalsAndExclusionsProtectTabs() async throws {361 let (_, coordinator, tabs, _) = try await fixture()362 await coordinator.setVisibleTabs([tabs[0].id])363 await coordinator.report(BlinkSignals(isPlayingAudio: true), for: tabs[4].id)364 await coordinator.setTabExcluded(tabs[3].id, true)365 try await coordinator.setHostExcluded(" Example ", true) // covers site2.example366 let plan = try await coordinator.plan(for: tabs)367 #expect(plan.protected == [tabs[0].id: .visible, tabs[4].id: .playingAudio, tabs[3].id: .excludedByUser,368 tabs[1].id: .excludedByUser, tabs[2].id: .excludedByUser])369 #expect(plan.decisions.isEmpty && plan.overLimitBy == 2)370371 try await coordinator.setHostExcluded("example", false)372 try await coordinator.setHostExcluded("site1.example", true)373 #expect(try await coordinator.excludedHosts() == ["site1.example"])374 let second = try await coordinator.plan(for: tabs)375 #expect(second.suspensions == [tabs[2].id])376 #expect(BlinkCoordinator.host("a.b.example.com", isCoveredBy: ["example.com"]))377 #expect(!BlinkCoordinator.host("notexample.com", isCoveredBy: ["example.com"]))378 }379380 @Test func confirmedDecisionsArePersistedAsLifecycleTransitions() async throws {381 let (database, coordinator, tabs, _) = try await fixture()382 await coordinator.setVisibleTabs([tabs[0].id])383 await coordinator.report(BlinkSignals(hasUnsubmittedInput: true), for: tabs[4].id)384 let plan = try await coordinator.plan(for: tabs, pressure: .critical)385 #expect(plan.suspensions == [tabs[3].id, tabs[2].id, tabs[1].id])386 for decision in plan.decisions { #expect(try await coordinator.confirm(decision) == .suspended) }387 #expect(try await database.tabs.tab(tabs[3].id)?.lifecycle == .suspended)388 #expect(try await database.tabs.tab(tabs[4].id)?.lifecycle == .live)389 #expect(await coordinator.suspensionCounts == [.memoryPressureCritical: 3])390 // Confirming twice records nothing more.391 #expect(try await coordinator.confirm(plan.decisions[0]) == nil)392 #expect(await coordinator.suspensionCounts == [.memoryPressureCritical: 3])393394 // The user comes back to a suspended tab.395 await coordinator.setVisibleTabs([tabs[3].id])396 let stored = try await database.tabs.tabs(window: tabs[0].windowID)397 let resume = try await coordinator.plan(for: stored)398 #expect(resume.resumptions == [tabs[3].id])399 #expect(try await coordinator.confirm(resume.decisions[0]) == .live)400 #expect(try await coordinator.record(.dormant, for: tabs[3].id) == .dormant)401 #expect(try await coordinator.record(.dormant, for: tabs[3].id) == nil)402 #expect(try await coordinator.record(.live, for: TabID()) == nil)403 }404405 @Test func privateTabsAreTrackedInMemoryOnly() async throws {406 let (database, coordinator, _, clock) = try await fixture()407 let ghostSpace = SpaceID(), ghostWindow = WindowID()408 database.ghosts.declare(profile: ProfileID(), spaces: [ghostSpace], windows: [ghostWindow])409 let ghost = Tab(windowID: ghostWindow, spaceID: ghostSpace, lifecycle: .live, lastActiveAt: clock.now.addingTimeInterval(-500))410 await coordinator.trackGhostTab(ghost.id, lifecycle: .live)411 let before = try await rowCount(database)412 let plan = try await coordinator.plan(for: [ghost], pressure: .critical)413 #expect(plan.suspensions == [ghost.id])414 #expect(try await coordinator.confirm(plan.decisions[0]) == .suspended)415 #expect(try await coordinator.states(for: [ghost]).first?.lifecycle == .suspended)416 #expect(try await rowCount(database) == before)417 #expect(try await database.tabs.tab(ghost.id) == nil)418 await coordinator.forget([ghost.id])419 #expect(try await coordinator.states(for: [ghost]).first?.lifecycle == .live)420 }421422 @Test func visibleTabsOfWindows() {423 let space = SpaceID(), a = TabID(), b = TabID(), c = TabID()424 let plain = BrowserWindow(spaceID: space, selectedTabID: a)425 let split = BrowserWindow(spaceID: space, selectedTabID: b, split: SplitLayout(panes: [b, c]))426 let closed = BrowserWindow(spaceID: space, selectedTabID: TabID(), closedAt: .now)427 #expect(BlinkCoordinator.visibleTabs(of: [plain, split, closed]) == [a, b, c])428 }429}430