rony-llm-agent/pkg/tools/registry_test.go

115 lines
2.3 KiB
Go
Raw Permalink Normal View History

package tools
import (
"fmt"
"sync"
"testing"
)
func TestNewRegistry(t *testing.T) {
reg := NewRegistry()
if reg == nil {
t.Fatal("expected non-nil registry")
}
if len(reg.List()) != 0 {
t.Errorf("expected empty registry, got %d tools", len(reg.List()))
}
}
func TestRegisterAndGet(t *testing.T) {
reg := NewRegistry()
tool := Tool{Name: "test", Description: "test tool"}
if err := reg.Register(tool); err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, ok := reg.Get("test")
if !ok {
t.Fatal("expected tool to be found")
}
if got.Name != "test" {
t.Errorf("expected name 'test', got %q", got.Name)
}
}
func TestRegisterDuplicate(t *testing.T) {
reg := NewRegistry()
tool := Tool{Name: "test"}
if err := reg.Register(tool); err != nil {
t.Fatalf("unexpected error on first register: %v", err)
}
if err := reg.Register(tool); err == nil {
t.Error("expected error for duplicate tool")
}
}
func TestList(t *testing.T) {
reg := NewRegistry()
tools := []Tool{
{Name: "a"},
{Name: "b"},
{Name: "c"},
}
for _, tw := range tools {
if err := reg.Register(tw); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
list := reg.List()
if len(list) != 3 {
t.Errorf("expected 3 tools, got %d", len(list))
}
}
func TestFilter(t *testing.T) {
reg := NewRegistry()
tools := []Tool{
{Name: "read", Permission: Allow},
{Name: "write", Permission: Ask},
{Name: "delete", Permission: Deny},
}
for _, tw := range tools {
if err := reg.Register(tw); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
allowed := reg.Filter(func(t Tool) bool {
return t.Permission == Allow
})
if len(allowed) != 1 {
t.Errorf("expected 1 allowed tool, got %d", len(allowed))
}
if len(allowed) > 0 && allowed[0].Name != "read" {
t.Errorf("expected 'read' tool, got %q", allowed[0].Name)
}
}
func TestGetNotFound(t *testing.T) {
reg := NewRegistry()
_, ok := reg.Get("nonexistent")
if ok {
t.Error("expected false for nonexistent tool")
}
}
func TestConcurrentRegister(t *testing.T) {
reg := NewRegistry()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
name := fmt.Sprintf("tool_%d", n)
_ = reg.Register(Tool{Name: name})
}(i)
}
wg.Wait()
list := reg.List()
if len(list) != 100 {
t.Errorf("expected 100 tools, got %d", len(list))
}
}