113 lines
5.3 KiB
Swift
113 lines
5.3 KiB
Swift
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)
|
|
}
|
|
}
|