diff --git a/tests/cache_lifecycle.rs b/tests/cache_lifecycle.rs new file mode 100644 index 0000000..3646669 --- /dev/null +++ b/tests/cache_lifecycle.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; +use std::path::PathBuf; +use tmuxido::cache::ProjectCache; + +#[test] +fn should_save_and_reload_cache() { + let projects = vec![PathBuf::from("/tmp/test_tmuxido_project")]; + let cache = ProjectCache::new(projects.clone(), HashMap::new()); + cache.save().unwrap(); + + let loaded = ProjectCache::load().unwrap().unwrap(); + assert_eq!(loaded.projects, projects); +} diff --git a/tests/scan.rs b/tests/scan.rs new file mode 100644 index 0000000..58280e2 --- /dev/null +++ b/tests/scan.rs @@ -0,0 +1,65 @@ +use std::fs; +use tempfile::tempdir; +use tmuxido::config::Config; +use tmuxido::scan_from_root; +use tmuxido::session::SessionConfig; + +fn make_config(max_depth: usize) -> Config { + Config { + paths: vec![], + max_depth, + cache_enabled: true, + cache_ttl_hours: 24, + default_session: SessionConfig { windows: vec![] }, + } +} + +/// `tempfile::tempdir()` creates hidden dirs (e.g. `/tmp/.tmpXXXXXX`) on this +/// system, which `scan_from_root`'s `filter_entry` would skip. Create a +/// visible subdirectory to use as the actual scan root. +fn make_scan_root() -> (tempfile::TempDir, std::path::PathBuf) { + let dir = tempdir().unwrap(); + let root = dir.path().join("scan_root"); + fs::create_dir_all(&root).unwrap(); + (dir, root) +} + +#[test] +fn should_find_git_repos_in_temp_dir() { + let (_dir, root) = make_scan_root(); + fs::create_dir_all(root.join("foo/.git")).unwrap(); + fs::create_dir_all(root.join("bar/.git")).unwrap(); + + let config = make_config(5); + let (projects, _) = scan_from_root(&root, &config).unwrap(); + + assert_eq!(projects.len(), 2); + assert!(projects.iter().any(|p| p.ends_with("foo"))); + assert!(projects.iter().any(|p| p.ends_with("bar"))); +} + +#[test] +fn should_not_descend_into_hidden_dirs() { + let (_dir, root) = make_scan_root(); + fs::create_dir_all(root.join(".hidden/repo/.git")).unwrap(); + + let config = make_config(5); + let (projects, _) = scan_from_root(&root, &config).unwrap(); + + assert!(projects.is_empty()); +} + +#[test] +fn should_respect_max_depth() { + let (_dir, root) = make_scan_root(); + // Shallow: project/.git at depth 2 from root — found with max_depth=2 + fs::create_dir_all(root.join("project/.git")).unwrap(); + // Deep: nested/deep/project/.git at depth 4 — excluded with max_depth=2 + fs::create_dir_all(root.join("nested/deep/project/.git")).unwrap(); + + let config = make_config(2); + let (projects, _) = scan_from_root(&root, &config).unwrap(); + + assert_eq!(projects.len(), 1); + assert!(projects[0].ends_with("project")); +} diff --git a/tests/session_config.rs b/tests/session_config.rs new file mode 100644 index 0000000..1a77c33 --- /dev/null +++ b/tests/session_config.rs @@ -0,0 +1,26 @@ +use std::fs; +use tempfile::tempdir; +use tmuxido::session::SessionConfig; + +#[test] +fn should_load_project_session_config() { + let dir = tempdir().unwrap(); + let config_content = r#" + [[windows]] + name = "editor" + panes = ["nvim ."] + "#; + fs::write(dir.path().join(".tmuxido.toml"), config_content).unwrap(); + + let result = SessionConfig::load_from_project(dir.path()).unwrap(); + assert!(result.is_some()); + let config = result.unwrap(); + assert_eq!(config.windows[0].name, "editor"); +} + +#[test] +fn should_return_none_when_no_project_config() { + let dir = tempdir().unwrap(); + let result = SessionConfig::load_from_project(dir.path()).unwrap(); + assert!(result.is_none()); +}