package tui import ( "context" "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "strings" "testing" ) func TestRunFormAndInspection(t *testing.T) { m := New(context.Background(), "") input := initialRunInput("the exact user's prompt") if len(input) != 1 || input["prompt"] != "the exact user's prompt" { t.Fatal("run form must send only user prompt, not invented input fields") } m.detail = &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{ID: 1, Name: "bug fix", Version: 4}} updated, _ := m.beginRun() m = updated.(Model) m.runPrompt.SetValue("align arrows") updated, _ = m.updateRunForm(tea.KeyMsg{Type: tea.KeyCtrlS}) m = updated.(Model) if m.runForm != 0 || !m.runOpen { t.Fatal("task submission should run without a file list or confirmation") } updated, _ = m.beginRun() m = updated.(Model) updated, _ = m.updateRunForm(tea.KeyMsg{Type: tea.KeyEsc}) m = updated.(Model) if m.runForm != 0 { t.Fatal("cancel failed") } m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 3, Name: "bug fix", Version: 4, Status: "failed"}, Steps: []protocol.RunStep{{Sequence: 1, Stage: "plan", Outcome: "error", Target: "#failure", SessionID: "ses_audit", Output: map[string]any{"reason": "missing evidence"}}}} m.runStepIndex = 1 m.toggleRunSection(4) text := strings.Join(m.runSectionLines(80), "\n") for _, want := range []string{"Stage progress", "Result", "ses_audit", "error → #failure", "missing evidence"} { if !strings.Contains(text, want) { t.Fatal("missing audit detail", want) } } } func TestRunTitleIsShownToOperator(t *testing.T) { titled := protocol.RunSummary{ID: 1, Name: "bug fix", Version: 2, Title: "Fix the flaky login test", Status: "succeeded"} if got := titled.Label(); got != "Fix the flaky login test" { t.Fatalf("Label = %q, want the generated title", got) } if got := (protocol.RunSummary{Name: "bug fix"}).Label(); got != "bug fix" { t.Fatalf("Label without a title = %q, want the pipeline name fallback", got) } m := New(context.Background(), "") m.width = 100 m.height = 28 m.runOpen = true m.runDetail = &protocol.RunDetail{RunSummary: titled} if view := ansi.Strip(m.runsDetailsView(24)); !strings.Contains(view, "Fix the flaky login test") { t.Fatalf("run detail header omitted the title:\n%s", view) } } func runInspectionModel() Model { m := New(context.Background(), "") m.width = 100 m.height = 28 m.runOpen = true m.keys.runs = true m.runDetailFocus = true m.requestedRunID = 8 m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 8, Name: "bug fix", Version: 4, Status: "running"}, Output: map[string]any{"summary": "Run summary"}, Steps: []protocol.RunStep{ {Sequence: 1, Stage: "implementer", Status: "completed", Outcome: "success", Target: "verifier", Input: map[string]any{"secret_test_marker": "hidden input"}, Output: map[string]any{"summary": "First attempt", "evidence": strings.Repeat("A long verification result with Unicode 宽. ", 100)}, SessionID: "ses_first"}, {Sequence: 2, Stage: "implementer", Status: "running", Output: map[string]any{"summary": "Second attempt"}, SessionID: "ses_second"}, }} return m } func runKey(m Model, key string) Model { keys := map[string]tea.KeyType{"esc": tea.KeyEsc, "tab": tea.KeyTab, "pgdown": tea.KeyPgDown, "pgup": tea.KeyPgUp, "home": tea.KeyHome, "end": tea.KeyEnd} msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key)} if kind, ok := keys[key]; ok { msg = tea.KeyMsg{Type: kind} } updated, _ := m.updateRuns(msg) return updated.(Model) } func TestRunVisitSelectorSectionsAndScrolling(t *testing.T) { m := runInspectionModel() m = runKey(m, "n") if m.runStepIndex != 1 { t.Fatal("visit selector did not select first attempt") } text := ansi.Strip(m.runsDetailsView(24)) for _, want := range []string{"1 implementer", "2 implementer", "done", "Details · implementer · step 1", "Result", "First attempt"} { if !strings.Contains(text, want) { t.Fatal("missing section/visit", want) } } if strings.Contains(text, "hidden input") || strings.Contains(text, "Second attempt") { t.Fatal("unselected or collapsed data leaked into details") } m = runKey(m, "i") if m.runSectionClosed(runSectionInput) { t.Fatal("input section did not expand") } m = runKey(m, "pgdown") if m.runTop != m.runPageSize() || !m.runDetailFocus { t.Fatal("page scrolling failed") } m = runKey(m, "end") end := max(0, len(m.runSectionLines(m.runDetailWidth()))-m.runPageSize()) if m.runTop != end { t.Fatal("End did not reach bottom") } m = runKey(m, "home") if m.runTop != 0 { t.Fatal("Home did not reach top") } m = runKey(m, "n") if m.runStepIndex != 2 || !m.runSectionClosed(runSectionInput) { t.Fatal("attempt state not independent") } m = runKey(m, "p") if m.runSectionClosed(runSectionInput) { t.Fatal("expansion not retained when revisiting attempt") } } func TestRunPollingCannotReopenOrResetNavigation(t *testing.T) { m := runInspectionModel() m = runKey(m, "n") m = runKey(m, "pgdown") top := m.runTop refreshed := *m.runDetail refreshed.Status = "succeeded" updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: &refreshed})) m = updated.(Model) if m.runTop != top || m.runStepIndex != 1 || !m.runDetailFocus { t.Fatal("poll changed view state") } m = runKey(m, "esc") updated, _ = m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: &refreshed})) m = updated.(Model) if m.runDetail != nil || m.requestedRunID != 0 || !m.runOpen { t.Fatal("late poll reopened run details after Esc") } m = runKey(m, "esc") updated, _ = m.Update(receivedMsg(protocol.Message{Type: protocol.RunStarted, ID: "old-start", RunDetail: &refreshed})) m = updated.(Model) if m.runOpen || m.keys.runs { t.Fatal("late start reopened closed view") } m = runInspectionModel() m.requestedRunID = 99 m.runDetail = nil updated, _ = m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: &refreshed})) m = updated.(Model) if m.runDetail != nil { t.Fatal("wrong run accepted while loading another") } m = runKey(m, "esc") if m.requestedRunID != 0 { t.Fatal("loading view cannot be dismissed") } } func TestRunViewResponsiveDimensions(t *testing.T) { for _, size := range []struct{ width, height int }{{160, 40}, {100, 24}, {64, 18}, {63, 18}, {36, 12}, {12, 7}, {5, 3}} { m := runInspectionModel() m.width = size.width m.height = size.height + 4 m.runStepIndex = 1 view := m.runsView(size.height) if lipgloss.Width(view) != size.width || lipgloss.Height(view) != size.height { t.Fatalf("run view %dx%d got %dx%d", size.width, size.height, lipgloss.Width(view), lipgloss.Height(view)) } } } func TestToolEvidenceReadableAndCollapsed(t *testing.T) { m := runInspectionModel() m.runStepIndex = 1 m.runDetail.Steps[0].Transcript = `{"data":[{"content":[{"type":"tool","name":"shell","state":{"status":"completed","input":{"command":"go test ./..."},"content":[{"text":"PASS pipelines/internal/tui"}]}}]}]}` if strings.Contains(strings.Join(m.runSectionLines(70), "\n"), "go test ./...") { t.Fatal("evidence should default collapsed") } m = runKey(m, "t") text := strings.Join(m.runSectionLines(70), "\n") for _, want := range []string{"shell · completed", "go test ./...", "PASS pipelines/internal/tui"} { if !strings.Contains(text, want) { t.Fatal("missing readable tool evidence", want) } } m.runDetail.Steps[0].Transcript = strings.TrimSuffix(m.runDetail.Steps[0].Transcript, "]}") + `,{"content":[` + "\n[preview truncated]" text = strings.Join(evidenceLines(m.runDetail.Steps[0]), "\n") if !strings.Contains(text, "preview is truncated") || !strings.Contains(text, "go test ./...") { t.Fatal("complete tool evidence lost in truncated audit preview") } } func TestRunOutputPrioritizesConclusions(t *testing.T) { text := ansi.Strip(strings.Join(valueLines(map[string]any{"acceptance_checks": "long supporting checks", "diagnosis": "root cause", "summary": "human summary", "repository": "context"}), "\n")) if !(strings.Index(text, "human summary") < strings.Index(text, "root cause") && strings.Index(text, "root cause") < strings.Index(text, "long supporting checks")) { t.Fatal("important output buried below handoff context", text) } } func TestRunSectionsOrderTitleAndNumberSelection(t *testing.T) { m := runInspectionModel() m = runKey(m, "1") text := ansi.Strip(strings.Join(m.runSectionLines(70), "\n")) if strings.Index(text, "Result") > strings.Index(text, "Task & input") { t.Fatal("output must precede input") } if m.runSectionClosed(runSectionResult) || !m.runSectionClosed(runSectionInput) { t.Fatal("output should remain visible; input initially collapsed") } view := ansi.Strip(m.runsView(24)) if strings.Count(view, "Details · implementer · step 1") != 1 { t.Fatal("selected stage must have one complete detail heading", view) } m = runKey(m, "2") if m.runStepIndex != 2 { t.Fatal("number did not select second attempt") } m = runKey(m, "0") if m.runStepIndex != 0 { t.Fatal("zero did not select overview") } } func TestPipelineRunShortcutFiltersAndReturns(t *testing.T) { m := panelModel(t) m.requestedPipelineID = 42 m.runs = []protocol.RunSummary{{ID: 1, PipelineID: 9}, {ID: 2, PipelineID: 42}} m = press(m, "r") if !m.runOpen || m.runPipelineID != 42 || len(m.visibleRuns()) != 1 || m.visibleRuns()[0].ID != 2 { t.Fatal("pipeline shortcut didn't show its runs") } m = runKey(m, "esc") if m.runOpen || !m.keys.detail { t.Fatal("closing runs didn't return to pipeline") } } // TestRunStepVerdictRenderedWhenPresent proves the rendered run detail states a // completed visit's chosen option and its reason and names its referenced // artifacts, that an absent reason still states the option, and that a step with a // nil verdict shows no verdict line at all. func TestRunStepVerdictRenderedWhenPresent(t *testing.T) { m := runInspectionModel() m.runDetail.Steps[0].Verdict = &protocol.StageVerdict{ Option: "success → verifier", Reason: "Tests passed and the evidence is retained", ReasonSource: protocol.VerdictReasonModel, Artifacts: []string{"captures/shot.png", "report.txt"}, } m.runStepIndex = 1 view := ansi.Strip(m.runsDetailsView(24)) for _, want := range []string{ "Verdict: success → verifier · Tests passed and the evidence is retained", "Referenced artifacts: captures/shot.png, report.txt", } { if !strings.Contains(view, want) { t.Fatalf("verdict detail missing %q:\n%s", want, view) } } // An absent reason still states the chosen option rather than dropping it. optionOnly := &protocol.StageVerdict{Option: "success → verifier", ReasonSource: protocol.VerdictReasonAbsent} if got := ansi.Strip(strings.Join(stageVerdictLines(optionOnly), "\n")); got != "Verdict: success → verifier" { t.Fatalf("option-only verdict = %q", got) } m.runDetail.Steps[0].Verdict = nil view = ansi.Strip(m.runsDetailsView(24)) if strings.Contains(view, "Verdict:") || strings.Contains(view, "Referenced artifacts:") { t.Fatalf("nil verdict must show no verdict line:\n%s", view) } }