Files
odidere/internal/tool/tool_test.go

147 lines
3.1 KiB
Go
Raw Normal View History

2026-02-13 15:02:07 +00:00
package tool
import (
"testing"
"time"
2026-06-19 14:31:41 +00:00
"code.chimeric.al/chimerical/odidere/internal/config"
2026-02-13 15:02:07 +00:00
)
2026-06-19 14:31:41 +00:00
func TestTool_OpenAI(t *testing.T) {
tool := &Tool{
Name: "test_tool",
Description: "test description",
Parameters: map[string]any{"type": "object"},
}
openaiTool := tool.OpenAI()
if openaiTool.Function.Name != "test_tool" {
t.Errorf("expected name test_tool, got %s", openaiTool.Function.Name)
}
}
func TestTool_ParseArguments(t *testing.T) {
tool := &Tool{
Arguments: []string{"--name {{.name}}", "{{if .flag}}--enabled{{end}}"},
}
tests := []struct {
name string
args string
expected []string
}{
{
name: "basic",
args: `{"name": "test"}`,
expected: []string{"--name test"},
},
{
name: "with flag",
args: `{"name": "test", "flag": true}`,
expected: []string{"--name test", "--enabled"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tool.ParseArguments(tt.args)
if err != nil {
t.Fatalf("ParseArguments failed: %v", err)
}
if len(got) != len(tt.expected) {
t.Errorf("expected len %d, got %d", len(tt.expected), len(got))
}
for i := range got {
if got[i] != tt.expected[i] {
t.Errorf("expected %q, got %q", tt.expected[i], got[i])
}
}
})
}
}
func TestNewTool(t *testing.T) {
2026-02-13 15:02:07 +00:00
tests := []struct {
name string
2026-06-19 14:31:41 +00:00
cfg config.ToolConfig
2026-02-13 15:02:07 +00:00
wantErr bool
}{
{
2026-06-19 14:31:41 +00:00
name: "valid",
cfg: config.ToolConfig{
2026-02-13 15:02:07 +00:00
Name: "test",
2026-06-19 14:31:41 +00:00
Description: "desc",
Command: "ls",
Timeout: "10s",
2026-02-13 15:02:07 +00:00
},
2026-06-19 14:31:41 +00:00
wantErr: false,
2026-02-13 15:02:07 +00:00
},
{
name: "invalid timeout",
2026-06-19 14:31:41 +00:00
cfg: config.ToolConfig{
2026-02-13 15:02:07 +00:00
Name: "test",
2026-06-19 14:31:41 +00:00
Description: "desc",
Command: "ls",
2026-02-13 15:02:07 +00:00
Timeout: "invalid",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2026-06-19 14:31:41 +00:00
tool, err := NewTool(tt.cfg)
2026-02-13 15:02:07 +00:00
if (err != nil) != tt.wantErr {
2026-06-19 14:31:41 +00:00
t.Errorf("NewTool() error = %v, wantErr %v", err, tt.wantErr)
}
if err == nil {
if tool.Name != tt.cfg.Name {
t.Errorf("expected name %s, got %s", tt.cfg.Name, tool.Name)
}
if tt.cfg.Timeout == "10s" && tool.timeout != 10*time.Second {
t.Errorf("expected timeout 10s, got %v", tool.timeout)
}
2026-02-13 15:02:07 +00:00
}
})
}
}
2026-06-19 14:31:41 +00:00
func TestRegistry_NewRegistry(t *testing.T) {
2026-02-13 15:02:07 +00:00
tests := []struct {
name string
2026-06-19 14:31:41 +00:00
tools []config.ToolConfig
2026-02-13 15:02:07 +00:00
wantErr bool
}{
{
2026-06-19 14:31:41 +00:00
name: "valid",
tools: []config.ToolConfig{
{Name: "t1", Description: "d1", Command: "c1"},
{Name: "t2", Description: "d2", Command: "c2"},
2026-02-13 15:02:07 +00:00
},
wantErr: false,
},
{
2026-06-19 14:31:41 +00:00
name: "duplicate",
tools: []config.ToolConfig{
{Name: "t1", Description: "d1", Command: "c1"},
{Name: "t1", Description: "d2", Command: "c2"},
2026-02-13 15:02:07 +00:00
},
wantErr: true,
},
{
name: "invalid tool",
2026-06-19 14:31:41 +00:00
tools: []config.ToolConfig{
{Name: "t1", Description: "d1", Command: "c1", Timeout: "invalid"},
2026-02-13 15:02:07 +00:00
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewRegistry(tt.tools)
if (err != nil) != tt.wantErr {
2026-06-19 14:31:41 +00:00
t.Errorf("NewRegistry() error = %v, wantErr %v", err, tt.wantErr)
2026-02-13 15:02:07 +00:00
}
})
}
}