use std::sync::Arc;
use tidalrs::{Authz, DeviceType, TidalApiError, TidalClient};
#[test]
fn test_builder_pattern_basic() {
let client = TidalClient::new("test_client_id".to_string());
assert_eq!(client.get_country_code(), "US");
assert_eq!(client.get_locale(), "en_US");
assert_eq!(client.get_device_type(), DeviceType::Browser);
assert_eq!(client.get_user_id(), None);
}
#[test]
fn test_builder_pattern_with_country_code() {
let client = TidalClient::new("test_client_id".to_string()).with_country_code("GB".to_string());
assert_eq!(client.get_country_code(), "GB");
assert_eq!(client.get_locale(), "en_US"); assert_eq!(client.get_device_type(), DeviceType::Browser); }
#[test]
fn test_builder_pattern_with_locale() {
let client = TidalClient::new("test_client_id".to_string()).with_locale("en_GB".to_string());
assert_eq!(client.get_country_code(), "US"); assert_eq!(client.get_locale(), "en_GB");
assert_eq!(client.get_device_type(), DeviceType::Browser); }
#[test]
fn test_builder_pattern_with_device_type() {
let client =
TidalClient::new("test_client_id".to_string()).with_device_type(DeviceType::Browser);
assert_eq!(client.get_country_code(), "US"); assert_eq!(client.get_locale(), "en_US"); assert_eq!(client.get_device_type(), DeviceType::Browser);
}
#[test]
fn test_builder_pattern_with_authz() {
let authz = Authz::new(
"test_access_token".to_string(),
"test_refresh_token".to_string(),
12345,
Some("CA".to_string()),
);
let client = TidalClient::new("test_client_id".to_string()).with_authz(authz.clone());
assert_eq!(client.get_user_id(), Some(12345));
assert_eq!(client.get_country_code(), "CA"); assert_eq!(client.get_locale(), "en_US");
if let Some(stored_authz) = client.get_authz() {
assert_eq!(stored_authz.access_token, "test_access_token");
assert_eq!(stored_authz.refresh_token, "test_refresh_token");
assert_eq!(stored_authz.user_id, 12345);
assert_eq!(stored_authz.country_code, Some("CA".to_string()));
} else {
panic!("Authz should be stored in client");
}
}
#[test]
fn test_builder_pattern_with_authz_refresh_callback() {
let callback_called = Arc::new(std::sync::atomic::AtomicBool::new(false));
let callback_called_clone = callback_called.clone();
let client =
TidalClient::new("test_client_id".to_string()).with_authz_refresh_callback(move |_authz| {
callback_called_clone.store(true, std::sync::atomic::Ordering::Relaxed);
});
assert_eq!(client.get_country_code(), "US");
assert_eq!(client.get_locale(), "en_US");
assert_eq!(client.get_device_type(), DeviceType::Browser);
}
#[test]
fn test_builder_pattern_chaining() {
let authz = Authz::new(
"test_access_token".to_string(),
"test_refresh_token".to_string(),
67890,
Some("AU".to_string()),
);
let callback_called = Arc::new(std::sync::atomic::AtomicBool::new(false));
let callback_called_clone = callback_called.clone();
let client = TidalClient::new("test_client_id".to_string())
.with_authz(authz)
.with_country_code("DE".to_string())
.with_locale("de_DE".to_string())
.with_device_type(DeviceType::Browser)
.with_authz_refresh_callback(move |_authz| {
callback_called_clone.store(true, std::sync::atomic::Ordering::Relaxed);
});
assert_eq!(client.get_user_id(), Some(67890));
assert_eq!(client.get_country_code(), "DE"); assert_eq!(client.get_locale(), "de_DE");
assert_eq!(client.get_device_type(), DeviceType::Browser);
if let Some(stored_authz) = client.get_authz() {
assert_eq!(stored_authz.user_id, 67890);
assert_eq!(stored_authz.country_code, Some("AU".to_string()));
} else {
panic!("Authz should be stored in client");
}
}
#[test]
fn test_builder_pattern_country_code_priority() {
let authz = Authz::new(
"test_access_token".to_string(),
"test_refresh_token".to_string(),
11111,
Some("FR".to_string()),
);
let client = TidalClient::new("test_client_id".to_string())
.with_authz(authz)
.with_country_code("JP".to_string());
assert_eq!(client.get_country_code(), "JP"); }
#[test]
fn test_builder_pattern_country_code_fallback() {
let authz = Authz::new(
"test_access_token".to_string(),
"test_refresh_token".to_string(),
22222,
Some("IT".to_string()),
);
let client = TidalClient::new("test_client_id".to_string()).with_authz(authz);
assert_eq!(client.get_country_code(), "IT"); }
#[test]
fn test_builder_pattern_country_code_final_fallback() {
let authz = Authz::new(
"test_access_token".to_string(),
"test_refresh_token".to_string(),
33333,
None, );
let client = TidalClient::new("test_client_id".to_string()).with_authz(authz);
assert_eq!(client.get_country_code(), "US"); }
#[test]
fn test_builder_pattern_with_client() {
let custom_client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()
.unwrap();
let client = TidalClient::new("test_client_id".to_string()).with_client(custom_client);
assert_eq!(client.get_country_code(), "US");
assert_eq!(client.get_locale(), "en_US");
assert_eq!(client.get_device_type(), DeviceType::Browser);
}
#[test]
fn test_tidal_api_error_deserialization_snake_case() {
let json = r#"{
"status": 400,
"sub_status": 1001,
"user_message": "Invalid request"
}"#;
let error: TidalApiError = serde_json::from_str(json).unwrap();
assert_eq!(error.status, 400);
assert_eq!(error.sub_status, 1001);
assert_eq!(error.user_message, "Invalid request");
}
#[test]
fn test_tidal_api_error_deserialization_camel_case() {
let json = r#"{
"status": 401,
"subStatus": 2001,
"userMessage": "Unauthorized access"
}"#;
let error: TidalApiError = serde_json::from_str(json).unwrap();
assert_eq!(error.status, 401);
assert_eq!(error.sub_status, 2001);
assert_eq!(error.user_message, "Unauthorized access");
}
#[test]
fn test_tidal_api_error_deserialization_mixed_case() {
let json = r#"{
"status": 403,
"sub_status": 3001,
"userMessage": "Forbidden access"
}"#;
let error: TidalApiError = serde_json::from_str(json).unwrap();
assert_eq!(error.status, 403);
assert_eq!(error.sub_status, 3001);
assert_eq!(error.user_message, "Forbidden access");
}
#[test]
fn test_tidal_api_error_deserialization_missing_user_message() {
let json = r#"{
"status": 500,
"sub_status": 4001
}"#;
let error: TidalApiError = serde_json::from_str(json).unwrap();
assert_eq!(error.status, 500);
assert_eq!(error.sub_status, 4001);
assert_eq!(error.user_message, "");
}