82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
package v071
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLibrarySearchIsolatedElasticsearch(t *testing.T) {
|
|
base := os.Getenv("KG_LIBRARY_TEST_ELASTIC_URL")
|
|
if base == "" {
|
|
t.Skip("requires explicit isolated Elasticsearch fixture opt-in")
|
|
}
|
|
if os.Getenv("KG_LIBRARY_TEST_ISOLATED") != "1" {
|
|
t.Fatal("isolated fixture authorization required")
|
|
}
|
|
var id [8]byte
|
|
if _, err := rand.Read(id[:]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
index := "kg_library_test_" + hex.EncodeToString(id[:])
|
|
client := &http.Client{Timeout: 20 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
|
request := func(method, path, body, contentType string) int {
|
|
t.Helper()
|
|
r, err := http.NewRequest(method, base+path, strings.NewReader(body))
|
|
if err != nil {
|
|
t.Fatal("invalid fixture endpoint")
|
|
}
|
|
r.Header.Set("Content-Type", contentType)
|
|
response, err := client.Do(r)
|
|
if err != nil {
|
|
t.Fatal("fixture request failed")
|
|
}
|
|
defer response.Body.Close()
|
|
return response.StatusCode
|
|
}
|
|
if status := request(http.MethodPut, "/"+index, `{"mappings":{"properties":{"tenant_id":{"type":"long"},"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"}}}}`, "application/json"); status != 200 {
|
|
t.Fatalf("create synthetic index HTTP %d", status)
|
|
}
|
|
defer func() {
|
|
if status := request(http.MethodDelete, "/"+index, "", "application/json"); status != 200 {
|
|
t.Errorf("remove fixture index HTTP %d", status)
|
|
}
|
|
if status := request(http.MethodHead, "/"+index, "", "application/json"); status != 404 {
|
|
t.Errorf("fixture index remains HTTP %d", status)
|
|
}
|
|
}()
|
|
var bulk bytes.Buffer
|
|
for i := 1; i <= 3; i++ {
|
|
baseID := "kb-safe"
|
|
if i == 3 {
|
|
baseID = "kb-other"
|
|
}
|
|
fmt.Fprintf(&bulk, "{\"index\":{\"_id\":\"chunk-%d\"}}\n{\"tenant_id\":10000,\"knowledge_base_id\":%q,\"knowledge_id\":\"doc-safe\"}\n", i, baseID)
|
|
}
|
|
if status := request(http.MethodPost, "/"+index+"/_bulk?refresh=true", bulk.String(), "application/x-ndjson"); status != 200 {
|
|
t.Fatalf("fixture bulk HTTP %d", status)
|
|
}
|
|
exporter, err := NewSearchExporter(SearchExporterConfig{BaseURL: base, Index: index, PageSize: 1, HTTPClient: client})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := exporter.ExportLibrary(context.Background(), SearchIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(s.Documents) != 2 {
|
|
t.Fatalf("documents=%d", len(s.Documents))
|
|
}
|
|
empty, err := exporter.ExportLibrary(context.Background(), SearchIdentity{TenantID: 10000, KnowledgeBaseID: "kb-empty"}, 10)
|
|
if err != nil || len(empty.Documents) != 0 {
|
|
t.Fatalf("empty snapshot err=%v documents=%d", err, len(empty.Documents))
|
|
}
|
|
}
|