(t *testing.T)
| 7512 | } |
| 7513 | |
| 7514 | func TestProcessChat_UserProviderKey_MissingKeyError(t *testing.T) { |
| 7515 | t.Parallel() |
| 7516 | |
| 7517 | db, ps := dbtestutil.NewDB(t) |
| 7518 | ctx := testutil.Context(t, testutil.WaitLong) |
| 7519 | |
| 7520 | var llmCalls atomic.Int32 |
| 7521 | openAIURL := chattest.NewOpenAI(t, func(req *chattest.OpenAIRequest) chattest.OpenAIResponse { |
| 7522 | llmCalls.Add(1) |
| 7523 | if !req.Stream { |
| 7524 | return chattest.OpenAINonStreamingResponse("unexpected non-streaming request") |
| 7525 | } |
| 7526 | return chattest.OpenAIStreamingResponse( |
| 7527 | chattest.OpenAITextChunks("unexpected streaming request")..., |
| 7528 | ) |
| 7529 | }) |
| 7530 | |
| 7531 | user, org, _, model := seedChatDependenciesWithProviderPolicy( |
| 7532 | t, |
| 7533 | db, |
| 7534 | "openai-compat", |
| 7535 | openAIURL, |
| 7536 | "", |
| 7537 | false, |
| 7538 | true, |
| 7539 | false, |
| 7540 | ) |
| 7541 | |
| 7542 | creator := newTestServer(t, db, ps, uuid.New()) |
| 7543 | chat, err := creator.CreateChat(ctx, chatd.CreateOptions{ |
| 7544 | OrganizationID: org.ID, |
| 7545 | OwnerID: user.ID, |
| 7546 | Title: "user-provider-key-missing", |
| 7547 | ModelConfigID: model.ID, |
| 7548 | InitialUserContent: []codersdk.ChatMessagePart{ |
| 7549 | codersdk.ChatMessageText("say hello"), |
| 7550 | }, |
| 7551 | }) |
| 7552 | require.NoError(t, err) |
| 7553 | |
| 7554 | _, events, cancel, ok := creator.Subscribe(ctx, chat.ID, nil, 0) |
| 7555 | require.True(t, ok) |
| 7556 | t.Cleanup(cancel) |
| 7557 | |
| 7558 | _ = newActiveTestServer(t, db, ps) |
| 7559 | |
| 7560 | terminalStatus := waitForTerminalChatStatusEvent(ctx, t, events) |
| 7561 | require.Equal(t, codersdk.ChatStatusError, terminalStatus) |
| 7562 | |
| 7563 | chatResult := waitForTerminalChat(ctx, t, db, chat.ID) |
| 7564 | require.Equal(t, database.ChatStatusError, chatResult.Status) |
| 7565 | persistedError := requireChatLastErrorPayload(t, chatResult.LastError) |
| 7566 | require.NotEmpty(t, persistedError.Message) |
| 7567 | require.NotContains(t, persistedError.Message, "panicked") |
| 7568 | require.Equal(t, codersdk.ChatErrorKindGeneric, persistedError.Kind) |
| 7569 | require.NotEqual(t, database.ChatStatusRunning, chatResult.Status) |
| 7570 | require.Zero(t, llmCalls.Load(), "missing user key should fail before any LLM request") |
| 7571 | } |
nothing calls this directly
no test coverage detected