Files
helder/Tests/HelderUITests/State/LayoutStoreTests.swift
T

160 lines
6.0 KiB
Swift

import HelderCore
import HelderServices
@testable import HelderUI
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.toggleGit()
#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.toggleGit()
let second = LayoutStore(session: MemorySession(), persistence: store)
second.windowResized(width: 1760)
second.restore(root: "/p")
#expect(!second.fluid)
#expect(!second.gitVisible)
#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.toggleGit()
layout.restore(root: "/b")
#expect(layout.gitVisible)
layout.restore(root: "/a")
#expect(!layout.gitVisible)
}
@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.toggleGit()
#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.toggleGit()
#expect(layout.neighbour(of: .explorer, step: -1) == .explorer)
}
}