yao/tools/webfetch/webfetch_test.go
Max 7da06a4ae1 feat(search): refactor builtinSearch to utilize websearch tool and enhance context handling
- Updated the builtinSearch function to delegate search operations to the websearch tool, improving modularity and maintainability.
- Enhanced context handling by passing the agent context to builtinSearch, allowing for user and team identification during searches.
- Added a new DecryptValue function in cloud.go to streamline value decryption, delegating to the existing config.DecryptValue method.
- Updated .gitignore to include tools/README.md for better project organization.
2026-05-02 19:58:03 +08:00

74 lines
1.8 KiB
Go

package webfetch
import (
"testing"
)
func TestDirectFetch_Success(t *testing.T) {
res, err := directFetch("https://example.com", false)
if err != nil {
t.Fatalf("directFetch failed: %v", err)
}
if res.StatusCode != 200 {
t.Errorf("expected 200, got %d", res.StatusCode)
}
if len(res.Body) < 100 {
t.Error("expected body with at least 100 bytes")
}
}
func TestDirectFetch_Bot(t *testing.T) {
res, err := directFetch("https://example.com", true)
if err != nil {
t.Fatalf("directFetch with bot failed: %v", err)
}
if res.StatusCode != 200 {
t.Errorf("expected 200, got %d", res.StatusCode)
}
}
func TestFetchHTML_Local(t *testing.T) {
cfg := &fetchConfig{}
resp := fetchHTML(cfg, "https://example.com")
if resp == nil {
t.Fatal("expected non-nil response")
}
if resp.Format != "html" {
t.Errorf("expected format 'html', got '%s'", resp.Format)
}
if resp.Content == "" {
t.Error("expected non-empty content")
}
}
func TestFetchMarkdown_Local(t *testing.T) {
cfg := &fetchConfig{}
resp := fetchMarkdown(cfg, "https://example.com")
if resp == nil {
t.Fatal("expected non-nil response")
}
if resp.Format != "markdown" {
t.Errorf("expected format 'markdown', got '%s'", resp.Format)
}
if resp.Content == "" {
t.Error("expected non-empty content")
}
}
func TestBuildMdURL(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"https://example.com/docs/page", "https://example.com/docs/page.md"},
{"https://example.com/docs/page/", "https://example.com/docs/page.md"},
{"https://example.com/docs/page.md", ""},
{"https://example.com/docs/page.MDX", ""},
}
for _, tt := range tests {
got := buildMdURL(tt.input)
if got != tt.expected {
t.Errorf("buildMdURL(%q) = %q, want %q", tt.input, got, tt.expected)
}
}
}