|
1 | 1 | package anthropic
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
4 | 5 | "testing"
|
| 6 | + |
| 7 | + "github.com/anthropics/anthropic-sdk-go" |
| 8 | + "github.com/danielmiessler/fabric/common" |
5 | 9 | )
|
6 | 10 |
|
7 | 11 | // Test generated using Keploy
|
@@ -63,3 +67,192 @@ func TestClient_ListModels_ReturnsCorrectModels(t *testing.T) {
|
63 | 67 | }
|
64 | 68 | }
|
65 | 69 | }
|
| 70 | + |
| 71 | +func TestBuildMessageParams_WithoutSearch(t *testing.T) { |
| 72 | + client := NewClient() |
| 73 | + opts := &common.ChatOptions{ |
| 74 | + Model: "claude-3-5-sonnet-latest", |
| 75 | + Temperature: 0.7, |
| 76 | + Search: false, |
| 77 | + } |
| 78 | + |
| 79 | + messages := []anthropic.MessageParam{ |
| 80 | + anthropic.NewUserMessage(anthropic.NewTextBlock("Hello")), |
| 81 | + } |
| 82 | + |
| 83 | + params := client.buildMessageParams(messages, opts) |
| 84 | + |
| 85 | + if params.Tools != nil { |
| 86 | + t.Error("Expected no tools when search is disabled, got tools") |
| 87 | + } |
| 88 | + |
| 89 | + if params.Model != anthropic.Model(opts.Model) { |
| 90 | + t.Errorf("Expected model %s, got %s", opts.Model, params.Model) |
| 91 | + } |
| 92 | + |
| 93 | + if params.Temperature.Value != opts.Temperature { |
| 94 | + t.Errorf("Expected temperature %f, got %f", opts.Temperature, params.Temperature.Value) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestBuildMessageParams_WithSearch(t *testing.T) { |
| 99 | + client := NewClient() |
| 100 | + opts := &common.ChatOptions{ |
| 101 | + Model: "claude-3-5-sonnet-latest", |
| 102 | + Temperature: 0.7, |
| 103 | + Search: true, |
| 104 | + } |
| 105 | + |
| 106 | + messages := []anthropic.MessageParam{ |
| 107 | + anthropic.NewUserMessage(anthropic.NewTextBlock("What's the weather today?")), |
| 108 | + } |
| 109 | + |
| 110 | + params := client.buildMessageParams(messages, opts) |
| 111 | + |
| 112 | + if params.Tools == nil { |
| 113 | + t.Fatal("Expected tools when search is enabled, got nil") |
| 114 | + } |
| 115 | + |
| 116 | + if len(params.Tools) != 1 { |
| 117 | + t.Errorf("Expected 1 tool, got %d", len(params.Tools)) |
| 118 | + } |
| 119 | + |
| 120 | + webTool := params.Tools[0].OfWebSearchTool20250305 |
| 121 | + if webTool == nil { |
| 122 | + t.Fatal("Expected web search tool, got nil") |
| 123 | + } |
| 124 | + |
| 125 | + if webTool.Name != "web_search" { |
| 126 | + t.Errorf("Expected tool name 'web_search', got %s", webTool.Name) |
| 127 | + } |
| 128 | + |
| 129 | + if webTool.Type != "web_search_20250305" { |
| 130 | + t.Errorf("Expected tool type 'web_search_20250305', got %s", webTool.Type) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestBuildMessageParams_WithSearchAndLocation(t *testing.T) { |
| 135 | + client := NewClient() |
| 136 | + opts := &common.ChatOptions{ |
| 137 | + Model: "claude-3-5-sonnet-latest", |
| 138 | + Temperature: 0.7, |
| 139 | + Search: true, |
| 140 | + SearchLocation: "America/Los_Angeles", |
| 141 | + } |
| 142 | + |
| 143 | + messages := []anthropic.MessageParam{ |
| 144 | + anthropic.NewUserMessage(anthropic.NewTextBlock("What's the weather in San Francisco?")), |
| 145 | + } |
| 146 | + |
| 147 | + params := client.buildMessageParams(messages, opts) |
| 148 | + |
| 149 | + if params.Tools == nil { |
| 150 | + t.Fatal("Expected tools when search is enabled, got nil") |
| 151 | + } |
| 152 | + |
| 153 | + webTool := params.Tools[0].OfWebSearchTool20250305 |
| 154 | + if webTool == nil { |
| 155 | + t.Fatal("Expected web search tool, got nil") |
| 156 | + } |
| 157 | + |
| 158 | + if webTool.UserLocation.Type != "approximate" { |
| 159 | + t.Errorf("Expected location type 'approximate', got %s", webTool.UserLocation.Type) |
| 160 | + } |
| 161 | + |
| 162 | + if webTool.UserLocation.Timezone.Value != opts.SearchLocation { |
| 163 | + t.Errorf("Expected timezone %s, got %s", opts.SearchLocation, webTool.UserLocation.Timezone.Value) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestCitationFormatting(t *testing.T) { |
| 168 | + // Test the citation formatting logic by creating a mock message with citations |
| 169 | + message := &anthropic.Message{ |
| 170 | + Content: []anthropic.ContentBlockUnion{ |
| 171 | + { |
| 172 | + Type: "text", |
| 173 | + Text: "Based on recent research, artificial intelligence is advancing rapidly.", |
| 174 | + Citations: []anthropic.TextCitationUnion{ |
| 175 | + { |
| 176 | + Type: "web_search_result_location", |
| 177 | + URL: "https://example.com/ai-research", |
| 178 | + Title: "AI Research Advances 2025", |
| 179 | + CitedText: "artificial intelligence is advancing rapidly", |
| 180 | + }, |
| 181 | + { |
| 182 | + Type: "web_search_result_location", |
| 183 | + URL: "https://another-source.com/tech-news", |
| 184 | + Title: "Technology News Today", |
| 185 | + CitedText: "recent developments in AI", |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + Type: "text", |
| 191 | + Text: " Machine learning models are becoming more sophisticated.", |
| 192 | + Citations: []anthropic.TextCitationUnion{ |
| 193 | + { |
| 194 | + Type: "web_search_result_location", |
| 195 | + URL: "https://example.com/ai-research", // Duplicate URL should be deduplicated |
| 196 | + Title: "AI Research Advances 2025", |
| 197 | + CitedText: "machine learning models", |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + } |
| 203 | + |
| 204 | + // Extract text and citations using the same logic as the Send method |
| 205 | + var textParts []string |
| 206 | + var citations []string |
| 207 | + citationMap := make(map[string]bool) |
| 208 | + |
| 209 | + for _, block := range message.Content { |
| 210 | + if block.Type == "text" && block.Text != "" { |
| 211 | + textParts = append(textParts, block.Text) |
| 212 | + |
| 213 | + for _, citation := range block.Citations { |
| 214 | + if citation.Type == "web_search_result_location" { |
| 215 | + citationKey := citation.URL + "|" + citation.Title |
| 216 | + if !citationMap[citationKey] { |
| 217 | + citationMap[citationKey] = true |
| 218 | + citationText := "- [" + citation.Title + "](" + citation.URL + ")" |
| 219 | + if citation.CitedText != "" { |
| 220 | + citationText += " - \"" + citation.CitedText + "\"" |
| 221 | + } |
| 222 | + citations = append(citations, citationText) |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + result := strings.Join(textParts, "") |
| 230 | + if len(citations) > 0 { |
| 231 | + result += "\n\n## Sources\n\n" + strings.Join(citations, "\n") |
| 232 | + } |
| 233 | + |
| 234 | + // Verify the result contains the expected text |
| 235 | + expectedText := "Based on recent research, artificial intelligence is advancing rapidly. Machine learning models are becoming more sophisticated." |
| 236 | + if !strings.Contains(result, expectedText) { |
| 237 | + t.Errorf("Expected result to contain text: %s", expectedText) |
| 238 | + } |
| 239 | + |
| 240 | + // Verify citations are included |
| 241 | + if !strings.Contains(result, "## Sources") { |
| 242 | + t.Error("Expected result to contain Sources section") |
| 243 | + } |
| 244 | + |
| 245 | + if !strings.Contains(result, "[AI Research Advances 2025](https://example.com/ai-research)") { |
| 246 | + t.Error("Expected result to contain first citation") |
| 247 | + } |
| 248 | + |
| 249 | + if !strings.Contains(result, "[Technology News Today](https://another-source.com/tech-news)") { |
| 250 | + t.Error("Expected result to contain second citation") |
| 251 | + } |
| 252 | + |
| 253 | + // Verify deduplication - should only have 2 unique citations, not 3 |
| 254 | + citationCount := strings.Count(result, "- [") |
| 255 | + if citationCount != 2 { |
| 256 | + t.Errorf("Expected 2 unique citations, got %d", citationCount) |
| 257 | + } |
| 258 | +} |
0 commit comments