package cli import ( "bytes" "context" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // The client --wait limit must not reuse the per-request --timeout, and reaching // it must leave admitted work active rather than canceling or replaying it. func TestWaitLimitIsIndependentOfRequestTimeout(t *testing.T) { app := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}} active := protocol.Message{RunDetail: &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 7, Status: "running"}}} o := &options{json: true, wait: true, timeout: time.Millisecond, waitTimeout: 50 * time.Millisecond} calls := 0 call := func(protocol.Message) (protocol.Message, error) { calls++ return active, nil } start := time.Now() err := app.finishRun(context.Background(), active, o, call) if err == nil || !strings.Contains(err.Error(), "remains active") { t.Fatalf("expected wait-limit message, got %v", err) } if elapsed := time.Since(start); elapsed > 2*time.Second { t.Fatalf("wait limit not honored: %s", elapsed) } if calls > 1 { t.Fatalf("wait limit replayed admitted work %d times", calls) } } // With no explicit wait limit, a per-request --timeout must not bound the wait. func TestRequestTimeoutDoesNotBoundWait(t *testing.T) { app := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}} active := protocol.Message{RunDetail: &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 9, Status: "running"}}} done := protocol.Message{RunDetail: &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 9, Status: "succeeded"}}} o := &options{json: true, wait: true, timeout: time.Millisecond} calls := 0 call := func(protocol.Message) (protocol.Message, error) { calls++ return done, nil } if err := app.finishRun(context.Background(), active, o, call); err != nil { t.Fatalf("wait lost terminal state: %v", err) } if calls != 1 { t.Fatalf("expected a single reattachment poll, got %d", calls) } } // The public help inventory must advertise the independent client wait limit and // the safe read-only reattachment advice for run and retry. func TestHelpDocumentsWaitTimeoutAndReattachment(t *testing.T) { for _, command := range []string{"run", "retry"} { if !strings.Contains(usage[command], "--wait-timeout") { t.Fatalf("%s usage does not document --wait-timeout: %q", command, usage[command]) } note := notes[command] if !strings.Contains(note, "--wait-timeout") { t.Fatalf("%s note does not document --wait-timeout", command) } if !strings.Contains(note, "--request-key") { t.Fatalf("%s note does not warn against a duplicate run keyed by --request-key", command) } } } // A negative client wait limit is a rejected input, not an unbounded wait, and is // caught before any server connection is attempted. func TestNegativeWaitTimeoutIsRejected(t *testing.T) { for _, args := range [][]string{ {"run", "--json", "--wait", "--wait-timeout", "-1s"}, {"retry", "5", "--json", "--wait", "--wait-timeout", "-1s"}, } { app := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}, In: strings.NewReader("")} err := app.Run(context.Background(), args) if err == nil || !strings.Contains(err.Error(), "wait-timeout nonnegative") { t.Fatalf("negative wait-timeout not rejected for %v: %v", args, err) } } } // Every documented command must build its flag set without a duplicate-flag panic, // and the filters the usage map advertises must actually be registered. func TestDocumentedCommandFlagsAreRegistered(t *testing.T) { app := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}} for command := range usage { command := command func() { defer func() { if recovered := recover(); recovered != nil { t.Fatalf("flags(%q) panicked: %v", command, recovered) } }() app.flags(command) }() } for _, check := range []struct{ command, flag string }{ {"run", "pipeline"}, {"run", "wait-timeout"}, {"runs", "pipeline"}, {"retry", "wait-timeout"}, } { if fs, _ := app.flags(check.command); fs.Lookup(check.flag) == nil { t.Fatalf("%s does not register --%s", check.command, check.flag) } } }