mirror of
https://github.com/samsonjs/vibetunnel.git
synced 2026-07-01 05:59:37 +00:00
200 lines
No EOL
7.1 KiB
Swift
200 lines
No EOL
7.1 KiB
Swift
import XCTest
|
|
@testable import VibeTunnel
|
|
|
|
final class TerminalLaunchTests: XCTestCase {
|
|
|
|
/// Test URL generation for each terminal type
|
|
func testTerminalURLGeneration() {
|
|
let testCases: [(Terminal, String, String?)] = [
|
|
// iTerm2 URL scheme tests
|
|
(.iTerm2, "echo 'Hello World'", "iterm2://run?command=echo%20%27Hello%20World%27"),
|
|
(.iTerm2, "cd /tmp && ls", "iterm2://run?command=cd%20%2Ftmp%20%26%26%20ls"),
|
|
|
|
// Other terminals don't support URL schemes
|
|
(.terminal, "echo test", nil),
|
|
(.alacritty, "echo test", nil),
|
|
(.hyper, "echo test", nil),
|
|
(.wezterm, "echo test", nil),
|
|
]
|
|
|
|
for (terminal, command, expectedURL) in testCases {
|
|
if let url = terminal.commandURL(for: command) {
|
|
XCTAssertEqual(url.absoluteString, expectedURL)
|
|
} else {
|
|
XCTAssertNil(expectedURL)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Test command argument generation for terminals
|
|
func testCommandArgumentGeneration() {
|
|
let command = "echo 'Hello World'"
|
|
|
|
// Test Alacritty arguments
|
|
let alacrittyArgs = Terminal.alacritty.commandArguments(for: command)
|
|
XCTAssertEqual(alacrittyArgs, ["-e", "/bin/bash", "-c", command])
|
|
|
|
// Test WezTerm arguments
|
|
let weztermArgs = Terminal.wezterm.commandArguments(for: command)
|
|
XCTAssertEqual(weztermArgs, ["start", "--", "/bin/bash", "-c", command])
|
|
|
|
// Test Terminal.app (limited support)
|
|
let terminalArgs = Terminal.terminal.commandArguments(for: command)
|
|
XCTAssertEqual(terminalArgs, [])
|
|
}
|
|
|
|
/// Test working directory support
|
|
func testWorkingDirectorySupport() {
|
|
let workDir = "/Users/test/projects"
|
|
let command = "ls -la"
|
|
|
|
// Alacritty with working directory
|
|
let alacrittyArgs = Terminal.alacritty.commandArguments(
|
|
for: command,
|
|
workingDirectory: workDir
|
|
)
|
|
XCTAssertEqual(alacrittyArgs, [
|
|
"--working-directory", workDir,
|
|
"-e", "/bin/bash", "-c", command
|
|
])
|
|
|
|
// WezTerm with working directory
|
|
let weztermArgs = Terminal.wezterm.commandArguments(
|
|
for: command,
|
|
workingDirectory: workDir
|
|
)
|
|
XCTAssertEqual(weztermArgs, [
|
|
"start", "--cwd", workDir,
|
|
"--", "/bin/bash", "-c", command
|
|
])
|
|
|
|
// iTerm2 URL with working directory
|
|
if let url = Terminal.iTerm2.commandURL(for: command, workingDirectory: workDir) {
|
|
XCTAssertTrue(url.absoluteString.contains("cd="))
|
|
XCTAssertTrue(url.absoluteString.contains(workDir.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""))
|
|
}
|
|
}
|
|
|
|
/// Test complex command encoding
|
|
func testComplexCommandEncoding() {
|
|
let complexCommand = "git log --oneline -10 && echo 'Done!'"
|
|
|
|
// Test iTerm2 URL encoding
|
|
if let url = Terminal.iTerm2.commandURL(for: complexCommand) {
|
|
let expectedEncoded = "git%20log%20--oneline%20-10%20%26%26%20echo%20%27Done%21%27"
|
|
XCTAssertTrue(url.absoluteString.contains(expectedEncoded))
|
|
}
|
|
|
|
// Test argument generation doesn't break the command
|
|
let alacrittyArgs = Terminal.alacritty.commandArguments(for: complexCommand)
|
|
XCTAssertEqual(alacrittyArgs.last, complexCommand)
|
|
}
|
|
|
|
/// Test terminal detection
|
|
func testTerminalDetection() {
|
|
// At least Terminal.app should be available on macOS
|
|
XCTAssertTrue(Terminal.installed.contains(.terminal))
|
|
|
|
// Check that installed terminals have valid paths
|
|
for terminal in Terminal.installed {
|
|
// Check if terminal is installed
|
|
XCTAssertNotNil(NSWorkspace.shared.urlForApplication(withBundleIdentifier: terminal.bundleIdentifier))
|
|
}
|
|
}
|
|
|
|
/// Test launching with environment variables
|
|
@MainActor
|
|
func testEnvironmentVariables() {
|
|
let _ = ["MY_VAR": "test_value", "PATH": "/custom/path:/usr/bin"]
|
|
let _ = "echo $MY_VAR"
|
|
|
|
// Test that environment variables can be passed
|
|
let _ = TerminalLauncher.shared
|
|
|
|
// This would need to be implemented in TerminalLauncher
|
|
// Just testing the concept here
|
|
XCTAssertNoThrow({
|
|
// In real implementation:
|
|
// try launcher.launchCommand(command, environment: env)
|
|
})
|
|
}
|
|
|
|
/// Test script file execution
|
|
func testScriptFileExecution() throws {
|
|
let tempDir = FileManager.default.temporaryDirectory
|
|
let scriptPath = tempDir.appendingPathComponent("test_script.sh")
|
|
|
|
// Create a test script
|
|
let scriptContent = """
|
|
#!/bin/bash
|
|
echo "Test script executed"
|
|
pwd
|
|
"""
|
|
try scriptContent.write(to: scriptPath, atomically: true, encoding: .utf8)
|
|
|
|
// Make executable
|
|
try FileManager.default.setAttributes(
|
|
[.posixPermissions: 0o755],
|
|
ofItemAtPath: scriptPath.path
|
|
)
|
|
|
|
// Test launching the script
|
|
// let launcher = TerminalLauncher.shared // Needs @MainActor
|
|
XCTAssertNoThrow({
|
|
// launchScript method not available
|
|
// try launcher.launchScript(at: scriptPath.path)
|
|
XCTAssertTrue(FileManager.default.fileExists(atPath: scriptPath.path))
|
|
})
|
|
|
|
// Cleanup
|
|
try? FileManager.default.removeItem(at: scriptPath)
|
|
}
|
|
}
|
|
|
|
// MARK: - Terminal Extension Tests
|
|
|
|
extension Terminal {
|
|
/// Generate command arguments for testing
|
|
/// This would be implemented in the actual Terminal enum
|
|
func commandArguments(for command: String, workingDirectory: String? = nil) -> [String] {
|
|
switch self {
|
|
case .alacritty:
|
|
var args: [String] = []
|
|
if let workDir = workingDirectory {
|
|
args += ["--working-directory", workDir]
|
|
}
|
|
args += ["-e", "/bin/bash", "-c", command]
|
|
return args
|
|
|
|
case .wezterm:
|
|
var args = ["start"]
|
|
if let workDir = workingDirectory {
|
|
args += ["--cwd", workDir]
|
|
}
|
|
args += ["--", "/bin/bash", "-c", command]
|
|
return args
|
|
|
|
default:
|
|
return []
|
|
}
|
|
}
|
|
|
|
/// Generate URL for terminals that support URL schemes
|
|
func commandURL(for command: String, workingDirectory: String? = nil) -> URL? {
|
|
switch self {
|
|
case .iTerm2:
|
|
var components = URLComponents(string: "iterm2://run")
|
|
var queryItems = [
|
|
URLQueryItem(name: "command", value: command)
|
|
]
|
|
if let workDir = workingDirectory {
|
|
queryItems.append(URLQueryItem(name: "cd", value: workDir))
|
|
}
|
|
components?.queryItems = queryItems
|
|
return components?.url
|
|
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
} |