Files
pipelines/internal/server/search_test.go
T
alan c700b28e6a Add cross-system read-only search
One query now finds anything in the system over the wire and from the CLI.

- protocol: add Search/SearchResults message types and a SearchRequest
  (query, optional kind filter, optional limit) / SearchResponse (typed,
  ranked matches plus truncation and scan-bound counters) pair, with typed
  payloads for pipeline, version, run, step/visit, field text and artifact.
- store: add Store.Search, a read-only query over pipelines,
  pipeline_versions, pipeline_runs, run_steps (+step_control) and
  step_artifacts. Ranks exact name (3) > name substring (2) > body text (1),
  breaks ties by recency then a stable kind/target order, and reports the
  matched field. Bounds work with a 50-default/200-max result cap, a 2000-row
  per-kind scan limit and a 2000-step / 1 MiB field-text scan budget.
- terms: a bare status word or lone date expression (today, yesterday, this
  week, YYYY-MM-DD) selects runs; anything else stays free text.
- server: dispatch protocol.Search to stores[0].Search; read-only and
  unconfirmed.
- cli: add `pipelines search QUERY [--type KIND] [--limit N] [--json]`, help
  text and a BROWSE line.
- store: add OpenReadOnly so the live-database check never runs DDL.
- docs: document the grammar, kinds, ranking and bounds in docs/cli.md,
  scope the TUI omnisearch to pipelines in docs/ui-ux-review.md, and add a
  search bullet to the packaged operations reference.
- tests: protocol round trip, store/ranking/term-parsing/limit/scan-bound
  unit tests, a server end-to-end test, and an env-guarded live check.

No new dependencies; go.mod/go.sum unchanged.
2026-10-16 15:43:25 +01:00

106 lines
3.4 KiB
Go

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)
}
}