Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions internal/cli/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,78 @@ func TestPrintWorkspaceDetailWithoutOptionalFields(t *testing.T) {
t.Errorf("expected no Secret field when secretRef is nil, got %q", output)
}
}

func TestPrintTaskResults(t *testing.T) {
tests := []struct {
name string
task *axonv1alpha1.Task
wantKeys []string
noKeys []string
}{
{
name: "shows pr and branch when present",
task: &axonv1alpha1.Task{
Status: axonv1alpha1.TaskStatus{
Results: map[string]string{
"branch": "axon-task-abc12",
"pr": "https://github.com/org/repo/pull/42",
"commit": "abc1234",
},
},
},
wantKeys: []string{"branch:", "pr:", "commit:"},
},
{
name: "shows cost and token usage when present",
task: &axonv1alpha1.Task{
Status: axonv1alpha1.TaskStatus{
Results: map[string]string{
"cost-usd": "1.23",
"input-tokens": "5000",
"output-tokens": "1200",
},
},
},
wantKeys: []string{"cost-usd:", "input-tokens:", "output-tokens:"},
},
{
name: "prints nothing when results are empty",
task: &axonv1alpha1.Task{
Status: axonv1alpha1.TaskStatus{},
},
noKeys: []string{"branch:", "pr:", "cost-usd:"},
},
{
name: "skips empty result values",
task: &axonv1alpha1.Task{
Status: axonv1alpha1.TaskStatus{
Results: map[string]string{
"branch": "",
"pr": "https://github.com/org/repo/pull/99",
},
},
},
wantKeys: []string{"pr:"},
noKeys: []string{"branch:"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
printTaskResults(&buf, tt.task)
output := buf.String()

for _, key := range tt.wantKeys {
if !strings.Contains(output, key) {
t.Errorf("expected %q in output, got %q", key, output)
}
}
for _, key := range tt.noKeys {
if strings.Contains(output, key) {
t.Errorf("expected %q NOT in output, got %q", key, output)
}
}
})
}
}
16 changes: 16 additions & 0 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"fmt"
"io"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -324,13 +325,28 @@ func watchTask(ctx context.Context, cl client.Client, name, namespace string) er
}

if task.Status.Phase == axonv1alpha1.TaskPhaseSucceeded || task.Status.Phase == axonv1alpha1.TaskPhaseFailed {
printTaskResults(os.Stdout, task)
return nil
}

time.Sleep(2 * time.Second)
}
}

// printTaskResults prints the key outputs (branch, PR, commit, cost) from a
// completed task. Only fields that are populated are shown.
func printTaskResults(w io.Writer, task *axonv1alpha1.Task) {
if len(task.Status.Results) == 0 {
return
}
keys := []string{"branch", "pr", "commit", "cost-usd", "input-tokens", "output-tokens"}
for _, k := range keys {
if v, ok := task.Status.Results[k]; ok && v != "" {
fmt.Fprintf(w, " %-20s%s\n", k+":", v)
}
}
}

// apiKeySecretKey returns the secret key name for API key credentials
// based on the agent type.
func apiKeySecretKey(agentType string) string {
Expand Down