Files
pipelines/internal/protocol/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

83 lines
3.1 KiB
Go

package protocol
import (
"encoding/json"
"strings"
"testing"
)
func TestSearchRequestResponseRoundTrip(t *testing.T) {
request := Message{Type: Search, ID: "req-1", Search: &SearchRequest{Query: "release", Kind: SearchKindRun, Limit: 5}}
encoded, err := json.Marshal(request)
if err != nil {
t.Fatal(err)
}
var decoded Message
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatal(err)
}
if decoded.Type != Search || decoded.ID != "req-1" || decoded.Search == nil {
t.Fatalf("request envelope lost: %+v", decoded)
}
if decoded.Search.Query != "release" || decoded.Search.Kind != SearchKindRun || decoded.Search.Limit != 5 {
t.Fatalf("search request lost: %+v", decoded.Search)
}
response := Message{Type: SearchResults, ID: "req-1", SearchResults: &SearchResponse{
Query: "release",
Results: []SearchResult{{Kind: SearchKindPipeline, Target: "pipeline:1", Score: 3, MatchedField: "name", Pipeline: &SearchPipeline{ID: 1, Name: "release", Version: 2, Stages: 3}}},
Cap: 50,
ScanBoundBytes: SearchMaxScanBytes,
}}
encoded, err = json.Marshal(response)
if err != nil {
t.Fatal(err)
}
var echoed Message
if err := json.Unmarshal(encoded, &echoed); err != nil {
t.Fatal(err)
}
if echoed.ID != "req-1" || echoed.Type != SearchResults || echoed.SearchResults == nil {
t.Fatalf("response envelope or ID echo lost: %+v", echoed)
}
got := echoed.SearchResults
if got.Query != "release" || got.Cap != 50 || got.ScanBoundBytes != SearchMaxScanBytes || len(got.Results) != 1 {
t.Fatalf("search response lost: %+v", got)
}
hit := got.Results[0]
if hit.Pipeline == nil || hit.Pipeline.Name != "release" || hit.Pipeline.Version != 2 || hit.Pipeline.Stages != 3 || hit.Score != 3 || hit.MatchedField != "name" || hit.Target != "pipeline:1" {
t.Fatalf("pipeline payload lost: %+v", hit)
}
}
func TestSearchResultOmitsAbsentPayloadsAndEmptyKind(t *testing.T) {
encoded, err := json.Marshal(SearchResult{Kind: SearchKindRun, Target: "run:1", Score: 1, MatchedField: "status"})
if err != nil {
t.Fatal(err)
}
for _, key := range []string{"pipeline", "version", "run", "step", "field", "artifact", "recency"} {
if strings.Contains(string(encoded), `"`+key+`":`) {
t.Fatalf("absent %s payload must be omitted: %s", key, encoded)
}
}
var decoded SearchResult
if err := json.Unmarshal([]byte(`{"kind":"run","target":"run:1","score":1,"matched_field":"status"}`), &decoded); err != nil {
t.Fatal(err)
}
if decoded.Pipeline != nil || decoded.Version != nil || decoded.Run != nil || decoded.Step != nil || decoded.Field != nil || decoded.Artifact != nil {
t.Fatalf("absent payloads must decode nil: %+v", decoded)
}
response := Message{Type: SearchResults, ID: "x", SearchResults: &SearchResponse{Query: "q", Results: []SearchResult{}}}
encoded, err = json.Marshal(response)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(encoded), `"kind"`) {
t.Fatalf("empty response kind must be omitted: %s", encoded)
}
if !strings.Contains(string(encoded), `"results":[]`) {
t.Fatalf("results must always be present: %s", encoded)
}
}