xonecas.io
60255
Zn

Zoea-Nova / commit d6b9f75

test(provider): add OpenAI compliance integration tests

xonecas2026-02-06 d6b9f75c17becd4a5c9dbaeb88f474b20825e679 parent 41d4895

2 files changed

internal/provider/ollama_test.go+433−0
@@ -0,0 +1,433 @@
1+package provider
2+
3+import (
4+ "context"
5+ "encoding/json"
6+ "net/http"
7+ "net/http/httptest"
8+ "strings"
9+ "testing"
10+)
11+
12+// TestOllama_SystemMessagesAnywhere tests that Ollama provider correctly handles
13+// system messages anywhere in the conversation (not just at the start).
14+func TestOllama_SystemMessagesAnywhere(t *testing.T) {
15+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16+ var req struct {
17+ Messages []struct {
18+ Role string `json:"role"`
19+ Content string `json:"content"`
20+ } `json:"messages"`
21+ }
22+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
23+ t.Fatalf("failed to decode request: %v", err)
24+ }
25+
26+ // Verify message order: system, user, system (merged), assistant
27+ // Ollama allows system messages anywhere, but consecutive ones should be merged
28+ expectedRoles := []string{"system", "user", "system", "assistant"}
29+ if len(req.Messages) != len(expectedRoles) {
30+ t.Errorf("expected %d messages, got %d", len(expectedRoles), len(req.Messages))
31+ }
32+
33+ for i, expected := range expectedRoles {
34+ if i >= len(req.Messages) {
35+ break
36+ }
37+ if req.Messages[i].Role != expected {
38+ t.Errorf("message %d: expected role=%s, got %s", i, expected, req.Messages[i].Role)
39+ }
40+ }
41+
42+ // Verify first system message
43+ if !strings.Contains(req.Messages[0].Content, "helpful assistant") {
44+ t.Error("expected first system message to contain 'helpful assistant'")
45+ }
46+
47+ // Verify third message merges two consecutive system messages
48+ if !strings.Contains(req.Messages[2].Content, "Be concise") {
49+ t.Error("expected merged system message to contain 'Be concise'")
50+ }
51+ if !strings.Contains(req.Messages[2].Content, "Use simple language") {
52+ t.Error("expected merged system message to contain 'Use simple language'")
53+ }
54+
55+ w.Header().Set("Content-Type", "application/json")
56+ json.NewEncoder(w).Encode(map[string]interface{}{
57+ "choices": []map[string]interface{}{
58+ {
59+ "message": map[string]interface{}{
60+ "role": "assistant",
61+ "content": "Got it!",
62+ },
63+ },
64+ },
65+ })
66+ }))
67+ defer server.Close()
68+
69+ // Remove /v1 suffix since NewOllama adds it
70+ baseURL := strings.TrimSuffix(server.URL, "/v1")
71+ provider := NewOllama(baseURL, "test-model")
72+ ctx := context.Background()
73+
74+ // System messages at different positions, including consecutive ones
75+ messages := []Message{
76+ {Role: "system", Content: "You are a helpful assistant."},
77+ {Role: "user", Content: "Hello"},
78+ {Role: "system", Content: "Be concise in your responses."},
79+ {Role: "system", Content: "Use simple language."},
80+ {Role: "assistant", Content: "Hi there!"},
81+ }
82+
83+ _, err := provider.Chat(ctx, messages)
84+ if err != nil {
85+ t.Fatalf("Chat() error: %v", err)
86+ }
87+}
88+
89+// TestOllama_ConsecutiveSystemMessagesMerge tests that consecutive system messages
90+// are merged in place (Ollama-specific behavior).
91+func TestOllama_ConsecutiveSystemMessagesMerge(t *testing.T) {
92+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
93+ var req struct {
94+ Messages []struct {
95+ Role string `json:"role"`
96+ Content string `json:"content"`
97+ } `json:"messages"`
98+ }
99+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
100+ t.Fatalf("failed to decode request: %v", err)
101+ }
102+
103+ // Verify: 3 system messages should be merged into 1
104+ systemCount := 0
105+ for i, msg := range req.Messages {
106+ if msg.Role == "system" {
107+ systemCount++
108+ // Should be first message
109+ if i != 0 {
110+ t.Errorf("expected system message at position 0, got %d", i)
111+ }
112+ // Should contain all three system prompts merged
113+ if !strings.Contains(msg.Content, "instruction one") {
114+ t.Error("expected merged system to contain 'instruction one'")
115+ }
116+ if !strings.Contains(msg.Content, "instruction two") {
117+ t.Error("expected merged system to contain 'instruction two'")
118+ }
119+ if !strings.Contains(msg.Content, "instruction three") {
120+ t.Error("expected merged system to contain 'instruction three'")
121+ }
122+ }
123+ }
124+
125+ if systemCount != 1 {
126+ t.Errorf("expected 1 system message after merge, got %d", systemCount)
127+ }
128+
129+ // Total messages should be 2 (1 merged system + 1 user)
130+ if len(req.Messages) != 2 {
131+ t.Errorf("expected 2 messages total, got %d", len(req.Messages))
132+ }
133+
134+ w.Header().Set("Content-Type", "application/json")
135+ json.NewEncoder(w).Encode(map[string]interface{}{
136+ "choices": []map[string]interface{}{
137+ {
138+ "message": map[string]interface{}{
139+ "role": "assistant",
140+ "content": "Ready.",
141+ },
142+ },
143+ },
144+ })
145+ }))
146+ defer server.Close()
147+
148+ baseURL := strings.TrimSuffix(server.URL, "/v1")
149+ provider := NewOllama(baseURL, "test-model")
150+ ctx := context.Background()
151+
152+ // Three consecutive system messages followed by user message
153+ messages := []Message{
154+ {Role: "system", Content: "This is instruction one."},
155+ {Role: "system", Content: "This is instruction two."},
156+ {Role: "system", Content: "This is instruction three."},
157+ {Role: "user", Content: "Hello"},
158+ }
159+
160+ _, err := provider.Chat(ctx, messages)
161+ if err != nil {
162+ t.Fatalf("Chat() error: %v", err)
163+ }
164+}
165+
166+// TestOllama_ToolCalls tests that Ollama provider correctly handles
167+// messages with tool calls.
168+func TestOllama_ToolCalls(t *testing.T) {
169+ requestCount := 0
170+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
171+ requestCount++
172+
173+ var req struct {
174+ Messages []struct {
175+ Role string `json:"role"`
176+ Content string `json:"content"`
177+ ToolCalls []struct {
178+ ID string `json:"id"`
179+ Function struct {
180+ Name string `json:"name"`
181+ Arguments string `json:"arguments"`
182+ } `json:"function"`
183+ } `json:"tool_calls,omitempty"`
184+ ToolCallID string `json:"tool_call_id,omitempty"`
185+ } `json:"messages"`
186+ Tools []map[string]interface{} `json:"tools,omitempty"`
187+ }
188+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
189+ t.Fatalf("failed to decode request: %v", err)
190+ }
191+
192+ w.Header().Set("Content-Type", "application/json")
193+
194+ // First request: return tool call
195+ if requestCount == 1 {
196+ // Verify tools are present
197+ if len(req.Tools) == 0 {
198+ t.Error("expected tools in request")
199+ }
200+
201+ // Verify tool structure matches Ollama format
202+ tool := req.Tools[0]
203+ if tool["type"] != "function" {
204+ t.Errorf("expected tool type=function, got %v", tool["type"])
205+ }
206+
207+ json.NewEncoder(w).Encode(map[string]interface{}{
208+ "choices": []map[string]interface{}{
209+ {
210+ "message": map[string]interface{}{
211+ "role": "assistant",
212+ "content": "",
213+ "tool_calls": []map[string]interface{}{
214+ {
215+ "id": "call_abc",
216+ "type": "function",
217+ "function": map[string]interface{}{
218+ "name": "calculate",
219+ "arguments": `{"operation":"add","a":5,"b":3}`,
220+ },
221+ },
222+ },
223+ },
224+ },
225+ },
226+ })
227+ return
228+ }
229+
230+ // Second request: verify tool result message
231+ if requestCount == 2 {
232+ foundToolResult := false
233+ for _, msg := range req.Messages {
234+ if msg.Role == "tool" && msg.ToolCallID == "call_abc" {
235+ foundToolResult = true
236+ if msg.Content != "8" {
237+ t.Errorf("expected tool result='8', got %q", msg.Content)
238+ }
239+ }
240+ }
241+ if !foundToolResult {
242+ t.Error("expected tool result message in second request")
243+ }
244+
245+ json.NewEncoder(w).Encode(map[string]interface{}{
246+ "choices": []map[string]interface{}{
247+ {
248+ "message": map[string]interface{}{
249+ "role": "assistant",
250+ "content": "The result is 8.",
251+ },
252+ },
253+ },
254+ })
255+ }
256+ }))
257+ defer server.Close()
258+
259+ baseURL := strings.TrimSuffix(server.URL, "/v1")
260+ provider := NewOllama(baseURL, "test-model")
261+ ctx := context.Background()
262+
263+ // First call with tools
264+ tools := []Tool{
265+ {
266+ Name: "calculate",
267+ Description: "Perform calculations",
268+ Parameters: json.RawMessage(`{"type":"object","properties":{"operation":{"type":"string"},"a":{"type":"number"},"b":{"type":"number"}}}`),
269+ },
270+ }
271+
272+ messages := []Message{
273+ {Role: "user", Content: "What is 5 plus 3?"},
274+ }
275+
276+ resp1, err := provider.ChatWithTools(ctx, messages, tools)
277+ if err != nil {
278+ t.Fatalf("ChatWithTools() error: %v", err)
279+ }
280+
281+ // Verify tool call
282+ if len(resp1.ToolCalls) != 1 {
283+ t.Fatalf("expected 1 tool call, got %d", len(resp1.ToolCalls))
284+ }
285+ if resp1.ToolCalls[0].Name != "calculate" {
286+ t.Errorf("expected tool name=calculate, got %s", resp1.ToolCalls[0].Name)
287+ }
288+
289+ // Second call with tool result
290+ messages = append(messages, Message{
291+ Role: "assistant",
292+ Content: "",
293+ ToolCalls: resp1.ToolCalls,
294+ })
295+ messages = append(messages, Message{
296+ Role: "tool",
297+ Content: "8",
298+ ToolCallID: "call_abc",
299+ })
300+
301+ resp2, err := provider.ChatWithTools(ctx, messages, tools)
302+ if err != nil {
303+ t.Fatalf("ChatWithTools() error: %v", err)
304+ }
305+
306+ if !strings.Contains(resp2.Content, "8") {
307+ t.Errorf("expected response to contain '8', got %q", resp2.Content)
308+ }
309+}
310+
311+// TestOllama_ReasoningField tests that Ollama provider extracts reasoning
312+// from both "reasoning" and "reasoning_content" fields (Ollama extension).
313+func TestOllama_ReasoningField(t *testing.T) {
314+ tests := []struct {
315+ name string
316+ responseField string
317+ responseValue string
318+ }{
319+ {
320+ name: "reasoning field",
321+ responseField: "reasoning",
322+ responseValue: "I need to think about this...",
323+ },
324+ {
325+ name: "reasoning_content field",
326+ responseField: "reasoning_content",
327+ responseValue: "Let me analyze this step by step.",
328+ },
329+ }
330+
331+ for _, tt := range tests {
332+ t.Run(tt.name, func(t *testing.T) {
333+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
334+ w.Header().Set("Content-Type", "application/json")
335+
336+ // Build response with specified reasoning field
337+ response := map[string]interface{}{
338+ "choices": []map[string]interface{}{
339+ {
340+ "message": map[string]interface{}{
341+ "role": "assistant",
342+ "content": "Answer",
343+ tt.responseField: tt.responseValue,
344+ },
345+ },
346+ },
347+ }
348+
349+ json.NewEncoder(w).Encode(response)
350+ }))
351+ defer server.Close()
352+
353+ baseURL := strings.TrimSuffix(server.URL, "/v1")
354+ provider := NewOllama(baseURL, "test-model")
355+ ctx := context.Background()
356+
357+ messages := []Message{
358+ {Role: "user", Content: "Question?"},
359+ }
360+
361+ tools := []Tool{
362+ {Name: "dummy", Description: "Dummy tool"},
363+ }
364+
365+ resp, err := provider.ChatWithTools(ctx, messages, tools)
366+ if err != nil {
367+ t.Fatalf("ChatWithTools() error: %v", err)
368+ }
369+
370+ if resp.Reasoning != tt.responseValue {
371+ t.Errorf("expected reasoning=%q, got %q", tt.responseValue, resp.Reasoning)
372+ }
373+ })
374+ }
375+}
376+
377+// TestOllama_SystemMessagesAtEnd tests that system messages at the end
378+// are preserved (Ollama allows this, unlike OpenAI).
379+func TestOllama_SystemMessagesAtEnd(t *testing.T) {
380+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
381+ var req struct {
382+ Messages []struct {
383+ Role string `json:"role"`
384+ Content string `json:"content"`
385+ } `json:"messages"`
386+ }
387+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
388+ t.Fatalf("failed to decode request: %v", err)
389+ }
390+
391+ // Verify last message is system
392+ lastMsg := req.Messages[len(req.Messages)-1]
393+ if lastMsg.Role != "system" {
394+ t.Errorf("expected last message role=system, got %s", lastMsg.Role)
395+ }
396+ if !strings.Contains(lastMsg.Content, "final instruction") {
397+ t.Error("expected last system message to contain 'final instruction'")
398+ }
399+
400+ w.Header().Set("Content-Type", "application/json")
401+ json.NewEncoder(w).Encode(map[string]interface{}{
402+ "choices": []map[string]interface{}{
403+ {
404+ "message": map[string]interface{}{
405+ "role": "assistant",
406+ "content": "Understood final instruction.",
407+ },
408+ },
409+ },
410+ })
411+ }))
412+ defer server.Close()
413+
414+ baseURL := strings.TrimSuffix(server.URL, "/v1")
415+ provider := NewOllama(baseURL, "test-model")
416+ ctx := context.Background()
417+
418+ // System message at the end
419+ messages := []Message{
420+ {Role: "user", Content: "Hello"},
421+ {Role: "assistant", Content: "Hi!"},
422+ {Role: "system", Content: "Here's a final instruction."},
423+ }
424+
425+ resp, err := provider.Chat(ctx, messages)
426+ if err != nil {
427+ t.Fatalf("Chat() error: %v", err)
428+ }
429+
430+ if !strings.Contains(resp, "final instruction") {
431+ t.Errorf("expected response to acknowledge final instruction, got %q", resp)
432+ }
433+}
internal/provider/opencode_test.go+330−0
@@ -0,0 +1,330 @@
1+package provider
2+
3+import (
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 adds "Begin." user message
13+// when only system prompts are present (OpenAI requirement).
14+func TestOpenCode_SystemPromptOnly(t *testing.T) {
15+ // Setup mock server
16+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17+ // Parse request body
18+ var req struct {
19+ Messages []struct {
20+ Role string `json:"role"`
21+ Content string `json:"content"`
22+ } `json:"messages"`
23+ }
24+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
25+ t.Fatalf("failed to decode request: %v", err)
26+ }
27+
28+ // Verify system message is first
29+ if len(req.Messages) < 1 {
30+ t.Fatal("expected at least 1 message")
31+ }
32+ if req.Messages[0].Role != "system" {
33+ t.Errorf("expected first message role=system, got %s", req.Messages[0].Role)
34+ }
35+
36+ // Verify "Begin." user message was added
37+ if len(req.Messages) < 2 {
38+ t.Fatal("expected 2 messages (system + auto-generated user)")
39+ }
40+ if req.Messages[1].Role != "user" {
41+ t.Errorf("expected second message role=user, got %s", req.Messages[1].Role)
42+ }
43+ if req.Messages[1].Content != "Begin." {
44+ t.Errorf("expected second message content='Begin.', got %q", req.Messages[1].Content)
45+ }
46+
47+ // Return mock response
48+ w.Header().Set("Content-Type", "application/json")
49+ json.NewEncoder(w).Encode(map[string]interface{}{
50+ "choices": []map[string]interface{}{
51+ {
52+ "message": map[string]interface{}{
53+ "role": "assistant",
54+ "content": "Ready.",
55+ },
56+ },
57+ },
58+ })
59+ }))
60+ defer server.Close()
61+
62+ // Create provider
63+ provider := NewOpenCode(server.URL, "test-model", "test-key")
64+
65+ // Send only system messages
66+ messages := []Message{
67+ {Role: "system", Content: "You are a helpful assistant."},
68+ }
69+
70+ ctx := context.Background()
71+ response, err := provider.Chat(ctx, messages)
72+ if err != nil {
73+ t.Fatalf("Chat() error: %v", err)
74+ }
75+
76+ if response != "Ready." {
77+ t.Errorf("expected response='Ready.', got %q", response)
78+ }
79+}
80+
81+// TestOpenCode_ToolCallsAndResults tests that OpenCode provider correctly handles
82+// messages with tool calls and tool results.
83+func TestOpenCode_ToolCallsAndResults(t *testing.T) {
84+ // Setup mock server
85+ requestCount := 0
86+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87+ requestCount++
88+
89+ var req struct {
90+ Messages []struct {
91+ Role string `json:"role"`
92+ Content string `json:"content"`
93+ ToolCalls []struct {
94+ ID string `json:"id"`
95+ Function struct {
96+ Name string `json:"name"`
97+ Arguments string `json:"arguments"`
98+ } `json:"function"`
99+ } `json:"tool_calls,omitempty"`
100+ ToolCallID string `json:"tool_call_id,omitempty"`
101+ } `json:"messages"`
102+ Tools []map[string]interface{} `json:"tools,omitempty"`
103+ }
104+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
105+ t.Fatalf("failed to decode request: %v", err)
106+ }
107+
108+ w.Header().Set("Content-Type", "application/json")
109+
110+ // First request: return tool call
111+ if requestCount == 1 {
112+ // Verify tools are present
113+ if len(req.Tools) == 0 {
114+ t.Error("expected tools in request")
115+ }
116+
117+ // Return tool call response
118+ json.NewEncoder(w).Encode(map[string]interface{}{
119+ "choices": []map[string]interface{}{
120+ {
121+ "message": map[string]interface{}{
122+ "role": "assistant",
123+ "content": "",
124+ "tool_calls": []map[string]interface{}{
125+ {
126+ "id": "call_123",
127+ "type": "function",
128+ "function": map[string]interface{}{
129+ "name": "get_weather",
130+ "arguments": `{"location":"NYC"}`,
131+ },
132+ },
133+ },
134+ },
135+ },
136+ },
137+ })
138+ return
139+ }
140+
141+ // Second request: verify tool result is included
142+ if requestCount == 2 {
143+ foundToolResult := false
144+ for _, msg := range req.Messages {
145+ if msg.Role == "tool" && msg.ToolCallID == "call_123" {
146+ foundToolResult = true
147+ if !strings.Contains(msg.Content, "72") {
148+ t.Errorf("expected tool result to contain '72', got %q", msg.Content)
149+ }
150+ }
151+ }
152+ if !foundToolResult {
153+ t.Error("expected tool result message in second request")
154+ }
155+
156+ // Return final response
157+ json.NewEncoder(w).Encode(map[string]interface{}{
158+ "choices": []map[string]interface{}{
159+ {
160+ "message": map[string]interface{}{
161+ "role": "assistant",
162+ "content": "The weather in NYC is 72°F.",
163+ },
164+ },
165+ },
166+ })
167+ }
168+ }))
169+ defer server.Close()
170+
171+ provider := NewOpenCode(server.URL, "test-model", "test-key")
172+ ctx := context.Background()
173+
174+ // First call with tools
175+ tools := []Tool{
176+ {
177+ Name: "get_weather",
178+ Description: "Get current weather",
179+ Parameters: json.RawMessage(`{"type":"object","properties":{"location":{"type":"string"}}}`),
180+ },
181+ }
182+
183+ messages := []Message{
184+ {Role: "user", Content: "What's the weather in NYC?"},
185+ }
186+
187+ resp1, err := provider.ChatWithTools(ctx, messages, tools)
188+ if err != nil {
189+ t.Fatalf("ChatWithTools() error: %v", err)
190+ }
191+
192+ // Verify tool call
193+ if len(resp1.ToolCalls) != 1 {
194+ t.Fatalf("expected 1 tool call, got %d", len(resp1.ToolCalls))
195+ }
196+ if resp1.ToolCalls[0].ID != "call_123" {
197+ t.Errorf("expected tool call ID=call_123, got %s", resp1.ToolCalls[0].ID)
198+ }
199+ if resp1.ToolCalls[0].Name != "get_weather" {
200+ t.Errorf("expected tool name=get_weather, got %s", resp1.ToolCalls[0].Name)
201+ }
202+
203+ // Second call with tool result
204+ messages = append(messages, Message{
205+ Role: "assistant",
206+ Content: "",
207+ ToolCalls: resp1.ToolCalls,
208+ })
209+ messages = append(messages, Message{
210+ Role: "tool",
211+ Content: `{"temp":72,"condition":"sunny"}`,
212+ ToolCallID: "call_123",
213+ })
214+
215+ resp2, err := provider.ChatWithTools(ctx, messages, tools)
216+ if err != nil {
217+ t.Fatalf("ChatWithTools() error: %v", err)
218+ }
219+
220+ if !strings.Contains(resp2.Content, "72") {
221+ t.Errorf("expected response to contain '72', got %q", resp2.Content)
222+ }
223+}
224+
225+// TestOpenCode_InvalidToolSchema tests that OpenCode provider returns error
226+// for invalid tool schemas.
227+func TestOpenCode_InvalidToolSchema(t *testing.T) {
228+ // Setup mock server (won't be called)
229+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
230+ t.Error("server should not be called with invalid tool schema")
231+ }))
232+ defer server.Close()
233+
234+ provider := NewOpenCode(server.URL, "test-model", "test-key")
235+ ctx := context.Background()
236+
237+ // Create tool with invalid JSON schema
238+ tools := []Tool{
239+ {
240+ Name: "bad_tool",
241+ Description: "Tool with invalid schema",
242+ Parameters: json.RawMessage(`{invalid json}`), // Invalid JSON
243+ },
244+ }
245+
246+ messages := []Message{
247+ {Role: "user", Content: "Do something"},
248+ }
249+
250+ _, err := provider.ChatWithTools(ctx, messages, tools)
251+ if err == nil {
252+ t.Fatal("expected error for invalid tool schema")
253+ }
254+
255+ // Verify error message mentions invalid schema
256+ errMsg := err.Error()
257+ if !strings.Contains(errMsg, "invalid tool schema") {
258+ t.Errorf("expected error message to contain 'invalid tool schema', got %q", errMsg)
259+ }
260+}
261+
262+// TestOpenCode_MultipleSystemMessages tests that multiple system messages
263+// are merged into a single message at the start (OpenAI requirement).
264+func TestOpenCode_MultipleSystemMessages(t *testing.T) {
265+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
266+ var req struct {
267+ Messages []struct {
268+ Role string `json:"role"`
269+ Content string `json:"content"`
270+ } `json:"messages"`
271+ }
272+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
273+ t.Fatalf("failed to decode request: %v", err)
274+ }
275+
276+ // Count system messages
277+ systemCount := 0
278+ for _, msg := range req.Messages {
279+ if msg.Role == "system" {
280+ systemCount++
281+ // Verify merged content contains both instructions
282+ if !strings.Contains(msg.Content, "helpful assistant") {
283+ t.Error("expected merged system message to contain 'helpful assistant'")
284+ }
285+ if !strings.Contains(msg.Content, "Be concise") {
286+ t.Error("expected merged system message to contain 'Be concise'")
287+ }
288+ }
289+ }
290+
291+ // Verify only one system message
292+ if systemCount != 1 {
293+ t.Errorf("expected 1 system message after merge, got %d", systemCount)
294+ }
295+
296+ // Verify system message is first
297+ if req.Messages[0].Role != "system" {
298+ t.Errorf("expected first message to be system, got %s", req.Messages[0].Role)
299+ }
300+
301+ w.Header().Set("Content-Type", "application/json")
302+ json.NewEncoder(w).Encode(map[string]interface{}{
303+ "choices": []map[string]interface{}{
304+ {
305+ "message": map[string]interface{}{
306+ "role": "assistant",
307+ "content": "Understood.",
308+ },
309+ },
310+ },
311+ })
312+ }))
313+ defer server.Close()
314+
315+ provider := NewOpenCode(server.URL, "test-model", "test-key")
316+ ctx := context.Background()
317+
318+ // Send multiple system messages scattered in conversation
319+ messages := []Message{
320+ {Role: "system", Content: "You are a helpful assistant."},
321+ {Role: "user", Content: "Hello"},
322+ {Role: "system", Content: "Be concise in your responses."},
323+ {Role: "assistant", Content: "Hi"},
324+ }
325+
326+ _, err := provider.Chat(ctx, messages)
327+ if err != nil {
328+ t.Fatalf("Chat() error: %v", err)
329+ }
330+}