-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathbuilder_test.go
More file actions
209 lines (176 loc) · 5.61 KB
/
builder_test.go
File metadata and controls
209 lines (176 loc) · 5.61 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package inventory
import (
"testing"
)
// mockToolWithScopes creates a mock tool with specified scope definitions
func mockToolWithScopes(name string, toolsetID string, requiredScopes, acceptedScopes []string) ServerTool {
tool := mockTool(name, toolsetID, false)
tool.RequiredScopes = requiredScopes
tool.AcceptedScopes = acceptedScopes
return tool
}
func TestWithRequireScopes_AllToolsHaveScopes(t *testing.T) {
tools := []ServerTool{
mockToolWithScopes("tool1", "toolset1", []string{"repo"}, []string{"repo", "admin:org"}),
mockToolWithScopes("tool2", "toolset1", []string{"repo", "user"}, []string{"repo", "user", "admin:org"}),
mockToolWithScopes("tool3", "toolset2", []string{}, []string{}), // empty slices are valid
}
// Should not panic when all tools have scope definitions
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(true).
Build()
}
func TestWithRequireScopes_ToolMissingBothScopes(t *testing.T) {
tools := []ServerTool{
mockToolWithScopes("tool1", "toolset1", []string{"repo"}, []string{"repo"}),
mockTool("tool2", "toolset1", false), // This tool has nil RequiredScopes and AcceptedScopes
}
// Should panic when a tool is missing both scope definitions
defer func() {
r := recover()
if r == nil {
t.Fatal("Build() should have panicked for tool missing scope definitions")
}
errMsg, ok := r.(string)
if !ok {
t.Fatalf("Expected panic message to be a string, got %T", r)
}
expectedMsg := `tool "tool2" missing scope definitions (both RequiredScopes and AcceptedScopes are nil)`
if errMsg != expectedMsg {
t.Errorf("Expected panic message %q, got %q", expectedMsg, errMsg)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(true).
Build()
}
func TestWithRequireScopes_OnlyRequiredScopesSet(t *testing.T) {
tool := mockTool("tool1", "toolset1", false)
tool.RequiredScopes = []string{"repo"}
tool.AcceptedScopes = nil
tools := []ServerTool{tool}
// Should not panic when only RequiredScopes is set (AcceptedScopes can be nil if RequiredScopes is set)
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(true).
Build()
}
func TestWithRequireScopes_OnlyAcceptedScopesSet(t *testing.T) {
tool := mockTool("tool1", "toolset1", false)
tool.RequiredScopes = nil
tool.AcceptedScopes = []string{"repo"}
tools := []ServerTool{tool}
// Should not panic when only AcceptedScopes is set (RequiredScopes can be nil if AcceptedScopes is set)
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(true).
Build()
}
func TestWithRequireScopes_EmptySlicesAllowed(t *testing.T) {
tools := []ServerTool{
mockToolWithScopes("tool1", "toolset1", []string{}, []string{}),
mockToolWithScopes("tool2", "toolset2", []string{}, []string{}),
}
// Should not panic when tools have empty slices (explicit "no scopes needed")
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(true).
Build()
}
func TestWithRequireScopes_False(t *testing.T) {
tools := []ServerTool{
mockTool("tool1", "toolset1", false), // Missing scope definitions
mockTool("tool2", "toolset1", false), // Missing scope definitions
}
// Should not panic when WithRequireScopes(false) or not set
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(false).
Build()
}
func TestWithRequireScopes_NotSet(t *testing.T) {
tools := []ServerTool{
mockTool("tool1", "toolset1", false), // Missing scope definitions
mockTool("tool2", "toolset1", false), // Missing scope definitions
}
// Should not panic when WithRequireScopes is not called (default is false)
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = NewBuilder().
SetTools(tools).
Build()
}
func TestWithRequireScopes_MixedTools(t *testing.T) {
tools := []ServerTool{
mockToolWithScopes("tool1", "toolset1", []string{"repo"}, []string{"repo"}),
mockToolWithScopes("tool2", "toolset1", []string{}, []string{}),
mockTool("tool3", "toolset2", false), // Missing scope definitions
}
// Should panic on the first tool with missing scope definitions
defer func() {
r := recover()
if r == nil {
t.Fatal("Build() should have panicked for tool missing scope definitions")
}
errMsg, ok := r.(string)
if !ok {
t.Fatalf("Expected panic message to be a string, got %T", r)
}
expectedMsg := `tool "tool3" missing scope definitions (both RequiredScopes and AcceptedScopes are nil)`
if errMsg != expectedMsg {
t.Errorf("Expected panic message %q, got %q", expectedMsg, errMsg)
}
}()
_ = NewBuilder().
SetTools(tools).
WithRequireScopes(true).
Build()
}
func TestWithRequireScopes_Chaining(t *testing.T) {
tools := []ServerTool{
mockToolWithScopes("tool1", "toolset1", []string{"repo"}, []string{"repo"}),
}
// Test that WithRequireScopes returns the builder for chaining
builder := NewBuilder()
result := builder.WithRequireScopes(true)
if result != builder {
t.Error("WithRequireScopes should return the same builder instance for chaining")
}
// Verify the build works
defer func() {
if r := recover(); r != nil {
t.Fatalf("Build() panicked unexpectedly: %v", r)
}
}()
_ = result.SetTools(tools).Build()
}