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

Function TestManualTitleEndpointsPassCallerAPIKeyToAIGateway

coderd/exp_chats_test.go:8605–8682  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8603}
8604
8605func TestManualTitleEndpointsPassCallerAPIKeyToAIGateway(t *testing.T) {
8606 t.Parallel()
8607
8608 for _, tt := range []struct {
8609 name string
8610 call func(context.Context, *codersdk.ExperimentalClient, uuid.UUID) error
8611 }{
8612 {
8613 name: "RegenerateChatTitle",
8614 call: func(ctx context.Context, client *codersdk.ExperimentalClient, chatID uuid.UUID) error {
8615 _, err := client.RegenerateChatTitle(ctx, chatID)
8616 return err
8617 },
8618 },
8619 {
8620 name: "ProposeChatTitle",
8621 call: func(ctx context.Context, client *codersdk.ExperimentalClient, chatID uuid.UUID) error {
8622 _, err := client.ProposeChatTitle(ctx, chatID)
8623 return err
8624 },
8625 },
8626 } {
8627 t.Run(tt.name, func(t *testing.T) {
8628 t.Parallel()
8629
8630 ctx := testutil.Context(t, testutil.WaitLong)
8631 values := chatDeploymentValues(t)
8632 require.NoError(t, values.AI.BridgeConfig.Enabled.Set("true"))
8633 require.NoError(t, values.AI.Chat.AIGatewayRoutingEnabled.Set("true"))
8634 client, db, api := newChatClientWithAPIAndDatabase(t, func(opts *coderdtest.Options) {
8635 opts.DeploymentValues = values
8636 })
8637 firstUser := coderdtest.CreateFirstUser(t, client.Client)
8638 modelConfig := createAdditionalChatModelConfig(t, client, "openai", "gpt-4.1")
8639 wantAPIKeyID := strings.Split(client.SessionToken(), "-")[0]
8640 wantTitle := "Fallback title"
8641 seenAPIKeyID := make(chan string, 1)
8642 stub := &stubTransportFactory{
8643 handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
8644 apiKeyID, _ := aibridge.DelegatedAPIKeyIDFromContext(r.Context())
8645 seenAPIKeyID <- apiKeyID
8646 rw.Header().Set("Content-Type", "application/json")
8647 text := strconv.Quote(`{"title":"` + wantTitle + `"}`)
8648 _, _ = io.WriteString(rw, `{"id":"resp_test","object":"response","created_at":0,"status":"completed","model":"gpt-4.1","output":[{"id":"msg_test","type":"message","role":"assistant","content":[{"type":"output_text","text":`+text+`}]}],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}`)
8649 }),
8650 calls: make(chan callRecord, 1),
8651 }
8652 var factory aibridge.TransportFactory = stub
8653 api.AIBridgeTransportFactory.Store(&factory)
8654 require.NoError(t, client.UpdateChatModelOverride(ctx, codersdk.ChatModelOverrideContextTitleGeneration, codersdk.UpdateChatModelOverrideRequest{
8655 ModelConfigID: modelConfig.ID.String(),
8656 }))
8657
8658 chat := dbgen.Chat(t, db, database.Chat{
8659 OrganizationID: firstUser.OrganizationID,
8660 OwnerID: firstUser.UserID,
8661 LastModelConfigID: modelConfig.ID,
8662 Title: "initial title",

Callers

nothing calls this directly

Calls 15

ContextFunction · 0.92
CreateFirstUserFunction · 0.92
ChatFunction · 0.92
MarshalPartsFunction · 0.92
ChatMessageTextFunction · 0.92
ChatMessageFunction · 0.92
RequireReceiveFunction · 0.92
chatDeploymentValuesFunction · 0.85
WriteStringMethod · 0.80

Tested by

no test coverage detected