Files
pipelines/internal/cli/wait_test.go
T
alan 2bde6b6344 CLI for agents: non-TTY safe run/confirmations, compact inspect, wait hints and exit codes
- Without a terminal, run behaves as --json, tui refuses and confirmations
  fail fast naming --yes instead of prompting.
- inspect prints a compact summary by default; --step N, --transcripts and
  --json give detail.
- wait names a failed run's stage and first diagnostic line, prints how to
  answer a run waiting on a person, and exits 2 when any run failed,
  was canceled or interrupted (1 stays for client errors).
- help describes the current # outcome / # handoff format.
- Task starter's Land stage drops edit to match format.md; skill docs
  document run --json --yes, retry --yes --json and the new behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 00:27:14 +00:00

175 lines
6.7 KiB
Go

package cli
import (
"bytes"
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// runMessage builds the GetRun envelope a real server returns for one run.
func runMessage(id int64, status, outcome, stage, created, finished string) protocol.Message {
return protocol.Message{Type: protocol.Run, RunDetail: &protocol.RunDetail{RunSummary: protocol.RunSummary{
ID: id,
Status: status,
Outcome: outcome,
CurrentStage: stage,
CreatedAt: created,
FinishedAt: finished,
}}}
}
// The report line carries the run id, status, outcome, stage and duration.
func TestWaitLineReportsStatusOutcomeStageAndDuration(t *testing.T) {
line := waitLine(runMessage(12, "succeeded", "success", "reviewer", "2026-10-09T00:00:00Z", "2026-10-09T00:01:02Z").RunDetail)
for _, want := range []string{"run 12", "succeeded", "success", "reviewer", "1m2s"} {
if !strings.Contains(line, want) {
t.Fatalf("wait line %q missing %q", line, want)
}
}
// A run with no recorded outcome or stage still renders a single line.
plain := waitLine(runMessage(13, "canceled", "", "", "2026-10-09T00:00:00Z", "2026-10-09T00:00:09Z").RunDetail)
if plain != "run 13 · canceled · 9s" {
t.Fatalf("sparse wait line = %q", plain)
}
// A missing start time omits the duration rather than inventing one.
if got := waitLine(runMessage(14, "failed", "error", "worker", "", "2026-10-09T00:00:09Z").RunDetail); got != "run 14 · failed (error) · worker" {
t.Fatalf("undated wait line = %q", got)
}
}
// Every named run is reported, and a run that finished unsuccessfully is still
// reported, but the wait exits with status 2 so a caller notices it.
func TestWaitReportsEachRunAndDoesNotFailOnUnsuccessfulRuns(t *testing.T) {
var out bytes.Buffer
app := App{Out: &out, Err: &out}
done := map[int64]protocol.Message{
2: runMessage(2, "succeeded", "success", "reviewer", "2026-10-09T00:00:00Z", "2026-10-09T00:01:02Z"),
3: runMessage(3, "failed", "error", "worker", "2026-10-09T00:00:00Z", "2026-10-09T00:00:09Z"),
}
call := func(request protocol.Message) (protocol.Message, error) { return done[request.RunID], nil }
var status *StatusError
if err := app.wait(context.Background(), []string{"2", "3"}, &options{}, call); !errors.As(err, &status) || status.Code != WaitRunsUnsuccessful || !strings.Contains(err.Error(), "run 3 failed") {
t.Fatalf("a finished failed run must exit %d: %v", WaitRunsUnsuccessful, err)
}
text := out.String()
for _, want := range []string{
"run 2 · succeeded (success) · reviewer · 1m2s",
"run 3 · failed (error) · worker · 9s",
} {
if !strings.Contains(text, want) {
t.Fatalf("wait output missing %q:\n%s", want, text)
}
}
}
// The wait returns as soon as the run goes terminal, using the same GetRun
// reattachment as run --wait.
func TestWaitReturnsWhenRunGoesTerminal(t *testing.T) {
var out bytes.Buffer
app := App{Out: &out, Err: &out}
reads := 0
call := func(protocol.Message) (protocol.Message, error) {
reads++
if reads <= 1 {
return runMessage(5, "running", "", "worker", "", ""), nil
}
return runMessage(5, "succeeded", "success", "worker", "2026-10-09T00:00:00Z", "2026-10-09T00:00:04Z"), nil
}
start := time.Now()
if err := app.wait(context.Background(), []string{"5"}, &options{}, call); err != nil {
t.Fatal(err)
}
if elapsed := time.Since(start); elapsed > 3*time.Second {
t.Fatalf("wait did not return promptly after terminal: %s", elapsed)
}
if !strings.Contains(out.String(), "run 5 · succeeded (success) · worker · 4s") {
t.Fatalf("wait output = %q", out.String())
}
if reads != 2 {
t.Fatalf("expected the initial read plus one poll, got %d", reads)
}
}
// An unknown run id is a client error and never blocks.
func TestWaitRefusesUnknownRunWithoutHanging(t *testing.T) {
app := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}}
call := func(protocol.Message) (protocol.Message, error) {
return protocol.Message{}, errors.New("run not found")
}
start := time.Now()
err := app.wait(context.Background(), []string{"999"}, &options{}, call)
if err == nil || !strings.Contains(err.Error(), "run not found") {
t.Fatalf("unknown run not refused: %v", err)
}
if elapsed := time.Since(start); elapsed > time.Second {
t.Fatalf("unknown run hung for %s", elapsed)
}
}
// A timeout says the run is still active and never cancels, re-reads or reports
// the run as its outcome.
func TestWaitTimeoutLeavesRunActiveAndSaysSo(t *testing.T) {
var out bytes.Buffer
app := App{Out: &out, Err: &out}
reads := 0
call := func(protocol.Message) (protocol.Message, error) {
reads++
return runMessage(7, "running", "", "worker", "", ""), nil
}
err := app.wait(context.Background(), []string{"7"}, &options{timeout: 50 * time.Millisecond}, call)
if err == nil {
t.Fatal("timeout not reported")
}
for _, want := range []string{"still active", "not canceled", "run 7"} {
if !strings.Contains(err.Error(), want) {
t.Fatalf("timeout message %q missing %q", err.Error(), want)
}
}
if reads != 1 {
t.Fatalf("timeout re-read the run %d times", reads)
}
if out.Len() != 0 {
t.Fatalf("timeout printed a report as if it were an outcome: %q", out.String())
}
}
// --json prints the final run envelope: one envelope for one id, an array for
// several.
func TestWaitJSONPrintsFinalRunEnvelope(t *testing.T) {
done := map[int64]protocol.Message{
2: runMessage(2, "succeeded", "success", "reviewer", "2026-10-09T00:00:00Z", "2026-10-09T00:00:01Z"),
3: runMessage(3, "failed", "error", "worker", "2026-10-09T00:00:00Z", "2026-10-09T00:00:02Z"),
}
call := func(request protocol.Message) (protocol.Message, error) { return done[request.RunID], nil }
var single bytes.Buffer
if err := (App{Out: &single, Err: &single}).wait(context.Background(), []string{"2"}, &options{json: true}, call); err != nil {
t.Fatal(err)
}
var envelope protocol.Message
if err := json.Unmarshal(single.Bytes(), &envelope); err != nil {
t.Fatalf("single-id --json is not one envelope: %v\n%s", err, single.String())
}
if envelope.Type != protocol.Run || envelope.RunDetail == nil || envelope.RunDetail.ID != 2 {
t.Fatalf("single-id --json envelope = %+v", envelope)
}
var many bytes.Buffer
if err := (App{Out: &many, Err: &many}).wait(context.Background(), []string{"2", "3"}, &options{json: true}, call); !errors.As(err, new(*StatusError)) {
t.Fatalf("a failed run in --json mode must still exit non-zero: %v", err)
}
var envelopes []protocol.Message
if err := json.Unmarshal(many.Bytes(), &envelopes); err != nil {
t.Fatalf("multi-id --json is not an array: %v\n%s", err, many.String())
}
if len(envelopes) != 2 || envelopes[0].RunDetail.ID != 2 || envelopes[1].RunDetail.ID != 3 {
t.Fatalf("multi-id --json envelopes = %+v", envelopes)
}
}