fix(web-search): normalize default tenant web search config at runtime

This commit is contained in:
sqkstwj
2026-04-30 10:00:42 +08:00
committed by lyingbug
parent 3b23713c54
commit a5f80b747c
6 changed files with 114 additions and 28 deletions
+35
View File
@@ -25,6 +25,41 @@ type WebSearchConfig struct {
ProxyURL string `json:"proxy_url,omitempty"` // Optional per-request proxy override; normally empty — use WebSearchProviderEntity.Parameters.proxy_url. Merged at call time when set.
}
const (
DefaultWebSearchMaxResults = 10
DefaultWebSearchCompressionMethod = "none"
)
// DefaultWebSearchConfig returns the shared default tenant-level web search configuration.
func DefaultWebSearchConfig() *WebSearchConfig {
return &WebSearchConfig{
MaxResults: DefaultWebSearchMaxResults,
IncludeDate: false,
CompressionMethod: DefaultWebSearchCompressionMethod,
Blacklist: []string{},
}
}
// EffectiveWebSearchConfig normalizes a possibly empty config to the effective runtime config.
func EffectiveWebSearchConfig(cfg *WebSearchConfig) *WebSearchConfig {
if cfg == nil {
return DefaultWebSearchConfig()
}
normalized := *cfg
if normalized.MaxResults <= 0 {
normalized.MaxResults = DefaultWebSearchMaxResults
}
if normalized.CompressionMethod == "" {
normalized.CompressionMethod = DefaultWebSearchCompressionMethod
}
if normalized.Blacklist == nil {
normalized.Blacklist = []string{}
}
return &normalized
}
// Value implements driver.Valuer interface for WebSearchConfig
func (c WebSearchConfig) Value() (driver.Value, error) {
return json.Marshal(c)
+54
View File
@@ -0,0 +1,54 @@
package types
import "testing"
func TestEffectiveWebSearchConfigUsesDefaultsForNilConfig(t *testing.T) {
cfg := EffectiveWebSearchConfig(nil)
if cfg.MaxResults != DefaultWebSearchMaxResults {
t.Fatalf("MaxResults = %d, want %d", cfg.MaxResults, DefaultWebSearchMaxResults)
}
if cfg.CompressionMethod != DefaultWebSearchCompressionMethod {
t.Fatalf("CompressionMethod = %q, want %q", cfg.CompressionMethod, DefaultWebSearchCompressionMethod)
}
if cfg.Blacklist == nil {
t.Fatal("Blacklist = nil, want empty slice")
}
if len(cfg.Blacklist) != 0 {
t.Fatalf("Blacklist length = %d, want 0", len(cfg.Blacklist))
}
}
func TestEffectiveWebSearchConfigNormalizesZeroValuesWithoutMutatingSource(t *testing.T) {
source := &WebSearchConfig{
IncludeDate: true,
}
cfg := EffectiveWebSearchConfig(source)
if cfg == source {
t.Fatal("EffectiveWebSearchConfig returned original pointer, want normalized copy")
}
if cfg.MaxResults != DefaultWebSearchMaxResults {
t.Fatalf("MaxResults = %d, want %d", cfg.MaxResults, DefaultWebSearchMaxResults)
}
if cfg.CompressionMethod != DefaultWebSearchCompressionMethod {
t.Fatalf("CompressionMethod = %q, want %q", cfg.CompressionMethod, DefaultWebSearchCompressionMethod)
}
if cfg.Blacklist == nil {
t.Fatal("Blacklist = nil, want empty slice")
}
if !cfg.IncludeDate {
t.Fatal("IncludeDate = false, want true")
}
if source.MaxResults != 0 {
t.Fatalf("source MaxResults mutated to %d, want 0", source.MaxResults)
}
if source.CompressionMethod != "" {
t.Fatalf("source CompressionMethod mutated to %q, want empty string", source.CompressionMethod)
}
if source.Blacklist != nil {
t.Fatalf("source Blacklist mutated to non-nil value: %#v", source.Blacklist)
}
}