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

Function TestRecordTokenUsage

aibridge/intercept/responses/base_internal_test.go:278–347  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

276}
277
278func TestRecordTokenUsage(t *testing.T) {
279 t.Parallel()
280
281 id := uuid.MustParse("22222222-2222-2222-2222-222222222222")
282
283 tests := []struct {
284 name string
285 response *oairesponses.Response
286 expected *recorder.TokenUsageRecord
287 }{
288 {
289 name: "nil_response",
290 response: nil,
291 expected: nil,
292 },
293 {
294 name: "with_all_token_details",
295 response: &oairesponses.Response{
296 ID: "resp_full",
297 Usage: oairesponses.ResponseUsage{
298 InputTokens: 10,
299 OutputTokens: 20,
300 TotalTokens: 30,
301 InputTokensDetails: oairesponses.ResponseUsageInputTokensDetails{
302 CachedTokens: 5,
303 },
304 OutputTokensDetails: oairesponses.ResponseUsageOutputTokensDetails{
305 ReasoningTokens: 5,
306 },
307 },
308 },
309 expected: &recorder.TokenUsageRecord{
310 InterceptionID: id.String(),
311 MsgID: "resp_full",
312 Input: 5, // 10 input - 5 cached
313 Output: 20,
314 CacheReadInputTokens: 5,
315 ExtraTokenTypes: map[string]int64{
316 "output_reasoning": 5,
317 "total_tokens": 30,
318 },
319 },
320 },
321 }
322
323 for _, tc := range tests {
324 t.Run(tc.name, func(t *testing.T) {
325 t.Parallel()
326
327 rec := &testutil.MockRecorder{}
328 base := &responsesInterceptionBase{
329 id: id,
330 recorder: rec,
331 logger: slog.Make(),
332 }
333
334 base.recordTokenUsage(t.Context(), tc.response)
335

Callers

nothing calls this directly

Calls 8

recordTokenUsageMethod · 0.95
RecordedTokenUsagesMethod · 0.95
RunMethod · 0.65
ContextMethod · 0.65
StringMethod · 0.45
EmptyMethod · 0.45
LenMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected