Files
xinmi/internal/infrastructure/web_search/metaso_test.go
T
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

88 lines
3.2 KiB
Go

package web_search
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/Tencent/WeKnora/internal/types"
)
func TestMetasoProviderSearch(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatalf("method = %s, want POST", r.Method)
}
if got := r.Header.Get("Authorization"); got != "Bearer mk-test" {
t.Fatalf("Authorization = %q", got)
}
var request metasoSearchRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
t.Fatal(err)
}
if request.Query != "WeKnora" || request.Scope != "scholar" || request.Size != 2 {
t.Fatalf("unexpected request: %+v", request)
}
if !request.IncludeSummary || request.IncludeRawContent || !request.ConciseSnippet {
t.Fatalf("unexpected content options: %+v", request)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"webpages":[
{"title":"First","link":"https://example.com/1","summary":"Summary","snippet":"Snippet","date":"2026-08-09"},
{"title":"Second","link":"https://example.com/2","snippet":"Fallback snippet","date":"invalid"},
{"title":"Third","link":"https://example.com/3","snippet":"must be capped"}
]}`))
}))
defer server.Close()
metaso := &MetasoProvider{client: server.Client(), baseURL: server.URL, apiKey: "mk-test", scope: "scholar"}
results, err := metaso.Search(context.Background(), " WeKnora ", 2, true)
if err != nil {
t.Fatal(err)
}
if len(results) != 2 {
t.Fatalf("len(results) = %d, want 2", len(results))
}
if results[0].Snippet != "Summary" || results[1].Snippet != "Fallback snippet" {
t.Fatalf("unexpected snippets: %q, %q", results[0].Snippet, results[1].Snippet)
}
if results[0].Source != "metaso" || results[0].PublishedAt == nil {
t.Fatalf("unexpected first result: %+v", results[0])
}
if want := time.Date(2026, 8, 9, 0, 0, 0, 0, time.UTC); !results[0].PublishedAt.Equal(want) {
t.Fatalf("date = %v, want %v", results[0].PublishedAt, want)
}
if results[1].PublishedAt != nil {
t.Fatalf("invalid date should be ignored: %v", results[1].PublishedAt)
}
}
func TestValidateMetasoParameters(t *testing.T) {
if err := ValidateMetasoParameters(types.WebSearchProviderParameters{}); err == nil {
t.Fatal("expected missing API key error")
}
if err := ValidateMetasoParameters(types.WebSearchProviderParameters{APIKey: "mk-test", ExtraConfig: map[string]string{"scope": "unknown"}}); err == nil {
t.Fatal("expected invalid scope error")
}
if err := ValidateMetasoParameters(types.WebSearchProviderParameters{APIKey: "mk-test"}); err != nil {
t.Fatalf("default parameters: %v", err)
}
}
func TestMetasoProviderHTTPError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"message":"invalid API key"}`))
}))
defer server.Close()
metaso := &MetasoProvider{client: server.Client(), baseURL: server.URL, apiKey: "bad", scope: defaultMetasoScope}
_, err := metaso.Search(context.Background(), "test", 1, false)
if err == nil || !strings.Contains(err.Error(), "invalid API key") {
t.Fatalf("error = %v", err)
}
}