|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/hookdeck/hookdeck-cli/pkg/config" |
| 16 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 17 | +) |
| 18 | + |
| 19 | +// captureListQuery runs a gateway list command against a stub API and returns |
| 20 | +// the query string it sent. |
| 21 | +// |
| 22 | +// A filter flag is only worth having if it reaches the API under the key the |
| 23 | +// API expects. Asserting the command exits zero proves nothing here: an |
| 24 | +// unrecognised query parameter is ignored by the API, so a misspelled key |
| 25 | +// silently returns the unfiltered list — the worst possible failure for a |
| 26 | +// filter, because the answer looks right. |
| 27 | +func captureListQuery(t *testing.T, args ...string) url.Values { |
| 28 | + t.Helper() |
| 29 | + |
| 30 | + var got url.Values |
| 31 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 32 | + w.Header().Set("Content-Type", "application/json") |
| 33 | + |
| 34 | + if r.URL.Path == hookdeck.APIPathPrefix+"/cli-auth/validate" { |
| 35 | + _ = json.NewEncoder(w).Encode(hookdeck.ValidateAPIKeyResponse{ |
| 36 | + ProjectID: "proj_1", |
| 37 | + ProjectMode: "inbound", |
| 38 | + }) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + got = r.URL.Query() |
| 43 | + _, _ = w.Write([]byte(`{"models":[],"pagination":{"limit":100}}`)) |
| 44 | + })) |
| 45 | + t.Cleanup(server.Close) |
| 46 | + |
| 47 | + config.ResetAPIClientForTesting() |
| 48 | + t.Cleanup(config.ResetAPIClientForTesting) |
| 49 | + |
| 50 | + old := Config |
| 51 | + t.Cleanup(func() { Config = old }) |
| 52 | + Config = config.Config{APIBaseURL: server.URL, LogLevel: "info"} |
| 53 | + Config.Profile.APIKey = "sk_test_123456789012" |
| 54 | + Config.Profile.ProjectId = "proj_1" |
| 55 | + Config.Profile.ProjectType = config.ProjectTypeGateway |
| 56 | + |
| 57 | + root := &cobra.Command{Use: "hookdeck", SilenceUsage: true, SilenceErrors: true} |
| 58 | + root.AddCommand(newGatewayCmd().cmd) |
| 59 | + root.SetArgs(args) |
| 60 | + root.SetOut(io.Discard) |
| 61 | + root.SetErr(io.Discard) |
| 62 | + |
| 63 | + require.NoError(t, root.Execute()) |
| 64 | + require.NotNil(t, got, "the command made no list request") |
| 65 | + return got |
| 66 | +} |
| 67 | + |
| 68 | +func TestEventListSendsNewFilters(t *testing.T) { |
| 69 | + query := captureListQuery(t, |
| 70 | + "gateway", "event", "list", |
| 71 | + "--search-term", "cus_1234", |
| 72 | + "--delivery-group", "grp_1", |
| 73 | + "--next-attempt-at-after", "2026-06-01T00:00:00Z", |
| 74 | + "--next-attempt-at-before", "2026-06-30T00:00:00Z", |
| 75 | + ) |
| 76 | + |
| 77 | + assert.Equal(t, "cus_1234", query.Get("search_term")) |
| 78 | + assert.Equal(t, "grp_1", query.Get("delivery_group")) |
| 79 | + // The date filters are operator keys, not plain fields: a bare |
| 80 | + // next_attempt_at= would be a different query the API rejects. |
| 81 | + assert.Equal(t, "2026-06-01T00:00:00Z", query.Get("next_attempt_at[gte]")) |
| 82 | + assert.Equal(t, "2026-06-30T00:00:00Z", query.Get("next_attempt_at[lte]")) |
| 83 | + assert.Empty(t, query.Get("next_attempt_at")) |
| 84 | +} |
| 85 | + |
| 86 | +func TestRequestListSendsNewFilters(t *testing.T) { |
| 87 | + query := captureListQuery(t, |
| 88 | + "gateway", "request", "list", |
| 89 | + "--search-term", "cus_1234", |
| 90 | + "--events-count", "0", |
| 91 | + "--ignored-count", "2", |
| 92 | + "--cli-events-count", "1", |
| 93 | + ) |
| 94 | + |
| 95 | + assert.Equal(t, "cus_1234", query.Get("search_term")) |
| 96 | + // events_count=0 — requests that produced no events — is the query that |
| 97 | + // explains a "missing" webhook, so the zero has to survive to the wire. |
| 98 | + assert.Equal(t, "0", query.Get("events_count")) |
| 99 | + assert.Equal(t, "2", query.Get("ignored_count")) |
| 100 | + assert.Equal(t, "1", query.Get("cli_events_count")) |
| 101 | +} |
| 102 | + |
| 103 | +// Omitted filters must not be sent at all. An empty value is not the same as no |
| 104 | +// filter to every API, and sending one narrows or widens the query by accident. |
| 105 | +func TestListFiltersAreOmittedWhenUnset(t *testing.T) { |
| 106 | + t.Run("event", func(t *testing.T) { |
| 107 | + query := captureListQuery(t, "gateway", "event", "list") |
| 108 | + for _, key := range []string{ |
| 109 | + "search_term", "delivery_group", "next_attempt_at[gte]", "next_attempt_at[lte]", |
| 110 | + } { |
| 111 | + assert.NotContains(t, query, key) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + t.Run("request", func(t *testing.T) { |
| 116 | + query := captureListQuery(t, "gateway", "request", "list") |
| 117 | + for _, key := range []string{ |
| 118 | + "search_term", "events_count", "ignored_count", "cli_events_count", |
| 119 | + } { |
| 120 | + assert.NotContains(t, query, key) |
| 121 | + } |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +// The API spec documents further parameters carrying x-docs-hide: Hookdeck |
| 126 | +// keeps them out of its public documentation deliberately, so the CLI must not |
| 127 | +// offer them either. Read without that context they look like filters we simply |
| 128 | +// forgot, which is how they would get added — this pins the omission as |
| 129 | +// intentional. The MCP side is pinned by |
| 130 | +// TestPluralToolsOmitParametersHiddenFromTheAPIDocs. |
| 131 | +func TestListCommandsOmitFlagsHiddenFromTheAPIDocs(t *testing.T) { |
| 132 | + root := &cobra.Command{Use: "hookdeck"} |
| 133 | + root.AddCommand(newGatewayCmd().cmd) |
| 134 | + |
| 135 | + cases := map[string][]string{ |
| 136 | + "event": {"bulk-retry-id", "include", "progressive", "event-data-id", "cli-user-id"}, |
| 137 | + "request": {"bulk-retry-id", "include", "progressive"}, |
| 138 | + } |
| 139 | + |
| 140 | + for resource, hidden := range cases { |
| 141 | + t.Run(resource, func(t *testing.T) { |
| 142 | + cmd, _, err := root.Find([]string{"gateway", resource, "list"}) |
| 143 | + require.NoError(t, err) |
| 144 | + require.Equal(t, "list", cmd.Name()) |
| 145 | + |
| 146 | + for _, name := range hidden { |
| 147 | + assert.Nil(t, cmd.Flags().Lookup(name), |
| 148 | + "gateway %s list must not offer --%s: it carries x-docs-hide in the API spec", |
| 149 | + resource, name) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments