diff --git a/pkg/cmd/agent-task/view/view.go b/pkg/cmd/agent-task/view/view.go index b751201db..2ff09386a 100644 --- a/pkg/cmd/agent-task/view/view.go +++ b/pkg/cmd/agent-task/view/view.go @@ -307,20 +307,15 @@ func printSession(opts *ViewOptions, session *capi.Session) { fmt.Fprintf(opts.IO.Out, "Started %s\n", text.FuzzyAgo(time.Now(), session.CreatedAt)) } - additionalNotes := make([]string, 0, 2) - - if session.PremiumRequests > 0 { - s := strings.TrimSuffix(fmt.Sprintf("%.1f", session.PremiumRequests), ".0") - additionalNotes = append(additionalNotes, fmt.Sprintf("Used %s premium request(s)", s)) - } + usedPremiumRequests := strings.TrimSuffix(fmt.Sprintf("%.1f", session.PremiumRequests), ".0") + usedPremiumRequestsNote := fmt.Sprintf("Used %s premium request(s)", usedPremiumRequests) + var durationNote string if session.CompletedAt.After(session.CreatedAt) { - additionalNotes = append(additionalNotes, fmt.Sprintf("Duration %s", session.CompletedAt.Sub(session.CreatedAt).Round(time.Second).String())) + durationNote = fmt.Sprintf(" • Duration %s", session.CompletedAt.Sub(session.CreatedAt).Round(time.Second).String()) } - if len(additionalNotes) > 0 { - fmt.Fprintf(opts.IO.Out, "%s\n", cs.Muted(strings.Join(additionalNotes, " • "))) - } + fmt.Fprintf(opts.IO.Out, "%s%s\n", cs.Muted(usedPremiumRequestsNote), cs.Muted(durationNote)) if !opts.Log { fmt.Fprintln(opts.IO.Out, "") diff --git a/pkg/cmd/agent-task/view/view_test.go b/pkg/cmd/agent-task/view/view_test.go index d39ac0a41..b8f872560 100644 --- a/pkg/cmd/agent-task/view/view_test.go +++ b/pkg/cmd/agent-task/view/view_test.go @@ -345,6 +345,89 @@ func Test_viewRun(t *testing.T) { gh agent-task view 'some-session-id' --log `), }, + { + name: "with session id, success, with zero premium requests (tty)", + tty: true, + opts: ViewOptions{ + SelectorArg: "some-session-id", + SessionID: "some-session-id", + }, + capiStubs: func(t *testing.T, m *capi.CapiClientMock) { + m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) { + assert.Equal(t, "some-session-id", id) + return &capi.Session{ + ID: "some-session-id", + State: "completed", + CreatedAt: sampleDate, + CompletedAt: sampleCompletedAt, + PremiumRequests: 0, + PullRequest: &api.PullRequest{ + Title: "fix something", + Number: 101, + URL: "https://github.com/OWNER/REPO/pull/101", + Repository: &api.PRRepository{ + NameWithOwner: "OWNER/REPO", + }, + }, + User: &api.GitHubUser{ + Login: "octocat", + }, + }, nil + } + }, + wantOut: heredoc.Doc(` + Ready for review • fix something • OWNER/REPO#101 + Started on behalf of octocat about 6 hours ago + Used 0 premium request(s) • Duration 5m0s + + For detailed session logs, try: + gh agent-task view 'some-session-id' --log + + View this session on GitHub: + https://github.com/OWNER/REPO/pull/101/agent-sessions/some-session-id + `), + }, + { + name: "with session id, success, duration not available (tty)", + tty: true, + opts: ViewOptions{ + SelectorArg: "some-session-id", + SessionID: "some-session-id", + }, + capiStubs: func(t *testing.T, m *capi.CapiClientMock) { + m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) { + assert.Equal(t, "some-session-id", id) + return &capi.Session{ + ID: "some-session-id", + State: "in_progress", + CreatedAt: sampleDate, + PremiumRequests: 1.5, + PullRequest: &api.PullRequest{ + Title: "fix something", + Number: 101, + URL: "https://github.com/OWNER/REPO/pull/101", + Repository: &api.PRRepository{ + NameWithOwner: "OWNER/REPO", + }, + }, + User: &api.GitHubUser{ + Login: "octocat", + }, + }, nil + } + }, + wantOut: heredoc.Doc(` + In progress • fix something • OWNER/REPO#101 + Started on behalf of octocat about 6 hours ago + Used 1.5 premium request(s) + + For detailed session logs, try: + gh agent-task view 'some-session-id' --log + + View this session on GitHub: + https://github.com/OWNER/REPO/pull/101/agent-sessions/some-session-id + `), + }, { name: "with session id, not found, web mode (tty)", tty: true,