#[ allow( unused_imports ) ]
use super::*;
#[ test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
fn test_workspace_token_loading_and_format_validation()
{
println!( "đ Token Loading & Format Validation Test" );
println!( "=========================================" );
println!( "đ Testing workspace token loading..." );
let secret = the_module::Secret::from_workspace()
.expect( "INTEGRATION FAILURE: Must have valid workspace secret in ../../secret/-secrets.sh" );
let api_key = &secret.ANTHROPIC_API_KEY;
println!( "đ Validating token format..." );
assert!( !api_key.is_empty(), "INTEGRATION FAILURE: API key cannot be empty" );
assert!( api_key.starts_with( "sk-ant-" ),
"INTEGRATION FAILURE: Invalid API key format. Expected sk-ant-*, got : {}...",
&api_key[..core::cmp::min( 10, api_key.len() )] );
assert!( api_key.len() >= 50,
"INTEGRATION FAILURE: API key too short. Expected >=50 chars, got : {}", api_key.len() );
println!( "â
Token format validation passed:" );
println!( " Format : â
Valid Anthropic format (sk-ant-...)" );
println!( " Length : â
{} characters", api_key.len() );
println!( " Prefix : {}", &api_key[..15] );
println!( " Source : â
Workspace secrets (../../secret/-secrets.sh)" );
println!( "\nđī¸ Testing client creation..." );
let client = the_module::Client::new( secret.clone() );
assert_eq!( client.secret().ANTHROPIC_API_KEY, *api_key );
assert_eq!( client.base_url(), "https://api.anthropic.com" );
println!( "â
Client creation passed:" );
println!( " Base URL: {}", client.base_url() );
println!( " Token match : â
Client token matches workspace token" );
println!( "\nđĸ Testing Client::from_workspace()..." );
let workspace_client = the_module::Client::from_workspace()
.expect( "INTEGRATION FAILURE: Client::from_workspace() must succeed when Secret::from_workspace() succeeds" );
assert_eq!( workspace_client.secret().ANTHROPIC_API_KEY, *api_key );
assert_eq!( workspace_client.base_url(), client.base_url() );
println!( "â
Workspace client creation passed:" );
println!( " Token consistency : â
Both methods load same token" );
println!( " Configuration match : â
Same base URL and settings" );
println!( "\nđ TOKEN VALIDATION: â
ALL TESTS PASSED" );
println!( "=========================================" );
}
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
async fn test_live_token_authentication_verification()
{
println!( "\nđ Live Token Authentication Verification" );
println!( "=========================================" );
let client = the_module::Client::from_workspace()
.expect( "INTEGRATION FAILURE: Must have workspace client for authentication test" );
let api_key = &client.secret().ANTHROPIC_API_KEY;
println!( "đ Testing authentication with token : {}...{}",
&api_key[..12], &api_key[api_key.len()-8..] );
let request = the_module::CreateMessageRequest
{
model : "claude-3-5-haiku-20241022".to_string(),
max_tokens : 10,
messages : vec![ the_module::Message::user( "Auth test".to_string() ) ],
system : None,
temperature : Some( 0.0 ), stream : None,
tools : None,
tool_choice : None,
};
let auth_start = std::time::Instant::now();
println!( "đ Making authenticated API call..." );
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 FAILURE: Authentication must succeed with valid token : {err}" ),
};
let auth_duration = auth_start.elapsed();
assert!( !response.id.is_empty(), "INTEGRATION FAILURE: Real API response must have message ID" );
assert!( response.id.starts_with( "msg_" ), "INTEGRATION FAILURE: Invalid message ID format : {}", response.id );
assert_eq!( response.r#type, "message", "INTEGRATION FAILURE: Response type must be 'message'" );
assert_eq!( response.role, "assistant", "INTEGRATION FAILURE: Response role must be 'assistant'" );
assert_eq!( response.model, "claude-3-5-haiku-20241022", "INTEGRATION FAILURE: Model mismatch" );
assert!( !response.content.is_empty(), "INTEGRATION FAILURE: Response must have content" );
assert_eq!( response.content[0].r#type, "text", "INTEGRATION FAILURE: Content type must be 'text'" );
assert!( response.usage.input_tokens > 0, "INTEGRATION FAILURE: Must track input tokens" );
assert!( response.usage.output_tokens > 0, "INTEGRATION FAILURE: Must track output tokens" );
println!( "â
Authentication verification successful:" );
println!( " Response ID: {}", response.id );
println!( " Response time : {auth_duration:?}" );
println!( " Model : {}", response.model );
println!( " Token usage : {} in, {} out", response.usage.input_tokens, response.usage.output_tokens );
if let Some( text ) = &response.content[0].text
{
println!( " Content preview : {}", &text[..core::cmp::min( 50, text.len() )] );
}
println!( "\nđ Testing authentication error handling..." );
let invalid_secret = the_module::Secret::new( "sk-ant-invalid-key-for-testing".to_string() )
.expect( "Invalid secret should construct but fail on API call" );
let invalid_client = the_module::Client::new( invalid_secret );
let invalid_request = the_module::CreateMessageRequest
{
model : "claude-3-5-haiku-20241022".to_string(),
max_tokens : 5,
messages : vec![ the_module::Message::user( "Test".to_string() ) ],
system : None,
temperature : None,
stream : None,
tools : None,
tool_choice : None,
};
let auth_error_result = invalid_client.create_message( invalid_request ).await;
assert!( auth_error_result.is_err(), "INTEGRATION FAILURE: Invalid token should cause authentication error" );
let error = auth_error_result.unwrap_err();
let error_str = error.to_string().to_lowercase();
assert!( error_str.contains( "authentication" ) || error_str.contains( "unauthorized" ) || error_str.contains( "invalid" ),
"INTEGRATION FAILURE: Authentication error should mention auth issue, got : {error}" );
println!( "â
Authentication error handling verified:" );
println!( " Invalid token properly rejected : {error}" );
println!( "\nđ AUTHENTICATION VERIFICATION: â
FULLY WORKING" );
println!( "================================================" );
println!( "â
Token loading : WORKING" );
println!( "â
Token format : VALID" );
println!( "â
API authentication : WORKING" );
println!( "â
Error handling : WORKING" );
println!( "â
Response validation : WORKING" );
}
#[ test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
fn test_token_security_and_workspace_integration()
{
println!( "\nđĄī¸ Token Security & Workspace Integration Test" );
println!( "===============================================" );
let secret_result = the_module::Secret::from_workspace();
assert!( secret_result.is_ok(), "INTEGRATION FAILURE: Workspace secret loading must work" );
let secret = secret_result.unwrap();
let api_key = &secret.ANTHROPIC_API_KEY;
println!( "đ Verifying token security properties..." );
assert!( api_key.len() >= 100, "INTEGRATION FAILURE: Token should be long enough for security" );
assert!( api_key.chars().all( |c| c.is_ascii_alphanumeric() || c == '-' || c == '_' ),
"INTEGRATION FAILURE: Token should only contain safe characters" );
assert!( !api_key.contains( "placeholder" ), "INTEGRATION FAILURE: Token appears to be placeholder" );
assert!( !api_key.contains( "example" ), "INTEGRATION FAILURE: Token appears to be example value" );
assert!( !api_key.contains( "test" ), "INTEGRATION FAILURE: Token appears to be test value" );
println!( "â
Token security validation passed:" );
println!( " Length : â
{} characters (secure length)", api_key.len() );
println!( " Characters : â
Safe ASCII characters only" );
println!( " Authenticity : â
Real API token (not placeholder/test)" );
println!( "\nđ Testing environment variable integration..." );
match std::env::var( "ANTHROPIC_API_KEY" )
{
Ok( env_key ) =>
{
println!( "âšī¸ ANTHROPIC_API_KEY found in environment" );
println!( " Length : {} characters", env_key.len() );
println!( " Matches workspace : {}", if env_key == *api_key { "â
Yes" } else { "â ī¸ No (using workspace)" } );
},
Err( _ ) =>
{
println!( "âšī¸ No ANTHROPIC_API_KEY in environment (expected - using workspace secrets)" );
}
}
println!( "\nđ SECURITY & INTEGRATION: â
VERIFIED" );
println!( "====================================" );
}