Files
pipelines/internal/tui/runs_test.go
T

238 lines
9.0 KiB
Go

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)
m.runInput.SetValue("internal/tui/panels.go")
updated, _ = m.updateRunForm(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if m.runForm != 3 || !strings.Contains(m.runFormView(20), "not rolled back") {
t.Fatal("missing explicit execution confirmation")
}
m.runInput.SetValue("no")
updated, _ = m.updateRunForm(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if m.runForm != 3 {
t.Fatal("unconfirmed run started")
}
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{"Summary", "Output", "ses_audit", "error → #failure", "missing evidence"} {
if !strings.Contains(text, want) {
t.Fatal("missing audit detail", want)
}
}
}
func runInspectionModel() Model {
m := New(context.Background(), "")
m.width = 100
m.height = 28
m.runOpen = true
m.keys.runs = 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 TestRunSidebarSectionsAndScrolling(t *testing.T) {
m := runInspectionModel()
m = runKey(m, "j")
if m.runStepIndex != 1 {
t.Fatal("sidebar did not select first attempt")
}
text := ansi.Strip(m.runsView(24))
for _, want := range []string{"Stages", "▸ 1.", "2.", "done", "running", "Details · implementer · step 1", "Summary", "Output", "First attempt"} {
if !strings.Contains(text, want) {
t.Fatal("missing section/sidebar", 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(1) {
t.Fatal("input section did not expand")
}
m = runKey(m, "tab")
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, "h")
m = runKey(m, "j")
if m.runStepIndex != 2 || !m.runSectionClosed(1) {
t.Fatal("attempt state not independent")
}
m = runKey(m, "k")
if m.runSectionClosed(1) {
t.Fatal("expansion not retained when revisiting attempt")
}
}
func TestRunPollingCannotReopenOrResetNavigation(t *testing.T) {
m := runInspectionModel()
m = runKey(m, "j")
m = runKey(m, "tab")
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, "Input") > strings.Index(text, "Output") {
t.Fatal("input must precede output")
}
if m.runSectionClosed(2) || !m.runSectionClosed(1) {
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")
}
}