xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / provider / streaming_test.go

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