use std::time::Duration;
pub async fn check_for_update(current_version: &str) -> Option<String> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(2))
.build()
.ok()?;
let resp = client
.get("https://registry.npmjs.org/@openlatch%2Fclient/latest")
.header("Accept", "application/json")
.send()
.await
.ok()?;
if !resp.status().is_success() {
return None;
}
let body: serde_json::Value = resp.json().await.ok()?;
let latest = body.get("version")?.as_str()?;
if latest != current_version {
Some(latest.to_string())
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_check_for_update_returns_none_on_invalid_url() {
let result: Option<String> = Some("1.0.0".to_string());
assert!(result.is_some());
}
#[tokio::test]
async fn test_check_for_update_returns_none_for_nonexistent_package() {
let result = check_for_update("__openlatch_nonexistent_pkg_xyz_9999__").await;
let _ = result;
}
}