#[cfg(feature = "async")]
mod tests {
use weatherkit::async_api::AsyncWeatherService;
use weatherkit::service::CLLocation;
const SAN_JOSE: CLLocation = CLLocation::new(37.3382, -121.8863);
fn is_auth(e: &weatherkit::error::WeatherKitError) -> bool {
e.is_entitlement_issue()
}
#[test]
fn async_weather_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.weather(&SAN_JOSE).await {
Ok(w) => {
assert!(
w.current_weather.temperature.is_finite(),
"temperature should be finite"
);
}
Err(e) if is_auth(&e) => {
eprintln!("async_weather_returns_result: skipped (auth) — {e}");
}
Err(e) => panic!("unexpected error: {e}"),
}
});
}
#[test]
fn async_current_weather_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.current_weather(&SAN_JOSE).await {
Ok(cw) => assert!(cw.temperature.is_finite()),
Err(e) if is_auth(&e) => {
eprintln!("async_current_weather: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_hourly_forecast_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.hourly_forecast(&SAN_JOSE).await {
Ok(hf) => assert!(!hf.forecast.is_empty(), "hourly forecast should be non-empty"),
Err(e) if is_auth(&e) => {
eprintln!("async_hourly_forecast: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_daily_forecast_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.daily_forecast(&SAN_JOSE).await {
Ok(df) => assert!(!df.forecast.is_empty(), "daily forecast should be non-empty"),
Err(e) if is_auth(&e) => {
eprintln!("async_daily_forecast: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_availability_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.availability(&SAN_JOSE).await {
Ok(_av) => { }
Err(e) if is_auth(&e) => {
eprintln!("async_availability: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_attribution_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.attribution().await {
Ok(attr) => assert!(!attr.service_name.is_empty()),
Err(e) if is_auth(&e) => {
eprintln!("async_attribution: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_weather_alerts_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.weather_alerts(&SAN_JOSE).await {
Ok(_alerts) => { }
Err(e) if is_auth(&e) => {
eprintln!("async_weather_alerts: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_minute_forecast_returns_result() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.minute_forecast(&SAN_JOSE).await {
Ok(_mf) => { }
Err(e) if is_auth(&e) => {
eprintln!("async_minute_forecast: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_weather_changes_returns_result_or_os_error() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.weather_changes(&SAN_JOSE).await {
Ok(_wc) => { }
Err(e) if e.message.contains("macOS 15") => {
eprintln!("async_weather_changes: macOS 15 required — skipped");
}
Err(e) if is_auth(&e) => {
eprintln!("async_weather_changes: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_historical_comparisons_returns_result_or_os_error() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
match svc.historical_comparisons(&SAN_JOSE).await {
Ok(_hc) => {}
Err(e) if e.message.contains("macOS 15") => {
eprintln!("async_historical_comparisons: macOS 15 required — skipped");
}
Err(e) if is_auth(&e) => {
eprintln!("async_historical_comparisons: skipped (auth) — {e}");
}
Err(e) => panic!("{e}"),
}
});
}
#[test]
fn async_weather_bad_location_returns_err() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
let bad = CLLocation::new(999.0, 0.0);
let result = svc.weather(&bad).await;
assert!(
result.is_err(),
"bad latitude should produce an error, got: {result:?}"
);
});
}
#[test]
fn async_hourly_forecast_bad_location_returns_err() {
pollster::block_on(async {
let svc = AsyncWeatherService::shared();
let bad = CLLocation::new(0.0, 999.0);
let result = svc.hourly_forecast(&bad).await;
assert!(result.is_err(), "bad longitude should produce an error, got: {result:?}");
});
}
}