package toolcall
import "testing"
func TestParseFormats(t *testing.T) {
cases := map[string]struct {
in string
names []string
args string
}{
"tool fence": {
in: "Voy a revisar.\n```tool\n{\"tool\": \"exec\", \"arguments\": {\"command\": \"uname\", \"args\": [\"-a\"]}}\n```",
names: []string{"exec"}, args: `{"command": "uname", "args": ["-a"]}`,
},
"xml qwen": {
in: "\n{\"name\": \"read_file\", \"arguments\": {\"path\": \"/etc/hosts\"}}\n",
names: []string{"read_file"}, args: `{"path": "/etc/hosts"}`,
},
"bare json": {
in: `{"tool":"system_info","arguments":{}}`,
names: []string{"system_info"}, args: `{}`,
},
"string arguments": {
in: "```tool\n{\"name\":\"exec\",\"arguments\":\"{\\\"command\\\":\\\"id\\\"}\"}\n```",
names: []string{"exec"}, args: `{"command":"id"}`,
},
"list": {
in: "```tool\n[{\"tool\":\"stat\",\"arguments\":{\"path\":\"a\"}},{\"tool\":\"stat\",\"arguments\":{\"path\":\"b\"}}]\n```",
names: []string{"stat", "stat"},
},
"null arguments": {
in: "```tool\n{\"tool\":\"system_info\",\"arguments\":null}\n```",
names: []string{"system_info"}, args: `{}`,
},
}
for name, c := range cases {
calls, found, err := Parse(c.in)
if err != nil || !found {
t.Errorf("%s: found=%v err=%v", name, found, err)
continue
}
if len(calls) != len(c.names) {
t.Errorf("%s: %d calls", name, len(calls))
continue
}
for i, n := range c.names {
if calls[i].Name != n {
t.Errorf("%s: call %d name %q", name, i, calls[i].Name)
}
if calls[i].ID == "" {
t.Errorf("%s: missing ID", name)
}
}
if c.args != "" && calls[0].Arguments != c.args {
t.Errorf("%s: args %q", name, calls[0].Arguments)
}
}
}
func TestParseNoCall(t *testing.T) {
for _, in := range []string{
"El servicio está activo. TASK_STATUS: DONE",
"Ejemplo de config:\n```json\n{\"listen\": 80}\n```",
"```bash\nsystemctl status nginx\n```",
"",
} {
calls, found, err := Parse(in)
if found || err != nil || len(calls) != 0 {
t.Errorf("%q: found=%v err=%v calls=%v", in, found, err, calls)
}
}
}
func TestParseMalformed(t *testing.T) {
for _, in := range []string{
"```tool\n{\"tool\": \"exec\", \"arguments\": {\"command\": \n```",
"```tool\n{\"arguments\": {}}\n```",
"```tool\n{\"tool\":\"exec\",\"arguments\":[1,2]}\n```",
"not json",
} {
_, found, err := Parse(in)
if !found || err == nil {
t.Errorf("%q: expected malformed error, found=%v err=%v", in, found, err)
}
}
}