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

Function TestRecordPromptUsage

coderd/aibridgedserver/aibridgedserver_test.go:1126–1202  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1124}
1125
1126func TestRecordPromptUsage(t *testing.T) {
1127 t.Parallel()
1128
1129 var (
1130 metadataProto = map[string]*anypb.Any{
1131 "key": mustMarshalAny(t, &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "value"}}),
1132 }
1133 metadataJSON = `{"key":"value"}`
1134 )
1135
1136 testRecordMethod(t,
1137 func(srv *aibridgedserver.Server, ctx context.Context, req *proto.RecordPromptUsageRequest) (*proto.RecordPromptUsageResponse, error) {
1138 return srv.RecordPromptUsage(ctx, req)
1139 },
1140 []testRecordMethodCase[*proto.RecordPromptUsageRequest]{
1141 {
1142 name: "valid prompt usage",
1143 request: &proto.RecordPromptUsageRequest{
1144 InterceptionId: uuid.NewString(),
1145 MsgId: "msg_123",
1146 Prompt: "yo",
1147 Metadata: metadataProto,
1148 CreatedAt: timestamppb.Now(),
1149 },
1150 setupMocks: func(t *testing.T, db *dbmock.MockStore, req *proto.RecordPromptUsageRequest) {
1151 interceptionID, err := uuid.Parse(req.GetInterceptionId())
1152 assert.NoError(t, err, "parse interception UUID")
1153
1154 db.EXPECT().InsertAIBridgeUserPrompt(gomock.Any(), gomock.Cond(func(p database.InsertAIBridgeUserPromptParams) bool {
1155 if !assert.NotEqual(t, uuid.Nil, p.ID, "ID") ||
1156 !assert.Equal(t, interceptionID, p.InterceptionID, "interception ID") ||
1157 !assert.Equal(t, req.GetMsgId(), p.ProviderResponseID, "provider response ID") ||
1158 !assert.Equal(t, req.GetPrompt(), p.Prompt, "prompt") ||
1159 !assert.JSONEq(t, metadataJSON, string(p.Metadata), "metadata") ||
1160 !assert.WithinDuration(t, req.GetCreatedAt().AsTime(), p.CreatedAt, time.Second, "created at") {
1161 return false
1162 }
1163 return true
1164 })).Return(database.AIBridgeUserPrompt{
1165 ID: uuid.New(),
1166 InterceptionID: interceptionID,
1167 ProviderResponseID: req.GetMsgId(),
1168 Prompt: req.GetPrompt(),
1169 Metadata: pqtype.NullRawMessage{
1170 RawMessage: json.RawMessage(metadataJSON),
1171 Valid: true,
1172 },
1173 CreatedAt: req.GetCreatedAt().AsTime(),
1174 }, nil)
1175 },
1176 },
1177 {
1178 name: "invalid interception ID",
1179 request: &proto.RecordPromptUsageRequest{
1180 InterceptionId: "not-a-uuid",
1181 MsgId: "msg_123",
1182 Prompt: "yo",
1183 CreatedAt: timestamppb.Now(),

Callers

nothing calls this directly

Calls 12

mustMarshalAnyFunction · 0.85
testRecordMethodFunction · 0.85
GetPromptMethod · 0.80
RecordPromptUsageMethod · 0.65
ParseMethod · 0.65
NewMethod · 0.65
GetInterceptionIdMethod · 0.45
EXPECTMethod · 0.45
EqualMethod · 0.45
GetMsgIdMethod · 0.45
GetCreatedAtMethod · 0.45

Tested by

no test coverage detected