|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/basecamp/cli/output" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCommandsStyledOutputRendersHumanCatalog(t *testing.T) { |
| 11 | + mock := NewMockClient() |
| 12 | + SetTestModeWithSDK(mock) |
| 13 | + SetTestFormat(output.FormatStyled) |
| 14 | + defer resetTest() |
| 15 | + |
| 16 | + if err := commandsCmd.RunE(commandsCmd, []string{}); err != nil { |
| 17 | + t.Fatalf("unexpected error: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + raw := TestOutput() |
| 21 | + if !strings.Contains(raw, "Name") { |
| 22 | + t.Fatalf("expected styled catalog header, got:\n%s", raw) |
| 23 | + } |
| 24 | + if !strings.Contains(raw, "fizzy auth") { |
| 25 | + t.Fatalf("expected styled catalog to include commands, got:\n%s", raw) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestCommandsJSONOutputReturnsStructuredCatalog(t *testing.T) { |
| 30 | + mock := NewMockClient() |
| 31 | + result := SetTestModeWithSDK(mock) |
| 32 | + defer resetTest() |
| 33 | + |
| 34 | + if err := commandsCmd.RunE(commandsCmd, []string{}); err != nil { |
| 35 | + t.Fatalf("unexpected error: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + if result.Response == nil || !result.Response.OK { |
| 39 | + t.Fatalf("expected OK JSON response, got %#v", result.Response) |
| 40 | + } |
| 41 | + |
| 42 | + items, ok := result.Response.Data.([]any) |
| 43 | + if !ok { |
| 44 | + t.Fatalf("expected command catalog slice, got %#v", result.Response.Data) |
| 45 | + } |
| 46 | + if len(items) == 0 { |
| 47 | + t.Fatal("expected command catalog entries") |
| 48 | + } |
| 49 | + |
| 50 | + found := false |
| 51 | + for _, item := range items { |
| 52 | + entry, ok := item.(map[string]any) |
| 53 | + if !ok { |
| 54 | + continue |
| 55 | + } |
| 56 | + if entry["name"] == "fizzy commands" { |
| 57 | + found = true |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + if !found { |
| 62 | + t.Fatalf("expected command catalog to include fizzy commands, got %#v", items) |
| 63 | + } |
| 64 | +} |
0 commit comments