(t *testing.T)
| 1202 | } |
| 1203 | |
| 1204 | func TestRecordToolUsage(t *testing.T) { |
| 1205 | t.Parallel() |
| 1206 | |
| 1207 | var ( |
| 1208 | metadataProto = map[string]*anypb.Any{ |
| 1209 | "key": mustMarshalAny(t, &structpb.Value{Kind: &structpb.Value_NumberValue{NumberValue: 123.45}}), |
| 1210 | } |
| 1211 | metadataJSON = `{"key":123.45}` |
| 1212 | ) |
| 1213 | |
| 1214 | testRecordMethod(t, |
| 1215 | func(srv *aibridgedserver.Server, ctx context.Context, req *proto.RecordToolUsageRequest) (*proto.RecordToolUsageResponse, error) { |
| 1216 | return srv.RecordToolUsage(ctx, req) |
| 1217 | }, |
| 1218 | []testRecordMethodCase[*proto.RecordToolUsageRequest]{ |
| 1219 | { |
| 1220 | name: "valid tool usage with all fields", |
| 1221 | request: &proto.RecordToolUsageRequest{ |
| 1222 | InterceptionId: uuid.NewString(), |
| 1223 | MsgId: "msg_123", |
| 1224 | ToolCallId: "call_xyz", |
| 1225 | ServerUrl: ptr.Ref("https://api.example.com"), |
| 1226 | Tool: "read_file", |
| 1227 | Input: `{"path": "/etc/hosts"}`, |
| 1228 | Injected: false, |
| 1229 | InvocationError: ptr.Ref("permission denied"), |
| 1230 | Metadata: metadataProto, |
| 1231 | CreatedAt: timestamppb.Now(), |
| 1232 | }, |
| 1233 | setupMocks: func(t *testing.T, db *dbmock.MockStore, req *proto.RecordToolUsageRequest) { |
| 1234 | interceptionID, err := uuid.Parse(req.GetInterceptionId()) |
| 1235 | assert.NoError(t, err, "parse interception UUID") |
| 1236 | |
| 1237 | dbServerURL := sql.NullString{} |
| 1238 | if req.ServerUrl != nil { |
| 1239 | dbServerURL.String = *req.ServerUrl |
| 1240 | dbServerURL.Valid = true |
| 1241 | } |
| 1242 | |
| 1243 | dbInvocationError := sql.NullString{} |
| 1244 | if req.InvocationError != nil { |
| 1245 | dbInvocationError.String = *req.InvocationError |
| 1246 | dbInvocationError.Valid = true |
| 1247 | } |
| 1248 | |
| 1249 | db.EXPECT().InsertAIBridgeToolUsage(gomock.Any(), gomock.Cond(func(p database.InsertAIBridgeToolUsageParams) bool { |
| 1250 | if !assert.NotEqual(t, uuid.Nil, p.ID, "ID") || |
| 1251 | !assert.Equal(t, interceptionID, p.InterceptionID, "interception ID") || |
| 1252 | !assert.Equal(t, req.GetMsgId(), p.ProviderResponseID, "provider response ID") || |
| 1253 | !assert.Equal(t, sql.NullString{String: "call_xyz", Valid: true}, p.ProviderToolCallID, "provider tool call ID") || |
| 1254 | !assert.Equal(t, req.GetTool(), p.Tool, "tool") || |
| 1255 | !assert.Equal(t, dbServerURL, p.ServerUrl, "server URL") || |
| 1256 | !assert.Equal(t, req.GetInput(), p.Input, "input") || |
| 1257 | !assert.Equal(t, req.GetInjected(), p.Injected, "injected") || |
| 1258 | !assert.Equal(t, dbInvocationError, p.InvocationError, "invocation error") || |
| 1259 | !assert.JSONEq(t, metadataJSON, string(p.Metadata), "metadata") || |
| 1260 | !assert.WithinDuration(t, req.GetCreatedAt().AsTime(), p.CreatedAt, time.Second, "created at") { |
| 1261 | return false |
nothing calls this directly
no test coverage detected