#[ allow( unused_imports ) ]
use super::*;
#[ tokio::test ]
async fn test_secret_validation_format()
{
let invalid_keys = vec![
"",
"invalid-key",
"sk-wrong-prefix",
"ant-missing-sk",
];
for key in invalid_keys
{
let result = the_module::Secret::new( key.to_string() );
assert!( result.is_err(), "Expected key {key} to be invalid" );
}
}
#[ tokio::test ]
async fn test_error_handling_types()
{
let http_error = the_module::AnthropicError::http_error( "Connection failed".to_string() );
let auth_error = the_module::AnthropicError::Authentication(
the_module::AuthenticationError::new( "Invalid API key".to_string() )
);
let invalid_arg_error = the_module::AnthropicError::InvalidArgument( "Missing parameter".to_string() );
assert!( http_error.to_string().contains( "Connection failed" ) );
assert!( auth_error.to_string().contains( "Invalid API key" ) );
assert!( invalid_arg_error.to_string().contains( "Missing parameter" ) );
}
#[ tokio::test ]
async fn test_client_from_workspace_method_exists()
{
let result = the_module::Client::from_workspace();
match result
{
Ok( _client ) => {}, Err( err ) =>
{
assert!( !err.to_string().is_empty() );
}
}
}
#[ tokio::test ]
async fn test_client_from_missing_environment_variable()
{
std::env::remove_var( "ANTHROPIC_API_KEY" );
let result = the_module::Client::from_env();
assert!( result.is_err() );
}
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
async fn integration_client_real_api_lifecycle()
{
let client = the_module::Client::from_workspace()
.expect( "INTEGRATION: Must have valid API key for client lifecycle testing" );
assert!( !client.secret().ANTHROPIC_API_KEY.is_empty() );
assert_eq!( client.base_url(), "https://api.anthropic.com" );
let request = the_module::CreateMessageRequest
{
model : "claude-3-5-haiku-20241022".to_string(),
max_tokens : 5,
messages : vec![ the_module::Message::user( "Hi".to_string() ) ],
system : None,
temperature : None,
stream : None,
tools : None,
tool_choice : None,
};
let response = match client.create_message( request ).await
{
Ok( response ) => 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" );
return;
},
Err( err ) => panic!( "INTEGRATION: Client must successfully make API call : {err}" ),
};
assert!( !response.id.is_empty(), "Real API response must have ID" );
assert_eq!( response.r#type, "message" );
assert_eq!( response.role, "assistant" );
assert!( !response.content.is_empty(), "Real API response must have content" );
println!( "✅ Client lifecycle integration test passed!" );
println!( " Client base URL: {}", client.base_url() );
println!( " Response ID: {}", response.id );
}
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
async fn integration_client_concurrent_requests()
{
let client = the_module::Client::from_workspace()
.expect( "INTEGRATION: Must have valid API key for concurrent testing" );
let request1 = the_module::CreateMessageRequest
{
model : "claude-3-5-haiku-20241022".to_string(),
max_tokens : 5,
messages : vec![ the_module::Message::user( "Test 1".to_string() ) ],
system : None,
temperature : None,
stream : None,
tools : None,
tool_choice : None,
};
let request2 = the_module::CreateMessageRequest
{
model : "claude-3-5-haiku-20241022".to_string(),
max_tokens : 5,
messages : vec![ the_module::Message::user( "Test 2".to_string() ) ],
system : None,
temperature : None,
stream : None,
tools : None,
tool_choice : None,
};
let (response1, response2) = tokio::join!(
client.create_message( request1 ),
client.create_message( request2 )
);
let response1 = match response1
{
Ok( response ) => 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" );
return;
},
Err( err ) => panic!( "INTEGRATION: First concurrent request must succeed : {err}" ),
};
let response2 = match response2
{
Ok( response ) => 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" );
return;
},
Err( err ) => panic!( "INTEGRATION: Second concurrent request must succeed : {err}" ),
};
assert!( !response1.id.is_empty() );
assert!( !response2.id.is_empty() );
assert_ne!( response1.id, response2.id, "Concurrent requests must have unique IDs" );
println!( "✅ Client concurrent requests integration test passed!" );
println!( " Response 1 ID: {}", response1.id );
println!( " Response 2 ID: {}", response2.id );
}