60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / provider / opencode_integration_test.go
4.9 KB
1package provider 2 3import ( 4 "testing" 5 6 openai "github.com/sashabaranov/go-openai" 7) 8 9// TestMergeSystemMessagesOpenAI_PreservesOrphanRemoval tests that the provider's 10// mergeSystemMessagesOpenAI function doesn't reintroduce orphaned messages 11// after they've been removed by the core package. 12// 13// This test verifies that the provider layer only merges system messages and 14// does NOT add synthetic "Begin." or "Continue." messages. Those are added by 15// getContextMemories() in the core layer. 16func TestMergeSystemMessagesOpenAI_PreservesOrphanRemoval(t *testing.T) { 17 tests := []struct { 18 name string 19 input []openai.ChatCompletionMessage 20 expectedCount int 21 }{ 22 { 23 name: "minimal_system_only", 24 input: []openai.ChatCompletionMessage{ 25 {Role: "system", Content: "You are a helpful assistant"}, 26 }, 27 expectedCount: 1, // system only (synthetic user added by getContextMemories) 28 }, 29 { 30 name: "system_plus_user", 31 input: []openai.ChatCompletionMessage{ 32 {Role: "system", Content: "You are a helpful assistant"}, 33 {Role: "user", Content: "Hello"}, 34 }, 35 expectedCount: 2, // no additions needed 36 }, 37 { 38 name: "ends_with_assistant", 39 input: []openai.ChatCompletionMessage{ 40 {Role: "system", Content: "You are a helpful assistant"}, 41 {Role: "user", Content: "Hello"}, 42 {Role: "assistant", Content: "Hi!"}, 43 }, 44 expectedCount: 3, // system + user + assistant (no synthetic continuation) 45 }, 46 } 47 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 result := mergeSystemMessagesOpenAI(tt.input) 51 52 if len(result) != tt.expectedCount { 53 t.Errorf("Expected %d messages, got %d", tt.expectedCount, len(result)) 54 } 55 56 // Verify no synthetic "Begin." or "Continue." messages were added 57 for _, msg := range result { 58 if msg.Content == "Begin." || msg.Content == "Continue." { 59 t.Errorf("Provider should not add synthetic messages, found: %q", msg.Content) 60 } 61 } 62 }) 63 } 64} 65 66// TestOpenCodeProvider_HandlesMinimalRequest tests that OpenCode provider 67// can handle a minimal valid request (system + user message). 68// 69// This reproduces the production scenario where the very first request fails. 70func TestOpenCodeProvider_HandlesMinimalRequest(t *testing.T) { 71 // Create a mock HTTP server that simulates OpenCode Zen API 72 // We can't test against real API without credentials, but we can 73 // test that our request formatting is correct. 74 t.Skip("Requires mock HTTP server - implement when debugging OpenCode issues") 75 76 // TODO: Implement mock server that: 77 // 1. Accepts the request 78 // 2. Validates message structure 79 // 3. Returns mock response 80 // This will catch formatting issues before they hit production 81} 82 83// TestProviderMessageFlow_AfterOrphanRemoval tests the complete flow: 84// 1. Core removes orphaned messages 85// 2. Provider receives cleaned messages 86// 3. Provider merges system messages 87// 4. Final message array is still valid 88// 89// This test exercises the ACTUAL code path that production uses. 90func TestProviderMessageFlow_AfterOrphanRemoval(t *testing.T) { 91 // Simulate what core.getContextMemories() returns after orphan removal 92 cleanedMessages := []Message{ 93 {Role: "system", Content: "System prompt"}, 94 {Role: "user", Content: "Hello"}, 95 {Role: "assistant", Content: "Hi!", ToolCalls: []ToolCall{}}, 96 // Note: No orphaned tool results - they were removed by core 97 } 98 99 // Convert to OpenAI format (what toOpenAIMessages does) 100 openaiMsgs := make([]openai.ChatCompletionMessage, len(cleanedMessages)) 101 for i, m := range cleanedMessages { 102 openaiMsgs[i] = openai.ChatCompletionMessage{ 103 Role: m.Role, 104 Content: m.Content, 105 } 106 } 107 108 // Apply provider's message manipulation 109 merged := mergeSystemMessagesOpenAI(openaiMsgs) 110 111 // ASSERTIONS: 112 // 1. Message count should be valid 113 if len(merged) < 2 { 114 t.Errorf("Merged messages too short: %d", len(merged)) 115 } 116 117 // 2. Should start with system 118 if merged[0].Role != "system" { 119 t.Errorf("First message should be system, got %s", merged[0].Role) 120 } 121 122 // 3. Provider should NOT add synthetic messages 123 for _, msg := range merged { 124 if msg.Content == "Begin." || msg.Content == "Continue." { 125 t.Errorf("Provider should not add synthetic messages, found: %q", msg.Content) 126 } 127 } 128 129 // 4. No orphaned tool messages should exist 130 validToolCalls := make(map[string]bool) 131 for _, msg := range merged { 132 if msg.Role == "assistant" && len(msg.ToolCalls) > 0 { 133 for _, tc := range msg.ToolCalls { 134 validToolCalls[tc.ID] = true 135 } 136 } 137 } 138 139 for _, msg := range merged { 140 if msg.Role == "tool" { 141 if msg.ToolCallID == "" { 142 t.Error("Tool message missing ToolCallID") 143 } else if !validToolCalls[msg.ToolCallID] { 144 t.Errorf("Orphaned tool message found: %s", msg.ToolCallID) 145 } 146 } 147 } 148 149 t.Logf("✅ Provider message flow validation passed") 150 t.Logf(" Input: %d messages", len(cleanedMessages)) 151 t.Logf(" After merge: %d messages", len(merged)) 152 t.Logf(" Valid tool calls: %d", len(validToolCalls)) 153}