- Add wikidata engine (wbsearchentities), tests, factory/planner/config - Wikipedia REST summary: infobox from extract, thumbnail, article URL - InfoboxView URL; render infobox list in results_inner + base styles - Preferences Wikidata toggle; engine badge color for wikidata Made-with: Cursor
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package engines
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/metamorphosis-dev/samsa/internal/contracts"
|
|
)
|
|
|
|
func TestWikidataEngine_Search(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Query().Get("action") != "wbsearchentities" {
|
|
t.Errorf("action=%q", r.URL.Query().Get("action"))
|
|
}
|
|
if got := r.URL.Query().Get("search"); got != "test" {
|
|
t.Errorf("search=%q want test", got)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"search":[{"id":"Q937","label":"Go","description":"Programming language"}]}`))
|
|
}))
|
|
defer ts.Close()
|
|
|
|
orig := wikidataAPIBase
|
|
t.Cleanup(func() { wikidataAPIBase = orig })
|
|
wikidataAPIBase = ts.URL + "/w/api.php"
|
|
|
|
e := &WikidataEngine{client: ts.Client()}
|
|
resp, err := e.Search(context.Background(), contracts.SearchRequest{
|
|
Query: "test",
|
|
Language: "en",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(resp.Results) != 1 {
|
|
t.Fatalf("expected 1 result, got %d", len(resp.Results))
|
|
}
|
|
r0 := resp.Results[0]
|
|
if r0.Engine != "wikidata" {
|
|
t.Errorf("engine=%q", r0.Engine)
|
|
}
|
|
if r0.Title != "Go" {
|
|
t.Errorf("title=%q", r0.Title)
|
|
}
|
|
if r0.URL == nil || !strings.Contains(*r0.URL, "Q937") {
|
|
t.Errorf("url=%v", r0.URL)
|
|
}
|
|
}
|