xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / provider / opencode_test.go

12.6 KB
1package provider
2
3import (
4	"context"
5	"encoding/json"
6	"net/http"
7	"net/http/httptest"
8	"strings"
9	"testing"
10)
11
12// TestOpenCode_SystemPromptOnly tests that OpenCode provider handles system-only
13// messages correctly by NOT adding synthetic messages at the provider layer.
14// Synthetic messages like "Continue your mission..." are added by getContextMemories()
15// in the core layer, not by the provider.
16func TestOpenCode_SystemPromptOnly(t *testing.T) {
17	// Setup mock server
18	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19		// Parse request body
20		var req struct {
21			Messages []struct {
22				Role    string `json:"role"`
23				Content string `json:"content"`
24			} `json:"messages"`
25		}
26		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
27			t.Fatalf("failed to decode request: %v", err)
28		}
29
30		// Verify system message is first
31		if len(req.Messages) < 1 {
32			t.Fatal("expected at least 1 message")
33		}
34		if req.Messages[0].Role != "system" {
35			t.Errorf("expected first message role=system, got %s", req.Messages[0].Role)
36		}
37
38		// Provider should NOT add "Begin." or "Continue." messages
39		// Those are added by getContextMemories() in core layer
40		for _, msg := range req.Messages {
41			if msg.Content == "Begin." || msg.Content == "Continue." {
42				t.Errorf("Provider should not add synthetic messages, found: %q", msg.Content)
43			}
44		}
45
46		// Return mock response
47		w.Header().Set("Content-Type", "application/json")
48		json.NewEncoder(w).Encode(map[string]interface{}{
49			"choices": []map[string]interface{}{
50				{
51					"message": map[string]interface{}{
52						"role":    "assistant",
53						"content": "Ready.",
54					},
55				},
56			},
57		})
58	}))
59	defer server.Close()
60
61	// Create provider
62	provider := NewOpenCode(server.URL, "test-model", "test-key")
63
64	// Send only system messages
65	messages := []Message{
66		{Role: "system", Content: "You are a helpful assistant."},
67	}
68
69	ctx := context.Background()
70	response, err := provider.Chat(ctx, messages)
71	if err != nil {
72		t.Fatalf("Chat() error: %v", err)
73	}
74
75	if response != "Ready." {
76		t.Errorf("expected response='Ready.', got %q", response)
77	}
78}
79
80// TestOpenCode_ToolCallsAndResults tests that OpenCode provider correctly handles
81// messages with tool calls and tool results.
82func TestOpenCode_ToolCallsAndResults(t *testing.T) {
83	// Setup mock server
84	requestCount := 0
85	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86		requestCount++
87
88		var req struct {
89			Messages []struct {
90				Role      string `json:"role"`
91				Content   string `json:"content"`
92				ToolCalls []struct {
93					ID       string `json:"id"`
94					Function struct {
95						Name      string `json:"name"`
96						Arguments string `json:"arguments"`
97					} `json:"function"`
98				} `json:"tool_calls,omitempty"`
99				ToolCallID string `json:"tool_call_id,omitempty"`
100			} `json:"messages"`
101			Tools []map[string]interface{} `json:"tools,omitempty"`
102		}
103		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
104			t.Fatalf("failed to decode request: %v", err)
105		}
106
107		w.Header().Set("Content-Type", "application/json")
108
109		// First request: return tool call
110		if requestCount == 1 {
111			// Verify tools are present
112			if len(req.Tools) == 0 {
113				t.Error("expected tools in request")
114			}
115
116			// Return tool call response
117			json.NewEncoder(w).Encode(map[string]interface{}{
118				"choices": []map[string]interface{}{
119					{
120						"message": map[string]interface{}{
121							"role":    "assistant",
122							"content": "",
123							"tool_calls": []map[string]interface{}{
124								{
125									"id":   "call_123",
126									"type": "function",
127									"function": map[string]interface{}{
128										"name":      "get_weather",
129										"arguments": `{"location":"NYC"}`,
130									},
131								},
132							},
133						},
134					},
135				},
136			})
137			return
138		}
139
140		// Second request: verify tool result is included
141		if requestCount == 2 {
142			foundToolResult := false
143			for _, msg := range req.Messages {
144				if msg.Role == "tool" && msg.ToolCallID == "call_123" {
145					foundToolResult = true
146					if !strings.Contains(msg.Content, "72") {
147						t.Errorf("expected tool result to contain '72', got %q", msg.Content)
148					}
149				}
150			}
151			if !foundToolResult {
152				t.Error("expected tool result message in second request")
153			}
154
155			// Return final response
156			json.NewEncoder(w).Encode(map[string]interface{}{
157				"choices": []map[string]interface{}{
158					{
159						"message": map[string]interface{}{
160							"role":    "assistant",
161							"content": "The weather in NYC is 72°F.",
162						},
163					},
164				},
165			})
166		}
167	}))
168	defer server.Close()
169
170	provider := NewOpenCode(server.URL, "test-model", "test-key")
171	ctx := context.Background()
172
173	// First call with tools
174	tools := []Tool{
175		{
176			Name:        "get_weather",
177			Description: "Get current weather",
178			Parameters:  json.RawMessage(`{"type":"object","properties":{"location":{"type":"string"}}}`),
179		},
180	}
181
182	messages := []Message{
183		{Role: "user", Content: "What's the weather in NYC?"},
184	}
185
186	resp1, err := provider.ChatWithTools(ctx, messages, tools)
187	if err != nil {
188		t.Fatalf("ChatWithTools() error: %v", err)
189	}
190
191	// Verify tool call
192	if len(resp1.ToolCalls) != 1 {
193		t.Fatalf("expected 1 tool call, got %d", len(resp1.ToolCalls))
194	}
195	if resp1.ToolCalls[0].ID != "call_123" {
196		t.Errorf("expected tool call ID=call_123, got %s", resp1.ToolCalls[0].ID)
197	}
198	if resp1.ToolCalls[0].Name != "get_weather" {
199		t.Errorf("expected tool name=get_weather, got %s", resp1.ToolCalls[0].Name)
200	}
201
202	// Second call with tool result
203	messages = append(messages, Message{
204		Role:      "assistant",
205		Content:   "",
206		ToolCalls: resp1.ToolCalls,
207	})
208	messages = append(messages, Message{
209		Role:       "tool",
210		Content:    `{"temp":72,"condition":"sunny"}`,
211		ToolCallID: "call_123",
212	})
213
214	resp2, err := provider.ChatWithTools(ctx, messages, tools)
215	if err != nil {
216		t.Fatalf("ChatWithTools() error: %v", err)
217	}
218
219	if !strings.Contains(resp2.Content, "72") {
220		t.Errorf("expected response to contain '72', got %q", resp2.Content)
221	}
222}
223
224// TestOpenCode_InvalidToolSchema tests that OpenCode provider returns error
225// for invalid tool schemas.
226func TestOpenCode_InvalidToolSchema(t *testing.T) {
227	// Setup mock server (won't be called)
228	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
229		t.Error("server should not be called with invalid tool schema")
230	}))
231	defer server.Close()
232
233	provider := NewOpenCode(server.URL, "test-model", "test-key")
234	ctx := context.Background()
235
236	// Create tool with invalid JSON schema
237	tools := []Tool{
238		{
239			Name:        "bad_tool",
240			Description: "Tool with invalid schema",
241			Parameters:  json.RawMessage(`{invalid json}`), // Invalid JSON
242		},
243	}
244
245	messages := []Message{
246		{Role: "user", Content: "Do something"},
247	}
248
249	_, err := provider.ChatWithTools(ctx, messages, tools)
250	if err == nil {
251		t.Fatal("expected error for invalid tool schema")
252	}
253
254	// Verify error message mentions invalid schema
255	errMsg := err.Error()
256	if !strings.Contains(errMsg, "invalid tool schema") {
257		t.Errorf("expected error message to contain 'invalid tool schema', got %q", errMsg)
258	}
259}
260
261// TestOpenCode_MultipleSystemMessages tests that multiple system messages
262// are merged into a single message at the start (OpenAI requirement).
263func TestOpenCode_MultipleSystemMessages(t *testing.T) {
264	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
265		var req struct {
266			Messages []struct {
267				Role    string `json:"role"`
268				Content string `json:"content"`
269			} `json:"messages"`
270		}
271		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
272			t.Fatalf("failed to decode request: %v", err)
273		}
274
275		// Count system messages
276		systemCount := 0
277		for _, msg := range req.Messages {
278			if msg.Role == "system" {
279				systemCount++
280				// Verify merged content contains both instructions
281				if !strings.Contains(msg.Content, "helpful assistant") {
282					t.Error("expected merged system message to contain 'helpful assistant'")
283				}
284				if !strings.Contains(msg.Content, "Be concise") {
285					t.Error("expected merged system message to contain 'Be concise'")
286				}
287			}
288		}
289
290		// Verify only one system message
291		if systemCount != 1 {
292			t.Errorf("expected 1 system message after merge, got %d", systemCount)
293		}
294
295		// Verify system message is first
296		if req.Messages[0].Role != "system" {
297			t.Errorf("expected first message to be system, got %s", req.Messages[0].Role)
298		}
299
300		w.Header().Set("Content-Type", "application/json")
301		json.NewEncoder(w).Encode(map[string]interface{}{
302			"choices": []map[string]interface{}{
303				{
304					"message": map[string]interface{}{
305						"role":    "assistant",
306						"content": "Understood.",
307					},
308				},
309			},
310		})
311	}))
312	defer server.Close()
313
314	provider := NewOpenCode(server.URL, "test-model", "test-key")
315	ctx := context.Background()
316
317	// Send multiple system messages scattered in conversation
318	messages := []Message{
319		{Role: "system", Content: "You are a helpful assistant."},
320		{Role: "user", Content: "Hello"},
321		{Role: "system", Content: "Be concise in your responses."},
322		{Role: "assistant", Content: "Hi"},
323	}
324
325	_, err := provider.Chat(ctx, messages)
326	if err != nil {
327		t.Fatalf("Chat() error: %v", err)
328	}
329}
330
331// TestOpenCode_PreservesConversationHistory tests that OpenCode provider
332// preserves conversation history and does not collapse messages aggressively.
333// This test simulates the exact failure scenario from logs where 21 messages
334// were being collapsed to 2 messages.
335func TestOpenCode_PreservesConversationHistory(t *testing.T) {
336	// Simulate the exact scenario from logs:
337	// 21 messages (20 system + 1 assistant) should NOT collapse to 2 messages
338
339	// Create mock server
340	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
341		// Parse request
342		var req struct {
343			Model    string `json:"model"`
344			Messages []struct {
345				Role    string `json:"role"`
346				Content string `json:"content"`
347			} `json:"messages"`
348			Stream bool `json:"stream"`
349		}
350		json.NewDecoder(r.Body).Decode(&req)
351
352		// Verify Stream is false
353		if req.Stream {
354			t.Error("Expected Stream:false, got Stream:true")
355		}
356
357		// Verify we have reasonable message count (not collapsed to 2)
358		if len(req.Messages) < 3 {
359			t.Errorf("Expected at least 3 messages (system + user + assistant), got %d", len(req.Messages))
360		}
361
362		// Verify first message is system
363		if req.Messages[0].Role != "system" {
364			t.Errorf("Expected first message to be system, got %s", req.Messages[0].Role)
365		}
366
367		// Verify we have user messages
368		hasUser := false
369		for _, msg := range req.Messages {
370			if msg.Role == "user" {
371				hasUser = true
372				break
373			}
374		}
375		if !hasUser {
376			t.Error("Expected at least one user message")
377		}
378
379		// Return valid response
380		w.Header().Set("Content-Type", "application/json")
381		json.NewEncoder(w).Encode(map[string]interface{}{
382			"choices": []map[string]interface{}{
383				{
384					"message": map[string]interface{}{
385						"role":    "assistant",
386						"content": "Response",
387					},
388				},
389			},
390		})
391	}))
392	defer server.Close()
393
394	// Create provider with mock server
395	provider := NewOpenCodeWithTemp("opencode_zen", server.URL, "test-model", "test-key", 0.7)
396
397	// Create messages simulating the failure scenario
398	messages := []Message{
399		{Role: "system", Content: "System prompt 1"},
400		{Role: "user", Content: "User message 1"},
401		{Role: "assistant", Content: "Assistant response 1"},
402		{Role: "system", Content: "Context update 1"},
403		{Role: "user", Content: "User message 2"},
404		{Role: "assistant", Content: "Assistant response 2"},
405	}
406
407	// Call Chat
408	_, err := provider.Chat(context.Background(), messages)
409	if err != nil {
410		t.Fatalf("Chat failed: %v", err)
411	}
412}
413
414// TestOpenCode_StreamParameterSetCorrectly tests that the Stream parameter
415// is explicitly set to false for non-streaming requests.
416func TestOpenCode_StreamParameterSetCorrectly(t *testing.T) {
417	// Verify Stream:false is explicitly set
418
419	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
420		var req struct {
421			Stream bool `json:"stream"`
422		}
423		json.NewDecoder(r.Body).Decode(&req)
424
425		// THIS IS THE CRITICAL TEST
426		if req.Stream {
427			t.Error("FAIL: Stream parameter is true, should be false")
428		}
429
430		w.Header().Set("Content-Type", "application/json")
431		json.NewEncoder(w).Encode(map[string]interface{}{
432			"choices": []map[string]interface{}{
433				{"message": map[string]interface{}{"role": "assistant", "content": "ok"}},
434			},
435		})
436	}))
437	defer server.Close()
438
439	provider := NewOpenCodeWithTemp("opencode_zen", server.URL, "test-model", "test-key", 0.7)
440
441	messages := []Message{
442		{Role: "user", Content: "Test"},
443	}
444
445	_, err := provider.Chat(context.Background(), messages)
446	if err != nil {
447		t.Fatalf("Chat failed: %v", err)
448	}
449}