adds claude-generated commit messages and a recent commits panel to the git view - adds log tailing in the explorer, branch merging and a clean up button
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
import Testing
|
||||
|
||||
@Suite struct CommitMessageServiceTests {
|
||||
/// A project folder plus a `bin` folder holding a fake `claude` with the given shell body.
|
||||
private func setup(claude body: String?) throws -> (dir: TempDirectory, bin: URL) {
|
||||
let dir = try TempDirectory()
|
||||
try dir.mkdir("project")
|
||||
try dir.mkdir("bin")
|
||||
if let body {
|
||||
try dir.write("bin/claude", "#!/bin/sh\n" + body + "\n")
|
||||
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: dir.url("bin/claude").path)
|
||||
}
|
||||
return (dir, dir.url("bin"))
|
||||
}
|
||||
|
||||
private func service(_ dir: TempDirectory, bin: URL, timeout: TimeInterval = 20) -> ClaudeCommitMessageService {
|
||||
ClaudeCommitMessageService(root: dir.url("project"), locator: ExecutableLocator(directories: [bin.path]), timeout: timeout)
|
||||
}
|
||||
|
||||
@Test func sendsThePromptOnStdinInTheProjectAndCleansTheAnswer() async throws {
|
||||
let (dir, bin) = try setup(claude: """
|
||||
cat > "$(dirname "$0")/stdin.txt"
|
||||
pwd > "$(dirname "$0")/cwd.txt"
|
||||
printf '%s\\n' "$@" > "$(dirname "$0")/args.txt"
|
||||
printf '\\n "Adds a thing" \\n\\nextra line\\n'
|
||||
""")
|
||||
let message = try await service(dir, bin: bin).generate(prompt: "the prompt\nwith two lines")
|
||||
#expect(message == "Adds a thing")
|
||||
#expect(try String(contentsOf: dir.url("bin/stdin.txt"), encoding: .utf8) == "the prompt\nwith two lines")
|
||||
// The child reports /private/var/…; resolving both sides drops the /private prefix alike.
|
||||
let cwd = try String(contentsOf: dir.url("bin/cwd.txt"), encoding: .utf8).trimmingCharacters(in: .newlines)
|
||||
#expect(URL(fileURLWithPath: cwd).resolvingSymlinksInPath().path == dir.url("project").resolvingSymlinksInPath().path)
|
||||
let args = try String(contentsOf: dir.url("bin/args.txt"), encoding: .utf8)
|
||||
#expect(args == ClaudeCommitMessageService.arguments.map { $0 + "\n" }.joined())
|
||||
}
|
||||
|
||||
@Test func theRunnerGetsThePromptAsInputAndClaudesFolderFirstOnPath() async throws {
|
||||
let (dir, bin) = try setup(claude: "exit 0")
|
||||
let runner = ScriptedRunner { _ in ProcessResult(exitCode: 0, stdout: "`Fixed the parser`\n", stderr: "") }
|
||||
let service = ClaudeCommitMessageService(root: dir.url("project"), runner: runner,
|
||||
locator: ExecutableLocator(directories: [bin.path]))
|
||||
#expect(try await service.generate(prompt: "write it") == "Fixed the parser")
|
||||
let call = try #require(runner.calls.first)
|
||||
#expect(call.executable == bin.appendingPathComponent("claude").path)
|
||||
#expect(call.arguments == ClaudeCommitMessageService.arguments)
|
||||
#expect(call.input == "write it")
|
||||
#expect(call.directory == dir.url("project"))
|
||||
#expect(call.environment?["PATH"]?.hasPrefix(bin.path + ":") == true)
|
||||
}
|
||||
|
||||
@Test func theArgumentsDisableToolsAndUseTheDefaultModel() {
|
||||
let args = ClaudeCommitMessageService.arguments
|
||||
#expect(args.first == "-p")
|
||||
#expect(args.suffix(2) == ["--tools", ""])
|
||||
#expect(!args.contains("--model"))
|
||||
}
|
||||
|
||||
@Test func aFailedRunReportsItsFirstStderrLinePastWarnings() async throws {
|
||||
let (dir, bin) = try setup(claude: """
|
||||
echo "Warning: this sign-in ends soon" >&2
|
||||
echo "Invalid API key" >&2
|
||||
echo "second" >&2
|
||||
exit 1
|
||||
""")
|
||||
await #expect(throws: CommitMessageError.failed("Invalid API key")) {
|
||||
try await service(dir, bin: bin).generate(prompt: "p")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func aSilentFailureReportsTheExitCode() async throws {
|
||||
let (dir, bin) = try setup(claude: "exit 3")
|
||||
await #expect(throws: CommitMessageError.failed("claude exited with code 3")) {
|
||||
try await service(dir, bin: bin).generate(prompt: "p")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func anEmptyAnswerIsAnError() async throws {
|
||||
let (dir, bin) = try setup(claude: "cat > /dev/null; printf '\\n \\n'")
|
||||
await #expect(throws: CommitMessageError.empty) {
|
||||
try await service(dir, bin: bin).generate(prompt: "p")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func aMissingClaudeIsNotFound() async throws {
|
||||
let (dir, bin) = try setup(claude: nil)
|
||||
await #expect(throws: CommitMessageError.notFound) {
|
||||
try await service(dir, bin: bin).generate(prompt: "p")
|
||||
}
|
||||
#expect(CommitMessageError.notFound.detail == "claude not found")
|
||||
}
|
||||
|
||||
@Test func aSlowRunTimesOut() async throws {
|
||||
let (dir, bin) = try setup(claude: "exec sleep 10")
|
||||
let started = Date()
|
||||
await #expect(throws: CommitMessageError.timedOut) {
|
||||
try await service(dir, bin: bin, timeout: 0.3).generate(prompt: "p")
|
||||
}
|
||||
#expect(Date().timeIntervalSince(started) < 5)
|
||||
#expect(CommitMessageError.timedOut.detail == "timed out")
|
||||
}
|
||||
|
||||
@Test func theAgentLocatorAddsTheClaudeInstallFolders() {
|
||||
let home = FileManager.default.homeDirectoryForCurrentUser.path
|
||||
let agent = ExecutableLocator.agent.directories
|
||||
#expect(agent.contains("\(home)/.local/bin"))
|
||||
#expect(agent.contains("\(home)/.claude/local"))
|
||||
#expect(Array(agent.prefix(ExecutableLocator.standard.directories.count)) == ExecutableLocator.standard.directories)
|
||||
}
|
||||
}
|
||||
@@ -207,6 +207,34 @@ import Testing
|
||||
let service = GitService(root: worktree)
|
||||
#expect(await service.isRepository())
|
||||
}
|
||||
|
||||
@Test func recentCommitsReadHeadNewestFirst() async throws {
|
||||
let repo = try repo()
|
||||
let before = await GitService(root: repo.root).recentCommits().count
|
||||
try repo.commit(["c.txt": "c\n"], message: "add \"c\" | 100% done")
|
||||
let commits = await GitService(root: repo.root).recentCommits()
|
||||
#expect(commits.count == before + 1)
|
||||
let commit = try #require(commits.first)
|
||||
#expect(commit.subject == "add \"c\" | 100% done")
|
||||
#expect(commit.author == "Helder Tests")
|
||||
#expect(!commit.hash.isEmpty)
|
||||
#expect(abs(commit.date.timeIntervalSinceNow) < 600)
|
||||
}
|
||||
|
||||
@Test func recentCommitsStopAtThirty() async throws {
|
||||
let repo = try repo()
|
||||
for index in 0..<32 {
|
||||
try repo.commit(["n.txt": "\(index)\n"], message: "commit \(index)")
|
||||
}
|
||||
let commits = await GitService(root: repo.root).recentCommits()
|
||||
#expect(commits.count == GitCommitLogParsing.limit)
|
||||
#expect(commits.first?.subject == "commit 31")
|
||||
}
|
||||
|
||||
@Test func aRepositoryWithoutCommitsHasNoRecentCommits() async throws {
|
||||
let repo = try TempGitRepo()
|
||||
#expect(await GitService(root: repo.root).recentCommits().isEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
/// Names git would read as globs or options unless every pathspec is literal.
|
||||
@@ -232,3 +260,30 @@ import Testing
|
||||
#expect(runner.calls.allSatisfy { $0.environment?["GIT_LITERAL_PATHSPECS"] == "1" })
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct GitServiceStagedDiffTests {
|
||||
@Test func stagedDiffHoldsOnlyTheStagedChanges() async throws {
|
||||
let repo = try TempGitRepo()
|
||||
try repo.commit(["a.txt": "1\n", "b.txt": "1\n"], message: "init")
|
||||
try repo.write("a.txt", "1\nstaged\n")
|
||||
try repo.run("add", "a.txt")
|
||||
try repo.write("b.txt", "1\nunstaged\n")
|
||||
let diff = try #require(await GitService(root: repo.root).stagedDiff())
|
||||
#expect(diff.stat.contains("a.txt"))
|
||||
#expect(!diff.stat.contains("b.txt"))
|
||||
#expect(diff.patch.contains("+staged"))
|
||||
#expect(!diff.patch.contains("unstaged"))
|
||||
}
|
||||
|
||||
@Test func nothingStagedIsAnEmptyDiff() async throws {
|
||||
let repo = try TempGitRepo()
|
||||
try repo.commit(["a.txt": "1\n"], message: "init")
|
||||
let diff = try #require(await GitService(root: repo.root).stagedDiff())
|
||||
#expect(diff.isEmpty)
|
||||
}
|
||||
|
||||
@Test func outsideARepositoryThereIsNoDiff() async throws {
|
||||
let dir = try TempDirectory()
|
||||
#expect(await GitService(root: dir.root).stagedDiff() == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,30 @@ import Testing
|
||||
#expect(result == ProcessResult(exitCode: 0, stdout: "", stderr: ""))
|
||||
}
|
||||
|
||||
@Test func inputReachesStdinAndThenEnds() async throws {
|
||||
let result = try await runner.run("cat", arguments: [], in: nil, environment: nil, input: "héllo\nworld")
|
||||
#expect(result == ProcessResult(exitCode: 0, stdout: "héllo\nworld", stderr: ""))
|
||||
}
|
||||
|
||||
@Test func largeInputIsWrittenWhole() async throws {
|
||||
let input = String(repeating: "x", count: 1_000_000)
|
||||
let result = try await runner.run("wc", arguments: ["-c"], in: nil, environment: nil, input: input)
|
||||
#expect(result.stdout.trimmingCharacters(in: .whitespacesAndNewlines) == "1000000")
|
||||
}
|
||||
|
||||
@Test func aChildThatIgnoresItsInputStillFinishes() async throws {
|
||||
let input = String(repeating: "x", count: 1_000_000)
|
||||
let result = try await runner.run("true", arguments: [], in: nil, environment: nil, input: input)
|
||||
#expect(result.exitCode == 0)
|
||||
}
|
||||
|
||||
@Test func aRunnerWithoutStdinSupportRefusesInput() async throws {
|
||||
let fake = CannedRunner(result: ProcessResult(exitCode: 0, stdout: "", stderr: ""))
|
||||
await #expect(throws: ProcessRunnerError.self) {
|
||||
try await fake.run("x", arguments: [], in: nil, environment: nil, input: "prompt")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func largeOutputOnBothPipesDoesNotDeadlock() async throws {
|
||||
let script = "i=0; while [ $i -lt 20000 ]; do echo line$i; echo err$i >&2; i=$((i+1)); done"
|
||||
let result = try await runner.run("sh", ["-c", script])
|
||||
|
||||
@@ -22,6 +22,8 @@ struct RecordedCall: Sendable, Equatable {
|
||||
var executable: String
|
||||
var arguments: [String]
|
||||
var environment: [String: String]?
|
||||
var directory: URL? = nil
|
||||
var input: String? = nil
|
||||
}
|
||||
|
||||
/// Records every call and answers from a script; for tests of argument construction and error paths.
|
||||
@@ -39,7 +41,14 @@ final class ScriptedRunner: ProcessRunning, @unchecked Sendable {
|
||||
}
|
||||
|
||||
func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?) async throws -> ProcessResult {
|
||||
let call = RecordedCall(executable: executable, arguments: arguments, environment: environment)
|
||||
let call = RecordedCall(executable: executable, arguments: arguments, environment: environment, directory: directory)
|
||||
lock.withLock { recorded.append(call) }
|
||||
return try await respond(call)
|
||||
}
|
||||
|
||||
func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?,
|
||||
input: String) async throws -> ProcessResult {
|
||||
let call = RecordedCall(executable: executable, arguments: arguments, environment: environment, directory: directory, input: input)
|
||||
lock.withLock { recorded.append(call) }
|
||||
return try await respond(call)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user