package cli import ( "bytes" "context" "os" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) func notInteractive() bool { return false } // Without a terminal a confirmation must fail at once, naming --yes, rather // than block on stdin that an agent's Bash tool never answers. func TestConfirmationFailsFastWithoutTerminal(t *testing.T) { var out, stderr bytes.Buffer a := App{Out: &out, Err: &stderr, In: strings.NewReader("y\n"), Interactive: notInteractive} err := a.confirmAction("Retry failed run #4?", false) if err == nil || !strings.Contains(err.Error(), "--yes") || !strings.Contains(err.Error(), "not a terminal") { t.Fatalf("non-interactive confirmation = %v", err) } if stderr.Len() != 0 { t.Fatalf("prompted without a terminal: %q", stderr.String()) } if err := a.confirmAction("Retry failed run #4?", true); err != nil { t.Fatalf("--yes refused: %v", err) } } // A real non-terminal file (a pipe, /dev/null) is detected without an override. func TestTerminalDetectionTreatsFilesAsNonInteractive(t *testing.T) { devnull, err := os.Open(os.DevNull) if err != nil { t.Fatal(err) } defer devnull.Close() a := App{In: devnull, Out: devnull} if a.stdinInteractive() || a.terminalUI() { t.Fatal("/dev/null treated as a terminal") } if (App{}).stdinInteractive() { t.Fatal("nil stdin treated as interactive") } } // `run NAME TASK` without a terminal takes the JSON path (no full-screen view) // and so fails fast on the missing confirmation instead of hanging. func TestRunWithoutTerminalUsesJSONPathAndNeedsYes(t *testing.T) { var out, stderr bytes.Buffer a := App{Out: &out, Err: &stderr, In: strings.NewReader(""), Interactive: notInteractive} o := &options{name: "demo", prompt: "do it", notifyMode: "auto", workspace: t.TempDir()} resolve := func(string) (protocol.PipelineSummary, error) { return protocol.PipelineSummary{ID: 1, Name: "demo", Version: 3}, nil } called := false call := func(protocol.Message) (protocol.Message, error) { called = true; return protocol.Message{}, nil } err := a.run(context.Background(), nil, o, call, resolve) if err == nil || !strings.Contains(err.Error(), "--yes") { t.Fatalf("run without terminal and --yes = %v", err) } if !o.json || called { t.Fatalf("run without terminal: json=%v called=%v", o.json, called) } } func sampleRun() *protocol.RunDetail { d := &protocol.RunDetail{} d.ID, d.Name, d.Version, d.Status, d.Outcome = 7, "task", 2, "failed", "error" d.CreatedAt, d.FinishedAt = "2026-01-01T00:00:00Z", "2026-01-01T00:02:00Z" d.Steps = []protocol.RunStep{ {Sequence: 1, Stage: "plan", Status: "succeeded", Outcome: "done", StartedAt: "2026-01-01T00:00:00Z", FinishedAt: "2026-01-01T00:00:30Z", Output: map[string]any{"markdown": "# handoff\nPlan: edit a.go\n\n# outcome\ndone"}, Transcript: strings.Repeat("plan transcript ", 2000)}, {Sequence: 2, Stage: "implement", Status: "failed", Diagnostic: "go test failed\nlong detail line", Transcript: "IMPLEMENT-TRANSCRIPT", StartedAt: "2026-01-01T00:00:30Z", FinishedAt: "2026-01-01T00:02:00Z"}, } return d } func TestInspectDefaultsToCompactSummary(t *testing.T) { var out bytes.Buffer if err := (App{Out: &out}).inspect(sampleRun(), &options{}); err != nil { t.Fatal(err) } text := out.String() for _, want := range []string{"run 7 · task v2 · failed (error) · 2m0s", "#1 plan · succeeded (done) · 30s", "#2 implement · failed · 1m30s · go test failed", "Plan: edit a.go", "--step N"} { if !strings.Contains(text, want) { t.Fatalf("summary missing %q:\n%s", want, text) } } if strings.Contains(text, "plan transcript") || strings.Contains(text, "long detail line") || len(text) > 2000 { t.Fatalf("summary is not compact (%d bytes):\n%s", len(text), text) } } func TestInspectStepAndTranscriptsAndJSON(t *testing.T) { var out bytes.Buffer if err := (App{Out: &out}).inspect(sampleRun(), &options{step: 2}); err != nil { t.Fatal(err) } if text := out.String(); !strings.Contains(text, "long detail line") || strings.Contains(text, "IMPLEMENT-TRANSCRIPT") { t.Fatalf("--step 2:\n%s", text) } out.Reset() if err := (App{Out: &out}).inspect(sampleRun(), &options{step: 2, transcripts: true}); err != nil || !strings.Contains(out.String(), "IMPLEMENT-TRANSCRIPT") { t.Fatalf("--step 2 --transcripts: %v\n%s", err, out.String()) } out.Reset() if err := (App{Out: &out}).inspect(sampleRun(), &options{transcripts: true}); err != nil || !strings.Contains(out.String(), "IMPLEMENT-TRANSCRIPT") { t.Fatalf("--transcripts: %v", err) } out.Reset() if err := (App{Out: &out}).inspect(sampleRun(), &options{json: true}); err != nil || !strings.Contains(out.String(), `"transcript": "IMPLEMENT-TRANSCRIPT"`) { t.Fatalf("--json must keep full detail: %v", err) } if err := (App{Out: &out}).inspect(sampleRun(), &options{step: 9}); err == nil { t.Fatal("unknown step accepted") } } func TestInspectStepListsCheckCommandsAndRepairs(t *testing.T) { d := &protocol.RunDetail{ID: 7, Steps: []protocol.RunStep{{Sequence: 1, Stage: "verify", Status: "completed", Outcome: "fail", Control: &protocol.ControlEvidence{OutcomeRepairs: 1}, Output: map[string]any{"markdown": "# outcome\n\nfail\n", "checks": []any{ map[string]any{"command": "go vet ./...", "status": "passed", "exit_code": float64(0), "duration_ms": float64(1200)}, map[string]any{"command": "go test ./...", "status": "failed", "exit_code": float64(1), "duration_ms": float64(51000)}, }}}}} var out bytes.Buffer if err := (App{Out: &out}).inspect(d, &options{step: 1}); err != nil { t.Fatal(err) } text := out.String() for _, want := range []string{"outcome repair follow-ups: 1", "passed exit 0 1.2s go vet ./...", "failed exit 1 51.0s go test ./..."} { if !strings.Contains(text, want) { t.Fatalf("missing %q:\n%s", want, text) } } } func TestWaitLineNamesFailingStageAndDiagnostic(t *testing.T) { line := waitLine(sampleRun()) if !strings.Contains(line, "failed at implement · go test failed") || strings.Contains(line, "long detail line") { t.Fatalf("wait line = %q", line) } } func TestWaitingHintTellsHowToDecide(t *testing.T) { d := &protocol.RunDetail{} d.ID, d.Status, d.CurrentStage = 12, protocol.RunStatusWaiting, "decide" d.Steps = []protocol.RunStep{{Sequence: 3, Stage: "decide", Status: protocol.RunStatusWaiting, Output: map[string]any{protocol.WaitingOptionsKey: []any{"approve", "revise"}, protocol.WaitingPromptKey: "Ship it?"}}} hint := waitingHint(d) for _, want := range []string{"waiting at decide", "pipelines decide 12 --option approve|revise", "Ship it?"} { if !strings.Contains(hint, want) { t.Fatalf("hint missing %q: %q", want, hint) } } d.ExportPendingSequence = 3 if waitingHint(d) != "" { t.Fatal("an export handoff is not a human decision") } }