package server import ( "bytes" "context" "encoding/json" "net/http/httptest" "path/filepath" "strconv" "strings" "testing" "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) } }