Files
knowledge-governance/internal/releaseadapter/v071/library_search_test.go
T
2026-10-27 23:44:12 +08:00

91 lines
3.1 KiB
Go

package v071
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestLibrarySearchUsesPITAndChecksExactTotal(t *testing.T) {
for _, mode := range []string{"complete", "partial", "truncated", "wrong-scope", "repeated-cursor", "limit"} {
t.Run(mode, func(t *testing.T) {
searches, closed := 0, false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.URL.Path == "/chunks/_mapping":
fmt.Fprint(w, `{"chunks":{"mappings":{"properties":{"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"},"tenant_id":{"type":"long"}}}}}`)
case r.Method == http.MethodPost && r.URL.Path == "/chunks/_pit":
if r.URL.Query().Get("allow_partial_search_results") != "false" {
t.Error("partial PIT allowed")
}
fmt.Fprint(w, `{"id":"pit-0"}`)
case r.Method == http.MethodPost && r.URL.Path == "/_search":
var body map[string]any
if json.NewDecoder(r.Body).Decode(&body) != nil {
t.Error("bad query")
}
searches++
if _, ok := body["from"]; ok {
t.Error("offset paging is not a fixed snapshot")
}
pit, _ := body["pit"].(map[string]any)
if pit["id"] != fmt.Sprintf("pit-%d", searches-1) {
t.Error("latest PIT identity not used")
}
if searches > 1 {
if _, ok := body["search_after"]; !ok {
t.Error("missing cursor")
}
}
failed := 0
if mode == "partial" {
failed = 1
}
base := "kb-safe"
if mode == "wrong-scope" {
base = "kb-wrong"
}
hits := fmt.Sprintf(`[{"_id":"chunk-%d","_source":{"tenant_id":10000,"knowledge_base_id":%q,"knowledge_id":"doc-safe"},"sort":[%d]}]`, searches, base, searches)
if mode == "truncated" && searches == 2 {
hits = "[]"
}
if mode == "repeated-cursor" && searches == 2 {
hits = `[{"_id":"chunk-2","_source":{"knowledge_base_id":"kb-safe","knowledge_id":"doc-safe"},"sort":[1]}]`
}
fmt.Fprintf(w, `{"pit_id":"pit-%d","timed_out":false,"_shards":{"total":1,"successful":1,"failed":%d},"hits":{"total":{"value":2,"relation":"eq"},"hits":%s}}`, searches, failed, hits)
case r.Method == http.MethodDelete && r.URL.Path == "/_pit":
closed = true
fmt.Fprint(w, `{"succeeded":true,"num_freed":1}`)
default:
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
http.Error(w, "unexpected", 400)
}
}))
defer server.Close()
exporter, err := NewSearchExporter(SearchExporterConfig{BaseURL: server.URL, Index: "chunks", PageSize: 1})
if err != nil {
t.Fatal(err)
}
limit := 10
if mode == "limit" {
limit = 1
}
snapshot, err := exporter.ExportLibrary(context.Background(), SearchIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, limit)
if mode == "complete" {
if err != nil || len(snapshot.Documents) != 2 {
t.Fatalf("export err=%v documents=%d", err, len(snapshot.Documents))
}
} else if err == nil {
t.Fatal("invalid snapshot accepted")
}
if !closed {
t.Fatal("PIT leaked")
}
})
}
}