-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_response_test.go
More file actions
380 lines (354 loc) · 12.7 KB
/
Copy patherror_response_test.go
File metadata and controls
380 lines (354 loc) · 12.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// errorEnvelope is the expected shape of all error responses from the gateway.
//
// {"error": {"code": "string", "message": "string", "details": {}, "request_id": "..."}}
type errorEnvelope struct {
Error struct {
Code string `json:"code"`
Message string `json:"message"`
Details map[string]interface{} `json:"details"`
RequestID interface{} `json:"request_id"`
} `json:"error"`
}
// assertErrorShape validates that the response body matches the canonical error shape.
func assertErrorShape(t *testing.T, w *httptest.ResponseRecorder, expectedStatus int, expectedCode string) errorEnvelope {
t.Helper()
assert.Equal(t, expectedStatus, w.Code, "unexpected HTTP status")
assert.Contains(t, w.Header().Get("Content-Type"), "application/json", "error response must be JSON")
var env errorEnvelope
err := json.Unmarshal(w.Body.Bytes(), &env)
require.NoError(t, err, "response body must be valid JSON")
assert.Equal(t, expectedCode, env.Error.Code, "error code mismatch")
assert.NotEmpty(t, env.Error.Message, "error message must not be empty")
assert.NotNil(t, env.Error.Details, "details must be present (even if empty)")
return env
}
// newTestServer creates a minimal Server suitable for testing error responses.
func newTestServer() (*Server, *gin.Engine) {
gin.SetMode(gin.TestMode)
router := gin.New()
// Add request ID middleware so error responses include it
router.Use(func(c *gin.Context) {
c.Set("request_id", "test-request-id")
c.Next()
})
s := &Server{
cfg: &ServerConfig{
Port: "7340",
Environment: "test",
},
orchestrations: NewOrchestrationStore(),
agents: make(map[string]*AgentRuntime),
}
return s, router
}
// TestErrorResponseShape_ChatCompletions verifies error responses from POST /v1/chat/completions.
func TestErrorResponseShape_ChatCompletions(t *testing.T) {
s, router := newTestServer()
router.POST("/v1/chat/completions", s.handleChatCompletions)
tests := []struct {
name string
body string
expectedCode string
expectedHTTP int
}{
{
name: "invalid JSON",
body: `{not json}`,
expectedCode: "invalid_request",
expectedHTTP: http.StatusBadRequest,
},
{
name: "missing model",
body: `{"messages": [{"role": "user", "content": "hi"}]}`,
expectedCode: "invalid_request",
expectedHTTP: http.StatusBadRequest,
},
{
name: "empty messages",
body: `{"model": "gpt-4", "messages": []}`,
expectedCode: "invalid_request",
expectedHTTP: http.StatusBadRequest,
},
{
name: "no provider configured",
body: `{"model": "gpt-4", "messages": [{"role": "user", "content": "hi"}]}`,
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewBufferString(tt.body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, tt.expectedHTTP, tt.expectedCode)
})
}
}
// TestErrorResponseShape_Tools verifies error responses from tool endpoints.
func TestErrorResponseShape_Tools(t *testing.T) {
s, router := newTestServer()
router.GET("/v1/tools/:name", s.handleGetTool)
router.POST("/v1/tools/:name/invoke", s.handleInvokeTool)
t.Run("GET /v1/tools/:name - not found", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/v1/tools/nonexistent_tool", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, http.StatusNotFound, "not_found")
})
t.Run("POST /v1/tools/:name/invoke - invalid body", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "/v1/tools/test/invoke", bytes.NewBufferString(`{not json}`))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, http.StatusBadRequest, "invalid_request")
})
t.Run("POST /v1/tools/:name/invoke - execution failure returns canonical error with details", func(t *testing.T) {
// Invoke a tool that doesn't exist — should return canonical error shape
body := `{"inputs": {"query": "test"}}`
req, _ := http.NewRequest(http.MethodPost, "/v1/tools/nonexistent_tool_xyz/invoke", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
env := assertErrorShape(t, w, http.StatusInternalServerError, "tool_execution_failed")
// Verify details contains tool metadata
assert.NotEmpty(t, env.Error.Details, "tool execution error should include details with tool_name, inputs, duration_ms")
})
}
// TestErrorResponseShape_Memory verifies error responses from memory endpoints.
func TestErrorResponseShape_Memory(t *testing.T) {
s, router := newTestServer()
router.POST("/v1/memory", s.handleStoreMemory)
router.GET("/v1/memory", s.handleListMemories)
router.GET("/v1/memory/:id", s.handleGetMemory)
router.PUT("/v1/memory/:id", s.handleUpdateMemory)
router.DELETE("/v1/memory/:id", s.handleDeleteMemory)
router.POST("/v1/memory/search", s.handleSearchMemory)
tests := []struct {
name string
method string
path string
body string
expectedCode string
expectedHTTP int
}{
{
name: "POST /v1/memory - no manager",
method: http.MethodPost,
path: "/v1/memory",
body: `{"type": "fact", "content": "test"}`,
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "POST /v1/memory - invalid body",
method: http.MethodPost,
path: "/v1/memory",
body: `{not json}`,
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable, // manager nil checked first
},
{
name: "GET /v1/memory - no manager",
method: http.MethodGet,
path: "/v1/memory",
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "GET /v1/memory/:id - no manager",
method: http.MethodGet,
path: "/v1/memory/some-id",
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "PUT /v1/memory/:id - no manager",
method: http.MethodPut,
path: "/v1/memory/some-id",
body: `{"content": "updated"}`,
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "DELETE /v1/memory/:id - no manager",
method: http.MethodDelete,
path: "/v1/memory/some-id",
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "POST /v1/memory/search - no manager",
method: http.MethodPost,
path: "/v1/memory/search",
body: `{"query": "test"}`,
expectedCode: "server_error",
expectedHTTP: http.StatusServiceUnavailable,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body *bytes.Buffer
if tt.body != "" {
body = bytes.NewBufferString(tt.body)
} else {
body = &bytes.Buffer{}
}
req, _ := http.NewRequest(tt.method, tt.path, body)
if tt.body != "" {
req.Header.Set("Content-Type", "application/json")
}
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, tt.expectedHTTP, tt.expectedCode)
})
}
}
// TestErrorResponseShape_Orchestrate verifies error responses from orchestration endpoints.
func TestErrorResponseShape_Orchestrate(t *testing.T) {
s, router := newTestServer()
router.POST("/v1/orchestrate", s.handleOrchestrate)
router.GET("/v1/orchestrate/:id/events", s.handleOrchestrationEvents)
t.Run("POST /v1/orchestrate - no engine", func(t *testing.T) {
body := `{"task_description": "test task"}`
req, _ := http.NewRequest(http.MethodPost, "/v1/orchestrate", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, http.StatusServiceUnavailable, "server_error")
})
t.Run("POST /v1/orchestrate - invalid body", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "/v1/orchestrate", bytes.NewBufferString(`{bad}`))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Engine nil check happens before JSON bind, so it returns service_unavailable
assertErrorShape(t, w, http.StatusServiceUnavailable, "server_error")
})
t.Run("GET /v1/orchestrate/:id/events - not found", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/v1/orchestrate/nonexistent/events", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, http.StatusNotFound, "not_found")
})
}
// TestErrorResponseShape_GatewayEndpoints verifies error responses from /v1/gateway/* endpoints.
func TestErrorResponseShape_GatewayEndpoints(t *testing.T) {
s, router := newTestServer()
router.GET("/v1/gateway/tools", s.handleGatewayListTools)
router.POST("/v1/gateway/agents", s.handleGatewayCreateAgent)
router.GET("/v1/gateway/agents/:id", s.handleGatewayGetAgent)
router.POST("/v1/gateway/agents/:id/chat", s.handleGatewayAgentChat)
router.POST("/v1/gateway/orchestrate", s.handleGatewayOrchestrate)
router.GET("/v1/gateway/orchestrate/:id/dag", s.handleGatewayOrchestrationDAG)
router.GET("/v1/gateway/traces/:id", s.handleGatewayGetTrace)
tests := []struct {
name string
method string
path string
body string
expectedCode string
expectedHTTP int
}{
{
name: "GET /v1/gateway/tools - no registry",
method: http.MethodGet,
path: "/v1/gateway/tools",
expectedCode: "service_unavailable",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "POST /v1/gateway/agents - invalid body",
method: http.MethodPost,
path: "/v1/gateway/agents",
body: `{bad}`,
expectedCode: "invalid_request",
expectedHTTP: http.StatusBadRequest,
},
{
name: "POST /v1/gateway/agents - no initializer",
method: http.MethodPost,
path: "/v1/gateway/agents",
body: `{"workspace_root": "/tmp"}`,
expectedCode: "service_unavailable",
expectedHTTP: http.StatusServiceUnavailable,
},
{
name: "GET /v1/gateway/agents/:id - not found",
method: http.MethodGet,
path: "/v1/gateway/agents/nonexistent",
expectedCode: "not_found",
expectedHTTP: http.StatusNotFound,
},
{
name: "POST /v1/gateway/agents/:id/chat - not found",
method: http.MethodPost,
path: "/v1/gateway/agents/nonexistent/chat",
body: `{"message": "hello"}`,
expectedCode: "not_found",
expectedHTTP: http.StatusNotFound,
},
{
name: "POST /v1/gateway/orchestrate - invalid body",
method: http.MethodPost,
path: "/v1/gateway/orchestrate",
body: `{bad}`,
expectedCode: "invalid_request",
expectedHTTP: http.StatusBadRequest,
},
{
name: "GET /v1/gateway/orchestrate/:id/dag - not found",
method: http.MethodGet,
path: "/v1/gateway/orchestrate/nonexistent/dag",
expectedCode: "not_found",
expectedHTTP: http.StatusNotFound,
},
{
name: "GET /v1/gateway/traces/:id - no logger",
method: http.MethodGet,
path: "/v1/gateway/traces/some-trace",
expectedCode: "service_unavailable",
expectedHTTP: http.StatusServiceUnavailable,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body *bytes.Buffer
if tt.body != "" {
body = bytes.NewBufferString(tt.body)
} else {
body = &bytes.Buffer{}
}
req, _ := http.NewRequest(tt.method, tt.path, body)
if tt.body != "" {
req.Header.Set("Content-Type", "application/json")
}
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, tt.expectedHTTP, tt.expectedCode)
})
}
}
// TestErrorResponseShape_AdminEndpoints verifies error responses from /admin/* endpoints.
func TestErrorResponseShape_AdminEndpoints(t *testing.T) {
s, router := newTestServer()
router.POST("/admin/config/reload", s.handleAdminConfigReload)
t.Run("POST /admin/config/reload - not implemented", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "/admin/config/reload", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assertErrorShape(t, w, http.StatusNotImplemented, "not_implemented")
})
}