#[cfg(target_os = "linux")]
use ngdp_cache::ribbit::RibbitCache;
#[cfg(target_os = "linux")]
use serial_test::serial;
#[cfg(target_os = "linux")]
use tempfile::TempDir;
#[cfg(target_os = "linux")]
#[tokio::test]
#[serial]
async fn test_ribbit_cache_directory_structure() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let unique_cache_dir = temp_dir.path().join("ribbit_dir_test");
unsafe {
std::env::set_var("XDG_CACHE_HOME", &unique_cache_dir);
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
let cache = RibbitCache::new().await?;
let regions = vec!["us", "eu", "kr", "cn"];
for region in ®ions {
cache.write(region, "summary", "test", b"test data").await?;
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
let cache_base = unique_cache_dir.join("ngdp").join("ribbit");
if !cache_base.exists() {
eprintln!("Cache base directory does not exist: {:?}", cache_base);
eprintln!(
"Contents of unique_cache_dir: {:?}",
std::fs::read_dir(&unique_cache_dir)
);
if let Ok(entries) = std::fs::read_dir(&unique_cache_dir) {
for entry in entries.flatten() {
eprintln!(" - {:?}", entry.path());
}
}
}
assert!(cache_base.exists(), "Cache base directory should exist");
for region in ®ions {
let region_dir = cache_base.join(region);
assert!(
region_dir.exists(),
"{} region directory should exist",
region.to_uppercase()
);
}
let incorrect_path = cache_base.join("cached");
assert!(
!incorrect_path.exists(),
"Incorrect 'cached' subdirectory should not exist"
);
unsafe {
std::env::remove_var("XDG_CACHE_HOME");
}
Ok(())
}
#[cfg(target_os = "linux")]
#[tokio::test]
#[serial]
async fn test_ribbit_cache_file_naming() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let unique_cache_dir = temp_dir.path().join("ribbit_file_test");
unsafe {
std::env::set_var("XDG_CACHE_HOME", &unique_cache_dir);
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
let cache = RibbitCache::new().await?;
let test_cases = vec![
("summary", "test", "summary data"),
("versions", "wow", "version data"),
("cdns", "wow", "cdn data"),
(
"certs",
"5168ff90af0207753cccd9656462a212b859723b",
"cert data",
),
];
for (endpoint, product, data) in &test_cases {
cache
.write("us", endpoint, product, data.as_bytes())
.await?;
}
let cache_dir = unique_cache_dir.join("ngdp").join("ribbit").join("us");
assert!(
cache_dir.join("summary").join("test").exists(),
"Summary cache file should exist"
);
assert!(
cache_dir.join("versions").join("wow").exists(),
"Versions cache file should exist"
);
assert!(
cache_dir.join("cdns").join("wow").exists(),
"CDNs cache file should exist"
);
assert!(
cache_dir
.join("certs")
.join("5168ff90af0207753cccd9656462a212b859723b")
.exists(),
"Certificate cache file should exist"
);
unsafe {
std::env::remove_var("XDG_CACHE_HOME");
}
Ok(())
}