metis_docs_cli/
workspace.rs

1use std::path::PathBuf;
2
3/// Check if we're in a Metis workspace by walking up the directory tree
4///
5/// Returns (found, metis_dir_path) where:
6/// - found: true if a valid .metis/ vault was found
7/// - metis_dir_path: absolute path to the .metis/ directory (if found)
8///
9/// Note: Only checks for .metis directory - database will be auto-created/synced as needed
10pub fn has_metis_vault() -> (bool, Option<PathBuf>) {
11    let Ok(mut current) = std::env::current_dir() else {
12        return (false, None);
13    };
14
15    loop {
16        let metis_dir = current.join(".metis");
17
18        // Check if this directory has a valid Metis vault (just .metis directory)
19        if metis_dir.exists() && metis_dir.is_dir() {
20            return (true, Some(metis_dir));
21        }
22
23        // Move up to parent directory
24        if let Some(parent) = current.parent() {
25            current = parent.to_path_buf();
26        } else {
27            // Reached filesystem root, no workspace found
28            break;
29        }
30    }
31
32    (false, None)
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use tempfile::tempdir;
39
40    #[test]
41    fn test_has_metis_vault_false_when_no_directory() {
42        let temp_dir = tempdir().unwrap();
43        std::env::set_current_dir(temp_dir.path()).unwrap();
44
45        let (found, _) = has_metis_vault();
46        assert!(!found);
47    }
48
49    #[test]
50    fn test_has_metis_vault_true_when_valid() {
51        let temp_dir = tempdir().unwrap();
52        let metis_dir = temp_dir.path().join(".metis");
53
54        std::fs::create_dir(&metis_dir).unwrap();
55        std::env::set_current_dir(temp_dir.path()).unwrap();
56
57        let (found, metis_dir_path) = has_metis_vault();
58        assert!(found);
59
60        // Canonicalize both paths to handle symlinks (e.g., /var vs /private/var on macOS)
61        let returned_path = metis_dir_path.unwrap().canonicalize().unwrap();
62        let expected_path = metis_dir.canonicalize().unwrap();
63        assert_eq!(returned_path, expected_path);
64    }
65}