TestClassifyProviderRow covers every branch of the classifier so the disabled, error, and enabled paths are exercised through the production code instead of relying on classifyRaw, the test mirror in reload_test.go.
(t *testing.T)
| 16 | // production code instead of relying on classifyRaw, the test mirror in |
| 17 | // reload_test.go. |
| 18 | func TestClassifyProviderRow(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | |
| 21 | enabledRow := func(name, baseURL string) database.AIProvider { |
| 22 | return database.AIProvider{ |
| 23 | Name: name, |
| 24 | Type: database.AiProviderTypeOpenai, |
| 25 | Enabled: true, |
| 26 | BaseUrl: baseURL, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | t.Run("Enabled", func(t *testing.T) { |
| 31 | t.Parallel() |
| 32 | |
| 33 | seen := map[string]string{} |
| 34 | got := classifyProviderRow(enabledRow("openai", "https://api.openai.com/v1"), seen) |
| 35 | assert.Equal(t, "openai", got.Name) |
| 36 | assert.Equal(t, string(database.AiProviderTypeOpenai), got.Type) |
| 37 | assert.Equal(t, aibridged.ProviderStatusEnabled, got.Status) |
| 38 | assert.Equal(t, "api.openai.com", got.Host) |
| 39 | assert.NoError(t, got.Err) |
| 40 | assert.Equal(t, "openai", seen["api.openai.com"]) |
| 41 | }) |
| 42 | |
| 43 | t.Run("DisabledRow", func(t *testing.T) { |
| 44 | t.Parallel() |
| 45 | |
| 46 | seen := map[string]string{} |
| 47 | row := enabledRow("off", "https://api.off.example.com/v1") |
| 48 | row.Enabled = false |
| 49 | got := classifyProviderRow(row, seen) |
| 50 | assert.Equal(t, aibridged.ProviderStatusDisabled, got.Status) |
| 51 | assert.Empty(t, got.Host, "disabled provider must not claim a host") |
| 52 | assert.NoError(t, got.Err) |
| 53 | assert.Empty(t, seen, "disabled provider must not occupy a host slot") |
| 54 | }) |
| 55 | |
| 56 | t.Run("EmptyBaseURL", func(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | |
| 59 | seen := map[string]string{} |
| 60 | got := classifyProviderRow(enabledRow("no-url", " "), seen) |
| 61 | assert.Equal(t, aibridged.ProviderStatusError, got.Status) |
| 62 | assert.Empty(t, got.Host) |
| 63 | assert.ErrorContains(t, got.Err, "base url is empty") |
| 64 | }) |
| 65 | |
| 66 | t.Run("MalformedBaseURL", func(t *testing.T) { |
| 67 | t.Parallel() |
| 68 | |
| 69 | seen := map[string]string{} |
| 70 | got := classifyProviderRow(enabledRow("bad", "://not-a-url"), seen) |
| 71 | assert.Equal(t, aibridged.ProviderStatusError, got.Status) |
| 72 | assert.ErrorContains(t, got.Err, "invalid base url") |
| 73 | }) |
| 74 | |
| 75 | t.Run("BaseURLWithoutHostname", func(t *testing.T) { |
nothing calls this directly
no test coverage detected