(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestHTTPJSONKB_Retrieve(t *testing.T) { |
| 15 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 16 | if r.Method != http.MethodPost { |
| 17 | t.Fatalf("method %s", r.Method) |
| 18 | } |
| 19 | b, _ := io.ReadAll(r.Body) |
| 20 | var body map[string]any |
| 21 | _ = json.Unmarshal(b, &body) |
| 22 | if body["query"] != "hello world" { |
| 23 | t.Fatalf("query=%v", body["query"]) |
| 24 | } |
| 25 | _ = json.NewEncoder(w).Encode(map[string]any{ |
| 26 | "chunks": []map[string]any{ |
| 27 | {"text": "chunk one", "source": "a", "score": 0.9}, |
| 28 | {"content": "chunk two", "source": "b"}, |
| 29 | }, |
| 30 | }) |
| 31 | })) |
| 32 | defer srv.Close() |
| 33 | |
| 34 | kb, err := NewHTTPJSONKB(config.KnowledgeBaseConfig{Name: "remote", Type: "http", Endpoint: srv.URL}) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | res, err := kb.Retrieve(context.Background(), "hello world", nil) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if len(res.Chunks) != 2 { |
| 43 | t.Fatalf("chunks=%d", len(res.Chunks)) |
| 44 | } |
| 45 | if res.Chunks[0].Text != "chunk one" || res.Chunks[1].Text != "chunk two" { |
| 46 | t.Fatalf("%+v", res.Chunks) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestParseOpenSearchHits(t *testing.T) { |
| 51 | raw := `{ |
nothing calls this directly
no test coverage detected