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