feat: add Keenable as a configurable web search provider
Add Keenable Search alongside the existing DuckDuckGo / Bing / Google / Tavily / Ollama / Baidu / SearXNG providers. - internal/infrastructure/web_search/keenable.go: KeenableProvider implementing the WebSearchProvider interface, following the Tavily provider pattern. Keyless by default: with no API key it calls the public endpoint (/v1/search/public, rate-limited); a key switches to /v1/search and lifts the cap. Base URL is hardcoded (SSRF-safe), proxy is honored. - Registered "keenable" in the container registry. - Added the keenable provider type + type metadata (RequiresAPIKey=false) and service validation (no required key, like DuckDuckGo). - Provider-specific empty-results diagnostic (keyless rate-limit hint). - Frontend: extend the provider union type with 'keenable'. - Unit tests: keyless vs keyed endpoint, attribution header, maxResults trim, result mapping, error status, name. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package web_search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestKeenableProvider_Search_Keyless(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// No key configured => keyless public endpoint, no X-API-Key.
|
||||
if r.URL.Path != "/v1/search/public" {
|
||||
t.Errorf("path = %q, want /v1/search/public", r.URL.Path)
|
||||
}
|
||||
if got := r.Header.Get("X-API-Key"); got != "" {
|
||||
t.Errorf("X-API-Key = %q, want empty for keyless", got)
|
||||
}
|
||||
if got := r.Header.Get("X-Keenable-Title"); got != "WeKnora" {
|
||||
t.Errorf("X-Keenable-Title = %q, want WeKnora", got)
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
if body["query"] != "hello" || body["mode"] != "pro" {
|
||||
t.Errorf("body = %v, want query=hello mode=pro", body)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"query": "hello",
|
||||
"results": []map[string]any{
|
||||
{"title": "T1", "url": "https://e/1", "description": "d1", "published_at": "2026-05-01T00:00:00Z"},
|
||||
{"title": "T2", "url": "https://e/2", "description": "d2"},
|
||||
{"title": "T3", "url": "https://e/3", "description": "d3"},
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
p := &KeenableProvider{client: srv.Client(), baseURL: srv.URL}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
results, err := p.Search(ctx, "hello", 2, true)
|
||||
if err != nil {
|
||||
t.Fatalf("Search: %v", err)
|
||||
}
|
||||
if len(results) != 2 {
|
||||
t.Fatalf("expected 2 results (maxResults trim), got %d", len(results))
|
||||
}
|
||||
if results[0].Source != "keenable" {
|
||||
t.Errorf("source = %q, want keenable", results[0].Source)
|
||||
}
|
||||
if results[0].Snippet != "d1" || results[0].URL != "https://e/1" {
|
||||
t.Errorf("unexpected first result: %+v", results[0])
|
||||
}
|
||||
if results[0].PublishedAt == nil {
|
||||
t.Errorf("expected PublishedAt to be set on the first result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeenableProvider_Search_Keyed(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// A configured key => authenticated endpoint + X-API-Key.
|
||||
if r.URL.Path != "/v1/search" {
|
||||
t.Errorf("path = %q, want /v1/search", r.URL.Path)
|
||||
}
|
||||
if got := r.Header.Get("X-API-Key"); got != "keen_test" {
|
||||
t.Errorf("X-API-Key = %q, want keen_test", got)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"results": []map[string]any{}})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
p := &KeenableProvider{client: srv.Client(), baseURL: srv.URL, apiKey: "keen_test"}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if _, err := p.Search(ctx, "hi", 5, false); err != nil {
|
||||
t.Fatalf("Search: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeenableProvider_Search_ErrorStatus(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte(`{"message":"rate limited"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
p := &KeenableProvider{client: srv.Client(), baseURL: srv.URL}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if _, err := p.Search(ctx, "x", 5, false); err == nil {
|
||||
t.Fatal("expected an error for a non-200 response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeenableProvider_Name(t *testing.T) {
|
||||
p, err := NewKeenableProvider(types.WebSearchProviderParameters{})
|
||||
if err != nil {
|
||||
t.Fatalf("NewKeenableProvider (keyless): %v", err)
|
||||
}
|
||||
if p.Name() != "keenable" {
|
||||
t.Errorf("Name() = %q, want keenable", p.Name())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user