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.
This commit is contained in:
+259
-31
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/sahilm/fuzzy"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/draft"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
@@ -19,29 +19,57 @@ func selectionWindow(count, index, capacity int) (int, int) {
|
||||
return start, min(count, start+capacity)
|
||||
}
|
||||
|
||||
func (m Model) searchMatches() []protocol.PipelineSummary {
|
||||
query := strings.TrimPrefix(strings.TrimSpace(m.commandInput.Value()), "/")
|
||||
if query == "" {
|
||||
return m.pipelines
|
||||
// searchNav remembers the step and section to focus once an asynchronously
|
||||
// requested run arrives, so Enter on a step/artifact/field match lands exactly
|
||||
// where it matched.
|
||||
type searchNav struct {
|
||||
runID int64
|
||||
sequence int
|
||||
section int
|
||||
artifact string
|
||||
}
|
||||
|
||||
// requestSearch asks the server for the current query. At most one request is in
|
||||
// flight: a keystroke while a request is outstanding is coalesced and the latest
|
||||
// text is sent when the response arrives. Stale responses are dropped by ID, so
|
||||
// no keystroke triggers overlapping or unbounded scans. The server owns parsing
|
||||
// (status words, dates and the optional kind filter), so the client sends the
|
||||
// raw text and needs no second matcher.
|
||||
func (m Model) requestSearch() (tea.Model, tea.Cmd) {
|
||||
query := strings.TrimSpace(m.commandInput.Value())
|
||||
if m.conn == nil {
|
||||
m.searchResults = nil
|
||||
m.searchText = ""
|
||||
return m, nil
|
||||
}
|
||||
names := make([]string, len(m.pipelines))
|
||||
for i, p := range m.pipelines {
|
||||
names[i] = p.Name
|
||||
if m.searchInFlight {
|
||||
m.searchQueued = true
|
||||
return m, nil
|
||||
}
|
||||
result := []protocol.PipelineSummary{}
|
||||
for _, match := range fuzzy.Find(query, names) {
|
||||
result = append(result, m.pipelines[match.Index])
|
||||
}
|
||||
return result
|
||||
m.nextID++
|
||||
m.searchRequest = strconv.FormatUint(m.nextID, 10)
|
||||
m.searchInFlight = true
|
||||
m.searchError = ""
|
||||
return m, m.send(protocol.Message{Type: protocol.Search, ID: m.searchRequest, Search: &protocol.SearchRequest{Query: query}})
|
||||
}
|
||||
|
||||
func (m *Model) closeSearch() {
|
||||
m.paletteOpen = false
|
||||
m.searchOpen = false
|
||||
m.keys.palette = false
|
||||
m.searchResults = nil
|
||||
m.searchText = ""
|
||||
m.searchRequest = ""
|
||||
m.searchInFlight = false
|
||||
m.searchQueued = false
|
||||
m.searchError = ""
|
||||
}
|
||||
|
||||
func (m Model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
items := m.searchMatches()
|
||||
items := m.searchResults
|
||||
switch msg.String() {
|
||||
case "esc":
|
||||
m.paletteOpen = false
|
||||
m.searchOpen = false
|
||||
m.keys.palette = false
|
||||
m.closeSearch()
|
||||
return m, nil
|
||||
case "down", "ctrl+n":
|
||||
m.commandIndex = min(m.commandIndex+1, max(0, len(items)-1))
|
||||
@@ -53,35 +81,235 @@ func (m Model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
if len(items) == 0 {
|
||||
return m, nil
|
||||
}
|
||||
selected := items[min(m.commandIndex, len(items)-1)]
|
||||
m.paletteOpen = false
|
||||
m.searchOpen = false
|
||||
m.keys.palette = false
|
||||
return m.requestPipeline(selected.ID, 0)
|
||||
return m.openSearchResult(items[min(m.commandIndex, len(items)-1)])
|
||||
}
|
||||
var cmd tea.Cmd
|
||||
m.commandInput, cmd = m.commandInput.Update(msg)
|
||||
m.commandIndex = 0
|
||||
return m, cmd
|
||||
updated, send := m.requestSearch()
|
||||
return updated, tea.Batch(cmd, send)
|
||||
}
|
||||
|
||||
// openSearchResult navigates straight to a result: a pipeline/version opens its
|
||||
// detail, and a run/step/artifact/field match opens the run with that step and
|
||||
// section selected once the run arrives.
|
||||
func (m Model) openSearchResult(result protocol.SearchResult) (tea.Model, tea.Cmd) {
|
||||
m.closeSearch()
|
||||
switch result.Kind {
|
||||
case protocol.SearchKindPipeline:
|
||||
if result.Pipeline != nil {
|
||||
return m.requestPipeline(result.Pipeline.ID, result.Pipeline.Version)
|
||||
}
|
||||
case protocol.SearchKindVersion:
|
||||
if result.Version != nil {
|
||||
return m.requestPipeline(result.Version.PipelineID, result.Version.Version)
|
||||
}
|
||||
}
|
||||
runID, sequence, field, artifact := searchResultTarget(result)
|
||||
if runID <= 0 {
|
||||
return m, nil
|
||||
}
|
||||
m.closeRuns()
|
||||
m.runOpen = true
|
||||
m.keys.runs = true
|
||||
m.runIndex = 0
|
||||
nav := &searchNav{runID: runID, sequence: sequence, section: runSectionNone}
|
||||
switch result.Kind {
|
||||
case protocol.SearchKindStep:
|
||||
nav.section = runSectionProgress
|
||||
case protocol.SearchKindField:
|
||||
nav.section = sectionForField(field)
|
||||
case protocol.SearchKindArtifact:
|
||||
// Artifacts have their own grouped view; open it with the matched entry
|
||||
// selected so the operator can open or copy its URL.
|
||||
nav.section = runSectionNone
|
||||
nav.artifact = artifact
|
||||
}
|
||||
m.pendingRunNav = nav
|
||||
return m.runRequest(protocol.Message{Type: protocol.GetRun, RunID: runID})
|
||||
}
|
||||
|
||||
// searchResultTarget returns the run, step, field and artifact a result points
|
||||
// at, read from its typed payload rather than re-parsing the target token.
|
||||
func searchResultTarget(result protocol.SearchResult) (runID int64, sequence int, field, artifact string) {
|
||||
switch result.Kind {
|
||||
case protocol.SearchKindRun:
|
||||
if result.Run != nil {
|
||||
runID = result.Run.ID
|
||||
}
|
||||
case protocol.SearchKindStep:
|
||||
if result.Step != nil {
|
||||
runID, sequence = result.Step.RunID, result.Step.Sequence
|
||||
}
|
||||
case protocol.SearchKindField:
|
||||
if result.Field != nil {
|
||||
runID, sequence, field = result.Field.RunID, result.Field.Sequence, result.Field.Field
|
||||
}
|
||||
case protocol.SearchKindArtifact:
|
||||
if result.Artifact != nil {
|
||||
runID, sequence, artifact = result.Artifact.RunID, result.Artifact.Sequence, result.Artifact.Name
|
||||
}
|
||||
}
|
||||
return runID, sequence, field, artifact
|
||||
}
|
||||
|
||||
func sectionForField(field string) int {
|
||||
switch field {
|
||||
case "input":
|
||||
return runSectionInput
|
||||
case "output":
|
||||
return runSectionResult
|
||||
case "evidence", "transcript":
|
||||
return runSectionEvidence
|
||||
default:
|
||||
return runSectionDetails
|
||||
}
|
||||
}
|
||||
|
||||
// applyRunNav selects the matched step and scrolls to the matched section.
|
||||
func (m *Model) applyRunNav(nav searchNav) {
|
||||
if m.runDetail == nil {
|
||||
return
|
||||
}
|
||||
if nav.sequence > 0 {
|
||||
for i, step := range m.runDetail.Steps {
|
||||
if step.Sequence == nav.sequence {
|
||||
m.selectRunStep(i + 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if nav.section != runSectionNone {
|
||||
m.runGraphFocus = false
|
||||
m.runDetailFocus = true
|
||||
m.focusRunSection(nav.section)
|
||||
}
|
||||
if nav.section == runSectionNone && nav.artifact != "" {
|
||||
m.openArtifacts()
|
||||
m.selectArtifactByName(nav.artifact)
|
||||
}
|
||||
}
|
||||
|
||||
// focusRunSection opens the section and scrolls the run detail to its header,
|
||||
// reusing the same shortcut markers runSectionLines renders.
|
||||
func (m *Model) focusRunSection(section int) {
|
||||
if m.runDetail == nil || section < 0 || section >= runSectionCount {
|
||||
return
|
||||
}
|
||||
if m.runCollapsed == nil {
|
||||
m.runCollapsed = map[string]bool{}
|
||||
}
|
||||
m.runCollapsed[m.runSectionKey(section)] = false
|
||||
marker := "[" + runSections[section].Shortcut + "]"
|
||||
for i, line := range m.runSectionLines(m.runDetailWidth()) {
|
||||
if strings.HasPrefix(ansi.Strip(line), marker) {
|
||||
m.runTop = i
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// searchBadge is the short type label a result shows. A pipeline version folds
|
||||
// into the pipeline badge and a body-text field into the match badge, so the
|
||||
// row reads the way the operator thinks about it rather than the wire kind.
|
||||
func searchBadge(kind string) string {
|
||||
switch kind {
|
||||
case protocol.SearchKindVersion:
|
||||
return "pipeline"
|
||||
case protocol.SearchKindField:
|
||||
return "match"
|
||||
default:
|
||||
return kind
|
||||
}
|
||||
}
|
||||
|
||||
// markedSnippet brackets the first case-insensitive occurrence of query in a
|
||||
// field snippet so the matched part is distinguishable without relying on
|
||||
// colour, then clips the result to budget runes with the bracketed match kept
|
||||
// inside the window. It tracks the exact marker span it inserts, so a snippet
|
||||
// that itself contains '[' cannot make the window centre on the wrong bracket.
|
||||
// The server centres the snippet on the match, so the text is present
|
||||
// case-insensitively; the snippet is shown unchanged when it is not. The budget
|
||||
// excludes the row's two-rune indent, since the result is clipped before the
|
||||
// row is styled.
|
||||
func markedSnippet(snippet, query string, budget int) string {
|
||||
runes := []rune(snippet)
|
||||
needle := []rune(strings.TrimSpace(query))
|
||||
if len(needle) == 0 || len(runes) == 0 {
|
||||
return snippet
|
||||
}
|
||||
at := protocol.RuneIndexFold(runes, needle)
|
||||
if at < 0 {
|
||||
return snippet
|
||||
}
|
||||
marked := string(runes[:at]) + "[" + string(runes[at:at+len(needle)]) + "]" + string(runes[at+len(needle):])
|
||||
markedRunes := []rune(marked)
|
||||
if budget <= 0 || len(markedRunes) <= budget {
|
||||
return marked
|
||||
}
|
||||
// The inserted '[' sits at rune `at` and the ']' at at+len(needle)+1, so the
|
||||
// span to keep in view is [at, at+len(needle)+2).
|
||||
spanStart, spanEnd := at, at+len(needle)+2
|
||||
if spanEnd-spanStart >= budget {
|
||||
return string(markedRunes[:budget])
|
||||
}
|
||||
lo := max(0, spanStart-(budget-(spanEnd-spanStart))/2)
|
||||
hi := min(len(markedRunes), lo+budget)
|
||||
lo = max(0, hi-budget)
|
||||
out := string(markedRunes[lo:hi])
|
||||
if lo > 0 {
|
||||
out = "…" + out
|
||||
}
|
||||
if hi < len(markedRunes) {
|
||||
out += "…"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// searchRowLines renders one result. A body-text match keeps its (windowed)
|
||||
// snippet on its own indented line so the marker is visible at the popup width
|
||||
// instead of being clipped after a long badge and label.
|
||||
func searchRowLines(result protocol.SearchResult, query string, snippetBudget int) []string {
|
||||
head := "[" + searchBadge(result.Kind) + "] " + result.Label()
|
||||
if result.Kind == protocol.SearchKindField && result.Field != nil && result.Field.Snippet != "" {
|
||||
return []string{head, " " + markedSnippet(result.Field.Snippet, query, snippetBudget)}
|
||||
}
|
||||
return []string{head}
|
||||
}
|
||||
|
||||
func (m Model) searchView(height int) string {
|
||||
field := m.commandInput
|
||||
field.Width = max(1, min(54, m.width-10))
|
||||
lines := []string{"Search pipelines", field.View(), ""}
|
||||
items := m.searchMatches()
|
||||
if len(items) == 0 {
|
||||
lines = append(lines, "No matching pipelines")
|
||||
field.Width = max(1, min(64, m.width-10))
|
||||
inner := min(72, max(1, m.width-6))
|
||||
snippetBudget := max(8, inner-4)
|
||||
lines := []string{"Search", field.View(), ""}
|
||||
items := m.searchResults
|
||||
query := strings.TrimSpace(m.commandInput.Value())
|
||||
switch {
|
||||
case m.searchError != "":
|
||||
lines = append(lines, "Search failed: "+m.searchError)
|
||||
case len(items) == 0 && m.searchInFlight:
|
||||
lines = append(lines, "Searching…")
|
||||
case len(items) == 0 && query != "":
|
||||
lines = append(lines, "No matches for "+query)
|
||||
case len(items) == 0:
|
||||
lines = append(lines, "Type to search pipelines, runs, steps, text and artifacts.")
|
||||
}
|
||||
start, end := selectionWindow(len(items), m.commandIndex, max(1, height-5))
|
||||
// A field match renders two lines, so budget the window by rendered lines.
|
||||
start, end := selectionWindow(len(items), m.commandIndex, max(1, (height-4)/2))
|
||||
for i := start; i < end; i++ {
|
||||
prefix := " "
|
||||
if i == m.commandIndex {
|
||||
prefix = "› "
|
||||
}
|
||||
lines = append(lines, menuOption(fmt.Sprintf("%s%s · v%d · %d stages", prefix, items[i].Name, items[i].Version, items[i].Stages), i == m.commandIndex))
|
||||
for j, row := range searchRowLines(items[i], m.searchText, snippetBudget) {
|
||||
if j == 0 {
|
||||
row = prefix + row
|
||||
}
|
||||
lines = append(lines, menuOption(row, i == m.commandIndex))
|
||||
}
|
||||
}
|
||||
return popup(m.width, height, 64, lines)
|
||||
return popup(m.width, height, 72, lines)
|
||||
}
|
||||
|
||||
func (m Model) requestPipeline(id int64, version int) (tea.Model, tea.Cmd) {
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// TestLiveOmniSearchAgainstServer is an env-gated check against a running
|
||||
// server: it drives the TUI's own '/' search, renders the popup, and presses
|
||||
// Enter on a live result of each kind, reporting where navigation lands. It
|
||||
// skips unless PIPELINES_LIVE_SEARCH is set so CI never needs a server.
|
||||
func TestLiveOmniSearchAgainstServer(t *testing.T) {
|
||||
if os.Getenv("PIPELINES_LIVE_SEARCH") == "" {
|
||||
t.Skip("set PIPELINES_LIVE_SEARCH=1 to query the running server")
|
||||
}
|
||||
url := os.Getenv("PIPELINES_URL")
|
||||
if url == "" {
|
||||
url = "ws://127.0.0.1:8080/connect"
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
m := New(ctx, url)
|
||||
m.width, m.height = 120, 40
|
||||
connected := m.connect()().(connectedMsg)
|
||||
defer connected.conn.CloseNow()
|
||||
updated, _ := m.Update(connected)
|
||||
m = updated.(Model)
|
||||
|
||||
// Plain-text (never a type prefix) candidates per kind. The live store keeps
|
||||
// changing as runs land, so a fixed query can stop matching a kind; the test
|
||||
// uses the first candidate that still returns the kind it is checking.
|
||||
candidates := map[string][]string{
|
||||
protocol.SearchKindPipeline: {"demo", "pipeline", "test", "a"},
|
||||
protocol.SearchKindVersion: {"1", "demo", "review", "test"},
|
||||
protocol.SearchKindRun: {"succeeded", "failed", "running", "1"},
|
||||
protocol.SearchKindStep: {"implementer", "reviewer", "planner", "success"},
|
||||
protocol.SearchKindField: {"identity", "search", "error", "the"},
|
||||
protocol.SearchKindArtifact: {"identity", "png", "log", "txt"},
|
||||
}
|
||||
for _, kind := range protocol.SearchKinds {
|
||||
picked, query := -1, ""
|
||||
for _, candidate := range candidates[kind] {
|
||||
m = resetLiveSearch(m)
|
||||
m = liveType(m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'/'}})
|
||||
m = liveType(m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(candidate)})
|
||||
m = drainLiveSearch(t, m)
|
||||
query = candidate
|
||||
picked = -1
|
||||
for i, result := range m.searchResults {
|
||||
if result.Kind == kind {
|
||||
picked = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if picked >= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if picked < 0 {
|
||||
t.Errorf("%s: no candidate query returned a %s result", kind, kind)
|
||||
continue
|
||||
}
|
||||
m.commandIndex = picked
|
||||
view := ansi.Strip(m.searchView(20))
|
||||
t.Logf("%s: query %q rendered\n%s", kind, query, view)
|
||||
|
||||
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
m = updated.(Model)
|
||||
if where := liveNavigation(m); where == "" {
|
||||
t.Errorf("%s: Enter did not navigate (result %+v)", kind, m.searchResults[picked])
|
||||
} else {
|
||||
t.Logf("%s: Enter navigates to %s", kind, where)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// liveType delivers one key and runs any command it produced (a text-input
|
||||
// update or a search request written to the socket), mirroring the real
|
||||
// bubbletea loop.
|
||||
func liveType(m Model, msg tea.Msg) Model {
|
||||
updated, cmd := m.Update(msg)
|
||||
m = updated.(Model)
|
||||
if cmd == nil {
|
||||
return m
|
||||
}
|
||||
if batch, ok := cmd().(tea.BatchMsg); ok {
|
||||
for _, c := range batch {
|
||||
if c != nil {
|
||||
c()
|
||||
}
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// drainLiveSearch reads server frames until the outstanding search request is
|
||||
// answered, so a slow or interleaved reply is still observed.
|
||||
func drainLiveSearch(t *testing.T, m Model) Model {
|
||||
t.Helper()
|
||||
for i := 0; i < 100; i++ {
|
||||
if !m.searchInFlight {
|
||||
return m
|
||||
}
|
||||
updated, _ := m.Update(m.read()())
|
||||
m = updated.(Model)
|
||||
}
|
||||
t.Fatalf("timed out waiting for the search reply")
|
||||
return m
|
||||
}
|
||||
|
||||
// resetLiveSearch returns the model to the home screen with no overlay, ready
|
||||
// for the next '/' query.
|
||||
func resetLiveSearch(m Model) Model {
|
||||
m.closeSearch()
|
||||
m.closeRuns()
|
||||
m.keys.detail = false
|
||||
m.detail = nil
|
||||
m.detailLoading = false
|
||||
m.requestedPipelineID = 0
|
||||
m.requestedVersion = 0
|
||||
m.artifactsOpen = false
|
||||
return m
|
||||
}
|
||||
|
||||
// liveNavigation describes where the last Enter landed, read from the same
|
||||
// state the views use.
|
||||
func liveNavigation(m Model) string {
|
||||
switch {
|
||||
case m.detailLoading:
|
||||
return "pipeline detail #" + strconv.FormatInt(m.requestedPipelineID, 10)
|
||||
case m.runOpen && m.pendingRunNav != nil && m.pendingRunNav.artifact != "":
|
||||
return "run #" + strconv.FormatInt(m.pendingRunNav.runID, 10) + " artifacts (" + m.pendingRunNav.artifact + ")"
|
||||
case m.runOpen && m.pendingRunNav != nil && m.pendingRunNav.section >= 0:
|
||||
return "run #" + strconv.FormatInt(m.pendingRunNav.runID, 10) + " section " + runSections[m.pendingRunNav.section].Title
|
||||
case m.runOpen && m.pendingRunNav != nil:
|
||||
return "run #" + strconv.FormatInt(m.pendingRunNav.runID, 10) + " detail"
|
||||
case m.artifactsOpen:
|
||||
return "artifacts view"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
"github.com/coder/websocket"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// searchFixtureModel renders the omni popup over a fixed, mixed result set so
|
||||
// the badges, labels and marked snippet are asserted without a server.
|
||||
func searchFixtureModel() Model {
|
||||
m := New(context.Background(), "ws://localhost/connect")
|
||||
m.width, m.height = 100, 30
|
||||
m.conn = &websocket.Conn{}
|
||||
m.paletteOpen = true
|
||||
m.searchOpen = true
|
||||
m.keys.palette = true
|
||||
m.searchText = "needle"
|
||||
m.searchResults = []protocol.SearchResult{
|
||||
{Kind: protocol.SearchKindPipeline, Target: "pipeline:1", Pipeline: &protocol.SearchPipeline{ID: 1, Name: "demo", Version: 2, Stages: 3}},
|
||||
{Kind: protocol.SearchKindRun, Target: "run:7", Run: &protocol.SearchRun{ID: 7, PipelineName: "demo", Status: "succeeded"}},
|
||||
{Kind: protocol.SearchKindStep, Target: "run:7:1", Step: &protocol.SearchStep{RunID: 7, Sequence: 1, Stage: "reviewer"}},
|
||||
{Kind: protocol.SearchKindArtifact, Target: "/artifacts/7/1/shot.png", Artifact: &protocol.SearchArtifact{RunID: 7, Sequence: 1, Name: "shot.png", URL: "/artifacts/7/1/shot.png"}},
|
||||
{Kind: protocol.SearchKindField, Target: "run:7:1:output", Field: &protocol.SearchField{RunID: 7, Sequence: 1, Field: "output", Snippet: "…needle output…"}},
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func TestOmniSearchShowsBadgesAndMarkedSnippet(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
view := ansi.Strip(m.searchView(20))
|
||||
for _, badge := range []string{"[pipeline]", "[run]", "[step]", "[artifact]", "[match]"} {
|
||||
if !strings.Contains(view, badge) {
|
||||
t.Fatalf("missing badge %q in %q", badge, view)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(view, "[needle]") {
|
||||
t.Fatalf("field snippet match must be marked without colour: %q", view)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOmniSearchRealSnippetMarkerVisible guards the regression where a body
|
||||
// match's snippet centres the token ~60 runes in, so a single-line
|
||||
// "[match] label · snippet" row was clipped and the marker was invisible at
|
||||
// every realistic width. The snippet is rendered on its own windowed line.
|
||||
func TestOmniSearchRealSnippetMarkerVisible(t *testing.T) {
|
||||
body := strings.Repeat("backgroundcontext ", 6) + "needle output"
|
||||
for _, width := range []int{80, 100} {
|
||||
m := searchFixtureModel()
|
||||
m.width = width
|
||||
m.searchResults[4].Field.Snippet = body
|
||||
view := ansi.Strip(m.View())
|
||||
if !strings.Contains(view, "[needle]") {
|
||||
t.Fatalf("width %d: matched span not visible after ansi.Strip: %q", width, view)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestOmniSearchSnippetWithOwnBracket guards the regression where the window
|
||||
// located the marker by the first '[' in the marked snippet. A snippet that
|
||||
// itself contains a '[' before the match (common in code/log evidence) centred
|
||||
// the window on the wrong bracket and clipped the real "[match]" span, so the
|
||||
// match became indistinguishable without colour.
|
||||
func TestOmniSearchSnippetWithOwnBracket(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.width = 100
|
||||
m.searchResults[4].Field.Snippet = "[x] " + strings.Repeat("word ", 30) + "needle"
|
||||
view := ansi.Strip(m.View())
|
||||
if !strings.Contains(view, "[needle]") {
|
||||
t.Fatalf("real bracket pair must survive a snippet that contains '[': %q", view)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOmniSearchEmptyStates(t *testing.T) {
|
||||
t.Run("empty query", func(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = nil
|
||||
if view := ansi.Strip(m.searchView(20)); !strings.Contains(view, "Type to search") {
|
||||
t.Fatalf("empty query must prompt: %q", view)
|
||||
}
|
||||
})
|
||||
t.Run("no matches", func(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = nil
|
||||
m.commandInput.SetValue("zzz")
|
||||
view := ansi.Strip(m.searchView(20))
|
||||
if !strings.Contains(view, "No matches for zzz") || strings.Contains(view, "Type to search") {
|
||||
t.Fatalf("non-empty query with no results must say so: %q", view)
|
||||
}
|
||||
})
|
||||
t.Run("error", func(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = nil
|
||||
m.commandInput.SetValue("zzz")
|
||||
m.searchError = "boom"
|
||||
view := ansi.Strip(m.searchView(20))
|
||||
if !strings.Contains(view, "Search failed: boom") {
|
||||
t.Fatalf("failed search must be distinct from no matches: %q", view)
|
||||
}
|
||||
})
|
||||
t.Run("in flight", func(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = nil
|
||||
m.commandInput.SetValue("zz")
|
||||
m.searchInFlight = true
|
||||
if view := ansi.Strip(m.searchView(20)); !strings.Contains(view, "Searching…") {
|
||||
t.Fatalf("outstanding request must show progress: %q", view)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestOmniSearchEscCancels(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
|
||||
m = updated.(Model)
|
||||
if m.searchOpen || m.paletteOpen || m.searchResults != nil {
|
||||
t.Fatal("Esc must cancel and clear the omni popup")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOmniSearchDropsStaleResponses(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = nil
|
||||
m.searchRequest = "2"
|
||||
updated, _ := m.Update(receivedMsg{Type: protocol.SearchResults, ID: "1", SearchResults: &protocol.SearchResponse{Results: []protocol.SearchResult{{Kind: protocol.SearchKindRun}}}})
|
||||
m = updated.(Model)
|
||||
if len(m.searchResults) != 0 {
|
||||
t.Fatal("stale search response replaced current results")
|
||||
}
|
||||
updated, _ = m.Update(receivedMsg{Type: protocol.SearchResults, ID: "2", SearchResults: &protocol.SearchResponse{Query: "demo", Results: []protocol.SearchResult{{Kind: protocol.SearchKindRun}}}})
|
||||
m = updated.(Model)
|
||||
if len(m.searchResults) != 1 || m.searchText != "demo" {
|
||||
t.Fatal("matching search response was dropped or its query not retained")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOmniSearchEnterNavigatesByKind(t *testing.T) {
|
||||
detail := &protocol.RunDetail{
|
||||
RunSummary: protocol.RunSummary{ID: 7, Name: "demo", Status: "succeeded"},
|
||||
Steps: []protocol.RunStep{{Sequence: 1, Stage: "one"}, {Sequence: 2, Stage: "two"}},
|
||||
}
|
||||
cases := []struct {
|
||||
result protocol.SearchResult
|
||||
section int
|
||||
}{
|
||||
{protocol.SearchResult{Kind: protocol.SearchKindRun, Run: &protocol.SearchRun{ID: 7}}, -1},
|
||||
{protocol.SearchResult{Kind: protocol.SearchKindStep, Step: &protocol.SearchStep{RunID: 7, Sequence: 2}}, 0},
|
||||
{protocol.SearchResult{Kind: protocol.SearchKindField, Field: &protocol.SearchField{RunID: 7, Sequence: 2, Field: "input"}}, 1},
|
||||
{protocol.SearchResult{Kind: protocol.SearchKindArtifact, Artifact: &protocol.SearchArtifact{RunID: 7, Sequence: 2, Name: "shot.png"}}, -2},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = []protocol.SearchResult{tc.result}
|
||||
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
m = updated.(Model)
|
||||
if !m.runOpen || m.searchOpen || m.pendingRunNav == nil {
|
||||
t.Fatalf("%s: Enter did not open the run: %+v", tc.result.Kind, m)
|
||||
}
|
||||
updated, _ = m.Update(receivedMsg{Type: protocol.Run, ID: "run", RunDetail: detail})
|
||||
m = updated.(Model)
|
||||
if sequence := searchResultSequence(tc.result); sequence > 0 && m.runStepIndex != 2 {
|
||||
t.Fatalf("%s: matched step not selected: %d", tc.result.Kind, m.runStepIndex)
|
||||
}
|
||||
if tc.result.Kind == protocol.SearchKindArtifact {
|
||||
if !m.artifactsOpen {
|
||||
t.Fatalf("artifact match must open the artifacts view: %+v", m)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if tc.section >= 0 {
|
||||
marker := "[" + runSections[tc.section].Shortcut + "]"
|
||||
lines := m.runSectionLines(m.runDetailWidth())
|
||||
if m.runTop >= len(lines) || !strings.HasPrefix(ansi.Strip(lines[m.runTop]), marker) {
|
||||
t.Fatalf("%s: section %d not focused: top=%d", tc.result.Kind, tc.section, m.runTop)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func searchResultSequence(result protocol.SearchResult) int {
|
||||
if result.Step != nil {
|
||||
return result.Step.Sequence
|
||||
}
|
||||
if result.Field != nil {
|
||||
return result.Field.Sequence
|
||||
}
|
||||
if result.Artifact != nil {
|
||||
return result.Artifact.Sequence
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func TestOmniSearchPipelineEnterOpensDetail(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
m.searchResults = []protocol.SearchResult{{Kind: protocol.SearchKindPipeline, Pipeline: &protocol.SearchPipeline{ID: 3, Version: 4}}}
|
||||
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
m = updated.(Model)
|
||||
if m.searchOpen || !m.detailLoading || m.requestedPipelineID != 3 || m.requestedVersion != 4 {
|
||||
t.Fatalf("pipeline Enter did not request the detail: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOmniSearchCoalescesInFlightRequests(t *testing.T) {
|
||||
m := searchFixtureModel()
|
||||
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}})
|
||||
m = updated.(Model)
|
||||
if !m.searchInFlight {
|
||||
t.Fatal("first keystroke must start a search")
|
||||
}
|
||||
before := m.nextID
|
||||
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'e'}})
|
||||
m = updated.(Model)
|
||||
if m.searchInFlight == false || !m.searchQueued || m.nextID != before {
|
||||
t.Fatalf("second keystroke must coalesce instead of overlapping: inFlight=%t queued=%t", m.searchInFlight, m.searchQueued)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOmniSearchLayoutStaysWithinWidth(t *testing.T) {
|
||||
for _, width := range []int{100, 40, 12} {
|
||||
m := searchFixtureModel()
|
||||
m.width = width
|
||||
m.searchResults[4].Field.Snippet = strings.Repeat("wide ", 40) + "needle"
|
||||
lines := strings.Split(ansi.Strip(m.View()), "\n")
|
||||
for _, line := range lines {
|
||||
if len([]rune(line)) > width {
|
||||
t.Fatalf("width %d: line too wide: %q", width, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user