#![ allow( unused_imports, dead_code ) ]
#![ allow( clippy::unreadable_literal ) ]
#![ allow( clippy::uninlined_format_args ) ]
#![ allow( clippy::std_instead_of_core ) ]
#![ allow( clippy::useless_vec ) ]
#![ allow( clippy::unused_async ) ]
#![ allow( clippy::must_use_candidate ) ]
#![ allow( clippy::missing_panics_doc ) ]
#![ allow( clippy::missing_errors_doc ) ]
#![ allow( clippy::doc_markdown ) ]
#![ allow( clippy::assertions_on_constants ) ]
use api_openai::ClientApiAccessors;
use api_openai::
{
Client,
error ::OpenAIError,
environment ::{ OpenaiEnvironment, OpenaiEnvironmentImpl, EnvironmentInterface },
secret ::Secret,
sync ::{ SyncClient, SyncEmbeddings, SyncChat, SyncModels },
};
use api_openai::components::
{
chat_shared ::{ ChatCompletionRequest, ChatCompletionRequestMessage, ChatCompletionRequestMessageContent },
embeddings_request ::CreateEmbeddingRequest,
};
use std::
{
sync ::{ Arc, atomic::{ AtomicU32, AtomicU64, Ordering }, Mutex },
time ::{ Duration, Instant },
thread,
};
use tokio::runtime::{ Runtime, Handle };
#[ derive( Debug, Default ) ]
struct SyncPerformanceMetrics
{
async_duration : Duration,
sync_duration : Duration,
overhead_ratio : f64,
}
#[ test ]
fn test_sync_client_creation()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env_result = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() );
assert!( env_result.is_ok() );
let env = env_result.unwrap();
let sync_client = SyncClient::new( env );
assert!( sync_client.is_ok() );
}
#[ test ]
fn test_sync_client_with_external_runtime()
{
let runtime = Arc::new( Runtime::new().unwrap() );
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env_result = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() );
assert!( env_result.is_ok() );
let env = env_result.unwrap();
let sync_client = SyncClient::with_runtime( env, runtime );
assert!( sync_client.is_ok() );
}
#[ test ]
fn test_sync_embeddings_api()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_embeddings = sync_client.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input".to_string(),
"text-embedding-ada-002".to_string()
);
let result = sync_embeddings.create( request );
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
}
#[ test ]
fn test_sync_chat_api()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_chat = sync_client.chat();
let message = ChatCompletionRequestMessage
{
role : "user".to_string(),
content : Some( ChatCompletionRequestMessageContent::Text( "Say 'test' and nothing else".to_string() ) ),
name : None,
tool_calls : None,
tool_call_id : None,
};
let request = ChatCompletionRequest
{
model : "gpt-5-nano".to_string(),
messages : vec![ message ],
temperature : None,
top_p : Some( 1.0 ),
max_tokens : None,
n : Some( 1 ),
stop : None,
stream : Some( false ),
system_prompt : None,
user : None,
tools : None,
tool_choice : None,
response_format : None,
seed : None,
logit_bias : None,
logprobs : None,
top_logprobs : None,
};
let result = sync_chat.create( request );
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
}
#[ test ]
fn test_sync_models_api()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_models = sync_client.models();
let result = sync_models.list();
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
}
#[ test ]
fn test_sync_api_thread_safety()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = Arc::new( SyncClient::new( env ).expect( "Sync client should be created" ) );
let request_count = Arc::new( AtomicU32::new( 0 ) );
let mut handles = Vec::new();
for _ in 0..5
{
let client = sync_client.clone();
let counter = request_count.clone();
let handle = thread::spawn( move || {
let sync_embeddings = client.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input".to_string(),
"text-embedding-ada-002".to_string()
);
let result = sync_embeddings.create( request );
if result.is_ok()
{
counter.fetch_add( 1, Ordering::Relaxed );
}
});
handles.push( handle );
}
for handle in handles
{
handle.join().unwrap();
}
let completed_requests = request_count.load( Ordering::Relaxed );
assert!( completed_requests <= 5 ); }
#[ test ]
fn test_sync_api_runtime_management()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
{
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_embeddings = sync_client.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input".to_string(),
"text-embedding-ada-002".to_string()
);
let result = sync_embeddings.create( request );
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
}
assert!( true );
}
#[ test ]
fn test_sync_api_error_handling()
{
let secret = Secret::new( "invalid-key".to_string() );
let env_result = if let Ok( secret ) = secret { OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ) } else { Err( api_openai::error::OpenAIError::InvalidArgument( "Invalid secret".to_string() ).into() ) };
assert!( env_result.is_err() );
}
#[ test ]
fn test_sync_api_timeout_behavior()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build(
secret,
None,
None,
api_openai ::environment::OpenAIRecommended::base_url().to_string(),
api_openai ::environment::OpenAIRecommended::realtime_base_url().to_string()
).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_embeddings = sync_client.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input".to_string(),
"text-embedding-ada-002".to_string()
);
let start = Instant::now();
let result = sync_embeddings.create( request );
let duration = start.elapsed();
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
assert!( duration < Duration::from_secs( 30 ), "Request took too long : {:?}", duration );
}
#[ test ]
fn test_sync_api_performance_overhead()
{
let secret1 = Secret::new( "sk-test1234567890123456789012345678901234567890123456".to_string() ).unwrap();
let secret2 = Secret::new( "sk-test1234567890123456789012345678901234567890123456".to_string() ).unwrap();
let env1 = OpenaiEnvironmentImpl::build( secret1, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let env2 = OpenaiEnvironmentImpl::build( secret2, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let _async_client = Client::build( env1 ).unwrap();
let sync_client = SyncClient::new( env2 ).expect( "Sync client should be created" );
let iterations = 10;
let mut async_total = Duration::new( 0, 0 );
let mut sync_total = Duration::new( 0, 0 );
let runtime = Runtime::new().unwrap();
for _ in 0..iterations
{
let start = Instant::now();
runtime.block_on( async {
tokio ::time::sleep( Duration::from_micros( 100 ) ).await;
});
async_total += start.elapsed();
}
for _ in 0..iterations
{
let start = Instant::now();
let sync_embeddings = sync_client.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input".to_string(),
"text-embedding-ada-002".to_string()
);
let _ = sync_embeddings.create( request );
sync_total += start.elapsed();
}
let async_avg = async_total / iterations;
let sync_avg = sync_total / iterations;
let overhead_ratio = sync_avg.as_nanos() as f64 / async_avg.as_nanos() as f64;
assert!( overhead_ratio > 0.0, "Overhead ratio should be positive : {}x", overhead_ratio );
assert!( sync_avg < Duration::from_secs( 5 ), "Sync operations should complete in reasonable time" );
}
#[ test ]
fn test_sync_api_integration_with_rate_limiting()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_embeddings = sync_client.embeddings();
for i in 0..3
{
let request = CreateEmbeddingRequest::new_single(
format!( "test input {}", i ),
"text-embedding-ada-002".to_string()
);
let result = sync_embeddings.create( request );
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
}
}
#[ test ]
fn test_sync_api_integration_with_caching()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_models = sync_client.models();
let result1 = sync_models.list();
let result2 = sync_models.list();
match ( &result1, &result2 )
{
( Ok( response1 ), Ok( response2 ) ) =>
{
assert_eq!( response1.data.len(), response2.data.len(), "Cached response should have same model count" );
assert_eq!( response1.object, response2.object, "Response object type should match" );
println!( "✅ Cache consistency verified" );
},
( Err( _error1 ), Err( _error2 ) ) =>
{
println!( "⚠️ Both API calls failed - skipping cache verification" );
},
( Ok( _response ), Err( error ) ) | ( Err( error ), Ok( _response ) ) =>
{
println!( "⚠️ API calls had different outcomes (transient network issue) - test passed but cache consistency not verified : {error:?}" );
},
}
}
#[ test ]
fn test_sync_api_integration_with_retry_logic()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
let sync_embeddings = sync_client.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input".to_string(),
"text-embedding-ada-002".to_string()
);
let result = sync_embeddings.create( request );
match result
{
Ok( _response ) => assert!( true ), Err( error ) =>
{
panic!( "Real API call failed : {error:?}" );
},
}
}
#[ test ]
fn test_sync_api_memory_efficiency()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let mut clients = Vec::new();
for _ in 0..10
{
let env = OpenaiEnvironmentImpl::build( secret.clone(), None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client = SyncClient::new( env ).expect( "Sync client should be created" );
clients.push( sync_client );
}
assert_eq!( clients.len(), 10 );
drop( clients );
assert!( true );
}
#[ test ]
fn test_sync_api_builder_pattern_compatibility()
{
let secret1 = Secret::new( "sk-test1234567890123456789012345678901234567890123456".to_string() ).unwrap();
let secret2 = Secret::new( "sk-test1234567890123456789012345678901234567890123456".to_string() ).unwrap();
let env1 = OpenaiEnvironmentImpl::build( secret1, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let env2 = OpenaiEnvironmentImpl::build( secret2, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let async_client = Client::build( env1 );
let sync_client = SyncClient::new( env2 );
assert!( async_client.is_ok() );
assert!( sync_client.is_ok() );
}
#[ test ]
fn test_sync_api_concurrent_runtime_usage()
{
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY").expect("Real API credentials required");
let runtime1 = Arc::new( Runtime::new().unwrap() );
let runtime2 = Arc::new( Runtime::new().unwrap() );
let env1 = OpenaiEnvironmentImpl::build( secret.clone(), None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let env2 = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() ).unwrap();
let sync_client1 = SyncClient::with_runtime( env1, runtime1 ).expect( "Sync client should be created" );
let sync_client2 = SyncClient::with_runtime( env2, runtime2 ).expect( "Sync client should be created" );
let results = Arc::new( Mutex::new( Vec::new() ) );
let results1 = results.clone();
let results2 = results.clone();
let handle1 = thread::spawn( move || {
let sync_embeddings = sync_client1.embeddings();
let request = CreateEmbeddingRequest::new_single(
"test input 1".to_string(),
"text-embedding-ada-002".to_string()
);
let result = sync_embeddings.create( request );
results1.lock().unwrap().push( result.is_ok() );
});
let handle2 = thread::spawn( move || {
let sync_models = sync_client2.models();
let result = sync_models.list();
results2.lock().unwrap().push( result.is_ok() );
});
handle1.join().unwrap();
handle2.join().unwrap();
let final_results = results.lock().unwrap();
assert_eq!( final_results.len(), 2 );
assert!( true ); }