package tui import ( "context" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/x/ansi" ) func TestRunPathUsesSharedRendererAndRecordedRoutesWithRepeatedVisits(t *testing.T) { m := New(context.Background(), "") m.width = 120 m.height = 40 m.runOpen = true m.keys.runs = true m.requestedRunID = 1 m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 1, Status: "running", CreatedAt: time.Now().Add(-time.Minute).Format(time.RFC3339)}, Steps: []protocol.RunStep{ {Sequence: 1, Stage: "plan", Status: "completed", Outcome: "success", Target: "implement"}, {Sequence: 2, Stage: "implement", Status: "completed", Outcome: "revise", Target: "plan"}, {Sequence: 3, Stage: "plan", Status: "running", AgentActive: true}, }} graph := m.runPathGraph() if len(graph.Stages) != 3 || graph.Stages[0].Key == graph.Stages[2].Key || graph.Stages[0].Edges["success"] != graph.Stages[1].Key || graph.Stages[1].Edges["revise"] != graph.Stages[2].Key || len(graph.Stages[2].Edges) != 0 { t.Fatal("run graph lost actual traversal or invented an edge", graph) } if got, want := strings.Join(m.runGraphLines(116), "\n"), strings.Join(renderGraph(graph, graph.Stages[2].Key), "\n"); got != want { t.Fatal("run graph diverged from common renderer") } view := ansi.Strip(m.runsView(36)) if strings.Index(view, "Run path") < 0 || strings.Index(view, "Run path") > strings.Index(view, "Run overview") || !strings.Contains(view, "3. plan (running)") { t.Fatal("run graph missing above details or current visit not visible", view) } refreshed := *m.runDetail refreshed.Steps = append([]protocol.RunStep(nil), m.runDetail.Steps...) refreshed.Status = "succeeded" refreshed.FinishedAt = time.Now().Format(time.RFC3339) refreshed.Steps[2].Status = "completed" refreshed.Steps[2].AgentActive = false refreshed.Steps[2].Outcome = "success" refreshed.Steps[2].Target = protocol.SuccessEnd updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: &refreshed})) m = updated.(Model) graph = m.runPathGraph() if graph.Stages[2].Edges["success"] != protocol.SuccessEnd || strings.Contains(graph.Stages[2].Key, "running") { t.Fatal("poll response did not refresh run graph", graph) } m = runKey(m, "g") if !m.runGraphFocus { t.Fatal("graph cannot be focused") } m = runKey(m, "home") m = runKey(m, "l") if m.runGraphTop != 0 || m.runGraphPan == 0 { t.Fatal("graph scrolling/panning failed") } m = runKey(m, "g") if m.runGraphFocus { t.Fatal("cannot return to details") } for _, size := range [][2]int{{120, 36}, {70, 24}, {35, 18}, {10, 10}} { m.width = size[0] view := m.runsView(size[1]) if len(strings.Split(view, "\n")) > size[1] { t.Fatal("run layout exceeds height", size) } for _, line := range strings.Split(view, "\n") { if ansi.StringWidth(line) > size[0] { t.Fatal("run layout exceeds width", size, ansi.StringWidth(line)) } } } } func TestRetryHiddenUntilExecutionAndAgentCleanupHaveCompleted(t *testing.T) { m := New(context.Background(), "") m.width = 180 m.runOpen = true m.keys.runs = true m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 1, Status: "failed", FinishedAt: "2026-10-09T00:00:00Z", Executing: true}} if m.selectedFailedRun() != 0 || strings.Contains(m.runActions(), "retry") || strings.Contains(ansi.Strip(m.footerContent()), "retry") { t.Fatal("retry advertised while execution is active") } m.runDetail.Executing = false m.runDetail.Steps = []protocol.RunStep{{AgentActive: true}} if m.selectedFailedRun() != 0 { t.Fatal("retry available before agent cleanup") } m.runDetail.Steps[0].AgentActive = false if m.selectedFailedRun() != 1 || !strings.Contains(m.runActions(), "retry") { t.Fatal("fully stopped failed run lacks retry") } m.runDetail.FinishedAt = "" if m.selectedFailedRun() != 0 { t.Fatal("retry available before terminal timestamp") } } func TestRunningDurationAndOpenCodeEvidenceShortcut(t *testing.T) { duration, err := time.ParseDuration(runDuration(time.Now().Add(-65*time.Second).Format(time.RFC3339), "")) if err != nil || duration < 65*time.Second || duration > 67*time.Second { t.Fatal("elapsed running duration not shown", duration, err) } if got := runDuration("2026-10-09T00:00:00Z", "2026-10-09T00:01:02Z"); got != "1m2s" { t.Fatal("finished duration changed", got) } graphModel := New(context.Background(), "") graphModel.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{CreatedAt: "2026-10-09T00:00:00Z", FinishedAt: "2026-10-09T00:01:02Z"}} title := graphModel.runGraphTitle() for _, field := range []string{"duration 1m2s", "started ", "finished "} { if !strings.Contains(title, field) { t.Fatal("graph title omitted run timing", title) } } if got := dateLabel("2026-10-09T00:00:00+02:00"); got != "08 Oct 2026 22:00 UTC" { t.Fatal("start time mislabeled as UTC", got) } m := runInspectionModel() m.runDetail.Steps[1].AgentActive = true m = runKey(m, "a") if m.runStepIndex != 2 || !m.runDetailFocus || m.runSectionClosed(3) || !m.runSectionClosed(0) { t.Fatal("OpenCode evidence shortcut did not select active session") } view := ansi.Strip(strings.Join(m.runSectionLines(90), "\n")) if !strings.Contains(view, "ses_second") { t.Fatal("session not exposed", view) } }