93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package knowledge
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os/exec"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"cyberstrike-ai/internal/agentruntime"
|
|
"cyberstrike-ai/internal/config"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func requirePiRuntime(t *testing.T) {
|
|
t.Helper()
|
|
if _, err := exec.LookPath("node"); err != nil {
|
|
t.Skip("Node not installed")
|
|
}
|
|
if _, err := agentruntime.ResolveWorker(""); err != nil {
|
|
t.Skip("build runtime/pi before Pi integration tests")
|
|
}
|
|
}
|
|
|
|
func piTextReply(w http.ResponseWriter, text string) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
for _, choice := range []map[string]any{
|
|
{"index": 0, "delta": map[string]any{"role": "assistant", "content": text}, "finish_reason": nil},
|
|
{"index": 0, "delta": map[string]any{}, "finish_reason": "stop"},
|
|
} {
|
|
body, _ := json.Marshal(map[string]any{"id": "fixture", "object": "chat.completion.chunk", "model": "fixture", "choices": []any{choice}})
|
|
fmt.Fprintf(w, "data: %s\n\n", body)
|
|
}
|
|
fmt.Fprint(w, "data: [DONE]\n\n")
|
|
}
|
|
|
|
type recordingRetriever struct{ queries []string }
|
|
|
|
func (r *recordingRetriever) Retrieve(_ context.Context, query string, _ ...RetrievalOption) ([]*Document, error) {
|
|
r.queries = append(r.queries, query)
|
|
return []*Document{{ID: query, Content: query}}, nil
|
|
}
|
|
|
|
func TestNativeMultiQueryExpandsThroughPi(t *testing.T) {
|
|
requirePiRuntime(t)
|
|
var systemPrompt string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var payload struct {
|
|
Messages []struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
} `json:"messages"`
|
|
Tools json.RawMessage `json:"tools"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
t.Error(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
if len(payload.Tools) != 0 {
|
|
t.Errorf("query expansion exposed tools: %s", payload.Tools)
|
|
}
|
|
for _, message := range payload.Messages {
|
|
if message.Role == "system" || message.Role == "developer" {
|
|
systemPrompt = message.Content
|
|
}
|
|
}
|
|
piTextReply(w, "```json\n[\"backup policy\",\"retention rules\",\"backup\"]\n```")
|
|
}))
|
|
defer server.Close()
|
|
|
|
inner := &recordingRetriever{}
|
|
q := &nativeMultiQuery{inner: inner, max: 3, logger: zap.NewNop(),
|
|
oa: config.OpenAIConfig{Model: "fixture-model", APIKey: "fixture-key", BaseURL: server.URL + "/v1"}}
|
|
docs, err := q.Retrieve(context.Background(), "backup")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := []string{"backup", "backup policy", "retention rules"}; !reflect.DeepEqual(inner.queries, want) {
|
|
t.Fatalf("expanded queries = %v, want %v", inner.queries, want)
|
|
}
|
|
if len(docs) != 3 {
|
|
t.Fatalf("fused documents = %d, want 3", len(docs))
|
|
}
|
|
if !strings.Contains(systemPrompt, "at most 2 alternative search queries") {
|
|
t.Fatalf("expansion budget missing from system prompt: %q", systemPrompt)
|
|
}
|
|
} |