zilliz 1.4.2

TUI and CLI tool for managing Zilliz Cloud clusters and Milvus operations
Documentation
use chrono::{Duration, Utc};
use tempfile::TempDir;
use zilliz::update_check::{load_cache, test_support, write_cache, UpdateCache};

/// Scope `ZILLIZ_CONFIG_DIR` to a tempdir for the duration of one test. Tests
/// in this file run serially (cargo's default for integration tests in the
/// same crate already serializes per-binary by default — each `tests/*.rs` is
/// its own binary, so cases inside this file share state via the env var).
/// To avoid cross-talk, we guard with a mutex.
fn with_tempdir<F: FnOnce(&TempDir)>(f: F) {
    static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
    let _g = LOCK.lock().unwrap();
    let dir = TempDir::new().unwrap();
    let prev = std::env::var("ZILLIZ_CONFIG_DIR").ok();
    std::env::set_var("ZILLIZ_CONFIG_DIR", dir.path());
    f(&dir);
    match prev {
        Some(v) => std::env::set_var("ZILLIZ_CONFIG_DIR", v),
        None => std::env::remove_var("ZILLIZ_CONFIG_DIR"),
    }
}

#[test]
fn cache_round_trip() {
    with_tempdir(|_dir| {
        assert!(load_cache().is_none(), "fresh tempdir has no cache");
        let entry = UpdateCache {
            last_checked_at: Utc::now(),
            latest_version: "1.5.0".to_string(),
        };
        write_cache(&entry);
        let loaded = load_cache().expect("cache should load after write");
        assert_eq!(loaded.latest_version, "1.5.0");
    });
}

#[test]
fn corrupt_json_is_treated_as_missing() {
    with_tempdir(|dir| {
        let path = dir.path().join("update-check.json");
        std::fs::write(&path, b"{ not valid json").unwrap();
        assert!(
            load_cache().is_none(),
            "corrupt JSON should be treated as missing"
        );
    });
}

#[test]
fn freshness_window_is_24h() {
    let now = Utc::now();
    let fresh = UpdateCache {
        last_checked_at: now - Duration::hours(1),
        latest_version: "1.0.0".to_string(),
    };
    assert!(test_support::is_fresh_for_test(&fresh, now));

    let stale = UpdateCache {
        last_checked_at: now - Duration::hours(25),
        latest_version: "1.0.0".to_string(),
    };
    assert!(!test_support::is_fresh_for_test(&stale, now));

    let boundary = UpdateCache {
        last_checked_at: now - Duration::hours(24),
        latest_version: "1.0.0".to_string(),
    };
    assert!(
        !test_support::is_fresh_for_test(&boundary, now),
        "exactly 24h old is stale (strictly less-than)"
    );
}