package tui import ( "context" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" ) func decisionModel() Model { m := New(context.Background(), "") m.width = 100 m.height = 30 m.runOpen = true m.keys.runs = true m.requestedRunID = 8 m.runDetail = &protocol.RunDetail{ RunSummary: protocol.RunSummary{ID: 8, Name: "release", Version: 1, Status: protocol.RunStatusWaiting, CurrentStage: "gate"}, Steps: []protocol.RunStep{{ Sequence: 1, Stage: "gate", Status: protocol.StepStatusWaiting, Output: protocol.NewWaitingEvidence("Approve the release?", []string{"go", "halt"}), }}, } return m } func TestWaitingRunRendersPromptAndOptions(t *testing.T) { m := decisionModel() text := ansi.Strip(strings.Join(m.runSectionLines(m.runDetailWidth()), "\n")) for _, want := range []string{"Waiting for a person · stage gate", "Approve the release?", "Options: go, halt"} { if !strings.Contains(text, want) { t.Fatalf("waiting run view missing %q:\n%s", want, text) } } detail := ansi.Strip(m.runsDetailsView(24)) if !strings.Contains(detail, "Waiting for a person") { t.Fatalf("run detail omitted the waiting banner:\n%s", detail) } } func TestDecisionOverlaySelectsAndEmits(t *testing.T) { m := decisionModel() updated, _ := m.openDecision() m = updated.(Model) if !m.decisionOpen || m.decisionStage != "gate" || strings.Join(m.decisionOptions, ",") != "go,halt" { t.Fatalf("overlay did not open with the recorded options: %+v", m.decisionOptions) } if view := ansi.Strip(m.decisionView(24)); !strings.Contains(view, "Decide gate") || !strings.Contains(view, "halt") { t.Fatalf("overlay view missing the choice:\n%s", view) } updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyDown}) m = updated.(Model) if m.decisionIndex != 1 { t.Fatalf("down did not move the selection: %d", m.decisionIndex) } updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyTab}) m = updated.(Model) if !m.decisionFeedbackFocus { t.Fatal("Tab did not focus the feedback field") } updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyEsc}) m = updated.(Model) if m.decisionOpen { t.Fatal("Esc did not close the overlay") } // Enter emits a DecideRun request; with no connection the send path records // the attempt and the overlay closes. updated, _ = m.openDecision() m = updated.(Model) m.decisionFeedback.SetValue("looks good") before := len(m.lines) updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if m.decisionOpen { t.Fatal("Enter did not close the overlay") } if len(m.lines) == before { t.Fatal("Enter did not attempt to send the decision") } if !strings.Contains(strings.Join(m.lines, "\n"), "not connected") { t.Fatalf("decision path did not run: %v", m.lines) } } // TestDecisionOverlayKeepsSelectionVisibleAtNarrowViewport proves the overlay // windows its option list like the palette: at the smallest reviewed viewport // the selected option stays visible with its marker and a count says more exist. func TestDecisionOverlayKeepsSelectionVisibleAtNarrowViewport(t *testing.T) { m := decisionModel() m.width = 40 m.height = 16 m.runDetail.Steps[0].Output = protocol.NewWaitingEvidence("Approve the release?", []string{"abort", "deploy", "revise"}) updated, _ := m.openDecision() m = updated.(Model) // Move onto the option a fixed, unwindowed list would clip at this viewport. updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyDown}) m = updated.(Model) updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyDown}) m = updated.(Model) view := ansi.Strip(m.View()) if !strings.Contains(view, "› revise") { t.Fatalf("selected option is not visible with a marker at 40x16:\n%s", view) } if !strings.Contains(view, "2/3") { t.Fatalf("option count affordance missing at 40x16:\n%s", view) } if strings.Contains(view, "Enter d…") { t.Fatalf("controls are still truncated at 40x16:\n%s", view) } } // TestDecidedHumanStepRendersNeutralFeedback locks the fix for the review // finding: a completed decision shows its feedback as a normal output field, // never as the error-style "Stopped:" line reserved for real stops. func TestDecidedHumanStepRendersNeutralFeedback(t *testing.T) { m := decisionModel() m.runDetail.Status = "succeeded" output := map[string]any{"option": "go", "feedback": "looks good"} m.runDetail.Steps[0] = protocol.RunStep{ Sequence: 1, Stage: "gate", Status: "completed", Outcome: "go", Target: "worker", Output: output, Verdict: protocol.NewStageVerdict("go", "worker", output, nil, "", ""), } m.runStepIndex = 1 view := ansi.Strip(strings.Join(m.runSectionLines(m.runDetailWidth()), "\n")) if strings.Contains(view, "Stopped:") { t.Fatalf("a deliberate decision must not render as a stop:\n%s", view) } if !strings.Contains(view, "looks good") { t.Fatalf("decision feedback is not visible:\n%s", view) } }