|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/basecamp/basecamp-cli/internal/output" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccountWideListsReadEveryPage(t *testing.T) { |
| 15 | + cases := []struct { |
| 16 | + name string |
| 17 | + cmd func() *cobra.Command |
| 18 | + args []string |
| 19 | + path string |
| 20 | + pages []string |
| 21 | + ids func(records any) []any |
| 22 | + also []stubRoute |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "overdue todos", cmd: NewTodosCmd, args: []string{"list", "--overdue"}, |
| 26 | + path: "/99999/todos/overdue.json", |
| 27 | + pages: []string{`[{"id":1,"title":"Alpha","due_on":"2020-01-01"}]`, `[{"id":2,"title":"Bravo","due_on":"2020-02-01"}]`}, |
| 28 | + ids: listIDs, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "overdue cards", cmd: NewCardsCmd, args: []string{"list", "--all-projects", "--overdue"}, |
| 32 | + path: "/99999/cards/overdue.json", |
| 33 | + pages: []string{`[{"id":1,"title":"Alpha","due_on":"2020-01-01"}]`, `[{"id":2,"title":"Bravo","due_on":"2020-02-01"}]`}, |
| 34 | + ids: listIDs, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "completed assignments", cmd: NewAssignmentsCmd, args: []string{"completed"}, |
| 38 | + path: "/99999/my/assignments/completed.json", |
| 39 | + pages: []string{`[{"id":1,"content":"Alpha"}]`, `[{"id":2,"content":"Bravo"}]`}, |
| 40 | + ids: listIDs, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "timesheet report", cmd: NewTimesheetCmd, args: []string{"report"}, |
| 44 | + path: "/99999/reports/timesheet.json", |
| 45 | + pages: []string{`[{"id":1,"hours":"1.0"}]`, `[{"id":2,"hours":"2.0"}]`}, |
| 46 | + ids: listIDs, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "assigned todos", cmd: NewReportsCmd, args: []string{"assigned", "42"}, |
| 50 | + path: "/99999/reports/todos/assigned/42", |
| 51 | + pages: []string{ |
| 52 | + `{"person":{"id":42,"name":"Ann"},"grouped_by":"","todos":[{"id":1,"title":"Alpha"}]}`, |
| 53 | + `{"person":{"id":42,"name":"Ann"},"grouped_by":"","todos":[{"id":2,"title":"Bravo"}]}`, |
| 54 | + }, |
| 55 | + ids: func(report any) []any { return listIDs(report.(map[string]any)["todos"]) }, |
| 56 | + also: []stubRoute{{method: http.MethodGet, path: "/99999/people.json", status: http.StatusOK, |
| 57 | + body: `[{"id":42,"name":"Ann"}]`}}, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range cases { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + listRoute := stubRoute{method: http.MethodGet, path: tc.path, status: http.StatusOK, pages: tc.pages, linked: true} |
| 64 | + app, transport, buf := setupAccountWideTodosApp(t, output.FormatJSON, append(tc.also, listRoute)...) |
| 65 | + |
| 66 | + require.NoError(t, executeRecordingCommand(tc.cmd(), app, tc.args...)) |
| 67 | + |
| 68 | + assert.Equal(t, []string{"", "page=2"}, transport.queriesFor(tc.path)) |
| 69 | + assert.Equal(t, []any{float64(1), float64(2)}, tc.ids(decodeTodosEnvelope(t, buf)["data"])) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func listIDs(records any) []any { |
| 75 | + list := records.([]any) |
| 76 | + ids := make([]any, 0, len(list)) |
| 77 | + for _, record := range list { |
| 78 | + ids = append(ids, record.(map[string]any)["id"]) |
| 79 | + } |
| 80 | + return ids |
| 81 | +} |
0 commit comments