Add limit flag to agent-task list command

Introduces a --limit flag to control the maximum number of agent tasks fetched. Validates that the limit is greater than zero and updates tests to cover custom and invalid limit scenarios.
This commit is contained in:
Kynan Ware 2025-08-29 17:52:27 -06:00
parent e750e71288
commit a49994defa
2 changed files with 44 additions and 7 deletions

View file

@ -44,6 +44,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
if f != nil {
opts.BaseRepo = f.BaseRepo
}
if opts.Limit < 1 {
return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit)
}
if runF != nil {
return runF(opts)
}
@ -55,6 +58,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
cmdutil.EnableRepoOverride(cmd, f)
}
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of agent tasks to fetch")
opts.CapiClient = func() (*capi.CAPIClient, error) {
cfg, err := opts.Config()
if err != nil {

View file

@ -3,6 +3,7 @@ package list
import (
"errors"
"net/http"
"strings"
"testing"
"time"
@ -23,6 +24,7 @@ func TestNewCmdList(t *testing.T) {
name string
args string
wantOpts ListOptions
wantErr string
}{
{
name: "no arguments",
@ -30,18 +32,35 @@ func TestNewCmdList(t *testing.T) {
Limit: defaultLimit,
},
},
{
name: "custom limit",
args: "--limit 15",
wantOpts: ListOptions{
Limit: 15,
},
},
{
name: "invalid limit",
args: "--limit 0",
wantErr: "invalid limit: 0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &cmdutil.Factory{}
var gotOpts *ListOptions
cmd := NewCmdList(f, func(opts *ListOptions) error {
gotOpts = opts
return nil
})
cmd.ExecuteC()
cmd := NewCmdList(f, func(opts *ListOptions) error { gotOpts = opts; return nil })
if tt.args != "" {
cmd.SetArgs(strings.Split(tt.args, " "))
}
_, err := cmd.ExecuteC()
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.wantOpts.Limit, gotOpts.Limit)
})
}
@ -58,6 +77,7 @@ func Test_listRun(t *testing.T) {
stubs func(*httpmock.Registry)
baseRepo ghrepo.Interface
baseRepoErr error
limit int
wantOut string
wantErr error
}{
@ -67,6 +87,18 @@ func Test_listRun(t *testing.T) {
stubs: func(reg *httpmock.Registry) { registerEmptySessionsMock(reg) },
wantOut: "no agent tasks found\n",
},
{
name: "limit truncates sessions",
tty: true,
limit: 3,
stubs: func(reg *httpmock.Registry) { registerManySessionsMock(reg, createdAt) },
wantOut: heredoc.Doc(`
SESSION ID PULL REQUEST REPO SESSION STATE CREATED
s1 #101 OWNER/REPO completed about 6 hours ago
s2 #102 OWNER/REPO failed about 6 hours ago
s3 #103 OWNER/REPO in_progress about 6 hours ago
`),
},
{
name: "single session (tty)",
tty: true,
@ -155,7 +187,7 @@ func Test_listRun(t *testing.T) {
opts := &ListOptions{
IO: ios,
Config: func() (gh.Config, error) { return cfg, nil },
Limit: 30,
Limit: tt.limit,
CapiClient: func() (*capi.CAPIClient, error) { return capiClient, nil },
}
if tt.baseRepo != nil || tt.baseRepoErr != nil {