|
| 1 | +package hookdeck |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +// The API spells the connection dimension webhook_id. The CLI and the MCP tool |
| 10 | +// both accept connection_id and translate outbound, but neither translated the |
| 11 | +// response, so a caller who asked for connection_id got results keyed |
| 12 | +// webhook_id and had to know an internal name to read their own answer (#442). |
| 13 | +// |
| 14 | +// These cover the round trip, which is the property that matters: whatever |
| 15 | +// dimension name went out is the one that comes back. |
| 16 | +func TestDimensionNamesRoundTrip(t *testing.T) { |
| 17 | + asked := []string{"connection_id", "status"} |
| 18 | + sent := MapDimensionsToAPI(asked) |
| 19 | + assert.Equal(t, []string{"webhook_id", "status"}, sent, "the API is asked in its own spelling") |
| 20 | + |
| 21 | + // What the API answers with, keyed as it sent it. |
| 22 | + response := MetricsResponse{ |
| 23 | + {Dimensions: map[string]interface{}{"webhook_id": "web_123", "status": "SUCCESSFUL"}}, |
| 24 | + } |
| 25 | + got := RestoreDimensionNames(response) |
| 26 | + |
| 27 | + assert.Equal(t, "web_123", got[0].Dimensions["connection_id"], |
| 28 | + "the answer must use the name the caller asked with") |
| 29 | + assert.NotContains(t, got[0].Dimensions, "webhook_id", |
| 30 | + "the internal name must not leak back to the caller") |
| 31 | + assert.Equal(t, "SUCCESSFUL", got[0].Dimensions["status"], |
| 32 | + "untranslated dimensions are left alone") |
| 33 | +} |
| 34 | + |
| 35 | +func TestRestoreDimensionNamesEdgeCases(t *testing.T) { |
| 36 | + t.Run("nil dimensions are left alone", func(t *testing.T) { |
| 37 | + got := RestoreDimensionNames(MetricsResponse{{Dimensions: nil}}) |
| 38 | + assert.Nil(t, got[0].Dimensions) |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("no webhook_id is a no-op", func(t *testing.T) { |
| 42 | + got := RestoreDimensionNames(MetricsResponse{ |
| 43 | + {Dimensions: map[string]interface{}{"status": "FAILED"}}, |
| 44 | + }) |
| 45 | + assert.Equal(t, map[string]interface{}{"status": "FAILED"}, got[0].Dimensions) |
| 46 | + }) |
| 47 | + |
| 48 | + // If the API ever answers with both, the one it called connection_id wins; |
| 49 | + // silently overwriting it would be the same class of bug this fixes. |
| 50 | + t.Run("an existing connection_id is never clobbered", func(t *testing.T) { |
| 51 | + got := RestoreDimensionNames(MetricsResponse{ |
| 52 | + {Dimensions: map[string]interface{}{"webhook_id": "web_1", "connection_id": "conn_real"}}, |
| 53 | + }) |
| 54 | + assert.Equal(t, "conn_real", got[0].Dimensions["connection_id"]) |
| 55 | + assert.Equal(t, "web_1", got[0].Dimensions["webhook_id"]) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("empty response", func(t *testing.T) { |
| 59 | + assert.Empty(t, RestoreDimensionNames(MetricsResponse{})) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func TestMapDimensionsToAPIIsIdempotent(t *testing.T) { |
| 64 | + once := MapDimensionsToAPI([]string{"connection_id"}) |
| 65 | + assert.Equal(t, once, MapDimensionsToAPI(once), |
| 66 | + "mapping an already-mapped slice must not change it") |
| 67 | + assert.Nil(t, MapDimensionsToAPI(nil)) |
| 68 | +} |
0 commit comments