xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / mcp / stub_test.go

11.0 KB
1package mcp
2
3import (
4	"context"
5	"encoding/json"
6	"testing"
7)
8
9func TestStubClient_NewStubClient(t *testing.T) {
10	client := NewStubClient()
11	if client == nil {
12		t.Fatal("NewStubClient returned nil")
13	}
14}
15
16func TestStubClient_Initialize(t *testing.T) {
17	client := NewStubClient()
18	ctx := context.Background()
19
20	result, err := client.Initialize(ctx, nil)
21	if err != nil {
22		t.Fatalf("Initialize failed: %v", err)
23	}
24
25	if result == nil {
26		t.Fatal("Initialize returned nil response")
27	}
28
29	if result.JSONRPC != "2.0" {
30		t.Errorf("Expected JSONRPC 2.0, got %s", result.JSONRPC)
31	}
32
33	if result.ID != 1 {
34		t.Errorf("Expected ID 1, got %v", result.ID)
35	}
36
37	// Parse and verify result
38	var initResult map[string]interface{}
39	if err := json.Unmarshal(result.Result, &initResult); err != nil {
40		t.Fatalf("Failed to parse initialize result: %v", err)
41	}
42
43	protocolVersion, ok := initResult["protocolVersion"].(string)
44	if !ok || protocolVersion == "" {
45		t.Error("Expected protocolVersion in result")
46	}
47
48	serverInfo, ok := initResult["serverInfo"].(map[string]interface{})
49	if !ok {
50		t.Error("Expected serverInfo in result")
51	} else {
52		if name, ok := serverInfo["name"].(string); !ok || name != "spacemolt-stub" {
53			t.Errorf("Expected serverInfo.name to be 'spacemolt-stub', got %v", serverInfo["name"])
54		}
55	}
56}
57
58func TestStubClient_Initialize_WithClientInfo(t *testing.T) {
59	client := NewStubClient()
60	ctx := context.Background()
61
62	clientInfo := map[string]interface{}{
63		"name":    "test-client",
64		"version": "1.0.0",
65	}
66
67	result, err := client.Initialize(ctx, clientInfo)
68	if err != nil {
69		t.Fatalf("Initialize with clientInfo failed: %v", err)
70	}
71
72	if result == nil {
73		t.Fatal("Initialize returned nil response")
74	}
75}
76
77func TestStubClient_ListTools(t *testing.T) {
78	client := NewStubClient()
79	ctx := context.Background()
80
81	tools, err := client.ListTools(ctx)
82	if err != nil {
83		t.Fatalf("ListTools failed: %v", err)
84	}
85
86	expectedTools := []string{"get_status", "get_system", "get_ship", "get_poi", "get_notifications"}
87	if len(tools) != len(expectedTools) {
88		t.Errorf("Expected %d tools, got %d", len(expectedTools), len(tools))
89	}
90
91	// Verify each expected tool is present
92	toolMap := make(map[string]Tool)
93	for _, tool := range tools {
94		toolMap[tool.Name] = tool
95	}
96
97	for _, expectedName := range expectedTools {
98		tool, ok := toolMap[expectedName]
99		if !ok {
100			t.Errorf("Expected tool %s not found", expectedName)
101			continue
102		}
103
104		if tool.Description == "" {
105			t.Errorf("Tool %s has empty description", expectedName)
106		}
107
108		if len(tool.InputSchema) == 0 {
109			t.Errorf("Tool %s has empty input schema", expectedName)
110		}
111
112		// Verify input schema is valid JSON
113		var schema map[string]interface{}
114		if err := json.Unmarshal(tool.InputSchema, &schema); err != nil {
115			t.Errorf("Tool %s has invalid input schema: %v", expectedName, err)
116		}
117	}
118}
119
120func TestStubClient_CallTool_AllTools(t *testing.T) {
121	client := NewStubClient()
122	ctx := context.Background()
123
124	testCases := []struct {
125		name         string
126		tool         string
127		expectedTick int64
128	}{
129		{"get_status", "get_status", 42},
130		{"get_system", "get_system", 42},
131		{"get_ship", "get_ship", 42},
132		{"get_poi", "get_poi", 42},
133		{"get_notifications", "get_notifications", 42},
134	}
135
136	for _, tc := range testCases {
137		t.Run(tc.name, func(t *testing.T) {
138			result, err := client.CallTool(ctx, tc.tool, nil)
139			if err != nil {
140				t.Fatalf("CallTool(%s) failed: %v", tc.tool, err)
141			}
142
143			if result == nil {
144				t.Fatal("CallTool returned nil result")
145			}
146
147			if result.IsError {
148				t.Errorf("CallTool(%s) returned error result", tc.tool)
149			}
150
151			if len(result.Content) == 0 {
152				t.Fatal("CallTool returned empty content")
153			}
154
155			if result.Content[0].Type != "text" {
156				t.Errorf("Expected content type 'text', got '%s'", result.Content[0].Type)
157			}
158
159			// Verify result is valid JSON
160			var data map[string]interface{}
161			if err := json.Unmarshal([]byte(result.Content[0].Text), &data); err != nil {
162				t.Errorf("Invalid JSON response: %v", err)
163			}
164
165			// Verify tick is present
166			if tick, ok := data["current_tick"]; ok {
167				if tick != float64(tc.expectedTick) {
168					t.Errorf("Expected tick %d, got %v", tc.expectedTick, tick)
169				}
170			} else {
171				t.Errorf("Expected current_tick in response")
172			}
173		})
174	}
175}
176
177func TestStubClient_CallTool_UnknownTool(t *testing.T) {
178	client := NewStubClient()
179	ctx := context.Background()
180
181	result, err := client.CallTool(ctx, "unknown_tool", nil)
182	if err != nil {
183		t.Fatalf("Expected error result, got actual error: %v", err)
184	}
185
186	if !result.IsError {
187		t.Error("Expected IsError=true for unknown tool")
188	}
189
190	if len(result.Content) == 0 {
191		t.Fatal("Expected error content")
192	}
193
194	if result.Content[0].Type != "text" {
195		t.Errorf("Expected content type 'text', got '%s'", result.Content[0].Type)
196	}
197
198	// Verify error message contains tool name
199	errorMsg := result.Content[0].Text
200	if errorMsg == "" {
201		t.Error("Expected non-empty error message")
202	}
203}
204
205func TestStubClient_CallTool_WithArguments(t *testing.T) {
206	client := NewStubClient()
207	ctx := context.Background()
208
209	args := map[string]interface{}{
210		"some_param": "test_value",
211	}
212
213	result, err := client.CallTool(ctx, "get_status", args)
214	if err != nil {
215		t.Fatalf("CallTool with arguments failed: %v", err)
216	}
217
218	if result == nil {
219		t.Fatal("CallTool returned nil result")
220	}
221
222	if result.IsError {
223		t.Error("CallTool returned error result")
224	}
225}
226
227func TestStubClient_CallTool_GetStatus_Structure(t *testing.T) {
228	client := NewStubClient()
229	ctx := context.Background()
230
231	result, err := client.CallTool(ctx, "get_status", nil)
232	if err != nil {
233		t.Fatalf("CallTool failed: %v", err)
234	}
235
236	var data map[string]interface{}
237	if err := json.Unmarshal([]byte(result.Content[0].Text), &data); err != nil {
238		t.Fatalf("Invalid JSON response: %v", err)
239	}
240
241	// Verify structure
242	if _, ok := data["player"]; !ok {
243		t.Error("Expected 'player' field in get_status response")
244	}
245
246	if _, ok := data["ship"]; !ok {
247		t.Error("Expected 'ship' field in get_status response")
248	}
249
250	if _, ok := data["current_tick"]; !ok {
251		t.Error("Expected 'current_tick' field in get_status response")
252	}
253}
254
255func TestStubClient_CallTool_GetSystem_Structure(t *testing.T) {
256	client := NewStubClient()
257	ctx := context.Background()
258
259	result, err := client.CallTool(ctx, "get_system", nil)
260	if err != nil {
261		t.Fatalf("CallTool failed: %v", err)
262	}
263
264	var data map[string]interface{}
265	if err := json.Unmarshal([]byte(result.Content[0].Text), &data); err != nil {
266		t.Fatalf("Invalid JSON response: %v", err)
267	}
268
269	// Verify structure
270	if _, ok := data["id"]; !ok {
271		t.Error("Expected 'id' field in get_system response")
272	}
273
274	if _, ok := data["name"]; !ok {
275		t.Error("Expected 'name' field in get_system response")
276	}
277
278	if _, ok := data["current_tick"]; !ok {
279		t.Error("Expected 'current_tick' field in get_system response")
280	}
281}
282
283func TestStubClient_CallTool_GetShip_Structure(t *testing.T) {
284	client := NewStubClient()
285	ctx := context.Background()
286
287	result, err := client.CallTool(ctx, "get_ship", nil)
288	if err != nil {
289		t.Fatalf("CallTool failed: %v", err)
290	}
291
292	var data map[string]interface{}
293	if err := json.Unmarshal([]byte(result.Content[0].Text), &data); err != nil {
294		t.Fatalf("Invalid JSON response: %v", err)
295	}
296
297	// Verify structure
298	if _, ok := data["id"]; !ok {
299		t.Error("Expected 'id' field in get_ship response")
300	}
301
302	if _, ok := data["name"]; !ok {
303		t.Error("Expected 'name' field in get_ship response")
304	}
305
306	if _, ok := data["current_tick"]; !ok {
307		t.Error("Expected 'current_tick' field in get_ship response")
308	}
309
310	if _, ok := data["cargo"]; !ok {
311		t.Error("Expected 'cargo' field in get_ship response")
312	}
313}
314
315func TestStubClient_CallTool_GetPOI_Structure(t *testing.T) {
316	client := NewStubClient()
317	ctx := context.Background()
318
319	result, err := client.CallTool(ctx, "get_poi", nil)
320	if err != nil {
321		t.Fatalf("CallTool failed: %v", err)
322	}
323
324	var data map[string]interface{}
325	if err := json.Unmarshal([]byte(result.Content[0].Text), &data); err != nil {
326		t.Fatalf("Invalid JSON response: %v", err)
327	}
328
329	// Verify structure
330	if _, ok := data["id"]; !ok {
331		t.Error("Expected 'id' field in get_poi response")
332	}
333
334	if _, ok := data["name"]; !ok {
335		t.Error("Expected 'name' field in get_poi response")
336	}
337
338	if _, ok := data["type"]; !ok {
339		t.Error("Expected 'type' field in get_poi response")
340	}
341
342	if _, ok := data["current_tick"]; !ok {
343		t.Error("Expected 'current_tick' field in get_poi response")
344	}
345}
346
347func TestStubClient_CallTool_GetNotifications_Structure(t *testing.T) {
348	client := NewStubClient()
349	ctx := context.Background()
350
351	result, err := client.CallTool(ctx, "get_notifications", nil)
352	if err != nil {
353		t.Fatalf("CallTool failed: %v", err)
354	}
355
356	var data map[string]interface{}
357	if err := json.Unmarshal([]byte(result.Content[0].Text), &data); err != nil {
358		t.Fatalf("Invalid JSON response: %v", err)
359	}
360
361	// Verify structure matches real API (server v0.44.4+)
362	if _, ok := data["count"]; !ok {
363		t.Error("Expected 'count' field in get_notifications response")
364	}
365
366	if _, ok := data["current_tick"]; !ok {
367		t.Error("Expected 'current_tick' field in get_notifications response")
368	}
369
370	if _, ok := data["notifications"]; !ok {
371		t.Error("Expected 'notifications' field in get_notifications response")
372	}
373
374	if _, ok := data["remaining"]; !ok {
375		t.Error("Expected 'remaining' field in get_notifications response")
376	}
377
378	// Verify notifications is an array
379	if notifications, ok := data["notifications"].([]interface{}); !ok {
380		t.Error("Expected 'notifications' to be an array")
381	} else if len(notifications) != 0 {
382		t.Error("Expected empty notifications array in stub")
383	}
384}
385
386func TestStubClient_CallTool_ContextCancellation(t *testing.T) {
387	client := NewStubClient()
388	ctx, cancel := context.WithCancel(context.Background())
389	cancel() // Cancel immediately
390
391	// CallTool should still work (stub doesn't check context)
392	result, err := client.CallTool(ctx, "get_status", nil)
393	if err != nil {
394		t.Fatalf("CallTool failed: %v", err)
395	}
396
397	if result == nil {
398		t.Fatal("Expected result even with cancelled context")
399	}
400}
401
402func TestStubClient_MultipleCallsSameClient(t *testing.T) {
403	client := NewStubClient()
404	ctx := context.Background()
405
406	// Call multiple tools in sequence
407	tools := []string{"get_status", "get_system", "get_ship", "get_poi", "get_notifications"}
408
409	for i := 0; i < 3; i++ {
410		for _, tool := range tools {
411			result, err := client.CallTool(ctx, tool, nil)
412			if err != nil {
413				t.Fatalf("CallTool(%s) iteration %d failed: %v", tool, i, err)
414			}
415
416			if result.IsError {
417				t.Errorf("CallTool(%s) iteration %d returned error", tool, i)
418			}
419		}
420	}
421}
422
423func TestStubClient_ConcurrentCalls(t *testing.T) {
424	client := NewStubClient()
425	ctx := context.Background()
426
427	done := make(chan bool, 10)
428
429	for i := 0; i < 10; i++ {
430		go func(id int) {
431			result, err := client.CallTool(ctx, "get_status", nil)
432			if err != nil {
433				t.Errorf("Concurrent call %d failed: %v", id, err)
434			}
435			if result == nil || result.IsError {
436				t.Errorf("Concurrent call %d returned invalid result", id)
437			}
438			done <- true
439		}(i)
440	}
441
442	// Wait for all goroutines
443	for i := 0; i < 10; i++ {
444		<-done
445	}
446}
447
448func TestStubClient_ImplementsUpstreamClient(t *testing.T) {
449	var _ UpstreamClient = (*StubClient)(nil)
450}