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

Function TestChatCostSummary_AdminDrilldown

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

Source from the content-addressed store, hash-verified

10230}
10231
10232func TestChatCostSummary_AdminDrilldown(t *testing.T) {
10233 t.Parallel()
10234
10235 client, db := newChatClientWithDatabase(t)
10236 firstUser := coderdtest.CreateFirstUser(t, client.Client)
10237 memberClientRaw, member := coderdtest.CreateAnotherUser(t, client.Client, firstUser.OrganizationID)
10238 memberClient := codersdk.NewExperimentalClient(memberClientRaw)
10239 modelConfig := createChatModelConfig(t, client)
10240
10241 chat := dbgen.Chat(t, db, database.Chat{
10242 OrganizationID: firstUser.OrganizationID,
10243 OwnerID: member.ID,
10244 LastModelConfigID: modelConfig.ID,
10245 Title: "member chat",
10246 })
10247
10248 message := dbgen.ChatMessage(t, db, database.ChatMessage{
10249 ChatID: chat.ID,
10250 ModelConfigID: uuid.NullUUID{UUID: modelConfig.ID, Valid: true},
10251 Role: database.ChatMessageRoleAssistant,
10252 InputTokens: sql.NullInt64{Int64: 200, Valid: true},
10253 OutputTokens: sql.NullInt64{Int64: 100, Valid: true},
10254 TotalCostMicros: sql.NullInt64{Int64: 750, Valid: true},
10255 })
10256
10257 options := codersdk.ChatCostSummaryOptions{
10258 // Pad the DB-assigned timestamp so the query window cannot race it.
10259 StartDate: message.CreatedAt.Add(-time.Minute),
10260 EndDate: message.CreatedAt.Add(time.Minute),
10261 }
10262
10263 t.Run("AdminCanDrilldown", func(t *testing.T) {
10264 t.Parallel()
10265
10266 ctx := testutil.Context(t, testutil.WaitLong)
10267 summary, err := client.GetChatCostSummary(ctx, member.ID.String(), options)
10268 require.NoError(t, err)
10269 require.Equal(t, int64(750), summary.TotalCostMicros)
10270 require.Equal(t, int64(1), summary.PricedMessageCount)
10271 })
10272
10273 t.Run("MemberCannotDrilldownOtherUser", func(t *testing.T) {
10274 t.Parallel()
10275
10276 ctx := testutil.Context(t, testutil.WaitLong)
10277 _, err := memberClient.GetChatCostSummary(ctx, firstUser.UserID.String(), options)
10278 require.Error(t, err)
10279 var sdkErr *codersdk.Error
10280 require.ErrorAs(t, err, &sdkErr)
10281 require.Equal(t, http.StatusNotFound, sdkErr.StatusCode())
10282 })
10283}
10284
10285func TestChatCostUsers(t *testing.T) {
10286 t.Parallel()

Callers

nothing calls this directly

Calls 15

GetChatCostSummaryMethod · 0.95
StatusCodeMethod · 0.95
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
NewExperimentalClientFunction · 0.92
ChatFunction · 0.92
ChatMessageFunction · 0.92
ContextFunction · 0.92
createChatModelConfigFunction · 0.70
AddMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected