xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / provider / provider_test.go

14.3 KB
1package provider
2
3import (
4	"context"
5	"encoding/json"
6	"errors"
7	"strings"
8	"testing"
9	"time"
10)
11
12func TestRegistry(t *testing.T) {
13	reg := NewRegistry()
14
15	// Register mock providers
16	reg.RegisterFactory("provider1", NewMockFactory("provider1", "response1"))
17	reg.RegisterFactory("provider2", NewMockFactory("provider2", "response2"))
18
19	// Get existing provider
20	p, err := reg.Create("provider1", "model", 0.7)
21	if err != nil {
22		t.Fatalf("Create() error: %v", err)
23	}
24	if p.Name() != "provider1" {
25		t.Errorf("expected name=provider1, got %s", p.Name())
26	}
27
28	// Get non-existent provider
29	_, err = reg.Create("nonexistent", "model", 0.7)
30	if !errors.Is(err, ErrProviderNotFound) {
31		t.Errorf("expected ErrProviderNotFound, got %v", err)
32	}
33
34	// List providers
35	names := reg.List()
36	if len(names) != 2 {
37		t.Errorf("expected 2 providers, got %d", len(names))
38	}
39}
40
41func TestMockProviderChat(t *testing.T) {
42	mock := NewMock("test", "Hello, World!")
43
44	ctx := context.Background()
45	messages := []Message{{Role: "user", Content: "Hi"}}
46
47	response, err := mock.Chat(ctx, messages)
48	if err != nil {
49		t.Fatalf("Chat() error: %v", err)
50	}
51	if response != "Hello, World!" {
52		t.Errorf("expected 'Hello, World!', got %s", response)
53	}
54}
55
56func TestMockProviderChatDelay(t *testing.T) {
57	delay := 30 * time.Millisecond
58	mock := NewMock("test", "ok").SetDelay(delay)
59
60	ctx := context.Background()
61	messages := []Message{{Role: "user", Content: "Hi"}}
62
63	start := time.Now()
64	_, err := mock.Chat(ctx, messages)
65	if err != nil {
66		t.Fatalf("Chat() error: %v", err)
67	}
68	elapsed := time.Since(start)
69	if elapsed < delay {
70		t.Fatalf("expected delay >= %v, got %v", delay, elapsed)
71	}
72}
73
74func TestMockProviderChatWithToolsDelay(t *testing.T) {
75	delay := 25 * time.Millisecond
76	mock := NewMock("test", "ok").SetDelay(delay)
77
78	ctx := context.Background()
79	messages := []Message{{Role: "user", Content: "Hi"}}
80
81	start := time.Now()
82	_, err := mock.ChatWithTools(ctx, messages, nil)
83	if err != nil {
84		t.Fatalf("ChatWithTools() error: %v", err)
85	}
86	elapsed := time.Since(start)
87	if elapsed < delay {
88		t.Fatalf("expected delay >= %v, got %v", delay, elapsed)
89	}
90}
91
92func TestMockProviderChatDelayContextCancel(t *testing.T) {
93	mock := NewMock("test", "ok").SetDelay(100 * time.Millisecond)
94
95	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
96	defer cancel()
97
98	messages := []Message{{Role: "user", Content: "Hi"}}
99	_, err := mock.Chat(ctx, messages)
100	if err == nil {
101		t.Fatal("expected context deadline error")
102	}
103}
104
105func TestMockProviderChatError(t *testing.T) {
106	expectedErr := errors.New("chat error")
107	mock := NewMock("test", "").WithChatError(expectedErr)
108
109	ctx := context.Background()
110	messages := []Message{{Role: "user", Content: "Hi"}}
111
112	_, err := mock.Chat(ctx, messages)
113	if !errors.Is(err, expectedErr) {
114		t.Errorf("expected %v, got %v", expectedErr, err)
115	}
116}
117
118func TestMockProviderStream(t *testing.T) {
119	mock := NewMock("test", "Streamed response")
120
121	ctx := context.Background()
122	messages := []Message{{Role: "user", Content: "Hi"}}
123
124	ch, err := mock.Stream(ctx, messages)
125	if err != nil {
126		t.Fatalf("Stream() error: %v", err)
127	}
128
129	var content string
130	var done bool
131	for chunk := range ch {
132		if chunk.Err != nil {
133			t.Fatalf("Stream chunk error: %v", chunk.Err)
134		}
135		if chunk.Done {
136			done = true
137			continue
138		}
139		content += chunk.Content
140	}
141
142	if content != "Streamed response" {
143		t.Errorf("expected 'Streamed response', got %s", content)
144	}
145	if !done {
146		t.Error("expected done=true")
147	}
148}
149
150func TestMockProviderStreamError(t *testing.T) {
151	expectedErr := errors.New("stream error")
152	mock := NewMock("test", "").WithStreamError(expectedErr)
153
154	ctx := context.Background()
155	messages := []Message{{Role: "user", Content: "Hi"}}
156
157	_, err := mock.Stream(ctx, messages)
158	if !errors.Is(err, expectedErr) {
159		t.Errorf("expected %v, got %v", expectedErr, err)
160	}
161}
162
163func TestOllamaProviderName(t *testing.T) {
164	p := NewOllama("http://localhost:11434", "llama3")
165	if p.Name() != "ollama" {
166		t.Errorf("expected name=ollama, got %s", p.Name())
167	}
168}
169
170func TestOpenCodeProviderName(t *testing.T) {
171	p := NewOpenCode("https://api.opencode.ai/v1", "zen-default", "test-key")
172	if p.Name() != "opencode_zen" {
173		t.Errorf("expected name=opencode_zen, got %s", p.Name())
174	}
175}
176
177func TestToOpenAIMessages(t *testing.T) {
178	messages := []Message{
179		{Role: "system", Content: "You are helpful."},
180		{Role: "user", Content: "Hello"},
181		{Role: "assistant", Content: "Hi there!"},
182	}
183
184	result := toOpenAIMessages(messages)
185
186	if len(result) != 3 {
187		t.Fatalf("expected 3 messages, got %d", len(result))
188	}
189	if result[0].Role != "system" {
190		t.Errorf("expected role=system, got %s", result[0].Role)
191	}
192	if result[1].Content != "Hello" {
193		t.Errorf("expected content=Hello, got %s", result[1].Content)
194	}
195}
196
197func TestToOpenAIMessagesWithToolCalls(t *testing.T) {
198	messages := []Message{
199		{Role: "user", Content: "What's the weather?"},
200		{
201			Role:    "assistant",
202			Content: "",
203			ToolCalls: []ToolCall{
204				{ID: "call_123", Name: "get_weather", Arguments: json.RawMessage(`{"location":"NYC"}`)},
205			},
206		},
207		{Role: "tool", Content: `{"temp": 72}`, ToolCallID: "call_123"},
208	}
209
210	result := toOpenAIMessages(messages)
211
212	if len(result) != 3 {
213		t.Fatalf("expected 3 messages, got %d", len(result))
214	}
215
216	// Check assistant message has tool calls
217	assistantMsg := result[1]
218	if len(assistantMsg.ToolCalls) != 1 {
219		t.Fatalf("expected 1 tool call, got %d", len(assistantMsg.ToolCalls))
220	}
221	if assistantMsg.ToolCalls[0].ID != "call_123" {
222		t.Errorf("expected tool call ID=call_123, got %s", assistantMsg.ToolCalls[0].ID)
223	}
224	if assistantMsg.ToolCalls[0].Function.Name != "get_weather" {
225		t.Errorf("expected function name=get_weather, got %s", assistantMsg.ToolCalls[0].Function.Name)
226	}
227
228	// Check tool result message has ToolCallID
229	toolMsg := result[2]
230	if toolMsg.ToolCallID != "call_123" {
231		t.Errorf("expected ToolCallID=call_123, got %s", toolMsg.ToolCallID)
232	}
233}
234
235func TestMockProviderChatWithTools(t *testing.T) {
236	toolCalls := []ToolCall{
237		{ID: "call_abc", Name: "test_tool", Arguments: json.RawMessage(`{}`)},
238	}
239	mock := NewMock("test", "thinking...").WithToolCalls(toolCalls)
240
241	ctx := context.Background()
242	messages := []Message{{Role: "user", Content: "Do something"}}
243	tools := []Tool{{Name: "test_tool", Description: "A test tool"}}
244
245	resp, err := mock.ChatWithTools(ctx, messages, tools)
246	if err != nil {
247		t.Fatalf("ChatWithTools() error: %v", err)
248	}
249
250	if resp.Content != "thinking..." {
251		t.Errorf("expected content='thinking...', got %s", resp.Content)
252	}
253	if len(resp.ToolCalls) != 1 {
254		t.Fatalf("expected 1 tool call, got %d", len(resp.ToolCalls))
255	}
256	if resp.ToolCalls[0].Name != "test_tool" {
257		t.Errorf("expected tool name=test_tool, got %s", resp.ToolCalls[0].Name)
258	}
259}
260
261func TestMockProviderChatWithToolsError(t *testing.T) {
262	expectedErr := errors.New("tools error")
263	mock := NewMock("test", "").WithChatError(expectedErr)
264
265	ctx := context.Background()
266	messages := []Message{{Role: "user", Content: "Do something"}}
267	tools := []Tool{{Name: "test_tool", Description: "A test tool"}}
268
269	_, err := mock.ChatWithTools(ctx, messages, tools)
270	if !errors.Is(err, expectedErr) {
271		t.Errorf("expected %v, got %v", expectedErr, err)
272	}
273}
274
275func TestMockProviderWithResponse(t *testing.T) {
276	mock := NewMock("test", "initial")
277
278	ctx := context.Background()
279	messages := []Message{{Role: "user", Content: "Hi"}}
280
281	// Initial response
282	resp, _ := mock.Chat(ctx, messages)
283	if resp != "initial" {
284		t.Errorf("expected 'initial', got %s", resp)
285	}
286
287	// Change response
288	mock.WithResponse("updated")
289	resp, _ = mock.Chat(ctx, messages)
290	if resp != "updated" {
291		t.Errorf("expected 'updated', got %s", resp)
292	}
293}
294
295func TestMockProviderWithReasoning(t *testing.T) {
296	reasoning := "thinking..."
297	mock := NewMock("test", "response").WithReasoning(reasoning)
298
299	ctx := context.Background()
300	messages := []Message{{Role: "user", Content: "Hi"}}
301	tools := []Tool{{Name: "test_tool", Description: "A test tool"}}
302
303	resp, err := mock.ChatWithTools(ctx, messages, tools)
304	if err != nil {
305		t.Fatalf("ChatWithTools() error: %v", err)
306	}
307	if resp.Reasoning != reasoning {
308		t.Errorf("expected reasoning=%q, got %q", reasoning, resp.Reasoning)
309	}
310}
311
312func TestMockProviderConcurrentAccess(t *testing.T) {
313	mock := NewMock("test", "response")
314	ctx := context.Background()
315	messages := []Message{{Role: "user", Content: "Hi"}}
316
317	// Run concurrent reads and writes
318	done := make(chan bool)
319	for i := 0; i < 10; i++ {
320		go func() {
321			mock.Chat(ctx, messages)
322			done <- true
323		}()
324		go func() {
325			mock.WithResponse("new response")
326			done <- true
327		}()
328	}
329
330	// Wait for all goroutines
331	for i := 0; i < 20; i++ {
332		<-done
333	}
334}
335
336func TestToOpenAIMessagesEmpty(t *testing.T) {
337	result := toOpenAIMessages([]Message{})
338	if len(result) != 0 {
339		t.Errorf("expected empty slice, got %d messages", len(result))
340	}
341}
342
343func TestToOpenAIMessagesMultipleToolCalls(t *testing.T) {
344	messages := []Message{
345		{
346			Role:    "assistant",
347			Content: "",
348			ToolCalls: []ToolCall{
349				{ID: "call_1", Name: "tool_a", Arguments: json.RawMessage(`{"a":1}`)},
350				{ID: "call_2", Name: "tool_b", Arguments: json.RawMessage(`{"b":2}`)},
351			},
352		},
353	}
354
355	result := toOpenAIMessages(messages)
356
357	if len(result[0].ToolCalls) != 2 {
358		t.Fatalf("expected 2 tool calls, got %d", len(result[0].ToolCalls))
359	}
360	if result[0].ToolCalls[0].Function.Arguments != `{"a":1}` {
361		t.Errorf("expected arguments={\"a\":1}, got %s", result[0].ToolCalls[0].Function.Arguments)
362	}
363	if result[0].ToolCalls[1].Function.Arguments != `{"b":2}` {
364		t.Errorf("expected arguments={\"b\":2}, got %s", result[0].ToolCalls[1].Function.Arguments)
365	}
366}
367
368func TestToOllamaMessagesSerializesEmptyContent(t *testing.T) {
369	messages := []Message{{Role: "assistant", Content: ""}}
370
371	result := toOllamaMessages(messages)
372	if len(result) != 1 {
373		t.Fatalf("expected 1 message, got %d", len(result))
374	}
375
376	data, err := json.Marshal(result[0])
377	if err != nil {
378		t.Fatalf("marshal error: %v", err)
379	}
380
381	if !strings.Contains(string(data), `"content":""`) {
382		t.Fatalf("expected content field to be serialized, got %s", string(data))
383	}
384}
385
386func TestRegistry_MultipleProvidersWithSameFactory(t *testing.T) {
387	registry := NewRegistry()
388
389	// Register two providers using same factory type
390	factory1 := NewMockFactory("mock1", "response1")
391	factory2 := NewMockFactory("mock2", "response2")
392
393	registry.RegisterFactory("provider-one", factory1)
394	registry.RegisterFactory("provider-two", factory2)
395
396	// Should be able to create both
397	p1, err := registry.Create("provider-one", "model1", 0.7)
398	if err != nil {
399		t.Fatalf("Create provider-one failed: %v", err)
400	}
401
402	p2, err := registry.Create("provider-two", "model2", 0.7)
403	if err != nil {
404		t.Fatalf("Create provider-two failed: %v", err)
405	}
406
407	// Should be different instances
408	if p1 == p2 {
409		t.Error("Expected different provider instances")
410	}
411
412	// Verify correct responses
413	ctx := context.Background()
414	messages := []Message{{Role: "user", Content: "Hi"}}
415
416	resp1, err := p1.Chat(ctx, messages)
417	if err != nil {
418		t.Fatalf("p1.Chat() error: %v", err)
419	}
420	if resp1 != "response1" {
421		t.Errorf("expected response1, got %s", resp1)
422	}
423
424	resp2, err := p2.Chat(ctx, messages)
425	if err != nil {
426		t.Fatalf("p2.Chat() error: %v", err)
427	}
428	if resp2 != "response2" {
429		t.Errorf("expected response2, got %s", resp2)
430	}
431}
432
433func TestRegistry_ConfigKeyOverridesFactoryName(t *testing.T) {
434	registry := NewRegistry()
435
436	// Factory has internal name "ollama", but register it with config key "ollama-llama"
437	factory := NewMockFactory("ollama", "llama-response")
438	registry.RegisterFactory("ollama-llama", factory)
439
440	// Should be accessible by config key, not factory name
441	p, err := registry.Create("ollama-llama", "llama3.1:8b", 0.7)
442	if err != nil {
443		t.Fatalf("Create ollama-llama failed: %v", err)
444	}
445
446	ctx := context.Background()
447	messages := []Message{{Role: "user", Content: "Hi"}}
448	resp, err := p.Chat(ctx, messages)
449	if err != nil {
450		t.Fatalf("Chat() error: %v", err)
451	}
452	if resp != "llama-response" {
453		t.Errorf("expected llama-response, got %s", resp)
454	}
455
456	// Should NOT be accessible by factory name
457	_, err = registry.Create("ollama", "model", 0.7)
458	if !errors.Is(err, ErrProviderNotFound) {
459		t.Errorf("expected ErrProviderNotFound for factory name, got %v", err)
460	}
461}
462
463func TestRegistry_ListReturnsConfigKeys(t *testing.T) {
464	registry := NewRegistry()
465
466	// Register with config keys
467	registry.RegisterFactory("zen-nano", NewMockFactory("opencode_zen", "nano"))
468	registry.RegisterFactory("zen-pickle", NewMockFactory("opencode_zen", "pickle"))
469	registry.RegisterFactory("ollama-llama", NewMockFactory("ollama", "llama"))
470	registry.RegisterFactory("ollama-qwen", NewMockFactory("ollama", "qwen"))
471
472	names := registry.List()
473	if len(names) != 4 {
474		t.Fatalf("expected 4 providers, got %d", len(names))
475	}
476
477	// Should contain config keys, not factory names
478	expectedKeys := map[string]bool{
479		"zen-nano":     false,
480		"zen-pickle":   false,
481		"ollama-llama": false,
482		"ollama-qwen":  false,
483	}
484
485	for _, name := range names {
486		if _, ok := expectedKeys[name]; ok {
487			expectedKeys[name] = true
488		} else {
489			t.Errorf("unexpected provider name: %s", name)
490		}
491	}
492
493	for key, found := range expectedKeys {
494		if !found {
495			t.Errorf("expected config key %s not found in list", key)
496		}
497	}
498}
499
500// TestMockProviderClose verifies Close() is idempotent and returns nil.
501func TestMockProviderClose(t *testing.T) {
502	mock := NewMock("test", "response")
503
504	// First close should succeed
505	if err := mock.Close(); err != nil {
506		t.Errorf("first Close() error: %v", err)
507	}
508
509	// Second close should also succeed (idempotent)
510	if err := mock.Close(); err != nil {
511		t.Errorf("second Close() error: %v", err)
512	}
513}
514
515// TestOllamaProviderClose verifies Close() is idempotent and returns nil.
516func TestOllamaProviderClose(t *testing.T) {
517	provider := NewOllama("http://localhost:11434", "llama3")
518
519	// First close should succeed
520	if err := provider.Close(); err != nil {
521		t.Errorf("first Close() error: %v", err)
522	}
523
524	// Second close should also succeed (idempotent)
525	if err := provider.Close(); err != nil {
526		t.Errorf("second Close() error: %v", err)
527	}
528}
529
530// TestOpenCodeProviderClose verifies Close() is idempotent and returns nil.
531func TestOpenCodeProviderClose(t *testing.T) {
532	provider := NewOpenCode("https://api.test.com", "model", "key")
533
534	// First close should succeed
535	if err := provider.Close(); err != nil {
536		t.Errorf("first Close() error: %v", err)
537	}
538
539	// Second close should also succeed (idempotent)
540	if err := provider.Close(); err != nil {
541		t.Errorf("second Close() error: %v", err)
542	}
543}