75 lines
2.7 KiB
Rust
75 lines
2.7 KiB
Rust
//! UX Test Assertions
|
|
//!
|
|
//! Custom assertion functions for UX testing
|
|
|
|
use serde_json::Value;
|
|
|
|
/// UX-specific assertions
|
|
pub struct UXAssertions;
|
|
|
|
impl UXAssertions {
|
|
/// Assert ResponseBuilder envelope format
|
|
pub fn assert_response_envelope(response: &Value) -> Result<(), Box<dyn std::error::Error>> {
|
|
if !response.get("success").is_some() {
|
|
return Err("Missing 'success' field in response envelope".into());
|
|
}
|
|
|
|
let success = response["success"].as_bool().unwrap_or(false);
|
|
|
|
if success {
|
|
if !response.get("data").is_some() {
|
|
return Err("Missing 'data' field in successful response".into());
|
|
}
|
|
} else {
|
|
if !response.get("error").is_some() {
|
|
return Err("Missing 'error' field in failed response".into());
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Assert currency format (TFC base currency)
|
|
pub fn assert_currency_format(amount: &Value) -> Result<(), Box<dyn std::error::Error>> {
|
|
if !amount.get("amount").is_some() {
|
|
return Err("Missing 'amount' field in currency object".into());
|
|
}
|
|
|
|
if !amount.get("currency").is_some() {
|
|
return Err("Missing 'currency' field in currency object".into());
|
|
}
|
|
|
|
if !amount.get("formatted").is_some() {
|
|
return Err("Missing 'formatted' field in currency object".into());
|
|
}
|
|
|
|
// Validate currency is one of the supported types
|
|
let currency = amount["currency"].as_str().unwrap_or("");
|
|
match currency {
|
|
"TFC" | "USD" | "EUR" | "CAD" => Ok(()),
|
|
_ => Err(format!("Unsupported currency: {}", currency).into()),
|
|
}
|
|
}
|
|
|
|
/// Assert cart consistency between API and UI
|
|
pub fn assert_cart_consistency(api_count: usize, api_total: f64, ui_count: usize, ui_total: f64) -> Result<(), Box<dyn std::error::Error>> {
|
|
if api_count != ui_count {
|
|
return Err(format!("Cart count mismatch: API shows {}, UI shows {}", api_count, ui_count).into());
|
|
}
|
|
|
|
let total_diff = (api_total - ui_total).abs();
|
|
if total_diff > 0.01 { // Allow for small floating point differences
|
|
return Err(format!("Cart total mismatch: API shows {}, UI shows {}", api_total, ui_total).into());
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Assert page load performance
|
|
pub fn assert_page_load_time(load_time_ms: u64, max_allowed_ms: u64) -> Result<(), Box<dyn std::error::Error>> {
|
|
if load_time_ms > max_allowed_ms {
|
|
return Err(format!("Page load too slow: {}ms > {}ms", load_time_ms, max_allowed_ms).into());
|
|
}
|
|
Ok(())
|
|
}
|
|
} |