|
| 1 | +package errors_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + "github.com/launchdarkly/ldcli/internal/errors" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSuggestionForStatus(t *testing.T) { |
| 12 | + t.Run("401 returns access token suggestion with baseURI", func(t *testing.T) { |
| 13 | + result := errors.SuggestionForStatus(401, "https://app.launchdarkly.com") |
| 14 | + |
| 15 | + assert.Contains(t, result, "ldcli login") |
| 16 | + assert.Contains(t, result, "LD_ACCESS_TOKEN") |
| 17 | + assert.Contains(t, result, "https://app.launchdarkly.com/settings/authorization") |
| 18 | + assert.NotContains(t, result, "{baseURI}") |
| 19 | + }) |
| 20 | + |
| 21 | + t.Run("403 returns permission suggestion", func(t *testing.T) { |
| 22 | + result := errors.SuggestionForStatus(403, "https://app.launchdarkly.com") |
| 23 | + |
| 24 | + assert.Contains(t, result, "permission") |
| 25 | + assert.Contains(t, result, "custom role") |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("404 returns resource not found suggestion", func(t *testing.T) { |
| 29 | + result := errors.SuggestionForStatus(404, "https://app.launchdarkly.com") |
| 30 | + |
| 31 | + assert.Contains(t, result, "not found") |
| 32 | + assert.Contains(t, result, "ldcli projects list") |
| 33 | + assert.Contains(t, result, "ldcli flags list") |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("409 returns conflict suggestion", func(t *testing.T) { |
| 37 | + result := errors.SuggestionForStatus(409, "https://app.launchdarkly.com") |
| 38 | + |
| 39 | + assert.Contains(t, result, "Conflict") |
| 40 | + assert.Contains(t, result, "latest version") |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("429 returns rate limit suggestion", func(t *testing.T) { |
| 44 | + result := errors.SuggestionForStatus(429, "https://app.launchdarkly.com") |
| 45 | + |
| 46 | + assert.Contains(t, result, "Rate limited") |
| 47 | + assert.Contains(t, result, "retry") |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("unknown status code returns empty string", func(t *testing.T) { |
| 51 | + result := errors.SuggestionForStatus(502, "https://app.launchdarkly.com") |
| 52 | + |
| 53 | + assert.Empty(t, result) |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("200 returns empty string", func(t *testing.T) { |
| 57 | + result := errors.SuggestionForStatus(200, "https://app.launchdarkly.com") |
| 58 | + |
| 59 | + assert.Empty(t, result) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("replaces baseURI placeholder with actual value", func(t *testing.T) { |
| 63 | + result := errors.SuggestionForStatus(401, "https://custom.ld.example.com") |
| 64 | + |
| 65 | + assert.Contains(t, result, "https://custom.ld.example.com/settings/authorization") |
| 66 | + assert.NotContains(t, result, "{baseURI}") |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("empty baseURI does not panic", func(t *testing.T) { |
| 70 | + result := errors.SuggestionForStatus(401, "") |
| 71 | + |
| 72 | + assert.Contains(t, result, "ldcli login") |
| 73 | + assert.Contains(t, result, "/settings/authorization") |
| 74 | + }) |
| 75 | +} |
0 commit comments