MCPcopy Index your code
hub / github.com/coder/coder / TestAIProvidersChangedPubsub

Function TestAIProvidersChangedPubsub

coderd/ai_providers_pubsub_test.go:23–62  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

21// The handlers publish best-effort and the payload is empty, so we
22// assert "at least one event per mutation" via a counter.
23func 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}

Callers

nothing calls this directly

Calls 12

NewWithAPIFunction · 0.92
CreateFirstUserFunction · 0.92
ContextFunction · 0.92
EventuallyFunction · 0.92
CreateAIProviderMethod · 0.80
DeleteAIProviderMethod · 0.80
SubscribeMethod · 0.65
AddMethod · 0.65
CleanupMethod · 0.65
UpdateAIProviderMethod · 0.65
LoadMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected