From 8582e197307e392c04b8cc9b1ca94bc2c8683deb Mon Sep 17 00:00:00 2001 From: cinco euzebio Date: Sat, 28 Feb 2026 20:16:09 -0300 Subject: [PATCH] test: add integration tests for scan, session config and cache lifecycle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tests/scan.rs: 3 tests exercising scan_from_root via the public API — finds git repos, skips hidden directories, and respects max_depth. Note: tempfile::tempdir() generates hidden paths (.tmpXXXXXX) on this system, so each test creates a visible scan_root/ subdirectory to avoid filter_entry silently skipping the walk root. tests/session_config.rs: 2 tests for SessionConfig::load_from_project — loads a .tmuxido.toml written into a temp dir and returns None when the file is absent. tests/cache_lifecycle.rs: round-trip test that saves a ProjectCache and reloads it, verifying the projects list survives serialisation. Co-Authored-By: Claude Sonnet 4.6 --- tests/cache_lifecycle.rs | 13 ++++++++ tests/scan.rs | 65 ++++++++++++++++++++++++++++++++++++++++ tests/session_config.rs | 26 ++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 tests/cache_lifecycle.rs create mode 100644 tests/scan.rs create mode 100644 tests/session_config.rs 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()); +}