"megatron/core/parallel_state.py" did not exist on "178436057d460343f26b370662a485d91018cafe"
registry_test.go 2.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tools

import (
	"testing"

	"github.com/ollama/ollama/api"
)

func TestRegistry_Register(t *testing.T) {
	r := NewRegistry()

	r.Register(&BashTool{})
	r.Register(&WebSearchTool{})

	if r.Count() != 2 {
		t.Errorf("expected 2 tools, got %d", r.Count())
	}

	names := r.Names()
	if len(names) != 2 {
		t.Errorf("expected 2 names, got %d", len(names))
	}
}

func TestRegistry_Get(t *testing.T) {
	r := NewRegistry()
	r.Register(&BashTool{})

	tool, ok := r.Get("bash")
	if !ok {
		t.Fatal("expected to find bash tool")
	}

	if tool.Name() != "bash" {
		t.Errorf("expected name 'bash', got '%s'", tool.Name())
	}

	_, ok = r.Get("nonexistent")
	if ok {
		t.Error("expected not to find nonexistent tool")
	}
}

func TestRegistry_Tools(t *testing.T) {
	r := NewRegistry()
	r.Register(&BashTool{})
	r.Register(&WebSearchTool{})

	tools := r.Tools()
	if len(tools) != 2 {
		t.Errorf("expected 2 tools, got %d", len(tools))
	}

	for _, tool := range tools {
		if tool.Type != "function" {
			t.Errorf("expected type 'function', got '%s'", tool.Type)
		}
	}
}

func TestRegistry_Execute(t *testing.T) {
	r := NewRegistry()
	r.Register(&BashTool{})

	// Test successful execution
	args := api.NewToolCallFunctionArguments()
	args.Set("command", "echo hello")
	result, err := r.Execute(api.ToolCall{
		Function: api.ToolCallFunction{
			Name:      "bash",
			Arguments: args,
		},
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if result != "hello\n" {
		t.Errorf("expected 'hello\\n', got '%s'", result)
	}

	// Test unknown tool
	_, err = r.Execute(api.ToolCall{
		Function: api.ToolCallFunction{
			Name:      "unknown",
			Arguments: api.NewToolCallFunctionArguments(),
		},
	})
	if err == nil {
		t.Error("expected error for unknown tool")
	}
}

func TestDefaultRegistry(t *testing.T) {
	r := DefaultRegistry()

	if r.Count() != 2 {
		t.Errorf("expected 2 tools in default registry, got %d", r.Count())
	}

	_, ok := r.Get("bash")
	if !ok {
		t.Error("expected bash tool in default registry")
	}

	_, ok = r.Get("web_search")
	if !ok {
		t.Error("expected web_search tool in default registry")
	}
}

func TestBashTool_Schema(t *testing.T) {
	tool := &BashTool{}

	schema := tool.Schema()
	if schema.Name != "bash" {
		t.Errorf("expected name 'bash', got '%s'", schema.Name)
	}

	if schema.Parameters.Type != "object" {
		t.Errorf("expected parameters type 'object', got '%s'", schema.Parameters.Type)
	}

	if _, ok := schema.Parameters.Properties.Get("command"); !ok {
		t.Error("expected 'command' property in schema")
	}
}

func TestWebSearchTool_Schema(t *testing.T) {
	tool := &WebSearchTool{}

	schema := tool.Schema()
	if schema.Name != "web_search" {
		t.Errorf("expected name 'web_search', got '%s'", schema.Name)
	}

	if schema.Parameters.Type != "object" {
		t.Errorf("expected parameters type 'object', got '%s'", schema.Parameters.Type)
	}

	if _, ok := schema.Parameters.Properties.Get("query"); !ok {
		t.Error("expected 'query' property in schema")
	}
}