Skip to main content

lean_ctx/core/
version_check.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3use std::time::{SystemTime, UNIX_EPOCH};
4
5const GITHUB_API_RELEASES: &str = "https://api.github.com/repos/yvgude/lean-ctx/releases/latest";
6const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
7const CACHE_TTL_SECS: u64 = 24 * 60 * 60;
8
9#[derive(Serialize, Deserialize)]
10struct VersionCache {
11    latest: String,
12    checked_at: u64,
13}
14
15fn cache_path() -> Option<PathBuf> {
16    crate::core::data_dir::lean_ctx_data_dir()
17        .ok()
18        .map(|d| d.join("latest-version.json"))
19}
20
21fn now_secs() -> u64 {
22    SystemTime::now()
23        .duration_since(UNIX_EPOCH)
24        .map_or(0, |d| d.as_secs())
25}
26
27fn read_cache() -> Option<VersionCache> {
28    let path = cache_path()?;
29    let content = std::fs::read_to_string(path).ok()?;
30    serde_json::from_str(&content).ok()
31}
32
33fn write_cache(latest: &str) {
34    if let Some(path) = cache_path() {
35        let cache = VersionCache {
36            latest: latest.to_string(),
37            checked_at: now_secs(),
38        };
39        if let Ok(json) = serde_json::to_string(&cache) {
40            let _ = std::fs::write(path, json);
41        }
42    }
43}
44
45fn is_cache_stale(cache: &VersionCache) -> bool {
46    let age = now_secs().saturating_sub(cache.checked_at);
47    age > CACHE_TTL_SECS
48}
49
50fn fetch_latest_version() -> Result<String, String> {
51    let agent = ureq::Agent::new_with_config(
52        ureq::config::Config::builder()
53            .timeout_global(Some(std::time::Duration::from_secs(5)))
54            .build(),
55    );
56
57    let body = agent
58        .get(GITHUB_API_RELEASES)
59        .header("User-Agent", &format!("lean-ctx/{CURRENT_VERSION}"))
60        .header("Accept", "application/vnd.github.v3+json")
61        .call()
62        .map_err(|e| e.to_string())?
63        .into_body()
64        .read_to_string()
65        .map_err(|e| e.to_string())?;
66
67    let release: serde_json::Value = serde_json::from_str(&body).map_err(|e| e.to_string())?;
68    let tag = release["tag_name"]
69        .as_str()
70        .ok_or_else(|| "missing tag_name in GitHub releases response".to_string())?;
71
72    let version = tag.trim().trim_start_matches('v').to_string();
73    if version.is_empty() || !version.contains('.') {
74        return Err("invalid version format".to_string());
75    }
76    Ok(version)
77}
78
79fn is_newer(latest: &str, current: &str) -> bool {
80    let parse =
81        |v: &str| -> Vec<u32> { v.split('.').filter_map(|p| p.parse::<u32>().ok()).collect() };
82    parse(latest) > parse(current)
83}
84
85/// Spawn a background thread to fetch latest version from GitHub Releases
86/// and write the result to the lean-ctx data dir (`latest-version.json`).
87/// Non-blocking, fire-and-forget. Skips if cache is fresh (<24h).
88/// Respects `update_check_disabled` config and `LEAN_CTX_NO_UPDATE_CHECK` env var.
89pub fn check_background() {
90    let cfg = super::config::Config::load();
91    if cfg.update_check_disabled_effective() {
92        return;
93    }
94
95    let cache = read_cache();
96    if let Some(ref c) = cache {
97        if !is_cache_stale(c) {
98            return;
99        }
100    }
101
102    std::thread::spawn(|| {
103        if let Ok(latest) = fetch_latest_version() {
104            write_cache(&latest);
105        }
106    });
107}
108
109/// Returns a formatted yellow update banner if a newer version is available.
110/// Reads only the local cache file — zero network calls, zero delay.
111pub fn get_update_banner() -> Option<String> {
112    let cache = read_cache()?;
113    if is_newer(&cache.latest, CURRENT_VERSION) {
114        Some(format!(
115            "  \x1b[33m\x1b[1m\u{27F3} Update available: v{CURRENT_VERSION} \u{2192} v{}\x1b[0m  \x1b[2m\u{2014} run:\x1b[0m \x1b[1mlean-ctx update\x1b[0m",
116            cache.latest
117        ))
118    } else {
119        None
120    }
121}
122
123/// Returns version info as JSON for the dashboard /api/version endpoint.
124pub fn version_info_json() -> String {
125    let cache = read_cache();
126    let (latest, update_available) = match cache {
127        Some(c) => {
128            let newer = is_newer(&c.latest, CURRENT_VERSION);
129            (c.latest, newer)
130        }
131        None => (CURRENT_VERSION.to_string(), false),
132    };
133
134    format!(
135        r#"{{"current":"{CURRENT_VERSION}","latest":"{latest}","update_available":{update_available}}}"#
136    )
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn newer_version_detected() {
145        assert!(is_newer("2.9.14", "2.9.13"));
146        assert!(is_newer("3.0.0", "2.9.99"));
147        assert!(is_newer("2.10.0", "2.9.14"));
148    }
149
150    #[test]
151    fn same_or_older_not_newer() {
152        assert!(!is_newer("2.9.13", "2.9.13"));
153        assert!(!is_newer("2.9.12", "2.9.13"));
154        assert!(!is_newer("1.0.0", "2.9.13"));
155    }
156
157    #[test]
158    fn cache_fresh_within_ttl() {
159        let fresh = VersionCache {
160            latest: "2.9.14".to_string(),
161            checked_at: now_secs(),
162        };
163        assert!(!is_cache_stale(&fresh));
164    }
165
166    #[test]
167    fn cache_stale_after_ttl() {
168        let old = VersionCache {
169            latest: "2.9.14".to_string(),
170            checked_at: now_secs() - CACHE_TTL_SECS - 1,
171        };
172        assert!(is_cache_stale(&old));
173    }
174
175    #[test]
176    fn version_json_has_required_fields() {
177        let json = version_info_json();
178        assert!(json.contains("current"));
179        assert!(json.contains("latest"));
180        assert!(json.contains("update_available"));
181    }
182
183    #[test]
184    fn banner_none_for_current_version() {
185        assert!(!is_newer(CURRENT_VERSION, CURRENT_VERSION));
186    }
187}