summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-22 20:17:18 +0200
committerPaul Buetow <paul@buetow.org>2026-03-22 20:17:18 +0200
commitd4c02a0a1d320d38858e6ff0fb607237bcf42cdd (patch)
tree786e3a259a10ec07c89f786ee57bf8260fdf9842
parentf306a6b5981f93562f3eed2087ee9c53fb01520b (diff)
Implement 'ask list' subcommand: parse export JSON, sort by priority/urgency, format UUID table
-rw-r--r--internal/askcli/command_list.go51
-rw-r--r--internal/askcli/command_list_test.go77
-rw-r--r--internal/askcli/dispatch.go4
3 files changed, 131 insertions, 1 deletions
diff --git a/internal/askcli/command_list.go b/internal/askcli/command_list.go
new file mode 100644
index 0000000..ee45e6f
--- /dev/null
+++ b/internal/askcli/command_list.go
@@ -0,0 +1,51 @@
+package askcli
+
+import (
+ "bytes"
+ "context"
+ "io"
+ "sort"
+ "strings"
+)
+
+func (d Dispatcher) handleList(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
+ filterArgs := []string{"export"}
+ for _, arg := range args[1:] {
+ if strings.HasPrefix(arg, "limit:") || strings.HasPrefix(arg, "sort:") ||
+ strings.HasPrefix(arg, "+") || arg == "started" {
+ filterArgs = append(filterArgs, arg)
+ }
+ }
+ var outBuf bytes.Buffer
+ code, err := d.runner.Run(ctx, filterArgs, nil, &outBuf, stderr)
+ if code != 0 {
+ return code, err
+ }
+ tasks, err := ParseTaskExport(&outBuf)
+ if err != nil {
+ return 1, nil
+ }
+ sort.Slice(tasks, func(i, j int) bool {
+ pi := priorityOrder(tasks[i].Priority)
+ pj := priorityOrder(tasks[j].Priority)
+ if pi != pj {
+ return pi < pj
+ }
+ return tasks[i].Urgency > tasks[j].Urgency
+ })
+ io.WriteString(stdout, FormatTaskList(tasks))
+ return 0, nil
+}
+
+func priorityOrder(p string) int {
+ switch p {
+ case "H":
+ return 1
+ case "M":
+ return 2
+ case "L":
+ return 3
+ default:
+ return 4
+ }
+}
diff --git a/internal/askcli/command_list_test.go b/internal/askcli/command_list_test.go
new file mode 100644
index 0000000..745a402
--- /dev/null
+++ b/internal/askcli/command_list_test.go
@@ -0,0 +1,77 @@
+package askcli
+
+import (
+ "bytes"
+ "context"
+ "io"
+ "strings"
+ "testing"
+)
+
+func TestHandleList_Success(t *testing.T) {
+ jsonData := `[{"uuid":"uuid-1","description":"Task 1","status":"pending","priority":"H","tags":["cli"],"urgency":15.0,"depends":[]},{"uuid":"uuid-2","description":"Task 2","status":"completed","priority":"M","tags":["agent"],"urgency":10.0,"depends":[]}]`
+ d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
+ if args[0] == "export" {
+ io.WriteString(stdout, jsonData)
+ }
+ return 0, nil
+ }})
+ var stdout, stderr bytes.Buffer
+ code, _ := d.Dispatch(context.Background(), []string{"list"}, nil, &stdout, &stderr)
+ if code != 0 {
+ t.Fatalf("list code = %d, want 0", code)
+ }
+ output := stdout.String()
+ if !strings.Contains(output, "uuid-1") || !strings.Contains(output, "uuid-2") {
+ t.Fatalf("output missing UUIDs: %s", output)
+ }
+}
+
+func TestHandleList_SortedByPriority(t *testing.T) {
+ jsonData := `[{"uuid":"uuid-2","description":"Task 2","status":"pending","priority":"M","tags":[],"urgency":10.0,"depends":[]},{"uuid":"uuid-1","description":"Task 1","status":"pending","priority":"H","tags":[],"urgency":5.0,"depends":[]}]`
+ d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
+ if args[0] == "export" {
+ io.WriteString(stdout, jsonData)
+ }
+ return 0, nil
+ }})
+ var stdout bytes.Buffer
+ d.Dispatch(context.Background(), []string{"list"}, nil, &stdout, &bytes.Buffer{})
+ output := stdout.String()
+ lines := strings.Split(strings.TrimSpace(output), "\n")
+ taskLine1 := lines[2]
+ if !strings.Contains(taskLine1, "uuid-1") {
+ t.Fatalf("first task should be H priority (uuid-1), got: %s", taskLine1)
+ }
+}
+
+func TestHandleList_EmptyList(t *testing.T) {
+ d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
+ if args[0] == "export" {
+ io.WriteString(stdout, "[]")
+ }
+ return 0, nil
+ }})
+ var stdout, stderr bytes.Buffer
+ code, _ := d.Dispatch(context.Background(), []string{"list"}, nil, &stdout, &stderr)
+ if code != 0 {
+ t.Fatalf("list code = %d, want 0 for empty list", code)
+ }
+}
+
+func TestHandleList_PassesFilters(t *testing.T) {
+ var capturedArgs []string
+ d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
+ capturedArgs = args
+ io.WriteString(stdout, "[]")
+ return 0, nil
+ }})
+ var stdout, stderr bytes.Buffer
+ d.Dispatch(context.Background(), []string{"list", "+READY", "limit:5", "sort:priority-"}, nil, &stdout, &stderr)
+ if len(capturedArgs) < 2 {
+ t.Fatalf("expected export args, got %v", capturedArgs)
+ }
+ if capturedArgs[0] != "export" {
+ t.Fatalf("first arg should be export, got %s", capturedArgs[0])
+ }
+}
diff --git a/internal/askcli/dispatch.go b/internal/askcli/dispatch.go
index 399e346..35b9397 100644
--- a/internal/askcli/dispatch.go
+++ b/internal/askcli/dispatch.go
@@ -28,8 +28,10 @@ func (d Dispatcher) Dispatch(ctx context.Context, args []string, stdin io.Reader
}
subcommand := args[0]
switch subcommand {
- case "add", "list", "info", "export":
+ case "add", "info", "export":
return d.runner.Run(ctx, args, stdin, stdout, stderr)
+ case "list":
+ return d.handleList(ctx, args, stdout, stderr)
case "dep":
return d.handleDep(ctx, args, stdout, stderr)
case "urgency":