Skip to main content

lean_ctx/core/
paths.rs

1//! Typed XDG base-directory resolvers for lean-ctx (GH #408 / GL #602).
2//!
3//! Historically every lean-ctx file joined onto a single [`lean_ctx_data_dir`]
4//! rooted at `$XDG_CONFIG_HOME/lean-ctx`, mixing `config.toml` with 30+ runtime
5//! data files (sessions, vectors, graphs, events, logs, caches). That violates
6//! the XDG Base Directory Spec and makes a read-only config sandbox impossible.
7//!
8//! This module introduces one typed resolver per XDG category so call-sites can
9//! migrate to the correct base over the following phases (GL #603/#604/#606/#607).
10//!
11//! ## Backward compatibility (single-dir mode)
12//!
13//! Existing installs MUST NOT split silently. `single_dir_override` returns
14//! `Some(dir)` when `LEAN_CTX_DATA_DIR` is set or a legacy/mixed install with
15//! data exists; in that case every category resolves to that one directory —
16//! byte-for-byte today's behavior. The real per-category split only applies to
17//! fresh installs (and, later, on-demand via `lean-ctx doctor --fix`).
18//!
19//! ## `data_dir()` and the fresh-install flip
20//!
21//! [`data_dir`] delegates to [`lean_ctx_data_dir`]. Config (`config.toml` +
22//! hooks) and the runtime STATE/CACHE files were migrated onto [`config_dir`],
23//! [`state_dir`] and [`cache_dir`] (GL #603/#604) so that, since GL #606, the
24//! data resolver defaults fresh installs to `$XDG_DATA_HOME/lean-ctx` without
25//! scattering config or state into the data dir. Legacy and pre-split mixed
26//! installs keep resolving every category to their existing single directory.
27//!
28//! Determinism (#498): every resolver is a pure function of environment + HOME;
29//! no timestamps, counters or randomness.
30
31use std::path::{Path, PathBuf};
32
33use super::data_dir::{ensure_dir_permissions, has_data_files, lean_ctx_data_dir};
34
35/// Reads a directory path from `name`, treating empty/whitespace as unset.
36fn env_path(name: &str) -> Option<PathBuf> {
37    std::env::var(name)
38        .ok()
39        .map(|v| v.trim().to_string())
40        .filter(|v| !v.is_empty())
41        .map(PathBuf::from)
42}
43
44/// Resolves an XDG base directory (e.g. `~/.config`), honoring the `env_name`
45/// override and falling back to `$HOME/<home_fallback>`. Returns the base only;
46/// callers append `lean-ctx`.
47fn xdg_base(env_name: &str, home_fallback: &str) -> Result<PathBuf, String> {
48    if let Some(p) = env_path(env_name) {
49        return Ok(p);
50    }
51    dirs::home_dir()
52        .map(|h| h.join(home_fallback))
53        .ok_or_else(|| "Cannot determine home directory".to_string())
54}
55
56/// Pure resolution order for a category: explicit override > single-dir
57/// backward-compat > XDG split default (`<base>/lean-ctx`).
58fn resolve(
59    category_override: Option<PathBuf>,
60    single: Option<PathBuf>,
61    xdg_base_dir: &Path,
62) -> PathBuf {
63    category_override
64        .or(single)
65        .unwrap_or_else(|| xdg_base_dir.join("lean-ctx"))
66}
67
68/// Returns the single directory that ALL categories must collapse onto for
69/// backward compatibility, or `None` for a fresh install that may split.
70///
71/// `Some` when an *explicit* `LEAN_CTX_DATA_DIR` points at a non-standard
72/// location (a deliberate single-dir choice: a custom dir, or `~/.lean-ctx` via
73/// env), or a legacy `~/.lean-ctx` / mixed `$XDG_CONFIG_HOME/lean-ctx` install
74/// already holds data. An empty directory does NOT trigger single-dir mode
75/// (matches [`lean_ctx_data_dir`] semantics).
76///
77/// A `LEAN_CTX_DATA_DIR` equal to the *standard* XDG data dir
78/// (`$XDG_DATA_HOME/lean-ctx`) is a DATA pin only, NOT a single-dir directive:
79/// editors used to bake that exact value into the MCP server's `env`, and
80/// honoring it as single-dir collapsed config/state/cache onto the data dir for
81/// the MCP process while the terminal CLI kept the XDG split — so the two read
82/// different `config.toml` files (#594). Falling through keeps config in
83/// `$XDG_CONFIG_HOME/lean-ctx` for both; data still resolves to the pin via
84/// [`lean_ctx_data_dir`], which consumes the env var before reaching here.
85pub(crate) fn single_dir_override() -> Option<PathBuf> {
86    if let Some(p) = env_path("LEAN_CTX_DATA_DIR")
87        && !is_standard_xdg_data_dir(&p)
88    {
89        return Some(p);
90    }
91    let home = dirs::home_dir()?;
92    let xdg_config_base = xdg_base("XDG_CONFIG_HOME", ".config").ok()?;
93    single_dir_override_fs(&home, &xdg_config_base)
94}
95
96/// True when `p` is exactly the standard XDG data dir (`$XDG_DATA_HOME/lean-ctx`,
97/// default `~/.local/share/lean-ctx`). Pure function of env + HOME (no filesystem
98/// access) so category resolution stays deterministic (#498).
99fn is_standard_xdg_data_dir(p: &Path) -> bool {
100    xdg_base("XDG_DATA_HOME", ".local/share")
101        .map(|base| base.join("lean-ctx"))
102        .is_ok_and(|standard| standard.as_path() == p)
103}
104
105/// Whether an editor-baked `LEAN_CTX_DATA_DIR` value (`pin`) would make that
106/// editor's MCP server resolve a *different* `config.toml` than the terminal
107/// CLI. Only a **non-standard** pin collapses config/state/cache onto the data
108/// dir (#594); a pin equal to the standard `$XDG_DATA_HOME/lean-ctx` is a
109/// data-only pin and keeps config parity. Pure function of `pin` + the current
110/// process' env/HOME (the terminal's view), so `doctor` can compare an editor's
111/// baked pin against the CLI's own resolution without mutating env or spawning.
112pub(crate) fn data_pin_diverges_config(pin: &Path) -> bool {
113    !is_standard_xdg_data_dir(pin)
114}
115
116/// Filesystem half of [`single_dir_override`], parameterized for hermetic tests.
117fn single_dir_override_fs(home: &Path, xdg_config_base: &Path) -> Option<PathBuf> {
118    // A committed XDG install is the single source of truth: never re-collapse
119    // it onto a stray legacy/mixed data marker (GL #623). The pin lives next to
120    // the mixed probe below, so the two reads always agree on `xdg_config_base`.
121    if crate::core::layout_pin::is_xdg_pinned_in(xdg_config_base) {
122        return None;
123    }
124    let legacy = home.join(".lean-ctx");
125    if legacy.exists() && has_data_files(&legacy) {
126        return Some(legacy);
127    }
128    let mixed = xdg_config_base.join("lean-ctx");
129    if mixed.exists() && has_data_files(&mixed) {
130        return Some(mixed);
131    }
132    None
133}
134
135/// Shared resolver for the config/state/cache categories.
136// Under `#[cfg(test)]` the body always succeeds (returns the sandbox); the
137// fallible XDG resolution only runs in real builds.
138#[cfg_attr(test, allow(clippy::unnecessary_wraps))]
139fn category_dir(cat_env: &str, xdg_env: &str, home_fallback: &str) -> Result<PathBuf, String> {
140    let category_override = env_path(cat_env);
141
142    // A category override always wins, even under #[cfg(test)] — this lets the
143    // RO-config sandbox integration test point each category at a temp dir.
144    #[cfg(test)]
145    {
146        if let Some(p) = category_override {
147            ensure_dir_permissions(&p);
148            return Ok(p);
149        }
150        // Unit tests share one per-process sandbox so stray store writes can't
151        // escape to a developer's real dirs. The branch logic itself is covered
152        // by the pure `resolve` / `single_dir_override_fs` tests below.
153        let _ = (xdg_env, home_fallback);
154        Ok(super::data_dir::test_sandbox_dir())
155    }
156    #[cfg(not(test))]
157    {
158        let base = xdg_base(xdg_env, home_fallback)?;
159        let dir = resolve(category_override, single_dir_override(), &base);
160        ensure_dir_permissions(&dir);
161        Ok(dir)
162    }
163}
164
165/// Config directory — `config.toml`, shell hooks, `env.sh`. RO-safe.
166/// Override: `LEAN_CTX_CONFIG_DIR`; default `$XDG_CONFIG_HOME/lean-ctx`.
167pub fn config_dir() -> Result<PathBuf, String> {
168    category_dir("LEAN_CTX_CONFIG_DIR", "XDG_CONFIG_HOME", ".config")
169}
170
171/// Resolve a member (a file or sub-directory) of the config dir, adopting a copy
172/// that older builds wrote under the OS-native `dirs::config_dir()` location.
173///
174/// Before #594 (A3), providers/personas/plugins/multi-repo config resolved via
175/// `dirs::config_dir()` — on macOS `~/Library/Application Support`, a *different*
176/// base than the `$XDG_CONFIG_HOME/lean-ctx` dir used for `config.toml`, so those
177/// features silently diverged from the main config. Routing them through
178/// [`config_dir`] unifies the base; this helper performs a one-time,
179/// non-destructive adoption so a user who already had config at the old path
180/// keeps it. The canonical location always wins — an existing canonical entry is
181/// never overwritten — and adoption is skipped entirely in tests so it can never
182/// touch a developer's real `~/Library/Application Support`.
183pub fn config_dir_member(sub: &str) -> Result<PathBuf, String> {
184    let canonical = config_dir()?.join(sub);
185    #[cfg(not(test))]
186    adopt_legacy_config_member(sub, &canonical);
187    Ok(canonical)
188}
189
190/// Decide whether a legacy config member should be adopted: only when the
191/// canonical entry is still absent, the legacy entry actually exists, and the two
192/// paths genuinely differ (on Linux the two bases coincide, making this a no-op).
193fn legacy_adoption_source(legacy: &Path, canonical: &Path) -> Option<PathBuf> {
194    if canonical.exists() || legacy == canonical || !legacy.exists() {
195        return None;
196    }
197    Some(legacy.to_path_buf())
198}
199
200/// Move `src` onto `dst` (file or directory). Prefers an atomic rename on the
201/// same filesystem; falls back to a recursive copy for the rare cross-device
202/// case so no config is ever left stranded. The caller guarantees `dst`'s parent
203/// exists.
204fn relocate(src: &Path, dst: &Path) -> std::io::Result<()> {
205    if std::fs::rename(src, dst).is_ok() {
206        return Ok(());
207    }
208    if src.is_dir() {
209        std::fs::create_dir_all(dst)?;
210        for entry in std::fs::read_dir(src)? {
211            let entry = entry?;
212            relocate(&entry.path(), &dst.join(entry.file_name()))?;
213        }
214        std::fs::remove_dir_all(src)?;
215    } else {
216        std::fs::copy(src, dst)?;
217        std::fs::remove_file(src)?;
218    }
219    Ok(())
220}
221
222/// One-time adoption of a legacy `dirs::config_dir()/lean-ctx/<sub>` member into
223/// the canonical config dir. Best-effort: any IO failure leaves the legacy copy
224/// in place and resolution simply proceeds against the canonical path.
225#[cfg(not(test))]
226fn adopt_legacy_config_member(sub: &str, canonical: &Path) {
227    let Some(legacy_base) = dirs::config_dir() else {
228        return;
229    };
230    let legacy = legacy_base.join("lean-ctx").join(sub);
231    let Some(src) = legacy_adoption_source(&legacy, canonical) else {
232        return;
233    };
234    if let Some(parent) = canonical.parent()
235        && std::fs::create_dir_all(parent).is_err()
236    {
237        return;
238    }
239    let _ = relocate(&src, canonical);
240}
241
242/// Data directory — sessions, vectors, graphs, knowledge, archives, memory.
243///
244/// Delegates to [`lean_ctx_data_dir`], which since GL #606 defaults fresh
245/// installs to `$XDG_DATA_HOME/lean-ctx`. Legacy `~/.lean-ctx` and pre-split
246/// mixed `$XDG_CONFIG_HOME/lean-ctx` installs (and an explicit
247/// `LEAN_CTX_DATA_DIR`) continue to resolve in place for backward compatibility.
248pub fn data_dir() -> Result<PathBuf, String> {
249    lean_ctx_data_dir()
250}
251
252/// State directory — events, stats, logs, journals, ledgers, captured keys.
253/// Override: `LEAN_CTX_STATE_DIR`; default `$XDG_STATE_HOME/lean-ctx`.
254pub fn state_dir() -> Result<PathBuf, String> {
255    category_dir("LEAN_CTX_STATE_DIR", "XDG_STATE_HOME", ".local/state")
256}
257
258/// Cache directory — semantic cache, models, learned patterns. tmpfs-safe.
259/// Override: `LEAN_CTX_CACHE_DIR`; default `$XDG_CACHE_HOME/lean-ctx`.
260pub fn cache_dir() -> Result<PathBuf, String> {
261    category_dir("LEAN_CTX_CACHE_DIR", "XDG_CACHE_HOME", ".cache")
262}
263
264/// Runtime directory — `daemon.pid`, `daemon.sock`. `$XDG_RUNTIME_DIR/lean-ctx`.
265///
266/// When `XDG_RUNTIME_DIR` is unset (common on macOS), falls back to
267/// [`state_dir`] so runtime files stay in a private, writable, non-config path
268/// rather than a world-readable temp location.
269pub fn runtime_dir() -> Result<PathBuf, String> {
270    if let Some(base) = env_path("XDG_RUNTIME_DIR") {
271        return Ok(base.join("lean-ctx"));
272    }
273    state_dir()
274}
275
276/// Raw per-category target dir for the four XDG categories, **bypassing**
277/// single-dir back-compat and the test sandbox. Honors an explicit
278/// `LEAN_CTX_<CAT>_DIR` override, otherwise `<XDG base>/lean-ctx`.
279///
280/// `category_dir`/[`data_dir`] deliberately collapse onto one directory for a
281/// legacy/mixed install; the `doctor --fix` migration (GH #408) needs to know
282/// where each category SHOULD live *after* a split, which is what this returns.
283fn raw_category_dir(cat_env: &str, xdg_env: &str, home_fallback: &str) -> Result<PathBuf, String> {
284    if let Some(p) = env_path(cat_env) {
285        return Ok(p);
286    }
287    Ok(xdg_base(xdg_env, home_fallback)?.join("lean-ctx"))
288}
289
290/// Split target for the config category (`$XDG_CONFIG_HOME/lean-ctx`).
291pub(crate) fn config_split_target() -> Result<PathBuf, String> {
292    raw_category_dir("LEAN_CTX_CONFIG_DIR", "XDG_CONFIG_HOME", ".config")
293}
294
295/// `$XDG_CONFIG_HOME/lean-ctx` (or `~/.config/lean-ctx`) — where `config.toml`
296/// and the layout pin (`layout.toml`) live. Resolved through the XDG config base
297/// only, bypassing single-dir collapse, so the pin that governs that collapse
298/// never depends on it (GL #623). `None` only when HOME cannot be determined.
299pub(crate) fn xdg_config_lean_ctx_dir() -> Option<PathBuf> {
300    xdg_base("XDG_CONFIG_HOME", ".config")
301        .ok()
302        .map(|b| b.join("lean-ctx"))
303}
304
305/// Split target for the data category (`$XDG_DATA_HOME/lean-ctx`).
306pub(crate) fn data_split_target() -> Result<PathBuf, String> {
307    raw_category_dir("LEAN_CTX_DATA_DIR", "XDG_DATA_HOME", ".local/share")
308}
309
310/// Split target for the state category (`$XDG_STATE_HOME/lean-ctx`).
311pub(crate) fn state_split_target() -> Result<PathBuf, String> {
312    raw_category_dir("LEAN_CTX_STATE_DIR", "XDG_STATE_HOME", ".local/state")
313}
314
315/// Split target for the cache category (`$XDG_CACHE_HOME/lean-ctx`).
316pub(crate) fn cache_split_target() -> Result<PathBuf, String> {
317    raw_category_dir("LEAN_CTX_CACHE_DIR", "XDG_CACHE_HOME", ".cache")
318}
319
320#[cfg(test)]
321mod tests {
322    use super::*;
323
324    #[test]
325    fn resolve_prefers_override_then_single_then_xdg() {
326        let over = PathBuf::from("/over/ride");
327        let single = PathBuf::from("/single/dir");
328        let base = PathBuf::from("/xdg/base");
329
330        assert_eq!(
331            resolve(Some(over.clone()), Some(single.clone()), &base),
332            over
333        );
334        assert_eq!(resolve(None, Some(single.clone()), &base), single);
335        assert_eq!(
336            resolve(None, None, &base),
337            PathBuf::from("/xdg/base/lean-ctx")
338        );
339    }
340
341    #[test]
342    fn single_dir_fs_detects_legacy_with_data() {
343        let home = tempfile::tempdir().unwrap();
344        let xdg = tempfile::tempdir().unwrap();
345        let legacy = home.path().join(".lean-ctx");
346        std::fs::create_dir_all(&legacy).unwrap();
347        std::fs::write(legacy.join("stats.json"), "{}").unwrap();
348
349        assert_eq!(
350            single_dir_override_fs(home.path(), xdg.path()),
351            Some(legacy)
352        );
353    }
354
355    #[test]
356    fn single_dir_fs_detects_mixed_with_data() {
357        let home = tempfile::tempdir().unwrap();
358        let xdg = tempfile::tempdir().unwrap();
359        let mixed = xdg.path().join("lean-ctx");
360        std::fs::create_dir_all(&mixed).unwrap();
361        // A real data marker (stats.json) — NOT config.toml, which post-split
362        // lives alone in the config dir and must not trigger single-dir mode.
363        std::fs::write(mixed.join("stats.json"), "{}").unwrap();
364
365        assert_eq!(single_dir_override_fs(home.path(), xdg.path()), Some(mixed));
366    }
367
368    #[test]
369    fn single_dir_fs_ignores_config_only_dir() {
370        // GH #408: a clean post-split config dir (only config.toml + hooks) must
371        // NOT collapse the four-dir layout.
372        let home = tempfile::tempdir().unwrap();
373        let xdg = tempfile::tempdir().unwrap();
374        let mixed = xdg.path().join("lean-ctx");
375        std::fs::create_dir_all(&mixed).unwrap();
376        std::fs::write(mixed.join("config.toml"), "").unwrap();
377        std::fs::write(mixed.join("shell-hook.zsh"), "").unwrap();
378
379        assert_eq!(single_dir_override_fs(home.path(), xdg.path()), None);
380    }
381
382    #[test]
383    fn single_dir_fs_prefers_legacy_over_mixed() {
384        let home = tempfile::tempdir().unwrap();
385        let xdg = tempfile::tempdir().unwrap();
386        let legacy = home.path().join(".lean-ctx");
387        std::fs::create_dir_all(&legacy).unwrap();
388        std::fs::write(legacy.join("sessions"), "x").unwrap();
389        let mixed = xdg.path().join("lean-ctx");
390        std::fs::create_dir_all(&mixed).unwrap();
391        std::fs::write(mixed.join("stats.json"), "{}").unwrap();
392
393        assert_eq!(
394            single_dir_override_fs(home.path(), xdg.path()),
395            Some(legacy)
396        );
397    }
398
399    #[test]
400    fn xdg_pinned_install_ignores_stray_legacy_marker() {
401        // GL #623: once committed to XDG (pin in the config dir), a stray
402        // `~/.lean-ctx/stats.json` (legacy residue, restored backup, concurrent
403        // old binary) must NOT re-collapse the layout onto the legacy dir.
404        let home = tempfile::tempdir().unwrap();
405        let xdg = tempfile::tempdir().unwrap();
406        crate::core::layout_pin::write_xdg_pin_in(xdg.path()).unwrap();
407
408        let legacy = home.path().join(".lean-ctx");
409        std::fs::create_dir_all(&legacy).unwrap();
410        std::fs::write(legacy.join("stats.json"), "{}").unwrap();
411
412        assert_eq!(single_dir_override_fs(home.path(), xdg.path()), None);
413    }
414
415    #[test]
416    fn xdg_pinned_install_ignores_stray_mixed_marker() {
417        // GL #623: same protection for a stray data marker that lands in the
418        // mixed `$XDG_CONFIG_HOME/lean-ctx` dir after the install committed.
419        let home = tempfile::tempdir().unwrap();
420        let xdg = tempfile::tempdir().unwrap();
421        crate::core::layout_pin::write_xdg_pin_in(xdg.path()).unwrap();
422
423        let mixed = xdg.path().join("lean-ctx");
424        std::fs::write(mixed.join("stats.json"), "{}").unwrap();
425
426        assert_eq!(single_dir_override_fs(home.path(), xdg.path()), None);
427    }
428
429    #[test]
430    fn single_dir_fs_ignores_empty_dirs() {
431        let home = tempfile::tempdir().unwrap();
432        let xdg = tempfile::tempdir().unwrap();
433        std::fs::create_dir_all(home.path().join(".lean-ctx")).unwrap();
434        std::fs::create_dir_all(xdg.path().join("lean-ctx")).unwrap();
435
436        assert_eq!(single_dir_override_fs(home.path(), xdg.path()), None);
437    }
438
439    #[test]
440    fn single_dir_fs_ignores_non_marker_files() {
441        let home = tempfile::tempdir().unwrap();
442        let xdg = tempfile::tempdir().unwrap();
443        let mixed = xdg.path().join("lean-ctx");
444        std::fs::create_dir_all(&mixed).unwrap();
445        std::fs::write(mixed.join("random.txt"), "x").unwrap();
446
447        assert_eq!(single_dir_override_fs(home.path(), xdg.path()), None);
448    }
449
450    #[test]
451    fn xdg_base_honors_env_then_home_fallback() {
452        let _lock = crate::core::data_dir::test_env_lock();
453        let tmp = tempfile::tempdir().unwrap();
454        crate::test_env::set_var("XDG_CONFIG_HOME", tmp.path());
455        let from_env = xdg_base("XDG_CONFIG_HOME", ".config").unwrap();
456        crate::test_env::remove_var("XDG_CONFIG_HOME");
457        assert_eq!(from_env, tmp.path());
458
459        // Unset var → falls back to $HOME/<home_fallback>.
460        let fallback = xdg_base("LEAN_CTX_NONEXISTENT_XDG_VAR", ".cache").unwrap();
461        assert!(fallback.ends_with(".cache"), "got: {}", fallback.display());
462    }
463
464    #[test]
465    fn legacy_adoption_source_only_when_canonical_absent() {
466        let tmp = tempfile::tempdir().unwrap();
467        let legacy = tmp.path().join("legacy");
468        let canonical = tmp.path().join("canonical");
469
470        // Legacy missing → nothing to adopt.
471        assert_eq!(legacy_adoption_source(&legacy, &canonical), None);
472
473        // Legacy present, canonical absent → adopt the legacy copy.
474        std::fs::create_dir_all(&legacy).unwrap();
475        assert_eq!(
476            legacy_adoption_source(&legacy, &canonical),
477            Some(legacy.clone())
478        );
479
480        // Canonical present → the newer location wins, never overwrite it.
481        std::fs::create_dir_all(&canonical).unwrap();
482        assert_eq!(legacy_adoption_source(&legacy, &canonical), None);
483    }
484
485    #[test]
486    fn relocate_moves_file_then_directory() {
487        let tmp = tempfile::tempdir().unwrap();
488
489        // File: dst parent must be created by the caller (as adopt does).
490        let src_file = tmp.path().join("providers.toml");
491        std::fs::write(&src_file, "id = \"x\"\n").unwrap();
492        let dst_file = tmp.path().join("config/lean-ctx/providers.toml");
493        std::fs::create_dir_all(dst_file.parent().unwrap()).unwrap();
494        relocate(&src_file, &dst_file).unwrap();
495        assert!(!src_file.exists(), "source file must be moved, not copied");
496        assert_eq!(std::fs::read_to_string(&dst_file).unwrap(), "id = \"x\"\n");
497
498        // Directory with nested content.
499        let src_dir = tmp.path().join("personas");
500        std::fs::create_dir_all(src_dir.join("nested")).unwrap();
501        std::fs::write(src_dir.join("a.toml"), "a").unwrap();
502        std::fs::write(src_dir.join("nested/b.toml"), "b").unwrap();
503        let dst_dir = tmp.path().join("config/lean-ctx/personas");
504        std::fs::create_dir_all(dst_dir.parent().unwrap()).unwrap();
505        relocate(&src_dir, &dst_dir).unwrap();
506        assert!(!src_dir.exists(), "source dir must be moved");
507        assert_eq!(
508            std::fs::read_to_string(dst_dir.join("a.toml")).unwrap(),
509            "a"
510        );
511        assert_eq!(
512            std::fs::read_to_string(dst_dir.join("nested/b.toml")).unwrap(),
513            "b"
514        );
515    }
516
517    #[test]
518    fn single_dir_override_honors_data_dir_env() {
519        let _lock = crate::core::data_dir::test_env_lock();
520        let tmp = tempfile::tempdir().unwrap();
521        crate::test_env::set_var("LEAN_CTX_DATA_DIR", tmp.path());
522        let got = single_dir_override();
523        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
524        // A custom (non-standard) data dir is a deliberate single-dir choice.
525        assert_eq!(got, Some(tmp.path().to_path_buf()));
526    }
527
528    #[test]
529    fn is_standard_xdg_data_dir_matches_xdg_data_home() {
530        let _lock = crate::core::data_dir::test_env_lock();
531        let data_home = tempfile::tempdir().unwrap();
532        crate::test_env::set_var("XDG_DATA_HOME", data_home.path());
533        let is_std = is_standard_xdg_data_dir(&data_home.path().join("lean-ctx"));
534        let is_custom = is_standard_xdg_data_dir(Path::new("/some/custom/lean-ctx"));
535        crate::test_env::remove_var("XDG_DATA_HOME");
536        assert!(is_std, "$XDG_DATA_HOME/lean-ctx is the standard data dir");
537        assert!(!is_custom, "a custom path is not the standard data dir");
538    }
539
540    #[test]
541    fn standard_data_pin_does_not_collapse_categories() {
542        // #594: an editor (MCP env) that pins LEAN_CTX_DATA_DIR to the *standard*
543        // XDG data dir must NOT drag config/state/cache along — single_dir_override
544        // must return None so they keep their own XDG bases, matching the CLI.
545        let _lock = crate::core::data_dir::test_env_lock();
546        let home = tempfile::tempdir().unwrap();
547        let xdg_config = tempfile::tempdir().unwrap();
548        let xdg_data = tempfile::tempdir().unwrap();
549        let data_pin = xdg_data.path().join("lean-ctx");
550        crate::test_env::set_var("HOME", home.path());
551        crate::test_env::set_var("XDG_CONFIG_HOME", xdg_config.path());
552        crate::test_env::set_var("XDG_DATA_HOME", xdg_data.path());
553        crate::test_env::set_var("LEAN_CTX_DATA_DIR", &data_pin);
554
555        let got = single_dir_override();
556
557        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
558        crate::test_env::remove_var("XDG_DATA_HOME");
559        crate::test_env::remove_var("XDG_CONFIG_HOME");
560        crate::test_env::remove_var("HOME");
561
562        assert_eq!(got, None);
563    }
564
565    #[test]
566    fn config_dir_honors_explicit_override() {
567        let _lock = crate::core::data_dir::test_env_lock();
568        let tmp = tempfile::tempdir().unwrap();
569        crate::test_env::set_var("LEAN_CTX_CONFIG_DIR", tmp.path());
570        let got = config_dir().unwrap();
571        crate::test_env::remove_var("LEAN_CTX_CONFIG_DIR");
572        assert_eq!(got, tmp.path());
573    }
574
575    #[test]
576    fn state_and_cache_dirs_honor_explicit_overrides() {
577        let _lock = crate::core::data_dir::test_env_lock();
578        let state = tempfile::tempdir().unwrap();
579        let cache = tempfile::tempdir().unwrap();
580        crate::test_env::set_var("LEAN_CTX_STATE_DIR", state.path());
581        crate::test_env::set_var("LEAN_CTX_CACHE_DIR", cache.path());
582        let got_state = state_dir().unwrap();
583        let got_cache = cache_dir().unwrap();
584        crate::test_env::remove_var("LEAN_CTX_STATE_DIR");
585        crate::test_env::remove_var("LEAN_CTX_CACHE_DIR");
586        assert_eq!(got_state, state.path());
587        assert_eq!(got_cache, cache.path());
588    }
589
590    #[test]
591    fn data_dir_matches_lean_ctx_data_dir() {
592        let _guard = crate::core::data_dir::isolated_data_dir();
593        assert_eq!(
594            data_dir().unwrap(),
595            crate::core::data_dir::lean_ctx_data_dir().unwrap()
596        );
597    }
598
599    #[test]
600    fn runtime_dir_honors_xdg_runtime_dir() {
601        let _lock = crate::core::data_dir::test_env_lock();
602        let tmp = tempfile::tempdir().unwrap();
603        crate::test_env::set_var("XDG_RUNTIME_DIR", tmp.path());
604        let got = runtime_dir().unwrap();
605        crate::test_env::remove_var("XDG_RUNTIME_DIR");
606        assert_eq!(got, tmp.path().join("lean-ctx"));
607    }
608}