TestAIProvidersChangedPubsub asserts that the CRUD handlers publish on AIProvidersChangedChannel for the operations that affect the runtime provider set. Subscribers (aibridged, aibridgeproxyd) depend on these notifications to trigger their pool reload. The handlers publish best-effort and the payl
(t *testing.T)
| 21 | // The handlers publish best-effort and the payload is empty, so we |
| 22 | // assert "at least one event per mutation" via a counter. |
| 23 | func TestAIProvidersChangedPubsub(t *testing.T) { |
| 24 | t.Parallel() |
| 25 | |
| 26 | client, _, api := coderdtest.NewWithAPI(t, nil) |
| 27 | _ = coderdtest.CreateFirstUser(t, client) |
| 28 | ctx := testutil.Context(t, testutil.WaitLong) |
| 29 | |
| 30 | var count atomic.Int64 |
| 31 | unsubscribe, err := api.Pubsub.Subscribe(coderpubsub.AIProvidersChangedChannel, func(_ context.Context, _ []byte) { |
| 32 | count.Add(1) |
| 33 | }) |
| 34 | require.NoError(t, err) |
| 35 | t.Cleanup(unsubscribe) |
| 36 | |
| 37 | // Create. |
| 38 | req := codersdk.CreateAIProviderRequest{ |
| 39 | Type: codersdk.AIProviderTypeOpenAI, |
| 40 | Name: "pubsub-openai", |
| 41 | Enabled: true, |
| 42 | BaseURL: "https://api.openai.com/v1/", |
| 43 | APIKeys: []string{"k1"}, |
| 44 | } |
| 45 | //nolint:gocritic // Owner role is the audience for this endpoint. |
| 46 | created, err := client.CreateAIProvider(ctx, req) |
| 47 | require.NoError(t, err) |
| 48 | testutil.Eventually(ctx, t, func(_ context.Context) bool { return count.Load() >= 1 }, testutil.IntervalFast) |
| 49 | |
| 50 | // Update. |
| 51 | newKey := "k2" |
| 52 | _, err = client.UpdateAIProvider(ctx, created.ID.String(), codersdk.UpdateAIProviderRequest{ |
| 53 | APIKeys: &[]codersdk.AIProviderKeyMutation{{APIKey: &newKey}}, |
| 54 | }) |
| 55 | require.NoError(t, err) |
| 56 | testutil.Eventually(ctx, t, func(_ context.Context) bool { return count.Load() >= 2 }, testutil.IntervalFast) |
| 57 | |
| 58 | // Delete. |
| 59 | err = client.DeleteAIProvider(ctx, created.ID.String()) |
| 60 | require.NoError(t, err) |
| 61 | testutil.Eventually(ctx, t, func(_ context.Context) bool { return count.Load() >= 3 }, testutil.IntervalFast) |
| 62 | } |
nothing calls this directly
no test coverage detected