style: fix gofmt formatting and update web search API in tests

- Remove trailing whitespace in web.go and base_test.go
- Update config_test.go and web_test.go for WebSearchToolOptions API
This commit is contained in:
Leandro Barbosa 2026-02-13 14:42:45 -03:00
parent e847f546c1
commit 60934d2f94
4 changed files with 17 additions and 25 deletions

View file

@ -50,4 +50,3 @@ func TestBaseChannelIsAllowed(t *testing.T) {
}) })
} }
} }

View file

@ -136,11 +136,11 @@ func TestDefaultConfig_WebTools(t *testing.T) {
cfg := DefaultConfig() cfg := DefaultConfig()
// Verify web tools defaults // Verify web tools defaults
if cfg.Tools.Web.Search.MaxResults != 5 { if cfg.Tools.Web.Brave.MaxResults != 5 {
t.Error("Expected MaxResults 5, got ", cfg.Tools.Web.Search.MaxResults) t.Error("Expected Brave MaxResults 5, got ", cfg.Tools.Web.Brave.MaxResults)
} }
if cfg.Tools.Web.Search.APIKey != "" { if cfg.Tools.Web.Brave.APIKey != "" {
t.Error("Search API key should be empty by default") t.Error("Brave API key should be empty by default")
} }
} }

View file

@ -114,7 +114,7 @@ func (p *DuckDuckGoSearchProvider) Search(ctx context.Context, query string, cou
func (p *DuckDuckGoSearchProvider) extractResults(html string, count int, query string) (string, error) { func (p *DuckDuckGoSearchProvider) extractResults(html string, count int, query string) (string, error) {
// Simple regex based extraction for DDG HTML // Simple regex based extraction for DDG HTML
// Strategy: Find all result containers or key anchors directly // Strategy: Find all result containers or key anchors directly
// Try finding the result links directly first, as they are the most critical // Try finding the result links directly first, as they are the most critical
// Pattern: <a class="result__a" href="...">Title</a> // Pattern: <a class="result__a" href="...">Title</a>
// The previous regex was a bit strict. Let's make it more flexible for attributes order/content // The previous regex was a bit strict. Let's make it more flexible for attributes order/content
@ -133,14 +133,14 @@ func (p *DuckDuckGoSearchProvider) extractResults(html string, count int, query
// But simple global search for snippets might mismatch order. // But simple global search for snippets might mismatch order.
// Since we only have the raw HTML string, let's just extract snippets globally and assume order matches (risky but simple for regex) // Since we only have the raw HTML string, let's just extract snippets globally and assume order matches (risky but simple for regex)
// Or better: Let's assume the snippet follows the link in the HTML // Or better: Let's assume the snippet follows the link in the HTML
// A better regex approach: iterate through text and find matches in order // A better regex approach: iterate through text and find matches in order
// But for now, let's grab all snippets too // But for now, let's grab all snippets too
reSnippet := regexp.MustCompile(`<a class="result__snippet[^"]*".*?>([\s\S]*?)</a>`) reSnippet := regexp.MustCompile(`<a class="result__snippet[^"]*".*?>([\s\S]*?)</a>`)
snippetMatches := reSnippet.FindAllStringSubmatch(html, count+5) snippetMatches := reSnippet.FindAllStringSubmatch(html, count+5)
maxItems := min(len(matches), count) maxItems := min(len(matches), count)
for i := 0; i < maxItems; i++ { for i := 0; i < maxItems; i++ {
urlStr := matches[i][1] urlStr := matches[i][1]
title := stripTags(matches[i][2]) title := stripTags(matches[i][2])
@ -157,7 +157,7 @@ func (p *DuckDuckGoSearchProvider) extractResults(html string, count int, query
} }
lines = append(lines, fmt.Sprintf("%d. %s\n %s", i+1, title, urlStr)) lines = append(lines, fmt.Sprintf("%d. %s\n %s", i+1, title, urlStr))
// Attempt to attach snippet if available and index aligns // Attempt to attach snippet if available and index aligns
if i < len(snippetMatches) { if i < len(snippetMatches) {
snippet := stripTags(snippetMatches[i][1]) snippet := stripTags(snippetMatches[i][1])

View file

@ -173,30 +173,23 @@ func TestWebTool_WebFetch_Truncation(t *testing.T) {
} }
} }
// TestWebTool_WebSearch_NoApiKey verifies error handling when API key is missing // TestWebTool_WebSearch_NoApiKey verifies that no tool is created when API key is missing
func TestWebTool_WebSearch_NoApiKey(t *testing.T) { func TestWebTool_WebSearch_NoApiKey(t *testing.T) {
tool := NewWebSearchTool("", 5) tool := NewWebSearchTool(WebSearchToolOptions{BraveEnabled: true, BraveAPIKey: ""})
ctx := context.Background() if tool != nil {
args := map[string]interface{}{ t.Errorf("Expected nil tool when Brave API key is empty")
"query": "test",
} }
result := tool.Execute(ctx, args) // Also nil when nothing is enabled
tool = NewWebSearchTool(WebSearchToolOptions{})
// Should return error result if tool != nil {
if !result.IsError { t.Errorf("Expected nil tool when no provider is enabled")
t.Errorf("Expected error when API key is missing")
}
// Should mention missing API key
if !strings.Contains(result.ForLLM, "BRAVE_API_KEY") && !strings.Contains(result.ForUser, "BRAVE_API_KEY") {
t.Errorf("Expected API key error message, got ForLLM: %s", result.ForLLM)
} }
} }
// TestWebTool_WebSearch_MissingQuery verifies error handling for missing query // TestWebTool_WebSearch_MissingQuery verifies error handling for missing query
func TestWebTool_WebSearch_MissingQuery(t *testing.T) { func TestWebTool_WebSearch_MissingQuery(t *testing.T) {
tool := NewWebSearchTool("test-key", 5) tool := NewWebSearchTool(WebSearchToolOptions{BraveEnabled: true, BraveAPIKey: "test-key", BraveMaxResults: 5})
ctx := context.Background() ctx := context.Background()
args := map[string]interface{}{} args := map[string]interface{}{}