File size: 5,928 Bytes
1766992 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | // Copyright (c) 2025-2026 libaxuan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package models
import (
"testing"
)
func TestGetStringContent(t *testing.T) {
tests := []struct {
name string
content interface{}
expected string
}{
{
name: "string content",
content: "Hello world",
expected: "Hello world",
},
{
name: "array content",
content: []ContentPart{
{Type: "text", Text: "Hello"},
{Type: "text", Text: " world"},
},
expected: "Hello world",
},
{
name: "empty array",
content: []ContentPart{},
expected: "",
},
{
name: "nil content",
content: nil,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := &Message{Content: tt.content}
result := msg.GetStringContent()
if result != tt.expected {
t.Errorf("GetStringContent() = %v, want %v", result, tt.expected)
}
})
}
}
func TestToCursorMessages(t *testing.T) {
tests := []struct {
name string
messages []Message
systemPrompt string
expectedLength int
expectedFirstMsg string
}{
{
name: "no system prompt",
messages: []Message{
{Role: "user", Content: "Hello"},
},
systemPrompt: "",
expectedLength: 1,
expectedFirstMsg: "Hello",
},
{
name: "with system prompt, no system message",
messages: []Message{
{Role: "user", Content: "Hello"},
},
systemPrompt: "You are a helpful assistant",
expectedLength: 2,
expectedFirstMsg: "You are a helpful assistant",
},
{
name: "with system prompt, has system message",
messages: []Message{
{Role: "system", Content: "Be helpful"},
{Role: "user", Content: "Hello"},
},
systemPrompt: "You are an AI",
expectedLength: 2,
expectedFirstMsg: "Be helpful\nYou are an AI",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ToCursorMessages(tt.messages, tt.systemPrompt)
if len(result) != tt.expectedLength {
t.Errorf("ToCursorMessages() length = %v, want %v", len(result), tt.expectedLength)
}
if len(result) > 0 && result[0].Parts[0].Text != tt.expectedFirstMsg {
t.Errorf("ToCursorMessages() first message = %v, want %v", result[0].Parts[0].Text, tt.expectedFirstMsg)
}
})
}
}
func TestNewChatCompletionResponse(t *testing.T) {
response := NewChatCompletionResponse(
"test-id",
"claude-sonnet-4.6",
Message{Role: "assistant", Content: "Hello world"},
"stop",
Usage{PromptTokens: 10, CompletionTokens: 5, TotalTokens: 15},
)
if response.ID != "test-id" {
t.Errorf("ID = %v, want test-id", response.ID)
}
if response.Model != "claude-sonnet-4.6" {
t.Errorf("Model = %v, want claude-sonnet-4.6", response.Model)
}
if response.Choices[0].Message.Content != "Hello world" {
t.Errorf("Content = %v, want Hello world", response.Choices[0].Message.Content)
}
if response.Usage.PromptTokens != 10 {
t.Errorf("PromptTokens = %v, want 10", response.Usage.PromptTokens)
}
}
func TestNewChatCompletionStreamResponse(t *testing.T) {
response := NewChatCompletionStreamResponse(
"test-id",
"claude-sonnet-4.6",
StreamDelta{Content: "Hello"},
stringPtr("stop"),
)
if response.ID != "test-id" {
t.Errorf("ID = %v, want test-id", response.ID)
}
if response.Choices[0].Delta.Content != "Hello" {
t.Errorf("Content = %v, want Hello", response.Choices[0].Delta.Content)
}
if response.Choices[0].FinishReason == nil || *response.Choices[0].FinishReason != "stop" {
t.Errorf("FinishReason = %v, want stop", response.Choices[0].FinishReason)
}
}
func TestResolveModelCapability(t *testing.T) {
capability := ResolveModelCapability("claude-sonnet-4.6-thinking")
if capability.BaseModel != "claude-sonnet-4.6" {
t.Fatalf("BaseModel = %v, want claude-sonnet-4.6", capability.BaseModel)
}
if !capability.ThinkingEnabled {
t.Fatalf("ThinkingEnabled = false, want true")
}
}
func TestExpandModelList(t *testing.T) {
models := ExpandModelList([]string{"claude-sonnet-4.6"})
expected := []string{"claude-sonnet-4.6", "claude-sonnet-4.6-thinking"}
if len(models) != len(expected) {
t.Fatalf("ExpandModelList() length = %v, want %v", len(models), len(expected))
}
for i := range expected {
if models[i] != expected[i] {
t.Fatalf("ExpandModelList()[%d] = %v, want %v", i, models[i], expected[i])
}
}
}
func TestNewErrorResponse(t *testing.T) {
response := NewErrorResponse("Test error", "test_error", "error_code")
if response.Error.Message != "Test error" {
t.Errorf("Message = %v, want Test error", response.Error.Message)
}
if response.Error.Type != "test_error" {
t.Errorf("Type = %v, want test_error", response.Error.Type)
}
if response.Error.Code != "error_code" {
t.Errorf("Code = %v, want error_code", response.Error.Code)
}
}
// Helper function
func stringPtr(s string) *string {
return &s
}
|