148 lines
4.9 KiB
Go
148 lines
4.9 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/cli"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// TestCLISearchEndToEndEveryKind seeds a store whose pipeline, source, run,
|
|
// stage, field text and artifact all contain one token, then runs the CLI
|
|
// search command over a real Handler and asserts the server returned one match
|
|
// of every kind and rejects an unknown --type rather than ignoring it.
|
|
func TestCLISearchEndToEndEveryKind(t *testing.T) {
|
|
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "search-e2e.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "Zephyr Build", pipeline.Template([]string{"zephyr"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, _, err := store.CreateRun(ctx, definition, "zephyr-key", "/work/zephyr", false, nil, map[string]any{"prompt": "zephyr task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, run, 1, "zephyr", map[string]any{"prompt": "zephyr task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.FinishStep(ctx, run, protocol.RunStep{
|
|
Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd,
|
|
Output: map[string]any{"summary": "zephyr completed"},
|
|
Artifacts: []protocol.StageArtifact{{Name: "zephyr.txt", SizeBytes: 12, ContentType: "text/plain", URL: "/artifacts/" + strconv.FormatInt(run, 10) + "/1/zephyr.txt"}},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := Handler(ctx, store)
|
|
defer func() { cancel(); WaitRuns(handler) }()
|
|
http := httptest.NewServer(handler)
|
|
defer http.Close()
|
|
url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect"
|
|
|
|
runCLI := func(args ...string) (string, error) {
|
|
var out bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
|
|
err := app.Run(ctx, append([]string{"--url", url}, args...))
|
|
return out.String(), err
|
|
}
|
|
output, err := runCLI("search", "zephyr", "--json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var response protocol.SearchResponse
|
|
if err := json.Unmarshal([]byte(output), &response); err != nil {
|
|
t.Fatal(err, output)
|
|
}
|
|
kinds := map[string]bool{}
|
|
for _, hit := range response.Results {
|
|
kinds[hit.Kind] = true
|
|
if hit.Target == "" || hit.MatchedField == "" || hit.Score == 0 {
|
|
t.Fatalf("hit lacks ranking/navigation context: %+v", hit)
|
|
}
|
|
}
|
|
for _, kind := range protocol.SearchKinds {
|
|
if !kinds[kind] {
|
|
t.Fatalf("kind %q missing from end-to-end search: %s", kind, output)
|
|
}
|
|
}
|
|
|
|
if output, err := runCLI("search", "zephyr", "--type", "run", "--json"); err != nil {
|
|
t.Fatal(err)
|
|
} else {
|
|
var filtered protocol.SearchResponse
|
|
if err := json.Unmarshal([]byte(output), &filtered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, hit := range filtered.Results {
|
|
if hit.Kind != protocol.SearchKindRun {
|
|
t.Fatalf("kind filter leaked %q", hit.Kind)
|
|
}
|
|
}
|
|
}
|
|
|
|
if _, err := runCLI("search", "zephyr", "--type", "bogus"); err == nil || !strings.Contains(err.Error(), "unknown search kind") {
|
|
t.Fatalf("invalid kind must error, got %v", err)
|
|
}
|
|
|
|
// The browser sends an actual empty query, not an invented wildcard. Exercise
|
|
// that unchanged wire shape over /connect against this throwaway store.
|
|
wireCtx, wireCancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer wireCancel()
|
|
conn, _, err := websocket.Dial(wireCtx, url, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.CloseNow()
|
|
for _, kind := range append(append([]string{}, protocol.SearchKinds...), "", "bogus") {
|
|
request := protocol.Message{Type: protocol.Search, ID: "recent-" + kind, Search: &protocol.SearchRequest{Query: "", Kind: kind, Limit: 1}}
|
|
if err := wsjson.Write(wireCtx, conn, request); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var reply protocol.Message
|
|
for reply.ID != request.ID {
|
|
if err := wsjson.Read(wireCtx, conn, &reply); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if kind == "bogus" {
|
|
if !strings.Contains(reply.Error, "unknown search kind") {
|
|
t.Fatalf("blank text must not bypass kind validation: %+v", reply)
|
|
}
|
|
continue
|
|
}
|
|
if reply.SearchResults == nil || reply.SearchResults.Cap != 1 || reply.SearchResults.Query != "" {
|
|
t.Fatalf("invalid kind-only reply: %+v", reply)
|
|
}
|
|
if kind == "" {
|
|
if len(reply.SearchResults.Results) != 0 {
|
|
t.Fatalf("blank unfiltered query must remain a no-op: %+v", reply)
|
|
}
|
|
} else if hits := reply.SearchResults.Results; len(hits) != 1 || hits[0].Kind != kind {
|
|
t.Fatalf("missing bounded kind-only result for %q: %+v", kind, reply)
|
|
}
|
|
}
|
|
}
|