package websearch import ( "context" "fmt" "testing" ) func TestTavilyIntegration(t *testing.T) { // 这是一个集成测试,会实际请求网络。 if testing.Short() { t.Skip("skipping integration test in short mode") } // 这里的 API Key 从环境变量中获取或手动填写的测试环境变量中读取。 apiKey := "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" if apiKey == "" { t.Skip("TAVILY_API_KEY is not set") } tool := New(Config{Engine: "tavily", APIKey: apiKey}, 4000, nil) ctx := context.Background() query := "wuhan weather today" result, err := tool.Call(ctx, query) if err != nil { t.Fatalf("Tavily search failed: %v", err) } fmt.Printf("Tavily search result for '%s':\n%s\n", query, result) if result == "" { t.Fatal("expected non-empty result from Tavily") } } func TestDuckDuckGoIntegration(t *testing.T) { // 这是一个集成测试,会实际请求网络。 // 在 CI 环境中可能需要跳过。 if testing.Short() { t.Skip("skipping integration test in short mode") } tool := New(Config{Engine: "duckduckgo"}, 4000, nil) ctx := context.Background() query := "wuhan weather" result, err := tool.Call(ctx, query) if err != nil { t.Fatalf("DuckDuckGo search failed: %v", err) } fmt.Printf("DuckDuckGo search result for '%s':\n%s\n", query, result) if result == "" { t.Fatal("expected non-empty result from DuckDuckGo") } } func TestNewDefaultEngine(t *testing.T) { tool := New(Config{}, 4000, nil) if tool.Name() != "web_search" { t.Fatalf("expected name web_search, got %s", tool.Name()) } if tool.engine != "duckduckgo" { t.Fatalf("expected default engine duckduckgo, got %s", tool.engine) } } func TestNewBraveEngine(t *testing.T) { tool := New(Config{Engine: "brave", APIKey: "test-key"}, 4000, nil) if tool.engine != "brave" { t.Fatalf("expected engine brave, got %s", tool.engine) } if tool.apiKey != "test-key" { t.Fatalf("expected apiKey test-key, got %s", tool.apiKey) } } func TestCallRejectsEmptyQuery(t *testing.T) { tool := New(Config{}, 4000, nil) _, err := tool.Call(nil, " ") if err == nil { t.Fatal("expected error for empty query") } } func TestFormatDuckDuckGoResultWithAnswer(t *testing.T) { tool := New(Config{}, 4000, nil) ddg := duckDuckGoResponse{ Answer: "42", AbstractText: "The answer to everything.", } result := tool.formatDuckDuckGoResult("meaning of life", ddg) if result == "" { t.Fatal("expected non-empty result") } if len(result) == 0 { t.Fatal("result should contain content") } } func TestFormatBraveResultEmpty(t *testing.T) { tool := New(Config{Engine: "brave"}, 4000, nil) resp := braveSearchResponse{} result := tool.formatBraveResult("test", resp) if result == "" { t.Fatal("expected non-empty result") } }