6119
My
mysis / files
mysis / internal / provider / streaming_test.go
12.0 KB
1package provider 2 3import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 "net/http/httptest" 9 "strings" 10 "testing" 11 "time" 12) 13 14// TestOllamaProvider_Stream tests basic streaming functionality 15func TestOllamaProvider_Stream(t *testing.T) { 16 // Mock server that streams chunks 17 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 18 // Verify request path 19 if !strings.HasSuffix(r.URL.Path, "/v1/chat/completions") { 20 t.Errorf("Expected path ending with /v1/chat/completions, got %s", r.URL.Path) 21 } 22 23 // Verify stream=true in request body (OpenAI client adds this) 24 // Since we're using the go-openai client, it handles streaming protocol 25 26 flusher, ok := w.(http.Flusher) 27 if !ok { 28 t.Fatal("Expected http.Flusher") 29 } 30 31 w.Header().Set("Content-Type", "text/event-stream") 32 w.Header().Set("Cache-Control", "no-cache") 33 w.Header().Set("Connection", "keep-alive") 34 35 // Stream chunks in SSE format (OpenAI streaming format) 36 chunks := []string{ 37 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}`, 38 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":" world"},"finish_reason":null}]}`, 39 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}`, 40 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`, 41 `data: [DONE]`, 42 } 43 44 for _, chunk := range chunks { 45 _, _ = fmt.Fprintf(w, "%s\n\n", chunk) 46 flusher.Flush() 47 time.Sleep(10 * time.Millisecond) // Simulate streaming delay 48 } 49 })) 50 defer server.Close() 51 52 provider := NewOllama(server.URL, "test-model") 53 54 ctx := context.Background() 55 messages := make([]Message, 0, 1) 56 messages = append(messages, Message{Role: "user", Content: "test"}) 57 58 ch, err := provider.Stream(ctx, messages) 59 if err != nil { 60 t.Fatalf("Stream() failed: %v", err) 61 } 62 63 var result string 64 var gotDone bool 65 for chunk := range ch { 66 if chunk.Err != nil { 67 t.Fatalf("Stream chunk error: %v", chunk.Err) 68 } 69 if chunk.Done { 70 gotDone = true 71 continue 72 } 73 result += chunk.Content 74 } 75 76 expected := "Hello world!" 77 if result != expected { 78 t.Errorf("Expected content %q, got %q", expected, result) 79 } 80 if !gotDone { 81 t.Error("Expected Done=true chunk") 82 } 83} 84 85// TestOllamaProvider_Stream_Cancellation tests context cancellation during streaming 86func TestOllamaProvider_Stream_Cancellation(t *testing.T) { 87 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 88 w.Header().Set("Content-Type", "text/event-stream") 89 flusher := w.(http.Flusher) 90 91 // Send chunks slowly 92 for i := 0; i < 10; i++ { 93 _, _ = fmt.Fprintf(w, "data: {\"id\":\"1\",\"object\":\"chat.completion.chunk\",\"created\":1234567890,\"model\":\"test\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"chunk %d\"},\"finish_reason\":null}]}\n\n", i) 94 flusher.Flush() 95 time.Sleep(100 * time.Millisecond) 96 } 97 })) 98 defer server.Close() 99 100 provider := NewOllama(server.URL, "test") 101 102 ctx, cancel := context.WithCancel(context.Background()) 103 104 messages := make([]Message, 0, 1) 105 messages = append(messages, Message{Role: "user", Content: "test"}) 106 ch, err := provider.Stream(ctx, messages) 107 if err != nil { 108 t.Fatalf("Stream() failed: %v", err) 109 } 110 111 // Cancel after first chunk 112 go func() { 113 time.Sleep(150 * time.Millisecond) 114 cancel() 115 }() 116 117 var chunks int 118 var gotError bool 119 for chunk := range ch { 120 if chunk.Err != nil { 121 gotError = true 122 // Context cancellation should result in error 123 break 124 } 125 if !chunk.Done { 126 chunks++ 127 } 128 } 129 130 if chunks >= 10 { 131 t.Errorf("Expected cancellation after few chunks, got %d chunks", chunks) 132 } 133 if !gotError { 134 t.Error("Expected error after context cancellation") 135 } 136} 137 138// TestOllamaProvider_Stream_EmptyChunks tests handling of empty content chunks 139func TestOllamaProvider_Stream_EmptyChunks(t *testing.T) { 140 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 141 w.Header().Set("Content-Type", "text/event-stream") 142 flusher := w.(http.Flusher) 143 144 chunks := []string{ 145 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":""},"finish_reason":null}]}`, 146 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":"Hi"},"finish_reason":null}]}`, 147 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":""},"finish_reason":null}]}`, 148 `data: [DONE]`, 149 } 150 151 for _, chunk := range chunks { 152 _, _ = fmt.Fprintf(w, "%s\n\n", chunk) 153 flusher.Flush() 154 } 155 })) 156 defer server.Close() 157 158 provider := NewOllama(server.URL, "test") 159 160 ctx := context.Background() 161 messages := make([]Message, 0, 1) 162 messages = append(messages, Message{Role: "user", Content: "test"}) 163 164 ch, err := provider.Stream(ctx, messages) 165 if err != nil { 166 t.Fatalf("Stream() failed: %v", err) 167 } 168 169 var result string 170 for chunk := range ch { 171 if chunk.Err != nil { 172 t.Fatalf("Stream chunk error: %v", chunk.Err) 173 } 174 if chunk.Done { 175 continue 176 } 177 result += chunk.Content 178 } 179 180 expected := "Hi" 181 if result != expected { 182 t.Errorf("Expected content %q, got %q", expected, result) 183 } 184} 185 186// TestOllamaProvider_Stream_ServerError tests handling of HTTP errors 187func TestOllamaProvider_Stream_ServerError(t *testing.T) { 188 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 189 w.WriteHeader(http.StatusInternalServerError) 190 _, _ = w.Write([]byte("Internal server error")) 191 })) 192 defer server.Close() 193 194 provider := NewOllama(server.URL, "test") 195 196 ctx := context.Background() 197 messages := make([]Message, 0, 1) 198 messages = append(messages, Message{Role: "user", Content: "test"}) 199 200 _, err := provider.Stream(ctx, messages) 201 if err == nil { 202 t.Fatal("Expected error from Stream(), got nil") 203 } 204 if !strings.Contains(err.Error(), "500") { 205 t.Errorf("Expected 500 error, got: %v", err) 206 } 207} 208 209// TestOpenCodeProvider_Stream tests basic streaming functionality 210func TestOpenCodeProvider_Stream(t *testing.T) { 211 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 212 // Verify Authorization header 213 auth := r.Header.Get("Authorization") 214 if auth != "Bearer test-api-key" { 215 t.Errorf("Expected Authorization header 'Bearer test-api-key', got %q", auth) 216 } 217 218 flusher, ok := w.(http.Flusher) 219 if !ok { 220 t.Fatal("Expected http.Flusher") 221 } 222 223 w.Header().Set("Content-Type", "text/event-stream") 224 225 chunks := []string{ 226 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":"OpenCode"},"finish_reason":null}]}`, 227 `data: {"id":"1","object":"chat.completion.chunk","created":1234567890,"model":"test","choices":[{"index":0,"delta":{"content":" response"},"finish_reason":null}]}`, 228 `data: [DONE]`, 229 } 230 231 for _, chunk := range chunks { 232 _, _ = fmt.Fprintf(w, "%s\n\n", chunk) 233 flusher.Flush() 234 } 235 })) 236 defer server.Close() 237 238 provider := NewOpenCode(server.URL, "test-model", "test-api-key") 239 240 ctx := context.Background() 241 messages := make([]Message, 0, 1) 242 messages = append(messages, Message{Role: "user", Content: "test"}) 243 244 ch, err := provider.Stream(ctx, messages) 245 if err != nil { 246 t.Fatalf("Stream() failed: %v", err) 247 } 248 249 var result string 250 var gotDone bool 251 for chunk := range ch { 252 if chunk.Err != nil { 253 t.Fatalf("Stream chunk error: %v", chunk.Err) 254 } 255 if chunk.Done { 256 gotDone = true 257 continue 258 } 259 result += chunk.Content 260 } 261 262 expected := "OpenCode response" 263 if result != expected { 264 t.Errorf("Expected content %q, got %q", expected, result) 265 } 266 if !gotDone { 267 t.Error("Expected Done=true chunk") 268 } 269} 270 271// TestOpenCodeProvider_Stream_Cancellation tests context cancellation 272func TestOpenCodeProvider_Stream_Cancellation(t *testing.T) { 273 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 274 w.Header().Set("Content-Type", "text/event-stream") 275 flusher := w.(http.Flusher) 276 277 for i := 0; i < 10; i++ { 278 _, _ = fmt.Fprintf(w, "data: {\"id\":\"1\",\"object\":\"chat.completion.chunk\",\"created\":1234567890,\"model\":\"test\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"chunk %d\"},\"finish_reason\":null}]}\n\n", i) 279 flusher.Flush() 280 time.Sleep(100 * time.Millisecond) 281 } 282 })) 283 defer server.Close() 284 285 provider := NewOpenCode(server.URL, "test", "key") 286 287 ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) 288 defer cancel() 289 290 messages := make([]Message, 0, 1) 291 messages = append(messages, Message{Role: "user", Content: "test"}) 292 ch, err := provider.Stream(ctx, messages) 293 if err != nil { 294 t.Fatalf("Stream() failed: %v", err) 295 } 296 297 var chunks int 298 for chunk := range ch { 299 if chunk.Err != nil { 300 // Expected timeout error 301 break 302 } 303 if !chunk.Done { 304 chunks++ 305 } 306 } 307 308 if chunks >= 10 { 309 t.Errorf("Expected timeout after few chunks, got %d chunks", chunks) 310 } 311} 312 313// TestStreamChunk_Fields tests StreamChunk field structure 314func TestStreamChunk_Fields(t *testing.T) { 315 tests := []struct { 316 name string 317 chunk StreamChunk 318 }{ 319 { 320 name: "content_chunk", 321 chunk: StreamChunk{Content: "hello", Done: false, Err: nil}, 322 }, 323 { 324 name: "done_chunk", 325 chunk: StreamChunk{Content: "", Done: true, Err: nil}, 326 }, 327 { 328 name: "error_chunk", 329 chunk: StreamChunk{Content: "", Done: false, Err: errors.New("error")}, 330 }, 331 } 332 333 for _, tt := range tests { 334 t.Run(tt.name, func(t *testing.T) { 335 if tt.name == "content_chunk" && tt.chunk.Content != "hello" { 336 t.Errorf("Expected content='hello', got %q", tt.chunk.Content) 337 } 338 if tt.name == "done_chunk" && !tt.chunk.Done { 339 t.Error("Expected Done=true") 340 } 341 if tt.name == "error_chunk" && tt.chunk.Err == nil { 342 t.Error("Expected non-nil error") 343 } 344 }) 345 } 346} 347 348// TestOllamaProvider_Stream_NoChoices tests handling of responses with no choices 349func TestOllamaProvider_Stream_NoChoices(t *testing.T) { 350 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 351 w.Header().Set("Content-Type", "text/event-stream") 352 flusher := w.(http.Flusher) 353 354 // Send chunk with empty choices array 355 _, _ = fmt.Fprintf(w, "data: {\"id\":\"1\",\"object\":\"chat.completion.chunk\",\"created\":1234567890,\"model\":\"test\",\"choices\":[]}\n\n") 356 flusher.Flush() 357 358 _, _ = fmt.Fprintf(w, "data: [DONE]\n\n") 359 flusher.Flush() 360 })) 361 defer server.Close() 362 363 provider := NewOllama(server.URL, "test") 364 365 ctx := context.Background() 366 messages := []Message{{Role: "user", Content: "test"}} 367 368 ch, err := provider.Stream(ctx, messages) 369 if err != nil { 370 t.Fatalf("Stream() failed: %v", err) 371 } 372 373 var contentChunks int 374 for chunk := range ch { 375 if chunk.Err != nil { 376 t.Fatalf("Stream chunk error: %v", chunk.Err) 377 } 378 if !chunk.Done && chunk.Content != "" { 379 contentChunks++ 380 } 381 } 382 383 // Should handle gracefully - no content chunks expected 384 if contentChunks > 0 { 385 t.Errorf("Expected 0 content chunks for empty choices, got %d", contentChunks) 386 } 387} 388 389// TestProvider_Stream_Interface tests that providers implement Stream correctly 390func TestProvider_Stream_Interface(t *testing.T) { 391 tests := []struct { 392 name string 393 provider Provider 394 }{ 395 { 396 name: "ollama_provider", 397 provider: NewOllama("http://localhost:11434", "test"), 398 }, 399 { 400 name: "opencode_provider", 401 provider: NewOpenCode("https://api.test.com", "test", "key"), 402 }, 403 } 404 405 for _, tt := range tests { 406 t.Run(tt.name, func(t *testing.T) { 407 // Verify Stream method exists and returns correct types 408 ctx := context.Background() 409 messages := make([]Message, 0, 1) 410 messages = append(messages, Message{Role: "user", Content: "test"}) 411 412 // This will fail if server is not available, but that's expected 413 // We're just verifying the method signature 414 _, err := tt.provider.Stream(ctx, messages) 415 // Error is expected (no server), we just want to verify method exists 416 _ = err 417 }) 418 } 419}