Refactor tool output format

This commit is contained in:
dwrz
2026-07-08 11:52:45 +00:00
parent 6592bae15d
commit 6c4b74fd6d
3 changed files with 285 additions and 33 deletions

View File

@@ -1,6 +1,9 @@
package llm
import (
"context"
"os/exec"
"strings"
"testing"
"time"
@@ -201,3 +204,155 @@ func TestTool_ToAPI(t *testing.T) {
t.Error("parameters is nil")
}
}
func TestToolResult_FormatResult(t *testing.T) {
// Get real ProcessState values from actual commands.
cmdOK := exec.Command("sh", "-c", "exit 0")
if err := cmdOK.Run(); err != nil {
t.Fatalf("cmdOK: %v", err)
}
cmdFail := exec.Command("sh", "-c", "exit 3")
cmdFail.Run() // expect non-nil error
tests := []struct {
name string
result ToolResult
wantContains []string
}{
{
name: "success with stdout",
result: ToolResult{Stdout: "hello\n", Stderr: "", ProcessState: cmdOK.ProcessState},
wantContains: []string{"▸ stdout:", "hello", "▸ stderr:", "┗ exit: 0"},
},
{
name: "failure with stderr",
result: ToolResult{Stdout: "", Stderr: "error occurred\n", ProcessState: cmdFail.ProcessState},
wantContains: []string{"▸ stdout:", "▸ stderr:", "error occurred", "┗ exit: 3"},
},
{
name: "both stdout and stderr",
result: ToolResult{Stdout: "output", Stderr: "warning", ProcessState: cmdOK.ProcessState},
wantContains: []string{"▸ stdout:", "output", "▸ stderr:", "warning", "┗ exit: 0"},
},
{
name: "empty output",
result: ToolResult{Stdout: "", Stderr: "", ProcessState: cmdOK.ProcessState},
wantContains: []string{"▸ stdout:", "▸ stderr:", "┗ exit: 0"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.result.FormatResult()
for _, want := range tt.wantContains {
if !strings.Contains(got, want) {
t.Errorf("FormatResult() = %q, want to contain %q", got, want)
}
}
})
}
}
func TestRegistry_Execute_Success(t *testing.T) {
reg, err := NewRegistry([]config.ToolConfig{
{Name: "echo", Description: "echo", Command: "echo", Arguments: []string{"hello"}},
})
if err != nil {
t.Fatalf("NewRegistry: %v", err)
}
result, err := reg.Execute(context.Background(), "echo", "{}")
if err != nil {
t.Fatalf("Execute: %v", err)
}
if result.ProcessState.ExitCode() != 0 {
t.Errorf("ExitCode = %d, want 0", result.ProcessState.ExitCode())
}
if !strings.Contains(result.Stdout, "hello") {
t.Errorf("Stdout = %q, want to contain hello", result.Stdout)
}
}
func TestRegistry_Execute_NonZeroExit(t *testing.T) {
reg, err := NewRegistry([]config.ToolConfig{
{Name: "fail", Description: "fail", Command: "sh", Arguments: []string{"-c", "exit 42"}},
})
if err != nil {
t.Fatalf("NewRegistry: %v", err)
}
result, err := reg.Execute(context.Background(), "fail", "{}")
if err != nil {
t.Fatalf("Execute: %v", err)
}
if result.ProcessState.ExitCode() != 42 {
t.Errorf("ExitCode = %d, want 42", result.ProcessState.ExitCode())
}
}
func TestRegistry_Execute_UnknownCommand(t *testing.T) {
reg, err := NewRegistry([]config.ToolConfig{
{Name: "nope", Description: "nope", Command: "nonexistent_binary_xyz", Arguments: []string{}},
})
if err != nil {
t.Fatalf("NewRegistry: %v", err)
}
_, err = reg.Execute(context.Background(), "nope", "{}")
if err == nil {
t.Fatal("Execute: want error, got nil")
}
}
func TestRegistry_Execute_UnknownTool(t *testing.T) {
reg, err := NewRegistry([]config.ToolConfig{})
if err != nil {
t.Fatalf("NewRegistry: %v", err)
}
_, err = reg.Execute(context.Background(), "missing", "{}")
if err == nil {
t.Fatal("Execute: want error, got nil")
}
if !strings.Contains(err.Error(), "unknown tool") {
t.Errorf("err = %v, want unknown tool error", err)
}
}
func TestRegistry_Execute_Timeout(t *testing.T) {
reg, err := NewRegistry([]config.ToolConfig{
{Name: "sleep", Description: "sleep", Command: "sleep", Arguments: []string{"10"}, Timeout: "50ms"},
})
if err != nil {
t.Fatalf("NewRegistry: %v", err)
}
_, err = reg.Execute(context.Background(), "sleep", "{}")
if err == nil {
t.Fatal("Execute: want timeout error, got nil")
}
if !strings.Contains(err.Error(), "timed out") {
t.Errorf("err = %v, want timed out error", err)
}
}
func TestRegistry_Execute_SignalKilled(t *testing.T) {
reg, err := NewRegistry([]config.ToolConfig{
{Name: "kill", Description: "kill", Command: "sh", Arguments: []string{"-c", "sleep 100 & kill -9 $!; wait"}},
})
if err != nil {
t.Fatalf("NewRegistry: %v", err)
}
result, err := reg.Execute(context.Background(), "kill", "{}")
if err != nil {
t.Fatalf("Execute: %v", err)
}
// The process we kill exits with signal, but the shell itself may exit 0.
// On some systems the exit code is 137 (128+9). Check ProcessState for signal info.
if result.ProcessState.ExitCode() == -1 {
if !strings.Contains(result.ProcessState.String(), "signal:") {
t.Errorf("ProcessState.String() = %q, want signal info", result.ProcessState.String())
}
}
}