okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petanswers: Markdown with numbered citations, as long as the question needs24e631b

main
2.4 KiB62 linesraw
1package app
2
3import (
4	"context"
5	"encoding/json"
6	"net/http"
7	"net/http/httptest"
8	"strings"
9	"testing"
10)
11
12func TestAnswerReadsOnlySourcesTheRerankerFoundUseful(t *testing.T) {
13	s, page := fixtureService(t)
14	status, body := callHTTP(t, s, "/answer", `{"query":"request limits"}`)
15	if status != 200 {
16		t.Fatal(status, string(body["error"]))
17	}
18	var citations []Result
19	_ = json.Unmarshal(body["citations"], &citations)
20	if len(citations) != 1 || citations[0].URL != page.URL {
21		t.Fatalf("a candidate scored 0.02 became a source: %+v", citations)
22	}
23	var digest string
24	_ = s.log.db.QueryRow("select request_json from completions where task='synthesis_digest'").Scan(&digest)
25	if !strings.Contains(digest, `\n[1] `) || strings.Contains(digest, `\n[2] `) || strings.Contains(digest, "[source 1]") {
26		t.Fatal("sources are not numbered as their citations")
27	}
28	var passages string
29	_ = s.log.db.QueryRow("select group_concat(request_json) from decisions where task='passage'").Scan(&passages)
30	if strings.Contains(passages, page.URL+"/bad") {
31		t.Fatal("a low-scored candidate was fetched and judged")
32	}
33}
34
35func TestAnswerKeepsTheBestSourceWhenAllScoreLow(t *testing.T) {
36	s, page := fixtureService(t)
37	s.searcher = fixedSearch{{ID: page.URL + "/bad", URL: page.URL + "/bad", Title: "bad candidate"}}
38	if status, body := callHTTP(t, s, "/answer", `{"query":"request limits"}`); status != 200 {
39		t.Fatal("the floor removed every source:", status, string(body["error"]))
40	}
41}
42
43func TestSearxCandidatesCarryTheirSnippetAndID(t *testing.T) {
44	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45		if got := r.URL.Query().Get("categories"); got != "general,it,science" {
46			t.Errorf("searx categories %q", got)
47		}
48		writeJSON(w, 200, map[string]any{"results": []map[string]string{
49			{"url": "https://docs.example.com/limits#top", "title": "limits", "content": "  requests may contain\n up to 8 mib "},
50			{"url": "https://docs.example.com/limits", "title": "limits again", "content": "duplicate"},
51			{"url": "not a url", "title": "junk", "content": ""},
52		}})
53	}))
54	t.Cleanup(srv.Close)
55	hits, err := (&Searx{BaseURL: srv.URL, HTTP: srv.Client(), maxBytes: 1 << 20}).Search(context.Background(), "limits", 10)
56	if err != nil {
57		t.Fatal(err)
58	}
59	if len(hits) != 1 || hits[0].ID != "https://docs.example.com/limits" || hits[0].snippet != "requests may contain up to 8 mib" {
60		t.Fatalf("unexpected searx candidates: %+v", hits)
61	}
62}