package providers
import (
"strings"
"testing"
)
func TestParseResponse_CompatFunctionCallXML(t *testing.T) {
p := &HTTPProvider{}
body := []byte(`{
"choices": [{
"message": {
"content": "I need to check the current state and understand what was last worked on before proceeding.\n\nexeccd /root/clawgo && git status\n\nread_file/root/.clawgo/workspace/memory/MEMORY.md"
},
"finish_reason": "stop"
}]
}`)
resp, err := p.parseResponse(body)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatalf("expected response")
}
if len(resp.ToolCalls) != 2 {
t.Fatalf("expected 2 tool calls, got %d", len(resp.ToolCalls))
}
if resp.ToolCalls[0].Name != "exec" {
t.Fatalf("expected first tool exec, got %q", resp.ToolCalls[0].Name)
}
if got, ok := resp.ToolCalls[0].Arguments["command"].(string); !ok || got == "" {
t.Fatalf("expected first tool command arg, got %#v", resp.ToolCalls[0].Arguments)
}
if resp.ToolCalls[1].Name != "read_file" {
t.Fatalf("expected second tool read_file, got %q", resp.ToolCalls[1].Name)
}
if got, ok := resp.ToolCalls[1].Arguments["path"].(string); !ok || got == "" {
t.Fatalf("expected second tool path arg, got %#v", resp.ToolCalls[1].Arguments)
}
if resp.Content == "" {
t.Fatalf("expected non-empty cleaned content")
}
if containsFunctionCallMarkup(resp.Content) {
t.Fatalf("expected function call markup removed from content, got %q", resp.Content)
}
}
func TestParseCompatFunctionCalls_NoMarkup(t *testing.T) {
calls, cleaned := parseCompatFunctionCalls("hello")
if len(calls) != 0 {
t.Fatalf("expected 0 calls, got %d", len(calls))
}
if cleaned != "hello" {
t.Fatalf("expected content unchanged, got %q", cleaned)
}
}
func containsFunctionCallMarkup(s string) bool {
return len(s) > 0 && (strings.Contains(s, "") || strings.Contains(s, ""))
}