SPB Git

Repository Public

WorkspaceTests.swift 16.2 KB 334 lines swift

1import AppKit2import SessionStore3import TerminalCore4import WindowManager5import WorkspaceKit6import XCTest7@testable import SPTerm89@MainActor10final class WorkspaceTests: AppTestCase {11    func testTabsCreateSelectAndClose() async {12        let model = makeModel()13        let window = model.openWindow()14        XCTAssertEqual(window.tabs.count, 1)15        let first = window.tabs[0].id16        let second = model.newTab(in: window)17        XCTAssertEqual(window.tabs.count, 2)18        XCTAssertEqual(window.selectedTabID, second)19        model.perform(.previousTab, in: window)20        XCTAssertEqual(window.selectedTabID, first)21        model.perform(.tab2, in: window)22        XCTAssertEqual(window.selectedTabID, second)23        model.closeTab(second, in: window)24        XCTAssertEqual(window.tabs.map(\.id), [first])25        XCTAssertEqual(model.sessions.count, 1)26        // Closing the last tab closes the window.27        model.closeTab(first, in: window)28        XCTAssertTrue(model.windows.isEmpty)29        XCTAssertTrue(model.sessions.isEmpty)30    }3132    func testRecursiveSplitsCreateIndependentSessions() async {33        let model = makeModel()34        let window = model.openWindow()35        let a = window.focusedPane!36        let b = model.split(axis: .horizontal, in: window)!37        let c = model.split(axis: .vertical, in: window)!38        XCTAssertEqual(window.selectedTab?.layout.panes, [a, b, c])39        XCTAssertEqual(window.selectedTab?.layout.depth, 3)40        XCTAssertEqual(window.focusedPane, c)41        XCTAssertTrue(window.selectedTab!.isConsistent)42        await start(window, in: model)43        let ttys = Set([a, b, c].compactMap { model.session(for: $0)?.ttyName })44        XCTAssertEqual(ttys.count, 3, "each pane must own its PTY")45        XCTAssertEqual(Set([a, b, c].compactMap { model.session(for: $0)?.pid }).count, 3)46        // Input in one pane does not reach the others.47        model.session(for: a)?.send(text: "echo only-in-A\r")48        let seen = await wait { self.text(a, model).contains("only-in-A\n") }49        XCTAssertTrue(seen)50        XCTAssertFalse(text(b, model).contains("only-in-A"))51        XCTAssertFalse(text(c, model).contains("only-in-A"))52        // Up to 8 panes in one tab.53        for _ in 0..<5 { model.split(axis: .horizontal, in: window) }54        XCTAssertEqual(window.selectedTab?.panes.count, 8)55        XCTAssertTrue(window.selectedTab!.isConsistent)56    }5758    func testKeyboardFocusNavigationZoomAndResize() {59        let model = makeModel()60        let window = model.openWindow()61        let a = window.focusedPane!62        let b = model.split(axis: .horizontal, in: window)!63        let c = model.split(axis: .vertical, in: window)!   // a | b over c64        model.perform(.focusLeft, in: window)65        XCTAssertEqual(window.focusedPane, a)66        model.perform(.focusRight, in: window)67        XCTAssertEqual(window.focusedPane, b)68        model.perform(.focusDown, in: window)69        XCTAssertEqual(window.focusedPane, c)70        model.perform(.nextPane, in: window)71        XCTAssertEqual(window.focusedPane, a)72        model.perform(.previousPane, in: window)73        XCTAssertEqual(window.focusedPane, c)74        // Zoom is temporary and does not change the layout.75        let layout = window.selectedTab!.layout76        model.perform(.zoomPane, in: window)77        XCTAssertEqual(window.selectedTab?.zoomedPane, c)78        XCTAssertEqual(window.selectedTab?.layout, layout)79        model.perform(.zoomPane, in: window)80        XCTAssertNil(window.selectedTab?.zoomedPane)81        // Keyboard resize changes the shares.82        let before = layout.frames(in: CGRect(x: 0, y: 0, width: 1000, height: 800), gap: 0)[c]!83        model.perform(.resizeUp, in: window)84        let after = window.selectedTab!.layout.frames(in: CGRect(x: 0, y: 0, width: 1000, height: 800), gap: 0)[c]!85        XCTAssertGreaterThan(after.height, before.height)86        model.perform(.equalizePanes, in: window)87        XCTAssertTrue(window.selectedTab!.isConsistent)88    }8990    func testDetachPaneAndMergeBackKeepTheProcessAlive() async {91        let model = makeModel()92        let window = model.openWindow()93        let a = window.focusedPane!94        let b = model.split(axis: .horizontal, in: window)!95        await start(window, in: model)96        let pid = model.session(for: b)!.pid97        let view = model.session(for: b)!.view98        model.session(for: b)?.send(text: "MARK=still-here\r")99100        let detached = model.detachPane(b, from: window)!101        XCTAssertEqual(model.windows.count, 2)102        XCTAssertEqual(window.allPanes, [a])103        XCTAssertEqual(detached.allPanes, [b])104        XCTAssertEqual(model.session(for: b)?.pid, pid, "detaching must not restart the shell")105        XCTAssertTrue(model.session(for: b)!.view === view)106107        model.mergeWindow(detached, into: window)108        XCTAssertEqual(model.windows.count, 1)109        XCTAssertEqual(window.tabs.count, 2)110        XCTAssertEqual(model.session(for: b)?.pid, pid)111        // The shell kept its state across both moves.112        model.session(for: b)?.send(text: "echo value=$MARK\r")113        let alive = await wait { self.text(b, model).contains("value=still-here") }114        XCTAssertTrue(alive)115116        // Detaching a whole tab works the same way.117        let tab = window.tabs[1].id118        let other = model.detachTab(tab, from: window)!119        XCTAssertEqual(other.tabs.map(\.id), [tab])120        XCTAssertEqual(model.session(for: b)?.pid, pid)121    }122123    func testMovePaneAcrossTabsByDrop() async {124        let model = makeModel()125        let window = model.openWindow()126        let a = window.focusedPane!127        model.newTab(in: window)128        let b = window.focusedPane!129        await start(window, in: model)130        let pid = model.session(for: b)!.pid131        model.movePane(b, to: .bottom, of: a)132        XCTAssertEqual(window.tabs.count, 1, "the emptied tab is removed")133        XCTAssertEqual(window.selectedTab?.layout.panes, [a, b])134        XCTAssertEqual(model.session(for: b)?.pid, pid)135        XCTAssertEqual(PaneDrag.pane(from: PaneDrag.token(for: a)), a)136        XCTAssertNil(PaneDrag.pane(from: "ls -la"))137        XCTAssertEqual(PaneDrag.edge(for: CGPoint(x: 0.05, y: 0.5)), .left)138        XCTAssertEqual(PaneDrag.edge(for: CGPoint(x: 0.5, y: 0.97)), .bottom)139    }140141    func testClosingABusyPaneAsksFirst() async {142        let model = makeModel()143        let window = model.openWindow()144        let a = window.focusedPane!145        let b = model.split(axis: .horizontal, in: window)!146        await start(window, in: model)147        model.session(for: b)?.send(text: "sleep 60\r")148        let busy = await wait { model.sampleSessions(); return model.session(for: b)?.isBusy == true }149        XCTAssertTrue(busy)150        answers.confirm = false151        model.closePane(b, in: window)152        XCTAssertEqual(answers.asked, ["Close this pane?"])153        XCTAssertEqual(window.allPanes, [a, b], "declining keeps the pane")154        XCTAssertTrue(model.session(for: b)!.status.isRunning)155        answers.confirm = true156        let pid = model.session(for: b)!.pid!157        model.closePane(b, in: window)158        XCTAssertEqual(window.allPanes, [a])159        let gone = await wait { kill(pid, 0) == -1 }160        XCTAssertTrue(gone, "the closed session's process must be terminated")161        // An idle pane closes without a question.162        answers.asked = []163        model.closePane(a, in: window)164        XCTAssertTrue(answers.asked.isEmpty)165        XCTAssertTrue(model.windows.isEmpty)166    }167168    func testRenameDuplicateAndRestart() async {169        let model = makeModel()170        let window = model.openWindow()171        let a = window.focusedPane!172        await start(window, in: model)173        model.rename(pane: a, to: "build")174        XCTAssertEqual(model.paneTitle(a), "build")175        model.rename(pane: a, to: "  ")176        XCTAssertNotEqual(model.paneTitle(a), "build")177        model.duplicatePane(a, in: window)178        XCTAssertEqual(window.selectedTab?.panes.count, 2)179        let pid = model.session(for: a)!.pid180        model.restartSession(a)181        let restarted = await wait { model.session(for: a)?.status.isRunning == true && model.session(for: a)?.pid != pid }182        XCTAssertTrue(restarted)183    }184185    func testPresets() {186        let model = makeModel()187        displays.screens = [FakeDisplays.laptop, FakeDisplays.external]188        model.open(preset: .fourPanes)189        XCTAssertEqual(model.windows.last?.selectedTab?.panes.count, 4)190        model.open(preset: .codeTestsLogs)191        XCTAssertEqual(model.windows.last?.selectedTab?.panes.compactMap(\.role), ["code", "tests", "logs"])192        let before = model.windows.count193        model.open(preset: .dualMonitor)194        XCTAssertEqual(model.windows.count, before + 2)195        let frames = model.windows.suffix(2).compactMap { $0.controller?.window?.frame }196        XCTAssertTrue(FakeDisplays.laptop.visibleFrame.contains(frames[0]), "first window on the main display")197        XCTAssertTrue(FakeDisplays.external.visibleFrame.contains(frames[1]), "second window on the second display")198        model.open(preset: .sshFleet, hosts: ["alpha", "beta", "gamma"])199        let fleet = model.windows.last!.tabs.flatMap(\.panes)200        XCTAssertEqual(fleet.map(\.kind), [.ssh(hostAlias: "alpha"), .ssh(hostAlias: "beta"), .ssh(hostAlias: "gamma")])201        XCTAssertTrue(model.windows.allSatisfy { $0.broadcastTabs.isEmpty }, "no preset turns broadcast on")202        model.open(preset: .agentCommandCenter, agentCommands: ["/bin/echo one", "/bin/echo two"])203        XCTAssertEqual(model.windows.last?.selectedTab?.panes.compactMap(\.role), ["supervisor", "agent", "agent"])204    }205}206207@MainActor208final class DisplayTests: AppTestCase {209    func testNewWindowGoesToTheChosenDisplayAndCanBeMoved() {210        displays.screens = [FakeDisplays.laptop, FakeDisplays.external]211        let model = makeModel()212        let onExternal = model.openWindow(on: 2)213        XCTAssertTrue(FakeDisplays.external.visibleFrame.contains(onExternal.controller!.window!.frame))214        let onLaptop = model.openWindow(on: 1)215        XCTAssertTrue(FakeDisplays.laptop.visibleFrame.contains(onLaptop.controller!.window!.frame))216        model.moveWindow(onLaptop, toScreen: 2)217        XCTAssertTrue(FakeDisplays.external.visibleFrame.contains(onLaptop.controller!.window!.frame))218        model.moveWindowToNextScreen(onLaptop)219        XCTAssertTrue(FakeDisplays.laptop.visibleFrame.contains(onLaptop.controller!.window!.frame))220    }221222    func testUnpluggingADisplayBringsItsWindowsBack() async {223        displays.screens = [FakeDisplays.laptop, FakeDisplays.external]224        let model = makeModel()225        let stays = model.openWindow(on: 1)226        let orphan1 = model.openWindow(on: 2)227        let orphan2 = model.openWindow(on: 2)228        model.split(axis: .horizontal, in: orphan1)229        await start(orphan1, in: model)230        let pids = orphan1.allPanes.map { model.session(for: $0)!.pid }231        let untouched = stays.controller!.window!.frame232233        // The external display disappears.234        displays.screens = [FakeDisplays.laptop]235        let rescued = model.handleScreenChange()236237        XCTAssertEqual(Set(rescued.keys), [orphan1.id, orphan2.id])238        XCTAssertEqual(stays.controller!.window!.frame, untouched, "windows on the remaining display are not moved")239        for window in model.windows {240            let frame = window.controller!.window!.frame241            XCTAssertTrue(PlacementEngine.isReachable(frame, in: displays.screens), "window \(window.id) is off screen: \(frame)")242            XCTAssertTrue(FakeDisplays.laptop.visibleFrame.contains(frame))243        }244        XCTAssertEqual(model.windows.count, 3, "no window is lost")245        XCTAssertEqual(orphan1.allPanes.map { model.session(for: $0)!.pid }, pids, "sessions survive the display change")246        XCTAssertNotNil(model.keyWindowState?.banner)247248        // Plugging it back does not move anything by itself.249        displays.screens = [FakeDisplays.laptop, FakeDisplays.external]250        XCTAssertTrue(model.handleScreenChange().isEmpty)251    }252253    func testResolutionChangeKeepsWindowsVisible() {254        displays.screens = [FakeDisplays.laptop]255        let model = makeModel()256        let window = model.openWindow(on: 1)257        window.controller!.window!.setFrame(CGRect(x: 900, y: 500, width: 600, height: 430), display: false)258        let smaller = ScreenDescriptor(id: 1, name: "Built-in Retina Display", frame: CGRect(x: 0, y: 0, width: 1024, height: 640),259                                       visibleFrame: CGRect(x: 0, y: 60, width: 1024, height: 555), isMain: true)260        displays.screens = [smaller]261        model.handleScreenChange()262        XCTAssertTrue(smaller.visibleFrame.contains(window.controller!.window!.frame))263    }264265    func testWorkspaceRoundTripOnTheSameDisplays() async throws {266        displays.screens = [FakeDisplays.laptop, FakeDisplays.external]267        let model = makeModel()268        let first = model.openWindow(on: 1)269        let a = first.focusedPane!270        model.split(axis: .horizontal, in: first)271        model.split(axis: .vertical, in: first)272        model.newTab(in: first)273        model.rename(pane: a, to: "editor")274        model.rename(tab: first.tabs[0].id, in: first, to: "work")275        let second = model.openWindow(on: 2)276        second.controller!.window!.setFrame(CGRect(x: 2000, y: 100, width: 1500, height: 900), display: false)277        await start(first, in: model)278        model.session(for: a)?.send(text: "cd /usr/share\r")279        let moved = await wait { model.sampleSessions(); return model.session(for: a)?.workingDirectory == "/usr/share" }280        XCTAssertTrue(moved)281        let oldPIDs = Set(model.sessions.values.compactMap(\.pid))282        let saved = model.currentSnapshot()283        model.saveLastSession()284        model.terminateAllSessions()285286        // A new launch.287        let relaunched = makeModel()288        let snapshot = try XCTUnwrap(relaunched.stores.loadLastSession())289        XCTAssertEqual(snapshot.windows.map(\.tabs), saved.windows.map(\.tabs))290        relaunched.open(workspace: snapshot, keepIDs: true)291        XCTAssertEqual(relaunched.windows.count, 2)292        let restored = relaunched.windows[0]293        XCTAssertEqual(restored.tabs.count, 2)294        XCTAssertEqual(restored.tabs[0].customTitle, "work")295        XCTAssertEqual(restored.tabs[0].layout, saved.windows[0].tabs[0].layout)296        XCTAssertEqual(restored.tabs[0].descriptor(for: a)?.customTitle, "editor")297        XCTAssertEqual(restored.tabs[0].descriptor(for: a)?.workingDirectory, "/usr/share")298        XCTAssertEqual(relaunched.windows[1].controller!.window!.frame, CGRect(x: 2000, y: 100, width: 1500, height: 900))299        // Honest restoration: the layout comes back, the processes are new.300        await start(restored, in: relaunched)301        let newPIDs = Set(relaunched.sessions.values.compactMap(\.pid))302        XCTAssertTrue(oldPIDs.isDisjoint(with: newPIDs))303        relaunched.session(for: a)?.send(text: "pwd\r")304        let inPlace = await wait { self.text(a, relaunched).contains("\n/usr/share") }305        XCTAssertTrue(inPlace, "the restored session starts in the saved directory")306    }307308    func testRestoreWhenTheSavedDisplayIsGone() async throws {309        displays.screens = [FakeDisplays.laptop, FakeDisplays.external]310        let model = makeModel()311        let window = model.openWindow(on: 2)312        window.controller!.window!.setFrame(CGRect(x: 2600, y: 300, width: 1300, height: 800), display: false)313        model.saveLastSession()314        model.terminateAllSessions()315316        displays.screens = [FakeDisplays.laptop]317        let relaunched = makeModel()318        relaunched.open(workspace: try XCTUnwrap(relaunched.stores.loadLastSession()), keepIDs: true)319        let frame = relaunched.windows[0].controller!.window!.frame320        XCTAssertTrue(FakeDisplays.laptop.visibleFrame.contains(frame), "restored off screen: \(frame)")321    }322323    func testPrivateModeWritesNoLayout() {324        let model = makeModel()325        model.openWindow()326        model.saveLastSession()327        XCTAssertTrue(FileManager.default.fileExists(atPath: model.stores.locations.lastSession.path))328        model.settings.privateMode = true329        XCTAssertFalse(FileManager.default.fileExists(atPath: model.stores.locations.lastSession.path), "turning private mode on deletes the saved layout")330        model.saveLastSession()331        XCTAssertFalse(FileManager.default.fileExists(atPath: model.stores.locations.lastSession.path))332    }333}334
Files main
Menu

Theme