diff --git a/internal/cmd/list.go b/internal/cmd/list.go index b9562fb..f265f77 100644 --- a/internal/cmd/list.go +++ b/internal/cmd/list.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/raksul/go-clickup/clickup" "github.com/spf13/cobra" "github.com/timimsms/cu/internal/api" "github.com/timimsms/cu/internal/cache" @@ -16,6 +17,36 @@ var ( isProjectFlag bool ) +// listRow is the table view of a clickup.List. +type listRow struct { + ID string `json:"id" yaml:"id"` + Name string `json:"name" yaml:"name"` + Default string `json:"default" yaml:"default"` + Tasks int `json:"tasks" yaml:"tasks"` + Archived bool `json:"archived" yaml:"archived"` +} + +// listTableRows converts lists into table rows, marking the default list with "*". +func listTableRows(lists []clickup.List, defaultListID string) []listRow { + rows := make([]listRow, 0, len(lists)) + for _, l := range lists { + defaultMarker := "" + if l.ID == defaultListID { + defaultMarker = "*" + } + // task_count may be absent; leave Tasks at zero in that case + tasks, _ := l.TaskCount.Int64() + rows = append(rows, listRow{ + ID: l.ID, + Name: l.Name, + Default: defaultMarker, + Tasks: int(tasks), + Archived: l.Archived, + }) + } + return rows +} + var listCmd = &cobra.Command{ Use: "list", Short: "Manage lists", @@ -54,7 +85,7 @@ var listListCmd = &cobra.Command{ os.Exit(1) } - var allLists []interface{} + var allLists []clickup.List if folderID != "" { // Get lists from folder @@ -109,39 +140,7 @@ var listListCmd = &cobra.Command{ format := cmd.Flag("output").Value.String() if format == "table" { - // Prepare table data - type listRow struct { - ID string `json:"id"` - Name string `json:"name"` - Default string `json:"default"` - Tasks int `json:"tasks"` - Archived bool `json:"archived"` - } - - var rows []listRow - for _, item := range allLists { - // Type assertion based on the actual type - switch v := item.(type) { - case map[string]interface{}: - id, _ := v["id"].(string) - name, _ := v["name"].(string) - archived, _ := v["archived"].(bool) - - defaultMarker := "" - if id == defaultListID { - defaultMarker = "*" - } - - row := listRow{ - ID: id, - Name: name, - Default: defaultMarker, - Archived: archived, - } - rows = append(rows, row) - } - } - + rows := listTableRows(allLists, defaultListID) if err := output.Format(format, rows); err != nil { fmt.Fprintf(os.Stderr, "Failed to format output: %v\n", err) os.Exit(1) diff --git a/internal/cmd/list_test.go b/internal/cmd/list_test.go index 8fe55db..b7ce7ba 100644 --- a/internal/cmd/list_test.go +++ b/internal/cmd/list_test.go @@ -1,9 +1,15 @@ package cmd import ( + "bytes" + "encoding/json" + "strings" "testing" + "github.com/raksul/go-clickup/clickup" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/timimsms/cu/internal/output" ) func TestListCommands_Structure(t *testing.T) { @@ -37,3 +43,29 @@ func TestListCommands_Structure(t *testing.T) { assert.NotNil(t, cmd.Run) }) } + +// Regression test for #24: table output must contain a row per list. +func TestListTableRows(t *testing.T) { + lists := []clickup.List{ + {ID: "901", Name: "Backlog", TaskCount: json.Number("12")}, + {ID: "902", Name: "Sprint", Archived: true}, + } + + rows := listTableRows(lists, "902") + require.Len(t, rows, 2) + assert.Equal(t, listRow{ID: "901", Name: "Backlog", Tasks: 12}, rows[0]) + assert.Equal(t, listRow{ID: "902", Name: "Sprint", Default: "*", Archived: true}, rows[1]) + + var buf bytes.Buffer + require.NoError(t, (&output.TableFormatter{Writer: &buf}).Format(rows)) + + lines := strings.Split(strings.TrimSpace(buf.String()), "\n") + require.Len(t, lines, 4, "header, separator and one line per list") + assert.Equal(t, []string{"id", "name", "default", "tasks", "archived"}, strings.Fields(lines[0])) + assert.Equal(t, []string{"901", "Backlog", "12", "false"}, strings.Fields(lines[2])) + assert.Equal(t, []string{"902", "Sprint", "*", "0", "true"}, strings.Fields(lines[3])) +} + +func TestListTableRows_Empty(t *testing.T) { + assert.Empty(t, listTableRows(nil, "")) +}