(t *testing.T)
| 1035 | } |
| 1036 | |
| 1037 | func TestRecordTokenUsage(t *testing.T) { |
| 1038 | t.Parallel() |
| 1039 | |
| 1040 | var ( |
| 1041 | metadataProto = map[string]*anypb.Any{ |
| 1042 | "key": mustMarshalAny(t, &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "value"}}), |
| 1043 | } |
| 1044 | metadataJSON = `{"key":"value"}` |
| 1045 | ) |
| 1046 | |
| 1047 | testRecordMethod(t, |
| 1048 | func(srv *aibridgedserver.Server, ctx context.Context, req *proto.RecordTokenUsageRequest) (*proto.RecordTokenUsageResponse, error) { |
| 1049 | return srv.RecordTokenUsage(ctx, req) |
| 1050 | }, |
| 1051 | []testRecordMethodCase[*proto.RecordTokenUsageRequest]{ |
| 1052 | { |
| 1053 | name: "valid token usage", |
| 1054 | request: &proto.RecordTokenUsageRequest{ |
| 1055 | InterceptionId: uuid.NewString(), |
| 1056 | MsgId: "msg_123", |
| 1057 | InputTokens: 100, |
| 1058 | OutputTokens: 200, |
| 1059 | CacheReadInputTokens: 50, |
| 1060 | CacheWriteInputTokens: 10, |
| 1061 | Metadata: metadataProto, |
| 1062 | CreatedAt: timestamppb.Now(), |
| 1063 | }, |
| 1064 | setupMocks: func(t *testing.T, db *dbmock.MockStore, req *proto.RecordTokenUsageRequest) { |
| 1065 | interceptionID, err := uuid.Parse(req.GetInterceptionId()) |
| 1066 | assert.NoError(t, err, "parse interception UUID") |
| 1067 | |
| 1068 | db.EXPECT().InsertAIBridgeTokenUsage(gomock.Any(), gomock.Cond(func(p database.InsertAIBridgeTokenUsageParams) bool { |
| 1069 | if !assert.NotEqual(t, uuid.Nil, p.ID, "ID") || |
| 1070 | !assert.Equal(t, interceptionID, p.InterceptionID, "interception ID") || |
| 1071 | !assert.Equal(t, req.GetMsgId(), p.ProviderResponseID, "provider response ID") || |
| 1072 | !assert.Equal(t, req.GetInputTokens(), p.InputTokens, "input tokens") || |
| 1073 | !assert.Equal(t, req.GetOutputTokens(), p.OutputTokens, "output tokens") || |
| 1074 | !assert.Equal(t, req.GetCacheReadInputTokens(), p.CacheReadInputTokens, "cache read input tokens") || |
| 1075 | !assert.Equal(t, req.GetCacheWriteInputTokens(), p.CacheWriteInputTokens, "cache write input tokens") || |
| 1076 | !assert.JSONEq(t, metadataJSON, string(p.Metadata), "metadata") || |
| 1077 | !assert.WithinDuration(t, req.GetCreatedAt().AsTime(), p.CreatedAt, time.Second, "created at") { |
| 1078 | return false |
| 1079 | } |
| 1080 | return true |
| 1081 | })).Return(database.AIBridgeTokenUsage{ |
| 1082 | ID: uuid.New(), |
| 1083 | InterceptionID: interceptionID, |
| 1084 | ProviderResponseID: req.GetMsgId(), |
| 1085 | InputTokens: req.GetInputTokens(), |
| 1086 | OutputTokens: req.GetOutputTokens(), |
| 1087 | CacheReadInputTokens: req.GetCacheReadInputTokens(), |
| 1088 | CacheWriteInputTokens: req.GetCacheWriteInputTokens(), |
| 1089 | Metadata: pqtype.NullRawMessage{ |
| 1090 | RawMessage: json.RawMessage(metadataJSON), |
| 1091 | Valid: true, |
| 1092 | }, |
| 1093 | CreatedAt: req.GetCreatedAt().AsTime(), |
| 1094 | }, nil) |
nothing calls this directly
no test coverage detected