xonecas.io
6119
My

mysis / files

mysis / internal / provider / opencode_test.go

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