260 lines
11 KiB
Swift
260 lines
11 KiB
Swift
import HelderCore
|
|
import HelderServices
|
|
@testable import HelderUI
|
|
import Foundation
|
|
import Testing
|
|
|
|
private struct MemorySession: SessionStoring {
|
|
func loadSession(root: String) -> SessionState? { nil }
|
|
func saveSession(_ session: SessionState, root: String) {}
|
|
func loadLayout() -> LayoutState { LayoutState(agentFraction: 0.9) }
|
|
func saveLayout(_ layout: LayoutState) {}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite struct LayoutStoreTests {
|
|
@Test func restoresAClampedAgentSplit() {
|
|
#expect(LayoutStore(session: MemorySession()).agentFraction == 0.82)
|
|
}
|
|
|
|
@Test func draggingLocksTheColumns() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
#expect(layout.fluid)
|
|
layout.dragExplorer(by: 1000)
|
|
#expect(!layout.fluid)
|
|
#expect(layout.widths.explorer == 520)
|
|
}
|
|
|
|
@Test func sourceControlDragsStayInsideTheirRange() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.dragSourceControl(by: -1000)
|
|
#expect(layout.widths.sourceControl == 160)
|
|
layout.dragSourceControl(by: 5000)
|
|
#expect(layout.widths.sourceControl == 460)
|
|
}
|
|
|
|
@Test func theAgentColumnNeverSqueezesTheEditorBelowItsMinimum() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
layout.dragAgent(by: -5000)
|
|
let editor = 1760 - 3 - layout.widths.sourceControl - layout.widths.explorer - layout.widths.agent
|
|
#expect(editor >= ColumnLayout.minEditorWidth)
|
|
}
|
|
|
|
@Test func roomyWindowsUseTheDesignProportions() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
#expect(layout.widths == ColumnWidths(sourceControl: 236, explorer: 289, agent: 480))
|
|
}
|
|
|
|
@Test func focusChangesTheFluidWidthsOnlyBelow1600() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1500)
|
|
layout.focus(.terminal)
|
|
#expect(layout.widths.agent == 750)
|
|
layout.focus(.editor)
|
|
#expect(layout.widths.agent == 300)
|
|
layout.windowResized(width: 1800)
|
|
layout.focus(.terminal)
|
|
#expect(layout.widths.agent == (1800 * 0.273).rounded())
|
|
}
|
|
|
|
@Test func autoFitBackOnRefitsImmediately() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
layout.dragExplorer(by: 100)
|
|
#expect(!layout.fluid)
|
|
layout.toggleFluid()
|
|
#expect(layout.fluid)
|
|
#expect(layout.widths.explorer == 289)
|
|
}
|
|
|
|
@Test func lockedColumnsShrinkToKeepTheEditorWhenTheWindowNarrows() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
layout.dragExplorer(by: 0)
|
|
layout.windowResized(width: 1100)
|
|
let total = layout.widths.sourceControl + layout.widths.explorer + layout.widths.agent
|
|
#expect(total <= 1100 - 3 - ColumnLayout.minEditorWidth)
|
|
}
|
|
|
|
@Test func hidingGitDropsItsFocus() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.focus(.sourceControl)
|
|
layout.togglePane(.sourceControl)
|
|
#expect(layout.activePanel == nil)
|
|
layout.focus(.sourceControl)
|
|
#expect(layout.activePanel == nil)
|
|
}
|
|
|
|
@Test func aProjectsColumnsSurviveARestart() {
|
|
let store = FakeLayouts()
|
|
let first = LayoutStore(session: MemorySession(), persistence: store)
|
|
first.restore(root: "/p")
|
|
first.windowResized(width: 1760)
|
|
first.dragExplorer(by: 40)
|
|
first.endDrag()
|
|
first.togglePane(.sourceControl)
|
|
|
|
let second = LayoutStore(session: MemorySession(), persistence: store)
|
|
second.windowResized(width: 1760)
|
|
second.restore(root: "/p")
|
|
#expect(!second.fluid)
|
|
#expect(!second.isVisible(.sourceControl))
|
|
#expect(second.widths.explorer == first.widths.explorer)
|
|
#expect(second.widths.agent == first.widths.agent)
|
|
}
|
|
|
|
@Test func projectsKeepSeparateLayouts() {
|
|
let store = FakeLayouts()
|
|
let layout = LayoutStore(session: MemorySession(), persistence: store)
|
|
layout.restore(root: "/a")
|
|
layout.togglePane(.sourceControl)
|
|
layout.restore(root: "/b")
|
|
#expect(layout.isVisible(.sourceControl))
|
|
layout.restore(root: "/a")
|
|
#expect(!layout.isVisible(.sourceControl))
|
|
}
|
|
|
|
@Test func dragsSaveOnlyWhenTheyEnd() {
|
|
let store = FakeLayouts()
|
|
let layout = LayoutStore(session: MemorySession(), persistence: store)
|
|
layout.restore(root: "/p")
|
|
layout.dragExplorer(by: 10)
|
|
#expect(store.saved["/p"] == nil)
|
|
layout.endDrag()
|
|
#expect(store.saved["/p"]?.fluid == false)
|
|
}
|
|
|
|
@Test func theAgentSplitFollowsTheDragAndStaysClamped() {
|
|
let session = FakeSession()
|
|
let layout = LayoutStore(session: session)
|
|
layout.dragAgentSplit(by: 100, height: 1000)
|
|
#expect(abs(layout.agentFraction - 0.62) < 0.0001)
|
|
#expect(abs(session.layout.agentFraction - 0.62) < 0.0001)
|
|
layout.dragAgentSplit(by: 5000, height: 1000)
|
|
#expect(layout.agentFraction == 0.82)
|
|
}
|
|
|
|
@Test func aWindowXMapsToTheColumnUnderIt() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
#expect(layout.panel(atX: 10) == .sourceControl)
|
|
#expect(layout.panel(atX: 300) == .explorer)
|
|
#expect(layout.panel(atX: 900) == .editor)
|
|
#expect(layout.panel(atX: 1700) == .terminal)
|
|
#expect(layout.panel(atX: 236, splitterMargin: 4) == nil)
|
|
layout.togglePane(.sourceControl)
|
|
#expect(layout.panel(atX: 10) == .explorer)
|
|
}
|
|
|
|
@Test func neighbourColumnsClampAtTheEdgesAndSkipAHiddenGitColumn() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
#expect(layout.neighbour(of: nil, step: 1) == .terminal)
|
|
#expect(layout.neighbour(of: .terminal, step: 1) == .terminal)
|
|
#expect(layout.neighbour(of: .explorer, step: -1) == .sourceControl)
|
|
layout.togglePane(.sourceControl)
|
|
#expect(layout.neighbour(of: .explorer, step: -1) == .explorer)
|
|
}
|
|
|
|
@Test func everyPaneStartsShownAndTogglesOnItsOwn() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
#expect(layout.visibility == .all)
|
|
layout.togglePane(.explorer)
|
|
layout.togglePane(.terminal)
|
|
#expect(layout.visibility == ColumnVisibility(sourceControl: true, explorer: false, editor: true, agent: false))
|
|
#expect(layout.columns == [.sourceControl, .editor])
|
|
layout.togglePane(.terminal)
|
|
#expect(layout.isVisible(.terminal))
|
|
}
|
|
|
|
@Test func hidingAFocusedPaneDropsItsFocusAndAHiddenPaneTakesNone() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.focus(.terminal)
|
|
layout.togglePane(.terminal)
|
|
#expect(layout.activePanel == nil)
|
|
layout.focus(.terminal)
|
|
#expect(layout.activePanel == nil)
|
|
layout.focus(.editor)
|
|
layout.togglePane(.explorer)
|
|
#expect(layout.activePanel == .editor)
|
|
}
|
|
|
|
@Test func allFourPaneFlagsSurviveARestart() {
|
|
let store = FakeLayouts()
|
|
let first = LayoutStore(session: MemorySession(), persistence: store)
|
|
first.restore(root: "/p")
|
|
first.togglePane(.explorer)
|
|
first.togglePane(.editor)
|
|
first.togglePane(.terminal)
|
|
|
|
let second = LayoutStore(session: MemorySession(), persistence: store)
|
|
second.restore(root: "/p")
|
|
#expect(second.visibility == ColumnVisibility(sourceControl: true, explorer: false, editor: false, agent: false))
|
|
}
|
|
|
|
@Test func anOlderSnapshotWithOnlyTheGitFlagStillLoads() throws {
|
|
let json = #"{"sourceControlWidth":240,"explorerWidth":300,"agentWidth":500,"fluid":false,"gitVisible":false}"#
|
|
let snapshot = try JSONDecoder().decode(ColumnLayoutSnapshot.self, from: Data(json.utf8))
|
|
#expect(snapshot.visibility == ColumnVisibility(sourceControl: false, explorer: true, editor: true, agent: true))
|
|
#expect(snapshot.explorerWidth == 300)
|
|
#expect(!snapshot.fluid)
|
|
}
|
|
|
|
@Test func aSnapshotRoundTripsItsPaneFlags() throws {
|
|
let visibility = ColumnVisibility(sourceControl: true, explorer: false, editor: true, agent: false)
|
|
let snapshot = ColumnLayoutSnapshot(sourceControlWidth: 236, explorerWidth: 288, agentWidth: 480, fluid: true, visibility: visibility)
|
|
let decoded = try JSONDecoder().decode(ColumnLayoutSnapshot.self, from: JSONEncoder().encode(snapshot))
|
|
#expect(decoded == snapshot)
|
|
#expect(decoded.visibility == visibility)
|
|
}
|
|
|
|
@Test func withTheEditorHiddenTheAgentColumnFillsTheRest() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
layout.togglePane(.editor)
|
|
#expect(layout.columns == [.sourceControl, .explorer, .terminal])
|
|
#expect(layout.span(of: .editor) == nil)
|
|
let agentStart = layout.widths.sourceControl + 1 + layout.widths.explorer + 1
|
|
#expect(layout.span(of: .terminal) == agentStart...1760)
|
|
#expect(layout.panel(atX: 1000) == .terminal)
|
|
}
|
|
|
|
@Test func withTheAgentHiddenTheEditorReachesTheWindowEdge() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
layout.togglePane(.terminal)
|
|
layout.togglePane(.sourceControl)
|
|
#expect(layout.span(of: .explorer) == 0...layout.widths.explorer)
|
|
#expect(layout.span(of: .editor) == (layout.widths.explorer + 1)...1760)
|
|
#expect(layout.span(of: .terminal) == nil)
|
|
#expect(layout.panel(atX: 1700) == .editor)
|
|
}
|
|
|
|
@Test func withEveryPaneHiddenNothingIsUnderThePointer() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
for panel in Panel.allCases { layout.togglePane(panel) }
|
|
#expect(layout.columns.isEmpty)
|
|
#expect(layout.panel(atX: 500) == nil)
|
|
#expect(layout.neighbour(of: .editor, step: 1) == .editor)
|
|
}
|
|
|
|
@Test func neighboursSkipEveryHiddenPane() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.togglePane(.editor)
|
|
layout.togglePane(.explorer)
|
|
#expect(layout.neighbour(of: .sourceControl, step: 1) == .terminal)
|
|
#expect(layout.neighbour(of: .terminal, step: -1) == .sourceControl)
|
|
}
|
|
|
|
@Test func theAgentMayGrowIntoTheRoomAHiddenExplorerLeaves() {
|
|
let layout = LayoutStore(session: MemorySession())
|
|
layout.windowResized(width: 1760)
|
|
layout.togglePane(.explorer)
|
|
layout.dragAgent(by: -5000)
|
|
#expect(layout.widths.agent == 1760 - 2 - layout.widths.sourceControl - ColumnLayout.minEditorWidth)
|
|
}
|
|
}
|