60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / provider / ollama_test.go
12.2 KB
1package provider 2 3import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 "net/http/httptest" 8 "strings" 9 "testing" 10) 11 12// TestOllama_SystemMessagesAnywhere tests that Ollama provider correctly handles 13// system messages anywhere in the conversation (not just at the start). 14func TestOllama_SystemMessagesAnywhere(t *testing.T) { 15 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 16 var req struct { 17 Messages []struct { 18 Role string `json:"role"` 19 Content string `json:"content"` 20 } `json:"messages"` 21 } 22 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 23 t.Fatalf("failed to decode request: %v", err) 24 } 25 26 // Verify message order: system, user, system (merged), assistant 27 // Ollama allows system messages anywhere, but consecutive ones should be merged 28 expectedRoles := []string{"system", "user", "system", "assistant"} 29 if len(req.Messages) != len(expectedRoles) { 30 t.Errorf("expected %d messages, got %d", len(expectedRoles), len(req.Messages)) 31 } 32 33 for i, expected := range expectedRoles { 34 if i >= len(req.Messages) { 35 break 36 } 37 if req.Messages[i].Role != expected { 38 t.Errorf("message %d: expected role=%s, got %s", i, expected, req.Messages[i].Role) 39 } 40 } 41 42 // Verify first system message 43 if !strings.Contains(req.Messages[0].Content, "helpful assistant") { 44 t.Error("expected first system message to contain 'helpful assistant'") 45 } 46 47 // Verify third message merges two consecutive system messages 48 if !strings.Contains(req.Messages[2].Content, "Be concise") { 49 t.Error("expected merged system message to contain 'Be concise'") 50 } 51 if !strings.Contains(req.Messages[2].Content, "Use simple language") { 52 t.Error("expected merged system message to contain 'Use simple language'") 53 } 54 55 w.Header().Set("Content-Type", "application/json") 56 json.NewEncoder(w).Encode(map[string]interface{}{ 57 "choices": []map[string]interface{}{ 58 { 59 "message": map[string]interface{}{ 60 "role": "assistant", 61 "content": "Got it!", 62 }, 63 }, 64 }, 65 }) 66 })) 67 defer server.Close() 68 69 // Remove /v1 suffix since NewOllama adds it 70 baseURL := strings.TrimSuffix(server.URL, "/v1") 71 provider := NewOllama(baseURL, "test-model") 72 ctx := context.Background() 73 74 // System messages at different positions, including consecutive ones 75 messages := []Message{ 76 {Role: "system", Content: "You are a helpful assistant."}, 77 {Role: "user", Content: "Hello"}, 78 {Role: "system", Content: "Be concise in your responses."}, 79 {Role: "system", Content: "Use simple language."}, 80 {Role: "assistant", Content: "Hi there!"}, 81 } 82 83 _, err := provider.Chat(ctx, messages) 84 if err != nil { 85 t.Fatalf("Chat() error: %v", err) 86 } 87} 88 89// TestOllama_ConsecutiveSystemMessagesMerge tests that consecutive system messages 90// are merged in place (Ollama-specific behavior). 91func TestOllama_ConsecutiveSystemMessagesMerge(t *testing.T) { 92 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 93 var req struct { 94 Messages []struct { 95 Role string `json:"role"` 96 Content string `json:"content"` 97 } `json:"messages"` 98 } 99 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 100 t.Fatalf("failed to decode request: %v", err) 101 } 102 103 // Verify: 3 system messages should be merged into 1 104 systemCount := 0 105 for i, msg := range req.Messages { 106 if msg.Role == "system" { 107 systemCount++ 108 // Should be first message 109 if i != 0 { 110 t.Errorf("expected system message at position 0, got %d", i) 111 } 112 // Should contain all three system prompts merged 113 if !strings.Contains(msg.Content, "instruction one") { 114 t.Error("expected merged system to contain 'instruction one'") 115 } 116 if !strings.Contains(msg.Content, "instruction two") { 117 t.Error("expected merged system to contain 'instruction two'") 118 } 119 if !strings.Contains(msg.Content, "instruction three") { 120 t.Error("expected merged system to contain 'instruction three'") 121 } 122 } 123 } 124 125 if systemCount != 1 { 126 t.Errorf("expected 1 system message after merge, got %d", systemCount) 127 } 128 129 // Total messages should be 2 (1 merged system + 1 user) 130 if len(req.Messages) != 2 { 131 t.Errorf("expected 2 messages total, got %d", len(req.Messages)) 132 } 133 134 w.Header().Set("Content-Type", "application/json") 135 json.NewEncoder(w).Encode(map[string]interface{}{ 136 "choices": []map[string]interface{}{ 137 { 138 "message": map[string]interface{}{ 139 "role": "assistant", 140 "content": "Ready.", 141 }, 142 }, 143 }, 144 }) 145 })) 146 defer server.Close() 147 148 baseURL := strings.TrimSuffix(server.URL, "/v1") 149 provider := NewOllama(baseURL, "test-model") 150 ctx := context.Background() 151 152 // Three consecutive system messages followed by user message 153 messages := []Message{ 154 {Role: "system", Content: "This is instruction one."}, 155 {Role: "system", Content: "This is instruction two."}, 156 {Role: "system", Content: "This is instruction three."}, 157 {Role: "user", Content: "Hello"}, 158 } 159 160 _, err := provider.Chat(ctx, messages) 161 if err != nil { 162 t.Fatalf("Chat() error: %v", err) 163 } 164} 165 166// TestOllama_ToolCalls tests that Ollama provider correctly handles 167// messages with tool calls. 168func TestOllama_ToolCalls(t *testing.T) { 169 requestCount := 0 170 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 171 requestCount++ 172 173 var req struct { 174 Messages []struct { 175 Role string `json:"role"` 176 Content string `json:"content"` 177 ToolCalls []struct { 178 ID string `json:"id"` 179 Function struct { 180 Name string `json:"name"` 181 Arguments string `json:"arguments"` 182 } `json:"function"` 183 } `json:"tool_calls,omitempty"` 184 ToolCallID string `json:"tool_call_id,omitempty"` 185 } `json:"messages"` 186 Tools []map[string]interface{} `json:"tools,omitempty"` 187 } 188 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 189 t.Fatalf("failed to decode request: %v", err) 190 } 191 192 w.Header().Set("Content-Type", "application/json") 193 194 // First request: return tool call 195 if requestCount == 1 { 196 // Verify tools are present 197 if len(req.Tools) == 0 { 198 t.Error("expected tools in request") 199 } 200 201 // Verify tool structure matches Ollama format 202 tool := req.Tools[0] 203 if tool["type"] != "function" { 204 t.Errorf("expected tool type=function, got %v", tool["type"]) 205 } 206 207 json.NewEncoder(w).Encode(map[string]interface{}{ 208 "choices": []map[string]interface{}{ 209 { 210 "message": map[string]interface{}{ 211 "role": "assistant", 212 "content": "", 213 "tool_calls": []map[string]interface{}{ 214 { 215 "id": "call_abc", 216 "type": "function", 217 "function": map[string]interface{}{ 218 "name": "calculate", 219 "arguments": `{"operation":"add","a":5,"b":3}`, 220 }, 221 }, 222 }, 223 }, 224 }, 225 }, 226 }) 227 return 228 } 229 230 // Second request: verify tool result message 231 if requestCount == 2 { 232 foundToolResult := false 233 for _, msg := range req.Messages { 234 if msg.Role == "tool" && msg.ToolCallID == "call_abc" { 235 foundToolResult = true 236 if msg.Content != "8" { 237 t.Errorf("expected tool result='8', got %q", msg.Content) 238 } 239 } 240 } 241 if !foundToolResult { 242 t.Error("expected tool result message in second request") 243 } 244 245 json.NewEncoder(w).Encode(map[string]interface{}{ 246 "choices": []map[string]interface{}{ 247 { 248 "message": map[string]interface{}{ 249 "role": "assistant", 250 "content": "The result is 8.", 251 }, 252 }, 253 }, 254 }) 255 } 256 })) 257 defer server.Close() 258 259 baseURL := strings.TrimSuffix(server.URL, "/v1") 260 provider := NewOllama(baseURL, "test-model") 261 ctx := context.Background() 262 263 // First call with tools 264 tools := []Tool{ 265 { 266 Name: "calculate", 267 Description: "Perform calculations", 268 Parameters: json.RawMessage(`{"type":"object","properties":{"operation":{"type":"string"},"a":{"type":"number"},"b":{"type":"number"}}}`), 269 }, 270 } 271 272 messages := []Message{ 273 {Role: "user", Content: "What is 5 plus 3?"}, 274 } 275 276 resp1, err := provider.ChatWithTools(ctx, messages, tools) 277 if err != nil { 278 t.Fatalf("ChatWithTools() error: %v", err) 279 } 280 281 // Verify tool call 282 if len(resp1.ToolCalls) != 1 { 283 t.Fatalf("expected 1 tool call, got %d", len(resp1.ToolCalls)) 284 } 285 if resp1.ToolCalls[0].Name != "calculate" { 286 t.Errorf("expected tool name=calculate, got %s", resp1.ToolCalls[0].Name) 287 } 288 289 // Second call with tool result 290 messages = append(messages, Message{ 291 Role: "assistant", 292 Content: "", 293 ToolCalls: resp1.ToolCalls, 294 }) 295 messages = append(messages, Message{ 296 Role: "tool", 297 Content: "8", 298 ToolCallID: "call_abc", 299 }) 300 301 resp2, err := provider.ChatWithTools(ctx, messages, tools) 302 if err != nil { 303 t.Fatalf("ChatWithTools() error: %v", err) 304 } 305 306 if !strings.Contains(resp2.Content, "8") { 307 t.Errorf("expected response to contain '8', got %q", resp2.Content) 308 } 309} 310 311// TestOllama_ReasoningField tests that Ollama provider extracts reasoning 312// from both "reasoning" and "reasoning_content" fields (Ollama extension). 313func TestOllama_ReasoningField(t *testing.T) { 314 tests := []struct { 315 name string 316 responseField string 317 responseValue string 318 }{ 319 { 320 name: "reasoning field", 321 responseField: "reasoning", 322 responseValue: "I need to think about this...", 323 }, 324 { 325 name: "reasoning_content field", 326 responseField: "reasoning_content", 327 responseValue: "Let me analyze this step by step.", 328 }, 329 } 330 331 for _, tt := range tests { 332 t.Run(tt.name, func(t *testing.T) { 333 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 334 w.Header().Set("Content-Type", "application/json") 335 336 // Build response with specified reasoning field 337 response := map[string]interface{}{ 338 "choices": []map[string]interface{}{ 339 { 340 "message": map[string]interface{}{ 341 "role": "assistant", 342 "content": "Answer", 343 tt.responseField: tt.responseValue, 344 }, 345 }, 346 }, 347 } 348 349 json.NewEncoder(w).Encode(response) 350 })) 351 defer server.Close() 352 353 baseURL := strings.TrimSuffix(server.URL, "/v1") 354 provider := NewOllama(baseURL, "test-model") 355 ctx := context.Background() 356 357 messages := []Message{ 358 {Role: "user", Content: "Question?"}, 359 } 360 361 tools := []Tool{ 362 {Name: "dummy", Description: "Dummy tool"}, 363 } 364 365 resp, err := provider.ChatWithTools(ctx, messages, tools) 366 if err != nil { 367 t.Fatalf("ChatWithTools() error: %v", err) 368 } 369 370 if resp.Reasoning != tt.responseValue { 371 t.Errorf("expected reasoning=%q, got %q", tt.responseValue, resp.Reasoning) 372 } 373 }) 374 } 375} 376 377// TestOllama_SystemMessagesAtEnd tests that system messages at the end 378// are preserved (Ollama allows this, unlike OpenAI). 379func TestOllama_SystemMessagesAtEnd(t *testing.T) { 380 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 381 var req struct { 382 Messages []struct { 383 Role string `json:"role"` 384 Content string `json:"content"` 385 } `json:"messages"` 386 } 387 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 388 t.Fatalf("failed to decode request: %v", err) 389 } 390 391 // Verify last message is system 392 lastMsg := req.Messages[len(req.Messages)-1] 393 if lastMsg.Role != "system" { 394 t.Errorf("expected last message role=system, got %s", lastMsg.Role) 395 } 396 if !strings.Contains(lastMsg.Content, "final instruction") { 397 t.Error("expected last system message to contain 'final instruction'") 398 } 399 400 w.Header().Set("Content-Type", "application/json") 401 json.NewEncoder(w).Encode(map[string]interface{}{ 402 "choices": []map[string]interface{}{ 403 { 404 "message": map[string]interface{}{ 405 "role": "assistant", 406 "content": "Understood final instruction.", 407 }, 408 }, 409 }, 410 }) 411 })) 412 defer server.Close() 413 414 baseURL := strings.TrimSuffix(server.URL, "/v1") 415 provider := NewOllama(baseURL, "test-model") 416 ctx := context.Background() 417 418 // System message at the end 419 messages := []Message{ 420 {Role: "user", Content: "Hello"}, 421 {Role: "assistant", Content: "Hi!"}, 422 {Role: "system", Content: "Here's a final instruction."}, 423 } 424 425 resp, err := provider.Chat(ctx, messages) 426 if err != nil { 427 t.Fatalf("Chat() error: %v", err) 428 } 429 430 if !strings.Contains(resp, "final instruction") { 431 t.Errorf("expected response to acknowledge final instruction, got %q", resp) 432 } 433}