package tui import ( "context" "fmt" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" ) func TestRunFullGraphDimsUnexecutedBranchesAndRoutes(t *testing.T) { previous := lipgloss.ColorProfile() lipgloss.SetColorProfile(termenv.ANSI) defer lipgloss.SetColorProfile(previous) m := New(context.Background(), "") m.width = 120 graph := protocol.Graph{Stages: []protocol.Stage{ {Key: "parity", Edges: map[string]string{"odd": "add_one", "even": "halve", "error": protocol.FailureEnd}}, {Key: "add_one", Edges: map[string]string{"success": "report", "revise": "parity"}}, {Key: "halve", Edges: map[string]string{"success": "report"}}, {Key: "report", Edges: map[string]string{"success": protocol.SuccessEnd}}, }} m.runDetail = &protocol.RunDetail{Graph: graph, Steps: []protocol.RunStep{ {Sequence: 1, Stage: "parity", Outcome: "odd", Target: "add_one"}, {Sequence: 2, Stage: "add_one", Outcome: "revise", Target: "parity"}, {Sequence: 3, Stage: "parity", Outcome: "odd", Target: "add_one"}, {Sequence: 4, Stage: "add_one", Outcome: "success", Target: "report"}, {Sequence: 5, Stage: "report", Outcome: "success", Target: protocol.SuccessEnd}, }} // An unrelated/open latest definition must not replace this run's graph. m.detail = &protocol.PipelineDetail{Graph: protocol.Graph{Stages: []protocol.Stage{{Key: "latest"}}}} full, selected, appearance := m.runGraphState() if len(full.Stages) != 4 || selected != "report" || appearance.nodes["halve"] || !appearance.nodes["parity"] { t.Fatal("full graph or visitation state lost", full, selected, appearance) } rows := m.runGraphLines(116) view := strings.Join(rows, "\n") for _, key := range []string{"parity", "add_one", "halve", "report", "SUCCESS", "FAILURE"} { if !strings.Contains(ansi.Strip(view), key) { t.Fatal("missing graph node", key) } } if foregroundAt(t, view, "halve") != 8 || foregroundAt(t, view, "FAILURE") != 8 || foregroundAt(t, view, "SUCCESS") != 2 { t.Fatal("unexecuted nodes/terminals not dimmed", view) } for _, edge := range layoutGraph(full).edges { color := foregroundAt(t, rows[edge.from], fmt.Sprintf("%s [%d]", edge.label, edge.id)) taken := appearance.routes[graphRoute{edge.source, edge.outcome, edge.target}] if (color == 8) == taken { t.Fatal("route dimming does not match audited traversal", edge, color, taken) } } m.runStepIndex = 3 _, selected, _ = m.runGraphState() if selected != "parity" { t.Fatal("repeated visit selection lost", selected) } // Queued runs still display every node, all dimmed, before the first visit. m.runDetail.Steps = nil _, _, appearance = m.runGraphState() if len(appearance.nodes) != 0 || !strings.Contains(ansi.Strip(strings.Join(m.runGraphLines(116), "\n")), "halve") { t.Fatal("queued graph hidden or marked executed") } } func TestCompactRunGraphRetainsDimmedNodes(t *testing.T) { previous := lipgloss.ColorProfile() lipgloss.SetColorProfile(termenv.ANSI) defer lipgloss.SetColorProfile(previous) m := New(context.Background(), "") m.runDetail = &protocol.RunDetail{Steps: []protocol.RunStep{{Stage: "stage_000"}}} for i := 0; i < 201; i++ { m.runDetail.Graph.Stages = append(m.runDetail.Graph.Stages, protocol.Stage{Key: fmt.Sprintf("stage_%03d", i), Edges: map[string]string{"success": protocol.SuccessEnd}}) } view := strings.Join(m.runGraphLines(80), "\n") if foregroundAt(t, view, "stage_200") != 8 || foregroundAt(t, view, "stage_000") == 8 { t.Fatal("compact run graph lost dimming") } } 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 graph") < 0 || strings.Index(view, "Run graph") < strings.Index(view, "Run overview") || !strings.Contains(view, "3. plan (running)") { t.Fatal("run graph missing below results 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", "recorded visits"} { 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(runSectionEvidence) || !m.runSectionClosed(runSectionProgress) { 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) } }