309 lines
13 KiB
Swift
309 lines
13 KiB
Swift
import Foundation
|
|
import HelderCore
|
|
@testable import HelderUI
|
|
import Testing
|
|
|
|
@MainActor
|
|
@Suite struct TerminalStoreSessionTests {
|
|
private let guiEnvironment = [
|
|
"SHELL": "/bin/zsh", "HOME": "/Users/me", "PATH": "/usr/bin:/bin",
|
|
"npm_config_prefix": "/opt/homebrew",
|
|
]
|
|
|
|
@Test func theAgentPaneRunsTheConfiguredCommandThroughAnInteractiveShellInTheProject() {
|
|
let store = TerminalStore(projectRoot: "/code/app", environment: guiEnvironment)
|
|
store.config.ai.command = "claude --continue"
|
|
let launch = store.launch(for: .agent)
|
|
#expect(launch.executable == "/bin/zsh")
|
|
#expect(launch.arguments == ["-i", "-c", "claude --continue"])
|
|
#expect(launch.workingDirectory == "/code/app")
|
|
}
|
|
|
|
@Test func theAgentPaneIsAPlainShellWhenAutoLaunchIsOff() {
|
|
let store = TerminalStore(projectRoot: "/code/app", environment: guiEnvironment)
|
|
store.config.ai.autoLaunch = false
|
|
#expect(store.launch(for: .agent).arguments.isEmpty)
|
|
}
|
|
|
|
@Test func theShellPaneUsesTheConfiguredShellOverTheLoginOne() {
|
|
let store = TerminalStore(projectRoot: "/code/app", environment: guiEnvironment)
|
|
#expect(store.launch(for: .shell).executable == "/bin/zsh")
|
|
store.config.terminal.shell = "/opt/homebrew/bin/fish"
|
|
let launch = store.launch(for: .shell)
|
|
#expect(launch.executable == "/opt/homebrew/bin/fish")
|
|
#expect(launch.arguments.isEmpty)
|
|
}
|
|
|
|
@Test func theChildEnvironmentGetsAUTF8LocaleTruecolorAndNoNpmPrefix() {
|
|
let env = TerminalStore(projectRoot: "/p", environment: guiEnvironment).launch(for: .shell).environment
|
|
#expect(env["TERM"] == "xterm-256color")
|
|
#expect(env["COLORTERM"] == "truecolor")
|
|
#expect(env["LANG"] == "en_US.UTF-8")
|
|
#expect(env["npm_config_prefix"] == nil)
|
|
#expect(env["PATH"] == "/usr/bin:/bin")
|
|
}
|
|
|
|
@Test func withoutAProjectThePanesStartInTheHomeFolder() {
|
|
let store = TerminalStore(projectRoot: nil, environment: guiEnvironment)
|
|
#expect(store.launch(for: .shell).workingDirectory == "/Users/me")
|
|
}
|
|
|
|
@Test func everyPaneStartsAsALoginShellSoTheProfileRepairsPath() {
|
|
#expect(TerminalStore.loginArgv0(for: "/bin/zsh") == "-zsh")
|
|
#expect(TerminalStore.loginArgv0(for: "/opt/homebrew/bin/fish") == "-fish")
|
|
}
|
|
|
|
@Test func startingAPaneAttachesItAndSpawnsItsLaunch() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
#expect(store.agent === agent)
|
|
#expect(agent.starts.count == 1)
|
|
#expect(agent.starts.first?.launch == store.launch(for: .agent))
|
|
#expect(agent.starts.first?.argv0 == "-zsh")
|
|
}
|
|
|
|
@Test func panesWaitUntilTheProjectConfigArrives() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
#expect(!store.isConfigured)
|
|
store.config = HelderConfig.defaults
|
|
#expect(store.isConfigured)
|
|
}
|
|
|
|
@Test func anExitIsReportedPerPane() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
store.start(.shell, on: FakeTerminal())
|
|
store.processExited(.shell, exitCode: 1)
|
|
#expect(store.hasExited(.shell))
|
|
#expect(!store.hasExited(.agent))
|
|
}
|
|
|
|
@Test func restartKillsTheOldProcessAndSpawnsAFreshOne() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.agentReceivedInput("half typed")
|
|
store.processExited(.agent, exitCode: 0)
|
|
store.restart(.agent)
|
|
#expect(agent.terminations == 1)
|
|
#expect(agent.starts.count == 2)
|
|
#expect(!store.agentExited)
|
|
#expect(!store.agentComposerHasText)
|
|
}
|
|
|
|
@Test func restartWithoutAPaneDoesNothing() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
store.restart(.shell)
|
|
#expect(store.shell == nil)
|
|
}
|
|
|
|
@Test func aMultiLinePasteIsWrappedAsOneBracketedPasteAndNeverSubmits() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.insertIntoAgent(text: "fix this\n```\nlet x = 1\n```")
|
|
#expect(agent.sent == ["\u{1b}[200~fix this\n```\nlet x = 1\n```\u{1b}[201~"])
|
|
#expect(agent.sent.allSatisfy { !$0.hasSuffix("\r") })
|
|
}
|
|
|
|
@Test func aPasteAfterTypingStartsOnAFreshLine() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.agentReceivedInput("h")
|
|
store.insertIntoAgent(text: "a.php:3")
|
|
#expect(agent.sent == ["\u{1b}[200~\na.php:3\u{1b}[201~"])
|
|
}
|
|
|
|
@Test func controlSequencesDoNotCountAsComposerText() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
store.start(.agent, on: FakeTerminal())
|
|
store.agentReceivedInput("\u{1b}[A")
|
|
#expect(!store.agentComposerHasText)
|
|
}
|
|
|
|
@Test func closingTheWindowTerminatesBothPanes() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
let shell = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.start(.shell, on: shell)
|
|
store.terminateAll()
|
|
#expect(agent.terminations == 1)
|
|
#expect(shell.terminations == 1)
|
|
#expect(store.agent == nil && store.shell == nil)
|
|
}
|
|
|
|
@Test func aClosingWindowReleasesItsPaneOnceEvenAfterTerminateAll() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
let shell = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.start(.shell, on: shell)
|
|
store.release(agent)
|
|
#expect(agent.terminations == 1)
|
|
#expect(store.agent == nil)
|
|
store.terminateAll()
|
|
store.release(shell)
|
|
#expect(agent.terminations == 1)
|
|
#expect(shell.terminations == 1)
|
|
}
|
|
|
|
@Test func pastingIntoTheAgentMarksItsComposerAsFilled() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let agent = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.paste("hello", into: .agent)
|
|
store.paste("", into: .agent)
|
|
#expect(agent.pasted == ["hello"])
|
|
#expect(store.agentComposerHasText)
|
|
}
|
|
|
|
@Test func anEmptySelectionReadsAsNone() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: guiEnvironment)
|
|
let shell = FakeTerminal()
|
|
store.start(.shell, on: shell)
|
|
#expect(store.selection(.shell) == nil)
|
|
shell.selectedText = ""
|
|
#expect(store.selection(.shell) == nil)
|
|
shell.selectedText = "ls -la"
|
|
#expect(store.selection(.shell) == "ls -la")
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite struct TerminalStoreTailTests {
|
|
private let environment = ["SHELL": "/bin/zsh", "HOME": "/Users/me"]
|
|
|
|
@Test func aTailRunsTailOnThePathInTheProjectWithoutALoginShell() {
|
|
let store = TerminalStore(projectRoot: "/code/app", environment: environment)
|
|
store.startTail("storage/logs/laravel.log")
|
|
let tail = FakeTerminal()
|
|
store.start(.tail, on: tail)
|
|
#expect(store.tailedPath == "storage/logs/laravel.log")
|
|
#expect(store.tail === tail)
|
|
#expect(tail.starts.first?.launch.executable == "/usr/bin/tail")
|
|
#expect(tail.starts.first?.launch.arguments == ["-f", "storage/logs/laravel.log"])
|
|
#expect(tail.starts.first?.launch.workingDirectory == "/code/app")
|
|
#expect(tail.starts.first?.argv0 == "tail")
|
|
}
|
|
|
|
@Test func withoutATailedPathThePaneSpawnsNothing() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
|
let tail = FakeTerminal()
|
|
store.start(.tail, on: tail)
|
|
#expect(tail.starts.isEmpty)
|
|
#expect(store.tail == nil)
|
|
}
|
|
|
|
@Test func tailingAnotherFileTerminatesTheOldTailAndAsksForAFreshPane() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
|
store.startTail("a.log")
|
|
let first = FakeTerminal()
|
|
store.start(.tail, on: first)
|
|
let generation = store.tailGeneration
|
|
store.startTail("b.log")
|
|
#expect(first.terminations == 1)
|
|
#expect(store.tail == nil)
|
|
#expect(store.tailedPath == "b.log")
|
|
#expect(store.tailGeneration != generation)
|
|
let second = FakeTerminal()
|
|
store.start(.tail, on: second)
|
|
#expect(second.starts.first?.launch.arguments == ["-f", "b.log"])
|
|
#expect(first.starts.count == 1)
|
|
}
|
|
|
|
@Test func closeTerminatesTheTailAndRemovesTheBlock() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
|
store.startTail("a.log")
|
|
let tail = FakeTerminal()
|
|
store.start(.tail, on: tail)
|
|
store.closeTail()
|
|
#expect(tail.terminations == 1)
|
|
#expect(store.tail == nil)
|
|
#expect(store.tailedPath == nil)
|
|
}
|
|
|
|
@Test func terminateAllEndsTheTailToo() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
|
let agent = FakeTerminal(), shell = FakeTerminal(), tail = FakeTerminal()
|
|
store.start(.agent, on: agent)
|
|
store.start(.shell, on: shell)
|
|
store.startTail("a.log")
|
|
store.start(.tail, on: tail)
|
|
store.terminateAll()
|
|
#expect([agent, shell, tail].allSatisfy { $0.terminations == 1 })
|
|
#expect(store.tail == nil)
|
|
#expect(store.tailedPath == nil)
|
|
store.release(tail)
|
|
#expect(tail.terminations == 1)
|
|
}
|
|
|
|
@Test func aTailExitIsReportedOnItsOwn() {
|
|
let store = TerminalStore(projectRoot: "/p", environment: environment)
|
|
store.startTail("a.log")
|
|
store.start(.tail, on: FakeTerminal())
|
|
store.processExited(.tail, exitCode: 1)
|
|
#expect(store.hasExited(.tail))
|
|
#expect(!store.hasExited(.shell))
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite struct TerminalPseudoTerminalTests {
|
|
@Test(.tags(.realProcess), .timeLimit(.minutes(1)))
|
|
func aRealChildRunsInAPseudoTerminalAndItsExitIsReported() async throws {
|
|
let store = TerminalStore(projectRoot: NSTemporaryDirectory(), environment: ["PATH": "/usr/bin:/bin"])
|
|
let view = HelderTerminalView(kind: .shell, store: store)
|
|
store.attach(view, as: .shell)
|
|
view.start(TerminalLaunch(executable: "/bin/echo", arguments: ["helder-smoke"],
|
|
environment: ["PATH": "/usr/bin:/bin", "TERM": "xterm-256color"],
|
|
workingDirectory: "/tmp"),
|
|
argv0: "echo")
|
|
let screen = { String(decoding: view.terminal.getBufferAsData(), as: UTF8.self) }
|
|
#expect(await eventually(timeout: .seconds(20)) { screen().contains("helder-smoke") })
|
|
#expect(await eventually(timeout: .seconds(20)) { store.shellExited })
|
|
#expect(await eventually { screen().contains("[process exited]") })
|
|
view.terminate()
|
|
}
|
|
|
|
@Test(.tags(.realProcess), .timeLimit(.minutes(1)))
|
|
func closingThePanesReapsTheChildSoNoZombieIsLeft() async throws {
|
|
let store = TerminalStore(projectRoot: NSTemporaryDirectory(), environment: ["PATH": "/usr/bin:/bin"])
|
|
let view = HelderTerminalView(kind: .shell, store: store)
|
|
store.attach(view, as: .shell)
|
|
view.start(TerminalLaunch(executable: "/bin/sleep", arguments: ["30"],
|
|
environment: ["PATH": "/usr/bin:/bin"], workingDirectory: "/tmp"),
|
|
argv0: "sleep")
|
|
let pid = try #require(view.processID)
|
|
#expect(kill(pid, 0) == 0)
|
|
store.terminateAll()
|
|
#expect(await eventually(timeout: .seconds(20)) { kill(pid, 0) == -1 && errno == ESRCH })
|
|
}
|
|
|
|
@Test(.tags(.realProcess), .timeLimit(.minutes(1)))
|
|
func restartingAPaneReapsTheOldChild() async throws {
|
|
let store = TerminalStore(projectRoot: NSTemporaryDirectory(), environment: ["PATH": "/usr/bin:/bin", "SHELL": "/bin/sh"])
|
|
store.config.ai.autoLaunch = false
|
|
let view = HelderTerminalView(kind: .shell, store: store)
|
|
store.start(.shell, on: view)
|
|
let first = try #require(view.processID)
|
|
store.restart(.shell)
|
|
let second = try #require(view.processID)
|
|
#expect(first != second)
|
|
#expect(await eventually(timeout: .seconds(20)) { kill(first, 0) == -1 && errno == ESRCH })
|
|
store.terminateAll()
|
|
#expect(await eventually(timeout: .seconds(20)) { kill(second, 0) == -1 && errno == ESRCH })
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite struct TerminalZoomTests {
|
|
@Test func theTerminalFontFollowsTheWindowZoom() {
|
|
var settings = HelderConfig.defaults.terminal
|
|
settings.fontSize = 10
|
|
#expect(TerminalAppearance.font(settings).pointSize == 10)
|
|
#expect(abs(TerminalAppearance.font(settings, scale: 1.2).pointSize - 12) < 0.001)
|
|
}
|
|
}
|