60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / core / mysis_tools_test.go
9.0 KB
1package core 2 3import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/xonecas/zoea-nova/internal/mcp" 12 "github.com/xonecas/zoea-nova/internal/provider" 13 "github.com/xonecas/zoea-nova/internal/store" 14) 15 16type failingUpstream struct { 17 tools []mcp.Tool 18 callErr error 19} 20 21func (f *failingUpstream) Initialize(ctx context.Context, clientInfo map[string]interface{}) (*mcp.Response, error) { 22 return nil, nil 23} 24 25func (f *failingUpstream) ListTools(ctx context.Context) ([]mcp.Tool, error) { 26 return f.tools, nil 27} 28 29func (f *failingUpstream) CallTool(ctx context.Context, name string, arguments interface{}) (*mcp.ToolResult, error) { 30 return nil, f.callErr 31} 32 33func TestMysisToolExecution(t *testing.T) { 34 s, bus, cleanup := setupMysisTest(t) 35 defer cleanup() 36 37 stored, _ := s.CreateMysis("tool-mysis", "mock", "test-model", 0.7) 38 39 // Mock provider that returns a tool call first, then a text response 40 mock := provider.NewMock("mock", "Initial response") 41 42 mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus, "") 43 44 // Setup MCP proxy with a local tool 45 proxy := mcp.NewProxy(nil) 46 proxy.RegisterTool(mcp.Tool{ 47 Name: "test_tool", 48 Description: "A test tool", 49 InputSchema: json.RawMessage(`{"type": "object"}`), 50 }, func(ctx context.Context, args json.RawMessage) (*mcp.ToolResult, error) { 51 return &mcp.ToolResult{ 52 Content: []mcp.ContentBlock{{Type: "text", Text: "tool result content"}}, 53 }, nil 54 }) 55 // Manually set MCP proxy for test (normally done in initializeMCP) 56 mysis.mcpProxy = proxy 57 58 events := bus.Subscribe() 59 mysis.Start() 60 61 // Wait for initial turn to finish 62 timeout := time.After(5 * time.Second) 63 initialFinished := false 64 for !initialFinished { 65 select { 66 case e := <-events: 67 if e.Type == EventMysisResponse { 68 if e.Message != nil && e.Message.Content == "Initial response" { 69 initialFinished = true 70 } 71 } 72 case <-timeout: 73 t.Fatal("timeout waiting for initial turn") 74 } 75 } 76 77 // Now set tool calls for the next turn 78 mock.WithResponse("Final response") 79 mock.WithToolCalls([]provider.ToolCall{ 80 { 81 ID: "call_1", 82 Name: "test_tool", 83 Arguments: json.RawMessage(`{"arg": "val"}`), 84 }, 85 }) 86 87 // Send message to trigger the loop 88 go mysis.SendMessage("Use the tool", store.MemorySourceDirect) 89 90 // Wait for tool call event 91 toolCalled := false 92 for !toolCalled { 93 select { 94 case e := <-events: 95 if e.Type == EventMysisMessage { 96 if e.Message != nil && e.Message.Role == "assistant" && e.Message.Content == "Calling tools: test_tool" { 97 toolCalled = true 98 // Now update mock to return NO tool calls for the next iteration 99 mock.WithToolCalls(nil) 100 } 101 } 102 case <-timeout: 103 t.Fatal("timeout waiting for tool call") 104 } 105 } 106 107 // Wait for final response 108 finalResponse := false 109 for !finalResponse { 110 select { 111 case e := <-events: 112 if e.Type == EventMysisResponse { 113 finalResponse = true 114 } 115 case <-timeout: 116 t.Fatal("timeout waiting for final response") 117 } 118 } 119 120 // Verify memories with retry 121 deadline := time.Now().Add(2 * time.Second) 122 foundToolResult := false 123 for time.Now().Before(deadline) { 124 memories, _ := s.GetMemories(stored.ID) 125 for _, m := range memories { 126 if m.Role == store.MemoryRoleTool && m.Content == "call_1:tool result content" { 127 foundToolResult = true 128 break 129 } 130 } 131 if foundToolResult { 132 break 133 } 134 time.Sleep(100 * time.Millisecond) 135 } 136 137 if !foundToolResult { 138 t.Error("tool result not found in memories") 139 } 140} 141 142func TestMysisToolError(t *testing.T) { 143 s, bus, cleanup := setupMysisTest(t) 144 defer cleanup() 145 146 stored, _ := s.CreateMysis("tool-err-mysis", "mock", "test-model", 0.7) 147 148 mock := provider.NewMock("mock", "Initial response") 149 150 mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus, "") 151 152 proxy := mcp.NewProxy(nil) 153 proxy.RegisterTool(mcp.Tool{ 154 Name: "error_tool", 155 }, func(ctx context.Context, args json.RawMessage) (*mcp.ToolResult, error) { 156 return nil, errors.New("tool failed") 157 }) 158 // Manually set MCP proxy for test (normally done in initializeMCP) 159 mysis.mcpProxy = proxy 160 161 events := bus.Subscribe() 162 mysis.Start() 163 164 // Wait for initial turn 165 timeout := time.After(5 * time.Second) 166 initialFinished := false 167 for !initialFinished { 168 select { 169 case e := <-events: 170 if e.Type == EventMysisResponse { 171 initialFinished = true 172 } 173 case <-timeout: 174 t.Fatal("timeout waiting for initial turn") 175 } 176 } 177 178 // Now set tool calls 179 mock.WithToolCalls([]provider.ToolCall{ 180 { 181 ID: "call_err", 182 Name: "error_tool", 183 Arguments: json.RawMessage(`{}`), 184 }, 185 }) 186 187 // Update mock after first call to stop the loop 188 go func() { 189 time.Sleep(100 * time.Millisecond) 190 mock.WithToolCalls(nil) 191 }() 192 193 mysis.SendMessage("Trigger error", store.MemorySourceDirect) 194 195 // Verify memory contains error with retry 196 deadline := time.Now().Add(2 * time.Second) 197 foundError := false 198 for time.Now().Before(deadline) { 199 memories, _ := s.GetMemories(stored.ID) 200 for _, m := range memories { 201 if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, "call_err:Error calling error_tool: tool failed") { 202 foundError = true 203 break 204 } 205 } 206 if foundError { 207 break 208 } 209 time.Sleep(100 * time.Millisecond) 210 } 211 212 if !foundError { 213 t.Error("tool error not found in memories") 214 } 215} 216 217func TestMysisToolTimeoutSetsErrored(t *testing.T) { 218 s, bus, cleanup := setupMysisTest(t) 219 defer cleanup() 220 221 stored, _ := s.CreateMysis("tool-timeout-mysis", "mock", "test-model", 0.7) 222 223 mock := provider.NewMock("mock", "Initial response") 224 mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus, "") 225 226 proxy := mcp.NewProxy(nil) 227 proxy.RegisterTool(mcp.Tool{ 228 Name: "timeout_tool", 229 }, func(ctx context.Context, args json.RawMessage) (*mcp.ToolResult, error) { 230 return nil, context.DeadlineExceeded 231 }) 232 // Manually set MCP proxy for test (normally done in initializeMCP) 233 mysis.mcpProxy = proxy 234 235 // Start and wait for initial turn 236 events := bus.Subscribe() 237 mysis.Start() 238 239 timeout := time.After(5 * time.Second) 240 initialFinished := false 241 for !initialFinished { 242 select { 243 case e := <-events: 244 if e.Type == EventMysisResponse { 245 initialFinished = true 246 } 247 case <-timeout: 248 t.Fatal("timeout waiting for initial turn") 249 } 250 } 251 252 // Trigger tool call that times out 253 mock.WithToolCalls([]provider.ToolCall{ 254 { 255 ID: "call_timeout", 256 Name: "timeout_tool", 257 Arguments: json.RawMessage(`{}`), 258 }, 259 }) 260 261 go mysis.SendMessage("Trigger timeout", store.MemorySourceDirect) 262 263 deadline := time.Now().Add(2 * time.Second) 264 for time.Now().Before(deadline) { 265 if mysis.State() == MysisStateErrored { 266 break 267 } 268 time.Sleep(50 * time.Millisecond) 269 } 270 271 if state := mysis.State(); state != MysisStateErrored { 272 t.Fatalf("expected state=errored, got %s", state) 273 } 274 275 // Verify memory contains timeout error 276 foundTimeout := false 277 memories, _ := s.GetMemories(stored.ID) 278 for _, m := range memories { 279 if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, "call_timeout") && strings.Contains(m.Content, "timed out") { 280 foundTimeout = true 281 break 282 } 283 } 284 285 if !foundTimeout { 286 t.Error("tool timeout error not found in memories") 287 } 288} 289 290func TestMysisToolRetryExhaustionSetsErrored(t *testing.T) { 291 s, bus, cleanup := setupMysisTest(t) 292 defer cleanup() 293 294 stored, _ := s.CreateMysis("tool-retry-mysis", "mock", "test-model", 0.7) 295 296 mock := provider.NewMock("mock", "Initial response") 297 mysis := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus, "") 298 299 upstream := &failingUpstream{ 300 tools: []mcp.Tool{{Name: "upstream_tool"}}, 301 callErr: errors.New("upstream unavailable"), 302 } 303 // Manually set MCP proxy for test (normally done in initializeMCP) 304 mysis.mcpProxy = mcp.NewProxy(upstream) 305 306 // Start and wait for initial turn 307 events := bus.Subscribe() 308 mysis.Start() 309 310 timeout := time.After(5 * time.Second) 311 initialFinished := false 312 for !initialFinished { 313 select { 314 case e := <-events: 315 if e.Type == EventMysisResponse { 316 initialFinished = true 317 } 318 case <-timeout: 319 t.Fatal("timeout waiting for initial turn") 320 } 321 } 322 323 // Trigger upstream tool call 324 mock.WithToolCalls([]provider.ToolCall{ 325 { 326 ID: "call_retry", 327 Name: "upstream_tool", 328 Arguments: json.RawMessage(`{}`), 329 }, 330 }) 331 332 go mysis.SendMessage("Trigger upstream retry", store.MemorySourceDirect) 333 334 // Wait for retry exhaustion: 3 retries with delays 2s, 5s, 10s = 17s minimum 335 deadline := time.Now().Add(20 * time.Second) 336 for time.Now().Before(deadline) { 337 if mysis.State() == MysisStateErrored { 338 break 339 } 340 time.Sleep(50 * time.Millisecond) 341 } 342 343 if state := mysis.State(); state != MysisStateErrored { 344 t.Fatalf("expected state=errored, got %s", state) 345 } 346 347 // Verify memory contains retry exhaustion error 348 foundRetry := false 349 memories, _ := s.GetMemories(stored.ID) 350 for _, m := range memories { 351 if m.Role == store.MemoryRoleTool && strings.Contains(m.Content, "call_retry") && strings.Contains(m.Content, "failed after retries") { 352 foundRetry = true 353 break 354 } 355 } 356 357 if !foundRetry { 358 t.Error("tool retry exhaustion error not found in memories") 359 } 360}