api_claude 0.4.0

Claude API for accessing Anthropic's 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
//! Comprehensive general diagnostics tests for Anthropic API client
//!
//! MANDATORY INTEGRATION TEST REQUIREMENTS:
//! - These tests validate general diagnostics functionality
//! - Tests MUST fail initially to validate TDD approach
//! - Tests MUST use feature gating for diagnostics functionality
//! - Tests MUST validate request/response lifecycle tracking
//!
//! General diagnostics provide comprehensive monitoring capabilities including
//! request tracking, performance metrics, error analysis, and integration
//! monitoring that complement CURL diagnostics for complete observability.


#[ allow( unused_imports ) ]
use super::*;

#[ cfg( feature = "general-diagnostics" ) ]
#[ allow( unused_imports ) ]
use the_module::*;

#[ cfg( feature = "general-diagnostics" ) ]
mod general_diagnostics_functionality_tests
{
  use super::*;

  /// Test request lifecycle tracking
  #[ test ]
  fn test_request_lifecycle_tracking()
  {
    // This test will fail until RequestTracker is implemented
    use the_module::RequestTracker;

    let mut tracker = RequestTracker::new();
    let request_id = tracker.start_request( "test-request-id" );

    // Validate request lifecycle
    assert!( tracker.is_active( &request_id ), "Request should be active" );
    assert_eq!( tracker.get_status( &request_id ), Some( RequestStatus::InProgress ) );

    // Complete the request
    tracker.complete_request( &request_id, RequestResult::Success );
    assert_eq!( tracker.get_status( &request_id ), Some( RequestStatus::Completed ) );
    assert!( !tracker.is_active( &request_id ), "Request should no longer be active" );
  }

  /// Test performance metrics collection
  #[ test ]
  fn test_performance_metrics_collection()
  {
    // This test will fail until PerformanceMetrics is implemented
    use the_module::PerformanceMetrics;
    use std::time::Duration;

    let mut metrics = PerformanceMetrics::new();

    // Record request metrics
    metrics.record_request_duration( "message_creation", Duration::from_millis( 150 ) );
    metrics.record_request_duration( "message_creation", Duration::from_millis( 200 ) );
    metrics.record_request_duration( "embedding_creation", Duration::from_millis( 75 ) );

    // Validate metrics collection
    let message_stats = metrics.get_operation_stats( "message_creation" );
    assert_eq!( message_stats.count(), 2 );
    assert_eq!( message_stats.average_duration(), Duration::from_millis( 175 ) );

    let embedding_stats = metrics.get_operation_stats( "embedding_creation" );
    assert_eq!( embedding_stats.count(), 1 );
    assert_eq!( embedding_stats.average_duration(), Duration::from_millis( 75 ) );
  }

  /// Test error analysis and categorization
  #[ test ]
  fn test_error_analysis()
  {
    // This test will fail until ErrorAnalyzer is implemented
    use the_module::ErrorAnalyzer;

    let mut analyzer = ErrorAnalyzer::new();

    // Record different types of errors
    analyzer.record_error( "authentication_failed", "Invalid API key", "401" );
    analyzer.record_error( "rate_limited", "Too many requests", "429" );
    analyzer.record_error( "authentication_failed", "Expired token", "401" );

    // Validate error analysis
    let auth_errors = analyzer.get_error_category( "authentication_failed" );
    assert_eq!( auth_errors.count(), 2 );
    assert!( auth_errors.contains_message( "Invalid API key" ) );
    assert!( auth_errors.contains_message( "Expired token" ) );

    let rate_limit_errors = analyzer.get_error_category( "rate_limited" );
    assert_eq!( rate_limit_errors.count(), 1 );

    // Test error trends
    let error_summary = analyzer.get_summary();
    assert_eq!( error_summary.total_errors(), 3 );
    assert_eq!( error_summary.most_common_category(), "authentication_failed" );
  }

  /// Test diagnostics context and correlation
  #[ test ]
  fn test_diagnostics_context()
  {
    // This test will fail until DiagnosticsContext is implemented
    use the_module::DiagnosticsContext;

    let context = DiagnosticsContext::new()
      .request_id( "req-123" )
      .user_id( "user-456" )
      .operation( "create_message" )
      .model( "claude-3-sonnet-20240229" );

    // Validate context construction
    assert_eq!( context.get_request_id(), "req-123" );
    assert_eq!( context.get_user_id(), Some( "user-456" ) );
    assert_eq!( context.get_operation(), "create_message" );
    assert_eq!( context.get_model(), Some( "claude-3-sonnet-20240229" ) );

    // Test context serialization for logging
    let context_json = context.to_json();
    assert!( context_json.contains( "req-123" ) );
    assert!( context_json.contains( "create_message" ) );
  }

  /// Test diagnostics aggregation and reporting
  #[ test ]
  fn test_diagnostics_aggregation()
  {
    // This test will fail until DiagnosticsAggregator is implemented
    use the_module::DiagnosticsAggregator;

    let mut aggregator = DiagnosticsAggregator::new();

    // Add various diagnostic events
    aggregator.record_request_start( "req-1", "create_message" );
    aggregator.record_request_duration( "req-1", std::time::Duration::from_millis( 100 ) );
    aggregator.record_request_success( "req-1" );

    aggregator.record_request_start( "req-2", "create_embedding" );
    aggregator.record_request_error( "req-2", "rate_limited" );

    // Validate aggregation
    let summary = aggregator.get_summary();
    assert_eq!( summary.total_requests(), 2 );
    assert_eq!( summary.successful_requests(), 1 );
    assert_eq!( summary.failed_requests(), 1 );
    assert_eq!( summary.average_duration(), std::time::Duration::from_millis( 100 ) );

    // Test operation-specific metrics
    let message_metrics = aggregator.get_operation_metrics( "create_message" );
    assert_eq!( message_metrics.success_rate(), 1.0 );

    let embedding_metrics = aggregator.get_operation_metrics( "create_embedding" );
    assert_eq!( embedding_metrics.success_rate(), 0.0 );
  }

  /// Test integration with CURL diagnostics
  #[ cfg( feature = "curl-diagnostics" ) ]
  #[ test ]
  fn test_curl_diagnostics_integration()
  {
    // This test will fail until DiagnosticsCollector is implemented
    use the_module::DiagnosticsCollector;

    let collector = DiagnosticsCollector::new();

    let request = the_module::CreateMessageRequest::builder()
      .model( "claude-3-sonnet-20240229" )
      .max_tokens( 1000 )
      .messages( vec![ the_module::Message::user( "Test integration" ) ] )
      .build();

    // Test diagnostics collection with CURL generation
    let diagnostics = collector.collect_for_request( &request );

    assert!( diagnostics.has_curl_representation(), "Should include cURL data" );
    assert!( diagnostics.curl_command().contains( "claude-3-sonnet-20240229" ) );
    assert!( diagnostics.request_size() > 0, "Should calculate request size" );
    assert!( diagnostics.estimated_cost().is_some(), "Should estimate API cost" );
  }

  /// Test diagnostics performance overhead
  #[ test ]
  fn test_diagnostics_performance_overhead()
  {
    use std::time::Instant;
    use the_module::DiagnosticsCollector;

    let collector = DiagnosticsCollector::new();
    let request = the_module::CreateMessageRequest::builder()
      .model( "claude-3-sonnet-20240229" )
      .max_tokens( 1000 )
      .messages( vec![ the_module::Message::user( "Performance test" ) ] )
      .build();

    let start = Instant::now();

    // Measure diagnostics collection overhead
    for _ in 0..1000
    {
      let _diagnostics = collector.collect_for_request( &request );
    }

    let duration = start.elapsed();

    // Performance expectation : 1000 collections should be under 50ms
    assert!( duration.as_millis() < 50, "Diagnostics overhead should be minimal : {}ms", duration.as_millis() );
  }

  /// Test real-time monitoring capabilities
  #[ test ]
  fn test_realtime_monitoring()
  {
    // This test will fail until RealtimeMonitor is implemented
    use the_module::RealtimeMonitor;

    let mut monitor = RealtimeMonitor::new();

    // Register monitoring callbacks
    monitor.on_high_latency( |duration| {
      // This is a mock implementation that doesn't need to modify external state
      let _ = duration;
    });

    // Simulate high latency events
    monitor.record_latency( std::time::Duration::from_millis( 1200 ) );
    monitor.record_latency( std::time::Duration::from_millis( 800 ) );
    monitor.record_latency( std::time::Duration::from_millis( 1500 ) );

    // Process monitoring events
    monitor.process_events();

    // Validate monitoring
    assert_eq!( monitor.get_alert_count(), 2, "Should trigger alerts for high latency" );
    assert!( monitor.get_current_status().is_degraded(), "Should detect degraded performance" );
  }

  /// Test diagnostics data export and serialization
  #[ test ]
  fn test_diagnostics_export()
  {
    // This test will fail until DiagnosticsExporter is implemented
    use the_module::DiagnosticsExporter;

    let exporter = DiagnosticsExporter::new();

    // Add sample diagnostic data
    let mut data = the_module::DiagnosticsData::new();
    data.add_request_metric( "req-1", "create_message", std::time::Duration::from_millis( 150 ), true );
    data.add_request_metric( "req-2", "create_embedding", std::time::Duration::from_millis( 75 ), false );

    // Test different export formats
    let json_export = exporter.to_json( &data );
    assert!( json_export.contains( "create_message" ) );
    assert!( json_export.contains( "150" ) );

    let csv_export = exporter.to_csv( &data );
    assert!( csv_export.contains( "request_id,operation,duration_ms,success" ) );
    assert!( csv_export.contains( "req-1,create_message,150,true" ) );

    let metrics_export = exporter.to_prometheus_metrics( &data );
    assert!( metrics_export.contains( "api_request_duration_ms" ) );
    assert!( metrics_export.contains( "api_request_total" ) );
  }
}

#[ cfg( feature = "general-diagnostics" ) ]
#[ cfg( feature = "integration" ) ]
mod general_diagnostics_integration_tests
{
  use super::*;

  /// Test end-to-end diagnostics collection
  #[ tokio::test ]
  #[ ignore = "Requires workspace secrets file" ]
async fn test_end_to_end_diagnostics()
  {
    use the_module::DiagnosticsCollector;

    let client = the_module::Client::from_workspace()
      .expect( "Must have valid API key for integration test" );

    let collector = DiagnosticsCollector::new();

    let request = the_module::CreateMessageRequest::builder()
      .model( "claude-3-haiku-20240307" )
      .max_tokens( 50 )
      .messages( vec![ the_module::Message::user( "Test diagnostics collection" ) ] )
      .build();

    // Start diagnostics collection
    let collection_id = collector.start_collection( &request );

    // Execute the request
    let response = client.create_message( request ).await;

    // Handle credit exhaustion gracefully for diagnostics testing
    match &response
    {
      Err( the_module::AnthropicError::Api( ref api_err ) ) if api_err.message.contains( "credit balance is too low" ) =>
      {
        println!( "INTEGRATION TEST SKIPPED: Credit balance exhausted - this confirms real API usage" );
        println!( "  Diagnostics test would validate credit exhaustion tracking in production" );
        return;
      },
      _ => {
        // Continue with normal diagnostics testing
      }
    }

    // Complete diagnostics collection
    let diagnostics = collector.complete_collection( collection_id, &response );

    // Validate comprehensive diagnostics
    if response.is_ok()
    {
      assert!( diagnostics.request_succeeded(), "Should track successful request" );
      assert!( diagnostics.response_time() > std::time::Duration::from_millis( 0 ) );
      assert!( diagnostics.tokens_used() > 0, "Should track token usage" );
    } else {
      assert!( diagnostics.request_failed(), "Should track failed request" );
      assert!( diagnostics.error_category().is_some(), "Should categorize error" );
    }

    // Test diagnostics persistence
    assert!( diagnostics.can_export(), "Should be exportable" );
    assert!( diagnostics.curl_equivalent().contains( "claude-3-haiku-20240307" ) );
  }

  /// Test diagnostics under load
  #[ tokio::test ]
  #[ ignore = "Requires workspace secrets file" ]
async fn test_diagnostics_under_load()
  {
    use the_module::{ DiagnosticsCollector, LoadTester };

    let client = the_module::Client::from_workspace()
      .expect( "Must have valid API key for integration test" );

    let collector = DiagnosticsCollector::new();
    let load_tester = LoadTester::new( collector.clone() );

    // Run concurrent requests with diagnostics
    let results = load_tester.run_concurrent_requests( &client, 5, 10 );

    // Validate diagnostics under load
    assert_eq!( results.total_requests(), 50 );
    assert!( results.average_latency() > std::time::Duration::from_millis( 0 ) );
    assert!( results.success_rate() >= 0.8, "Should maintain high success rate under load" );

    // Test diagnostics aggregation accuracy
    let aggregated_diagnostics = collector.get_aggregated_metrics();
    assert_eq!( aggregated_diagnostics.request_count(), 50 );
    assert!( aggregated_diagnostics.has_performance_data(), "Should collect performance data" );
  }

  /// Test diagnostics alerting system
  #[ tokio::test ]
  async fn test_diagnostics_alerting()
  {
    use the_module::{ DiagnosticsCollector, AlertingSystem };

    let collector = DiagnosticsCollector::new();
    let mut alerting = AlertingSystem::new( collector );

    // Configure alerting thresholds
    alerting.set_latency_threshold( std::time::Duration::from_millis( 2000 ) );
    alerting.set_error_rate_threshold( 0.1 ); // 10% error rate

    let client = the_module::Client::new(
      the_module::Secret::new_unchecked( "sk-ant-invalid-key".to_string() )
    );

    // Generate requests that should trigger alerts
    for _ in 0..20
    {
      let request = the_module::CreateMessageRequest::builder()
        .model( "claude-3-haiku-20240307" )
        .max_tokens( 50 )
        .messages( vec![ the_module::Message::user( "Alert test" ) ] )
        .build();

      let _result = client.create_message( request ).await;
    }

    // Process alerting
    alerting.process_alerts();

    // Validate alerting
    let alerts = alerting.get_recent_alerts();
    assert!( !alerts.is_empty(), "Should generate alerts for high error rate" );
    assert!( alerts.iter().any( |a| a.alert_type() == "high_error_rate" ) );
  }
}

#[ cfg( not( feature = "general-diagnostics" ) ) ]
mod general_diagnostics_feature_disabled_tests
{
  /// Test that general diagnostics functionality is properly feature-gated
  #[ test ]
  fn test_general_diagnostics_feature_gated()
  {
    // When general-diagnostics feature is disabled, diagnostics types should not be available
    // This test validates proper feature gating

    // Compilation should succeed without diagnostics types when feature is disabled
    // This serves as a compile-time test for proper feature gating
    assert!( true, "Feature gating working correctly - general diagnostics types not available" );
  }
}