test(wm): add window manager unit tests

Created a test that creates the WM instance and ensures the instance is
running. The test creates a custom socket and then cleans up the socket
file after completion.

Created a test that creates a WM instance, monitor instance, and
workpace. The tests checks to ensure that the expected workspace is
focused properly.

Included recommended fixes to ensure that the focus_workspace function
is used correctly and that the test accurately checks the workspaces
length, current workspace index, and switching to an existing workspace.
This commit is contained in:
Jerry Kingsbury
2025-03-05 01:39:58 -06:00
committed by LGUG2Z
parent e4f9d8af86
commit f40e80cd61
5 changed files with 103 additions and 2 deletions

10
Cargo.lock generated
View File

@@ -2678,6 +2678,7 @@ dependencies = [
"tracing-appender",
"tracing-subscriber",
"uds_windows",
"uuid",
"which",
"win32-display-data",
"windows 0.60.0",
@@ -5546,6 +5547,15 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0f540e3240398cce6128b64ba83fdbdd86129c16a3aa1a3a252efd66eb3d587"
dependencies = [
"getrandom 0.3.1",
]
[[package]]
name = "v_frame"
version = "0.3.8"

View File

@@ -55,6 +55,7 @@ shadow-rs = { workspace = true }
[dev-dependencies]
reqwest = { version = "0.12", features = ["blocking"] }
uuid = { version = "1", features = ["v4"] }
[features]
default = ["schemars"]

View File

@@ -235,6 +235,7 @@ fn main() -> Result<()> {
} else {
Arc::new(Mutex::new(WindowManager::new(
winevent_listener::event_rx(),
None,
)?))
};

View File

@@ -398,8 +398,11 @@ impl EnforceWorkspaceRuleOp {
impl WindowManager {
#[tracing::instrument]
pub fn new(incoming: Receiver<WindowManagerEvent>) -> Result<Self> {
let socket = DATA_DIR.join("komorebi.sock");
pub fn new(
incoming: Receiver<WindowManagerEvent>,
custom_socket_path: Option<PathBuf>,
) -> Result<Self> {
let socket = custom_socket_path.unwrap_or_else(|| DATA_DIR.join("komorebi.sock"));
match std::fs::remove_file(&socket) {
Ok(()) => {}

View File

@@ -0,0 +1,86 @@
#[cfg(test)]
mod window_manager_tests {
use color_eyre::eyre::anyhow;
use crossbeam_channel::bounded;
use crossbeam_channel::Receiver;
use crossbeam_channel::Sender;
use komorebi::monitor;
use komorebi::window_manager::WindowManager;
use komorebi::Rect;
use komorebi::WindowManagerEvent;
use komorebi::DATA_DIR;
use uuid::Uuid;
#[test]
fn test_create_window_manager() {
let (_sender, receiver): (Sender<WindowManagerEvent>, Receiver<WindowManagerEvent>) =
bounded(1);
let socket_name = format!("komorebi-test-{}.sock", Uuid::new_v4());
let socket = Some(DATA_DIR.join(socket_name));
let wm = WindowManager::new(receiver, socket.clone());
assert!(wm.is_ok());
if let Some(ref socket_path) = socket {
let _ = std::fs::remove_file(socket_path);
}
}
#[test]
fn test_focus_workspace() {
let (_sender, receiver): (Sender<WindowManagerEvent>, Receiver<WindowManagerEvent>) =
bounded(1);
let socket_name = format!("komorebi-test-{}.sock", Uuid::new_v4());
let socket = Some(DATA_DIR.join(socket_name));
let mut wm = WindowManager::new(receiver, socket.clone()).unwrap();
let m = monitor::new(
0,
Rect::default(),
Rect::default(),
"TestMonitor".to_string(),
"TestDevice".to_string(),
"TestDeviceID".to_string(),
Some("TestMonitorID".to_string()),
);
wm.monitors.elements_mut().push_back(m);
let workspace_idx = {
let monitor = wm
.focused_monitor_mut()
.ok_or_else(|| anyhow!("there is no workspace"))
.unwrap();
monitor.new_workspace_idx()
};
{
let monitor = wm
.focused_monitor_mut()
.ok_or_else(|| anyhow!("there is no workspace"))
.unwrap();
monitor
.focus_workspace(workspace_idx)
.expect("failed to focus workspace");
}
{
let monitor = wm
.focused_monitor_mut()
.ok_or_else(|| anyhow!("there is no workspace"))
.unwrap();
monitor
.focus_workspace(workspace_idx + 1)
.expect("failed to focus workspace");
assert_eq!(monitor.workspaces().len(), 3)
}
assert_eq!(wm.focused_workspace_idx().unwrap(), 2);
wm.focus_workspace(0).ok();
assert_eq!(wm.focused_workspace_idx().unwrap(), 0);
if let Some(ref socket_path) = socket {
let _ = std::fs::remove_file(socket_path);
}
}
}