api_gemini 0.5.0

Gemini's API for accessing large language models (LLMs).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Comprehensive tests for count tokens functionality in `api_gemini`
//!
//! These tests validate the countTokens endpoint which returns the number of tokens
//! that would be used for a given input text, helping developers understand token
//! usage and costs before making generation requests.
//!
//! ## Test Coverage
//!
//! **Core Functionality:**
//! - Simple text token counting
//! - Multimodal content (text + images) token counting
//! - Conversation context token counting
//! - Different model types and token limits
//! - Error handling for invalid inputs
//!
//! **API Integration:**
//! - Request/response serialization
//! - HTTP error handling
//! - Rate limiting behavior
//! - Authentication error handling
//!
//! ## Implementation Status
//!
//! These tests are designed to fail until the `count_tokens` functionality is implemented
//! in Task 063. Each test validates the expected behavior and will pass once the
//! implementation is complete.


use api_gemini::
{
client ::{ Client, ClientBuilder },
models ::{ Content, Part, CountTokensRequest, Blob },
  error ::Error,
};

/// Test basic text token counting functionality
///
/// This test validates that the `count_tokens` method can successfully count
/// tokens for simple text input. The response should contain a valid token count.
#[ tokio::test ]
async fn test_count_tokens_simple_text()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  let content = Content
  {
    parts: vec!
    [
    Part
    {
      text: Some( "Hello, world! This is a simple text for token counting.".to_string() ),
      inline_data: None,
      function_call: None,
      function_response: None,
      ..Default::default()
    }
    ],
    role: "user".to_string(),
  };

  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None,
  };

  // Now test the actual implementation
  let result = models_api.count_tokens( "gemini-flash-latest", &request ).await;

  match result
  {
    Ok( response ) =>
    {
      assert!( response.total_tokens > 0, "Token count should be positive for non-empty text" );
      assert!( response.total_tokens < 100, "Token count should be reasonable for short text" );

      // Optional field should be handled properly
      if let Some( cached_tokens ) = response.cached_content_token_count
      {
        assert!( cached_tokens >= 0, "Cached token count should be non-negative" );
      }

    println!( "✅ Simple text token count : {}", response.total_tokens );
    },
    Err( e ) =>
    {
      // If we get authentication errors, that's expected in CI
      match e
      {
        Error::AuthenticationError( _ ) =>
        {
        println!( "⚠️  Authentication error (expected without API key): {e}" );
        },
      _ => panic!( "Count tokens failed : {e:?}" ),
      }
    }
  }
}

/// Test token counting for multimodal content (text + image)
///
/// This test validates that the `count_tokens` method can handle multimodal
/// inputs containing both text and image data.
#[ tokio::test ]
async fn test_count_tokens_multimodal_content()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  // Create sample base64 encoded image data (1x1 pixel PNG)
  let sample_image_data = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==";

  let content = Content
  {
    parts: vec!
    [
    Part
    {
      text: Some( "What do you see in this image?".to_string() ),
      inline_data: None,
      function_call: None,
      function_response: None,
      ..Default::default()
    },
    Part
    {
      text: None,
      inline_data: Some( Blob
      {
        mime_type: "image/png".to_string(),
        data: sample_image_data.to_string(),
      }),
      function_call: None,
      function_response: None,
      ..Default::default()
    }
    ],
    role: "user".to_string(),
  };

  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None,
  };

  // Now test the actual implementation
  let result = models_api.count_tokens( "gemini-flash-latest", &request ).await;

  match result
  {
    Ok( response ) =>
    {
      assert!( response.total_tokens > 0, "Token count should be positive for multimodal content" );
    println!( "✅ Multimodal token count : {}", response.total_tokens );
    },
    Err( e ) =>
    {
      match e
      {
        Error::AuthenticationError( _ ) =>
        {
        println!( "⚠️  Authentication error (expected without API key): {e}" );
        },
      _ => panic!( "Multimodal count tokens failed : {e:?}" ),
      }
    }
  }
}

/// Test token counting for conversation context
///
/// This test validates that the `count_tokens` method can handle multi-turn
/// conversation contexts and provide accurate token counts.
#[ tokio::test ]
async fn test_count_tokens_conversation_context()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  let contents = vec!
  [
  Content
  {
  parts : vec![ Part { text : Some( "Hello, I'm starting a conversation.".to_string() ), inline_data : None, function_call : None, function_response : None, ..Default::default() } ],
    role: "user".to_string(),
  },
  Content
  {
  parts : vec![ Part { text : Some( "Hello! I'm happy to help you today.".to_string() ), inline_data : None, function_call : None, function_response : None, ..Default::default() } ],
    role: "model".to_string(),
  },
  Content
  {
  parts : vec![ Part { text : Some( "Can you explain quantum computing?".to_string() ), inline_data : None, function_call : None, function_response : None, ..Default::default() } ],
    role: "user".to_string(),
  },
  ];

  let request = CountTokensRequest
  {
    contents,
    generate_content_request: None,
  };

  // Now test the actual implementation
  let result = models_api.count_tokens( "gemini-flash-latest", &request ).await;

  match result
  {
    Ok( response ) =>
    {
      assert!( response.total_tokens > 0, "Token count should be positive for conversation" );
    println!( "✅ Conversation token count : {}", response.total_tokens );
    },
    Err( e ) =>
    {
      match e
      {
        Error::AuthenticationError( _ ) =>
        {
        println!( "⚠️  Authentication error (expected without API key): {e}" );
        },
      _ => panic!( "Conversation count tokens failed : {e:?}" ),
      }
    }
  }
}

/// Test token counting with different model types
///
/// This test validates that token counting works across different Gemini models
/// and respects their specific token counting behaviors.
#[ tokio::test ]
async fn test_count_tokens_different_models()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  let content = Content
  {
    parts: vec!
    [
    Part
    {
      text: Some( "This is a test message for token counting across different models.".to_string() ),
      inline_data: None,
      function_call: None,
      function_response: None,
      ..Default::default()
    }
    ],
    role: "user".to_string(),
  };

  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None,
  };

  // Test with multiple models
  let models_to_test = vec![ "gemini-flash-latest", "gemini-flash-latest" ];

  for model in models_to_test
  {
    let result = models_api.count_tokens( model, &request ).await;

    match result
    {
      Ok( response ) =>
      {
      assert!( response.total_tokens > 0, "Token count should be positive for model : {model}" );
    println!( "✅ Model {model} token count : {token_count}", token_count = response.total_tokens );
      },
      Err( e ) =>
      {
        match e
        {
          Error::AuthenticationError( _ ) =>
          {
        println!( "⚠️  Authentication error for {model} (expected without API key): {e}" );
          },
          Error::InvalidArgument( _ ) =>
          {
          println!( "⚠️  Model {model} not available, skipping" );
          },
      _ => panic!( "Count tokens failed for model {model}: {e:?}" ),
        }
      }
    }
  }
}

/// Test error handling for invalid inputs
///
/// This test validates that the `count_tokens` method properly handles various
/// error conditions and returns appropriate error types.
#[ tokio::test ]
async fn test_count_tokens_error_handling()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  // Test 1: Empty content
  let empty_request = CountTokensRequest
  {
    contents: vec![],
    generate_content_request: None,
  };

  // Test 2: Invalid model name
  let content = Content
  {
  parts : vec![ Part { text : Some( "Test content".to_string() ), inline_data : None, function_call : None, function_response : None, ..Default::default() } ],
    role: "user".to_string(),
  };

  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None,
  };

  // Test 1: Empty content should fail
  let result1 = models_api.count_tokens( "gemini-flash-latest", &empty_request ).await;
  assert!( result1.is_err(), "Empty content should result in error" );

  // Test 2: Invalid model name should fail
  let result2 = models_api.count_tokens( "invalid-model-name", &request ).await;

  match result2
  {
    Ok( _ ) => panic!( "Invalid model should result in error" ),
    Err( e ) =>
    {
      match e
      {
        Error::InvalidArgument( _ ) => println!( "✅ Correctly rejected invalid model" ),
        Error::AuthenticationError( _ ) => println!( "⚠️  Authentication error (API key needed to test invalid model)" ),
        Error::ServerError( _ ) => println!( "✅ Server correctly rejected invalid model" ),
      _ => println!( "⚠️  Unexpected error type for invalid model : {e:?}" ),
      }
    }
  }
}

/// Test token counting with generation configuration
///
/// This test validates that token counting can optionally include generation
/// configuration parameters that might affect token usage calculations.
#[ tokio::test ]
async fn test_count_tokens_with_generation_config()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  let content = Content
  {
    parts: vec!
    [
    Part
    {
      text: Some( "Generate a creative story about space exploration.".to_string() ),
      inline_data: None,
      function_call: None,
      function_response: None,
      ..Default::default()
    }
    ],
    role: "user".to_string(),
  };

  // Note : The Gemini API expects model to be specified in URL path, not in generate_content_request
  // So we test basic token counting without generation config since that's not supported in count tokens
  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None, // API doesn't support generation config in count tokens
  };

  // Now test the actual implementation (basic token counting)
  let result = models_api.count_tokens( "gemini-flash-latest", &request ).await;

  match result
  {
    Ok( response ) =>
    {
      assert!( response.total_tokens > 0, "Token count should be positive" );
    println!( "✅ Token count (basic counting): {}", response.total_tokens );
    },
    Err( e ) =>
    {
      match e
      {
        Error::AuthenticationError( _ ) =>
        {
        println!( "⚠️  Authentication error (expected without API key): {e}" );
        },
      _ => panic!( "Count tokens (basic counting) failed : {e:?}" ),
      }
    }
  }
}

/// Test token counting rate limiting behavior
///
/// This test validates that the `count_tokens` method respects rate limits
/// and handles rate limiting errors appropriately.
#[ tokio::test ]
async fn test_count_tokens_rate_limiting()
{
  let Ok(client) = Client::new() else {
    panic!( "API key required for count tokens tests. Set GEMINI_API_KEY environment variable." );
  };

  let models_api = client.models();

  let content = Content
  {
  parts : vec![ Part { text : Some( "Rate limit test content".to_string() ), inline_data : None, function_call : None, function_response : None, ..Default::default() } ],
    role: "user".to_string(),
  };

  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None,
  };

  // Make multiple rapid requests to test rate limiting
  let mut results = Vec::new();

  for i in 0..3
  {
    let result = models_api.count_tokens( "gemini-flash-latest", &request ).await;
    results.push( ( i, result ) );

    // Small delay between requests
    tokio ::time::sleep( core::time::Duration::from_millis( 100 ) ).await;
  }

  // Check results
  for ( i, result ) in results
  {
    match result
    {
      Ok( response ) =>
      {
      assert!( response.total_tokens > 0, "Request {i} should have positive token count" );
    println!( "✅ Request {i} succeeded with {} tokens", response.total_tokens );
      },
      Err( e ) =>
      {
        match e
        {
          Error::RateLimitError( _ ) =>
          {
          println!( "⚠️  Request {i} hit rate limit (expected behavior)" );
          },
          Error::AuthenticationError( _ ) =>
          {
          println!( "⚠️  Authentication error for request {i} (expected without API key)" );
          },
          _ =>
          {
        println!( "⚠️  Request {i} failed with error : {e:?}" );
          }
        }
      }
    }
  }
}

/// Test token counting with authentication errors
///
/// This test validates that `count_tokens` properly handles authentication
/// errors when invalid API keys are provided.
#[ tokio::test ]
async fn test_count_tokens_authentication_error()
{
  // Create client with invalid API key
  let client = ClientBuilder::new()
  .api_key( "invalid_api_key_for_testing".to_string() )
  .build()
  .expect( "Client should build with invalid key" );

  let models_api = client.models();

  let content = Content
  {
  parts : vec![ Part { text : Some( "Test content".to_string() ), inline_data : None, function_call : None, function_response : None, ..Default::default() } ],
    role: "user".to_string(),
  };

  let request = CountTokensRequest
  {
    contents: vec![ content ],
    generate_content_request: None,
  };

  // Test with invalid API key
  let result = models_api.count_tokens( "gemini-flash-latest", &request ).await;

  match result
  {
    Ok( _ ) => panic!( "Invalid API key should result in error" ),
    Err( e ) =>
    {
      match e
      {
        Error::AuthenticationError( _ ) =>
        {
          println!( "✅ Correctly rejected invalid API key" );
        },
        Error::ServerError( _ ) =>
        {
          println!( "✅ Server correctly rejected invalid API key (403/401)" );
        },
      _ => panic!( "Unexpected error type for authentication : {e:?}" ),
      }
    }
  }
}