diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index cadd6992..e4504992 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -84,12 +84,17 @@ impl Container { ) -> eyre::Result<()> { self.windows_mut().push_back(window.clone()); self.focus_window(self.windows().len().saturating_sub(1)); - let focused_window_idx = self.focused_window_idx(); - for (i, window) in self.windows_mut().iter_mut().enumerate() { - if i != focused_window_idx { - window.hide(hiding_position)?; + if !cfg!(test) { + let focused_window_idx = self.focused_window_idx(); + + for (i, window) in self.windows_mut().iter_mut().enumerate() { + if i != focused_window_idx { + window.hide(hiding_position)?; + } } + } else { + tracing::info!("not hiding windows to {hiding_position} during test execution") } Ok(()) @@ -158,3 +163,140 @@ impl Container { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json; + + #[test] + fn test_contains_window() { + let mut container = Container::default(); + + for i in 0..3 { + container + .add_window(&Window::from(i), WindowHidingPosition::BottomLeft) + .unwrap(); + } + + // Should return true for existing windows + assert!(container.contains_window(1)); + assert_eq!(container.idx_for_window(1), Some(1)); + + // Should return false since window 4 doesn't exist + assert!(!container.contains_window(4)); + assert_eq!(container.idx_for_window(4), None); + } + + #[test] + fn test_remove_window_by_idx() { + let mut container = Container::default(); + + for i in 0..3 { + container + .add_window(&Window::from(i), WindowHidingPosition::BottomLeft) + .unwrap(); + } + + // Remove window 1 + container.remove_window_by_idx(1); + + // Should only have 2 windows left + assert_eq!(container.windows().len(), 2); + + // Should return false since window 1 was removed + assert!(!container.contains_window(1)); + } + + #[test] + fn test_remove_focused_window() { + let mut container = Container::default(); + + for i in 0..3 { + container + .add_window(&Window::from(i), WindowHidingPosition::BottomLeft) + .unwrap(); + } + + // Should be focused on the last created window + assert_eq!(container.focused_window_idx(), 2); + + // Remove the focused window + container.remove_focused_window(); + + // Should be focused on the window before the removed one + assert_eq!(container.focused_window_idx(), 1); + + // Should only have 2 windows left + assert_eq!(container.windows().len(), 2); + } + + #[test] + fn test_add_window() { + let mut container = Container::default(); + + container + .add_window(&Window::from(1), WindowHidingPosition::BottomLeft) + .unwrap(); + + assert_eq!(container.windows().len(), 1); + assert_eq!(container.focused_window_idx(), 0); + assert!(container.contains_window(1)); + } + + #[test] + fn test_focus_window() { + let mut container = Container::default(); + + for i in 0..3 { + container + .add_window(&Window::from(i), WindowHidingPosition::BottomLeft) + .unwrap(); + } + + // Should focus on the last created window + assert_eq!(container.focused_window_idx(), 2); + + // focus on the window at index 1 + container.focus_window(1); + + // Should be focused on window 1 + assert_eq!(container.focused_window_idx(), 1); + + // focus on the window at index 0 + container.focus_window(0); + + // Should be focused on window 0 + assert_eq!(container.focused_window_idx(), 0); + } + + #[test] + fn test_idx_for_window() { + let mut container = Container::default(); + + for i in 0..3 { + container + .add_window(&Window::from(i), WindowHidingPosition::BottomLeft) + .unwrap(); + } + + // Should return the index of the window + assert_eq!(container.idx_for_window(1), Some(1)); + + // Should return None since window 4 doesn't exist + assert_eq!(container.idx_for_window(4), None); + } + + #[test] + fn serializes_and_deserializes() { + let mut container = Container::default(); + container.set_locked(true); + + let serialized = serde_json::to_string(&container).expect("Should serialize"); + let deserialized: Container = + serde_json::from_str(&serialized).expect("Should deserialize"); + + assert!(deserialized.locked); + assert_eq!(deserialized.id, container.id); + } +} diff --git a/komorebi/src/core/pathext.rs b/komorebi/src/core/pathext.rs index ad383677..3713ac40 100644 --- a/komorebi/src/core/pathext.rs +++ b/komorebi/src/core/pathext.rs @@ -186,13 +186,27 @@ mod tests { ); assert_eq!(resolve("/path/$ASD/to/d"), expected("/path/$ASD/to/d")); - // Set a $env:USERPROFILE variable for testing - unsafe { - std::env::set_var("USERPROFILE", "C:\\Users\\user"); + #[cfg(target_os = "windows")] + { + // Set a $env:USERPROFILE variable for testing + unsafe { + std::env::set_var("USERPROFILE", "C:\\Users\\user"); + } + + // ~ and $HOME should be replaced with $Env:USERPROFILE + assert_eq!(resolve("~"), expected("C:\\Users\\user")); + assert_eq!(resolve("$HOME"), expected("C:\\Users\\user")); } - // ~ and $HOME should be replaced with $Env:USERPROFILE - assert_eq!(resolve("~"), expected("C:\\Users\\user")); - assert_eq!(resolve("$HOME"), expected("C:\\Users\\user")); + #[cfg(target_os = "macos")] + { + // Set a $env:USERPROFILE variable for testing + unsafe { + std::env::set_var("HOME", "/Users/user"); + } + + // ~ and $HOME should be replaced with $Env:USERPROFILE + assert_eq!(resolve("$HOME"), expected("/Users/user")); + } } } diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 82a75040..4765845c 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -192,6 +192,19 @@ pub struct Window { pub details: Option, } +#[cfg(test)] +impl From for Window { + fn from(id: u32) -> Self { + Self { + id, + element: Default::default(), + application: Default::default(), + observer: Default::default(), + details: None, + } + } +} + #[allow(clippy::module_name_repetitions)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct WindowDetails {