-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge_test.go
More file actions
234 lines (202 loc) · 6.4 KB
/
bridge_test.go
File metadata and controls
234 lines (202 loc) · 6.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// ── ToolBridge ─────────────────────────────────────────────────────────
func TestToolBridge_Good_RegisterAndServe(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
bridge := api.NewToolBridge("/tools")
bridge.Add(api.ToolDescriptor{
Name: "file_read",
Description: "Read a file",
Group: "files",
}, func(c *gin.Context) {
c.JSON(http.StatusOK, api.OK("result1"))
})
bridge.Add(api.ToolDescriptor{
Name: "file_write",
Description: "Write a file",
Group: "files",
}, func(c *gin.Context) {
c.JSON(http.StatusOK, api.OK("result2"))
})
rg := engine.Group(bridge.BasePath())
bridge.RegisterRoutes(rg)
// POST /tools/file_read
w1 := httptest.NewRecorder()
req1, _ := http.NewRequest(http.MethodPost, "/tools/file_read", nil)
engine.ServeHTTP(w1, req1)
if w1.Code != http.StatusOK {
t.Fatalf("expected 200 for file_read, got %d", w1.Code)
}
var resp1 api.Response[string]
if err := json.Unmarshal(w1.Body.Bytes(), &resp1); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp1.Data != "result1" {
t.Fatalf("expected Data=%q, got %q", "result1", resp1.Data)
}
// POST /tools/file_write
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest(http.MethodPost, "/tools/file_write", nil)
engine.ServeHTTP(w2, req2)
if w2.Code != http.StatusOK {
t.Fatalf("expected 200 for file_write, got %d", w2.Code)
}
var resp2 api.Response[string]
if err := json.Unmarshal(w2.Body.Bytes(), &resp2); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp2.Data != "result2" {
t.Fatalf("expected Data=%q, got %q", "result2", resp2.Data)
}
}
func TestToolBridge_Good_BasePath(t *testing.T) {
bridge := api.NewToolBridge("/api/v1/tools")
if bridge.BasePath() != "/api/v1/tools" {
t.Fatalf("expected BasePath=%q, got %q", "/api/v1/tools", bridge.BasePath())
}
if bridge.Name() != "tools" {
t.Fatalf("expected Name=%q, got %q", "tools", bridge.Name())
}
}
func TestToolBridge_Good_Describe(t *testing.T) {
bridge := api.NewToolBridge("/tools")
bridge.Add(api.ToolDescriptor{
Name: "file_read",
Description: "Read a file from disk",
Group: "files",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"path": map[string]any{"type": "string"},
},
},
OutputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"content": map[string]any{"type": "string"},
},
},
}, func(c *gin.Context) {})
bridge.Add(api.ToolDescriptor{
Name: "metrics_query",
Description: "Query metrics data",
Group: "metrics",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{"type": "string"},
},
},
}, func(c *gin.Context) {})
// Verify DescribableGroup interface satisfaction.
var dg api.DescribableGroup = bridge
descs := dg.Describe()
if len(descs) != 2 {
t.Fatalf("expected 2 descriptions, got %d", len(descs))
}
// First tool.
if descs[0].Method != "POST" {
t.Fatalf("expected descs[0].Method=%q, got %q", "POST", descs[0].Method)
}
if descs[0].Path != "/file_read" {
t.Fatalf("expected descs[0].Path=%q, got %q", "/file_read", descs[0].Path)
}
if descs[0].Summary != "Read a file from disk" {
t.Fatalf("expected descs[0].Summary=%q, got %q", "Read a file from disk", descs[0].Summary)
}
if len(descs[0].Tags) != 1 || descs[0].Tags[0] != "files" {
t.Fatalf("expected descs[0].Tags=[files], got %v", descs[0].Tags)
}
if descs[0].RequestBody == nil {
t.Fatal("expected descs[0].RequestBody to be non-nil")
}
if descs[0].Response == nil {
t.Fatal("expected descs[0].Response to be non-nil")
}
// Second tool.
if descs[1].Path != "/metrics_query" {
t.Fatalf("expected descs[1].Path=%q, got %q", "/metrics_query", descs[1].Path)
}
if len(descs[1].Tags) != 1 || descs[1].Tags[0] != "metrics" {
t.Fatalf("expected descs[1].Tags=[metrics], got %v", descs[1].Tags)
}
if descs[1].Response != nil {
t.Fatalf("expected descs[1].Response to be nil, got %v", descs[1].Response)
}
}
func TestToolBridge_Good_ToolsAccessor(t *testing.T) {
bridge := api.NewToolBridge("/tools")
bridge.Add(api.ToolDescriptor{Name: "alpha", Description: "Tool A", Group: "a"}, func(c *gin.Context) {})
bridge.Add(api.ToolDescriptor{Name: "beta", Description: "Tool B", Group: "b"}, func(c *gin.Context) {})
bridge.Add(api.ToolDescriptor{Name: "gamma", Description: "Tool C", Group: "c"}, func(c *gin.Context) {})
tools := bridge.Tools()
if len(tools) != 3 {
t.Fatalf("expected 3 tools, got %d", len(tools))
}
expected := []string{"alpha", "beta", "gamma"}
for i, want := range expected {
if tools[i].Name != want {
t.Fatalf("expected tools[%d].Name=%q, got %q", i, want, tools[i].Name)
}
}
}
func TestToolBridge_Bad_EmptyBridge(t *testing.T) {
gin.SetMode(gin.TestMode)
bridge := api.NewToolBridge("/tools")
// RegisterRoutes should not panic with no tools.
engine := gin.New()
rg := engine.Group(bridge.BasePath())
bridge.RegisterRoutes(rg)
// Describe should return empty slice.
descs := bridge.Describe()
if len(descs) != 0 {
t.Fatalf("expected 0 descriptions, got %d", len(descs))
}
// Tools should return empty slice.
tools := bridge.Tools()
if len(tools) != 0 {
t.Fatalf("expected 0 tools, got %d", len(tools))
}
}
func TestToolBridge_Good_IntegrationWithEngine(t *testing.T) {
gin.SetMode(gin.TestMode)
e, err := api.New()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
bridge := api.NewToolBridge("/tools")
bridge.Add(api.ToolDescriptor{
Name: "ping",
Description: "Ping tool",
Group: "util",
}, func(c *gin.Context) {
c.JSON(http.StatusOK, api.OK("pong"))
})
e.Register(bridge)
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/tools/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp api.Response[string]
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !resp.Success {
t.Fatal("expected Success=true")
}
if resp.Data != "pong" {
t.Fatalf("expected Data=%q, got %q", "pong", resp.Data)
}
}