#![ cfg( feature = "enabled" ) ]
use api_openai_compatible::{ OpenAiCompatEnvironment, OpenAiCompatEnvironmentImpl };
use core::time::Duration;
#[ test ]
fn new_has_default_base_url_and_timeout()
{
let env = OpenAiCompatEnvironmentImpl::new( "sk-test-key" )
.expect( "new() must succeed with a non-empty key" );
assert_eq!(
env.base_url(),
"https://api.openai.com/v1/",
"default base URL must be the OpenAI v1 endpoint with trailing slash",
);
assert_eq!(
env.timeout().as_secs(),
30,
"default timeout must be 30 seconds",
);
}
#[ test ]
fn new_fails_with_empty_key()
{
let result = OpenAiCompatEnvironmentImpl::new( "" );
assert!(
result.is_err(),
"new() must return Err when given an empty API key, not Ok",
);
}
#[ test ]
fn with_base_url_overrides()
{
let custom = "https://api.kie.ai/my-model/v1/";
let env = OpenAiCompatEnvironmentImpl::new( "sk-test-key" )
.expect( "new() must succeed" )
.with_base_url( custom );
assert_eq!(
env.base_url(),
custom,
"base_url() must reflect the value passed to with_base_url()",
);
}
#[ test ]
fn with_timeout_overrides()
{
let custom = Duration::from_secs( 120 );
let env = OpenAiCompatEnvironmentImpl::new( "sk-test-key" )
.expect( "new() must succeed" )
.with_timeout( custom );
assert_eq!(
env.timeout(),
custom,
"timeout() must reflect the value passed to with_timeout()",
);
}
#[ test ]
fn headers_returns_bearer_and_content_type()
{
let key = "sk-my-secret-key-12345";
let env = OpenAiCompatEnvironmentImpl::new( key )
.expect( "new() must succeed" );
let headers = env.headers().expect( "headers() must succeed with a valid environment" );
let auth = headers
.get( "Authorization" )
.expect( "Authorization header must be present" )
.to_str()
.expect( "Authorization value must be valid UTF-8" );
assert!(
auth.starts_with( "Bearer " ),
"Authorization header must use the Bearer scheme; got: {auth}",
);
assert!(
auth.contains( key ),
"Authorization header must contain the API key; got: {auth}",
);
let ct = headers
.get( "Content-Type" )
.expect( "Content-Type header must be present" )
.to_str()
.expect( "Content-Type value must be valid UTF-8" );
assert_eq!(
ct,
"application/json",
"Content-Type must be application/json",
);
}
#[ test ]
fn new_fails_with_whitespace_only_key()
{
let result = OpenAiCompatEnvironmentImpl::new( " " );
assert!(
result.is_err(),
"new() must return Err for a whitespace-only API key, not Ok",
);
}
#[ test ]
fn new_succeeds_with_key_containing_printable_special_chars()
{
let key = "sk-test_key-ABCD1234.special+chars";
let result = OpenAiCompatEnvironmentImpl::new( key );
assert!(
result.is_ok(),
"new() must succeed with a key containing printable ASCII special characters; got: {:?}",
result.err(),
);
let env = result.unwrap();
assert_eq!(
env.api_key(),
key,
"api_key() must return the exact key passed to new()",
);
}