mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-08-28 22:11:38 +00:00
Add web search capabilities
This commit is contained in:
@@ -875,3 +875,84 @@ func TestThinkingConfigLoad(t *testing.T) {
|
||||
t.Errorf("MaxTokens = %d, want 4096", cfg.MaxTokens)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSearchConfig(t *testing.T) {
|
||||
t.Run("absent leaves nil", func(t *testing.T) {
|
||||
var cfg BotConfig
|
||||
if err := json.Unmarshal([]byte(`{}`), &cfg); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if cfg.WebSearch != nil {
|
||||
t.Errorf("WebSearch = %+v, want nil when absent", cfg.WebSearch)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("loads allowlist", func(t *testing.T) {
|
||||
jsonData := `{
|
||||
"web_search": {
|
||||
"allowed_domains": ["example.com/hc", "docs.example.com"],
|
||||
"max_uses": 3,
|
||||
"fetch": true,
|
||||
"max_content_tokens": 50000
|
||||
}
|
||||
}`
|
||||
var cfg BotConfig
|
||||
if err := json.Unmarshal([]byte(jsonData), &cfg); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if cfg.WebSearch == nil {
|
||||
t.Fatalf("WebSearch = nil, want populated")
|
||||
}
|
||||
if len(cfg.WebSearch.AllowedDomains) != 2 {
|
||||
t.Errorf("AllowedDomains = %v, want 2 entries", cfg.WebSearch.AllowedDomains)
|
||||
}
|
||||
if cfg.WebSearch.MaxUses != 3 {
|
||||
t.Errorf("MaxUses = %d, want 3", cfg.WebSearch.MaxUses)
|
||||
}
|
||||
if !cfg.WebSearch.Fetch {
|
||||
t.Error("Fetch = false, want true")
|
||||
}
|
||||
if cfg.WebSearch.MaxContentTokens != 50000 {
|
||||
t.Errorf("MaxContentTokens = %d, want 50000", cfg.WebSearch.MaxContentTokens)
|
||||
}
|
||||
})
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
ws *WebSearchConfig
|
||||
wantErr string
|
||||
}{
|
||||
{"nil ok", nil, ""},
|
||||
{"allowlist ok", &WebSearchConfig{AllowedDomains: []string{"example.com"}, MaxUses: 3}, ""},
|
||||
{"blocklist ok", &WebSearchConfig{BlockedDomains: []string{"evil.com"}}, ""},
|
||||
{"empty ok", &WebSearchConfig{}, ""},
|
||||
{"both allow and block rejected",
|
||||
&WebSearchConfig{AllowedDomains: []string{"a.com"}, BlockedDomains: []string{"b.com"}},
|
||||
"cannot set both allowed_domains and blocked_domains"},
|
||||
{"negative max_uses rejected",
|
||||
&WebSearchConfig{AllowedDomains: []string{"a.com"}, MaxUses: -1},
|
||||
"'web_search.max_uses' must be greater than 0"},
|
||||
{"negative max_content_tokens rejected",
|
||||
&WebSearchConfig{AllowedDomains: []string{"a.com"}, MaxContentTokens: -1},
|
||||
"'web_search.max_content_tokens' must be greater than 0"},
|
||||
}
|
||||
for i, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := thinkingTestConfig(fmt.Sprintf("bot-web-%d", i))
|
||||
cfg.WebSearch = tc.ws
|
||||
err := validateConfig(&cfg, map[string]bool{}, map[string]bool{})
|
||||
if tc.wantErr == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("validateConfig(web_search=%+v) = %v, want nil", tc.ws, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("validateConfig(web_search=%+v) = nil, want error containing %q", tc.ws, tc.wantErr)
|
||||
}
|
||||
if !contains(err.Error(), tc.wantErr) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tc.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user