This commit is contained in:
HugeFrog24
2026-07-17 11:53:45 +02:00
parent 7fd7818142
commit 564f96c97a
5 changed files with 160 additions and 19 deletions
+87 -4
View File
@@ -165,8 +165,8 @@ func TestWebSearchTools(t *testing.T) {
if search == nil {
t.Fatalf("tools[0] is not a web_search tool")
}
if len(search.AllowedDomains) != 2 {
t.Errorf("search AllowedDomains = %v, want 2 entries", search.AllowedDomains)
if !sameStrings(search.AllowedDomains, []string{"example.com/hc", "docs.example.com"}) {
t.Errorf("search AllowedDomains = %v, want the path-scoped list unchanged", search.AllowedDomains)
}
if search.MaxUses.Value != 3 {
t.Errorf("search MaxUses = %d, want 3", search.MaxUses.Value)
@@ -176,8 +176,8 @@ func TestWebSearchTools(t *testing.T) {
if fetch == nil {
t.Fatalf("tools[1] is not a web_fetch tool")
}
if len(fetch.AllowedDomains) != 2 {
t.Errorf("fetch AllowedDomains = %v, want 2 entries", fetch.AllowedDomains)
if !sameStrings(fetch.AllowedDomains, []string{"example.com", "docs.example.com"}) {
t.Errorf("fetch AllowedDomains = %v, want host-only [example.com docs.example.com]", fetch.AllowedDomains)
}
if fetch.MaxContentTokens.Value != 50000 {
t.Errorf("fetch MaxContentTokens = %d, want 50000", fetch.MaxContentTokens.Value)
@@ -187,6 +187,54 @@ func TestWebSearchTools(t *testing.T) {
}
})
t.Run("fetch hosts are deduped", func(t *testing.T) {
tools := webSearchTools(&WebSearchConfig{
AllowedDomains: []string{"a.com/x", "a.com/y", "b.com"},
Fetch: true,
})
fetch := tools[1].OfWebFetchTool20250910
if fetch == nil {
t.Fatalf("tools[1] is not a web_fetch tool")
}
if !sameStrings(fetch.AllowedDomains, []string{"a.com", "b.com"}) {
t.Errorf("fetch AllowedDomains = %v, want deduped [a.com b.com]", fetch.AllowedDomains)
}
})
t.Run("social host is search-only via fetch_allowed_domains", func(t *testing.T) {
tools := webSearchTools(&WebSearchConfig{
AllowedDomains: []string{"helpshift.example/hc", "x.com/thatskygame"},
FetchAllowedDomains: []string{"helpshift.example"},
Fetch: true,
})
search := tools[0].OfWebSearchTool20250305
if search == nil {
t.Fatalf("tools[0] is not a web_search tool")
}
var searchHasSocial bool
for _, d := range search.AllowedDomains {
if d == "x.com/thatskygame" {
searchHasSocial = true
}
}
if !searchHasSocial {
t.Errorf("search AllowedDomains = %v, want it to include x.com/thatskygame", search.AllowedDomains)
}
fetch := tools[1].OfWebFetchTool20250910
if fetch == nil {
t.Fatalf("tools[1] is not a web_fetch tool")
}
if !sameStrings(fetch.AllowedDomains, []string{"helpshift.example"}) {
t.Errorf("fetch AllowedDomains = %v, want only [helpshift.example]", fetch.AllowedDomains)
}
for _, d := range fetch.AllowedDomains {
if strings.HasPrefix(d, "x.com") {
t.Errorf("fetch AllowedDomains leaked the social host: %v", fetch.AllowedDomains)
}
}
})
t.Run("wire shape carries allowed_domains", func(t *testing.T) {
tools := webSearchTools(&WebSearchConfig{
AllowedDomains: []string{"thatgamecompany.helpshift.com/hc"},
@@ -211,6 +259,41 @@ func TestWebSearchTools(t *testing.T) {
})
}
func sameStrings(got, want []string) bool {
if len(got) != len(want) {
return false
}
for i := range got {
if got[i] != want[i] {
return false
}
}
return true
}
func TestFetchHosts(t *testing.T) {
cases := []struct {
name string
in []string
want []string
}{
{"nil in nil out", nil, nil},
{"empty in nil out", []string{}, nil},
{"host passthrough", []string{"example.com"}, []string{"example.com"}},
{"strip path", []string{"example.com/hc/en"}, []string{"example.com"}},
{"dedup after strip", []string{"a.com/x", "a.com/y"}, []string{"a.com"}},
{"preserve order and subdomains", []string{"docs.example.com/a", "example.com"}, []string{"docs.example.com", "example.com"}},
{"drop empty leading slash", []string{"/oops", "ok.com"}, []string{"ok.com"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := fetchHosts(tc.in); !sameStrings(got, tc.want) {
t.Errorf("fetchHosts(%v) = %v, want %v", tc.in, got, tc.want)
}
})
}
}
func TestEmptyStreamError(t *testing.T) {
err := emptyStreamError("max_tokens", 3900, 4000)
for _, want := range []string{"output budget exhausted", "3900", "4000"} {