package app import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" ) func TestAnswerReadsOnlySourcesTheRerankerFoundUseful(t *testing.T) { s, page := fixtureService(t) status, body := callHTTP(t, s, "/answer", `{"query":"request limits"}`) if status != 200 { t.Fatal(status, string(body["error"])) } var citations []Result _ = json.Unmarshal(body["citations"], &citations) if len(citations) != 1 || citations[0].URL != page.URL { t.Fatalf("a candidate scored 0.02 became a source: %+v", citations) } var digest string _ = s.log.db.QueryRow("select request_json from completions where task='synthesis_digest'").Scan(&digest) if !strings.Contains(digest, `\n[1] `) || strings.Contains(digest, `\n[2] `) || strings.Contains(digest, "[source 1]") { t.Fatal("sources are not numbered as their citations") } var passages string _ = s.log.db.QueryRow("select group_concat(request_json) from decisions where task='passage'").Scan(&passages) if strings.Contains(passages, page.URL+"/bad") { t.Fatal("a low-scored candidate was fetched and judged") } } func TestAnswerKeepsTheBestSourceWhenAllScoreLow(t *testing.T) { s, page := fixtureService(t) s.searcher = fixedSearch{{ID: page.URL + "/bad", URL: page.URL + "/bad", Title: "bad candidate"}} if status, body := callHTTP(t, s, "/answer", `{"query":"request limits"}`); status != 200 { t.Fatal("the floor removed every source:", status, string(body["error"])) } } func TestSearxCandidatesCarryTheirSnippetAndID(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if got := r.URL.Query().Get("categories"); got != "general,it,science" { t.Errorf("searx categories %q", got) } writeJSON(w, 200, map[string]any{"results": []map[string]string{ {"url": "https://docs.example.com/limits#top", "title": "limits", "content": " requests may contain\n up to 8 mib "}, {"url": "https://docs.example.com/limits", "title": "limits again", "content": "duplicate"}, {"url": "not a url", "title": "junk", "content": ""}, }}) })) t.Cleanup(srv.Close) hits, err := (&Searx{BaseURL: srv.URL, HTTP: srv.Client(), maxBytes: 1 << 20}).Search(context.Background(), "limits", 10) if err != nil { t.Fatal(err) } if len(hits) != 1 || hits[0].ID != "https://docs.example.com/limits" || hits[0].snippet != "requests may contain up to 8 mib" { t.Fatalf("unexpected searx candidates: %+v", hits) } }