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

Function TestAIBridgeTransportFactory_Registration

coderd/aibridge_test.go:50–100  ·  view source on GitHub ↗

Verify that a factory stored on coderd.API.AIBridgeTransportFactory is observable through the normal API lifecycle: cli/server.go registers it when the bridge daemon starts (see RegisterInMemoryAIBridgedHTTPHandler).

(t *testing.T)

Source from the content-addressed store, hash-verified

48// observable through the normal API lifecycle: cli/server.go registers it
49// when the bridge daemon starts (see RegisterInMemoryAIBridgedHTTPHandler).
50func TestAIBridgeTransportFactory_Registration(t *testing.T) {
51 t.Parallel()
52
53 _, _, api := coderdtest.NewWithAPI(t, nil)
54
55 require.Nil(t, api.AIBridgeTransportFactory.Load(),
56 "AGPL coderd must not pre-populate the factory")
57
58 stub := &stubTransportFactory{
59 handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60 w.Header().Set("Content-Type", "application/json")
61 w.WriteHeader(http.StatusOK)
62 _, _ = w.Write([]byte(`{"bridged":true}`))
63 }),
64 calls: make(chan callRecord, 4),
65 }
66
67 var asInterface aibridge.TransportFactory = stub
68 api.AIBridgeTransportFactory.Store(&asInterface)
69
70 loaded := api.AIBridgeTransportFactory.Load()
71 require.NotNil(t, loaded)
72
73 providerName := "openai"
74 rt, err := (*loaded).TransportFor(providerName, aibridge.SourceAgents)
75 require.NoError(t, err)
76 require.NotNil(t, rt)
77
78 select {
79 case got := <-stub.calls:
80 require.Equal(t, providerName, got.providerName)
81 require.Equal(t, aibridge.SourceAgents, got.source)
82 default:
83 t.Fatal("factory was not invoked")
84 }
85
86 ctx := testutil.Context(t, testutil.WaitShort)
87 req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://aibridge/v1/messages", nil)
88 require.NoError(t, err)
89
90 client := &http.Client{Transport: rt}
91 resp, err := client.Do(req)
92 require.NoError(t, err)
93 defer resp.Body.Close()
94
95 body, err := io.ReadAll(resp.Body)
96 require.NoError(t, err)
97 require.Equal(t, http.StatusOK, resp.StatusCode)
98 require.Equal(t, `{"bridged":true}`, string(body))
99 require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
100}

Callers

nothing calls this directly

Calls 15

NewWithAPIFunction · 0.92
ContextFunction · 0.92
FatalMethod · 0.80
SetMethod · 0.65
WriteMethod · 0.65
TransportForMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
LoadMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected