Files
pipelines/internal/protocol/search_test.go
T
alan 80232a4ffe Add omni search: ranked mixed results in the TUI and CLI
Back the TUI "/" popup with the server's search command and add a matching
pipelines search CLI command.

- protocol: SearchRequest/SearchResponse and per-kind SearchResult payloads,
  one shared Label for the six-kind badge dispatch and RuneIndexFold for
  case-insensitive matching.
- pipeline: the read-only, ranked, bounded Store.Search implementation.
- cli: search QUERY [--type KIND] [--limit N] [--json], printing the shared
  label plus a text-field snippet when present.
- tui: type to search, up/down to choose, Enter to open and Esc to cancel over
  one mixed ranked list, with short type badges and a bracketed non-colour
  match marker; Enter navigates to the matched pipeline, run, step, field
  section or artifact entry.
- tests: protocol and TUI coverage, including an env-gated live test against
  the running server.
2026-10-16 23:03:17 +01:00

126 lines
5.3 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)
}
}
// TestRuneIndexFold pins the shared case-insensitive matcher used by both the
// server's snippet window and the TUI's marker, so it cannot drift per package.
func TestRuneIndexFold(t *testing.T) {
cases := []struct {
text, query string
want int
}{
{text: "prefix café résumé", query: "CAFÉ", want: 7},
{text: "Needle in a haystack", query: "needle", want: 0},
{text: "absent", query: "xyz", want: -1},
{text: "abc", query: "", want: -1},
{text: "ab", query: "abc", want: -1},
}
for _, tc := range cases {
if got := RuneIndexFold([]rune(tc.text), []rune(tc.query)); got != tc.want {
t.Fatalf("RuneIndexFold(%q, %q) = %d, want %d", tc.text, tc.query, got, tc.want)
}
}
}
// TestSearchLabelPerKind pins the single per-kind label shared by the CLI and
// TUI, so the six-kind dispatch lives in one place.
func TestSearchLabelPerKind(t *testing.T) {
cases := []struct {
result SearchResult
want string
}{
{SearchResult{Kind: SearchKindPipeline, Target: "pipeline:1", Pipeline: &SearchPipeline{Name: "demo", Version: 2, Stages: 3}}, "demo · v2 · 3 stages"},
{SearchResult{Kind: SearchKindVersion, Target: "pipeline:1:v2", Version: &SearchVersion{PipelineID: 1, Version: 2, ContentHash: "abc"}}, "pipeline 1 · v2 · abc"},
{SearchResult{Kind: SearchKindRun, Target: "run:7", Run: &SearchRun{ID: 7, PipelineName: "demo", Status: "succeeded"}}, "run 7 · demo · succeeded"},
{SearchResult{Kind: SearchKindStep, Target: "run:7:2", Step: &SearchStep{RunID: 7, Sequence: 2, Stage: "reviewer", Outcome: "success"}}, "run 7 · step 2 · reviewer · success"},
{SearchResult{Kind: SearchKindField, Target: "run:7:2:output", Field: &SearchField{RunID: 7, Sequence: 2, Field: "output", Snippet: "[needle] output"}}, "run 7 · step 2 · output"},
{SearchResult{Kind: SearchKindArtifact, Target: "/artifacts/7/2/shot.png", Artifact: &SearchArtifact{RunID: 7, Sequence: 2, Name: "shot.png", ContentType: "image/png"}}, "run 7 · step 2 · shot.png · image/png"},
// A result missing its typed payload falls back to the navigable target.
{SearchResult{Kind: SearchKindRun, Target: "run:9"}, "run:9"},
}
for _, tc := range cases {
if got := tc.result.Label(); got != tc.want {
t.Fatalf("Label(%s %s) = %q, want %q", tc.result.Kind, tc.result.Target, got, tc.want)
}
}
}
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)
}
}