Skip to content

Commit

Permalink
test(wm): colocate tests with src files, add socket msg test
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Mar 7, 2025
1 parent f40e80c commit 7600238
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 116 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ jobs:
key: ${{ matrix.platform.target }}
- run: cargo +nightly fmt --check
- run: cargo clippy
- run: cargo test --package komorebi --test compat
# sockets don't work properly on the windows runner
- run: cargo test -- --skip window_manager --skip process_command
- uses: houseabsolute/actions-rust-cross@v1
with:
command: "build"
Expand Down
71 changes: 71 additions & 0 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,3 +2069,74 @@ pub fn read_commands_tcp(

Ok(())
}

#[cfg(test)]
mod tests {
use crate::monitor;
use crate::window_manager::WindowManager;
use crate::Rect;
use crate::SocketMessage;
use crate::WindowManagerEvent;
use crate::DATA_DIR;
use crossbeam_channel::bounded;
use crossbeam_channel::Receiver;
use crossbeam_channel::Sender;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::str::FromStr;
use std::time::Duration;
use uds_windows::UnixStream;
use uuid::Uuid;

fn send_socket_message(socket: &str, message: SocketMessage) {
let socket = DATA_DIR.join(socket);
let mut stream = UnixStream::connect(socket).unwrap();
stream
.set_write_timeout(Some(Duration::from_secs(1)))
.unwrap();
stream
.write_all(serde_json::to_string(&message).unwrap().as_bytes())
.unwrap();
}

#[test]
fn test_receive_socket_message() {
let (_sender, receiver): (Sender<WindowManagerEvent>, Receiver<WindowManagerEvent>) =
bounded(1);
let socket_name = format!("komorebi-test-{}.sock", Uuid::new_v4());
let socket_path = DATA_DIR.join(&socket_name);
let mut wm = WindowManager::new(receiver, Some(socket_path.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_mut().push_back(m);

// send a message
send_socket_message(&socket_name, SocketMessage::FocusWorkspaceNumber(5));

let (stream, _) = wm.command_listener.accept().unwrap();
let reader = BufReader::new(stream.try_clone().unwrap());
let next = reader.lines().next();

// read and deserialize the message
let message_string = next.unwrap().unwrap();
let message = SocketMessage::from_str(&message_string).unwrap();
assert!(matches!(message, SocketMessage::FocusWorkspaceNumber(5)));

// process the message
wm.process_command(message, stream).unwrap();

// check the updated window manager state
assert_eq!(wm.focused_workspace_idx().unwrap(), 5);

std::fs::remove_file(socket_path).unwrap();
}
}
33 changes: 33 additions & 0 deletions komorebi/src/static_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,3 +1721,36 @@ fn populate_rules(

Ok(())
}

#[cfg(test)]
mod tests {
use crate::StaticConfig;

#[test]
fn backwards_compat() {
let root = vec!["0.1.17", "0.1.18", "0.1.19"];
let docs = vec![
"0.1.20", "0.1.21", "0.1.22", "0.1.23", "0.1.24", "0.1.25", "0.1.26", "0.1.27",
"0.1.28", "0.1.29", "0.1.30", "0.1.31", "0.1.32", "0.1.33", "0.1.34",
];

let mut versions = vec![];

let client = reqwest::blocking::Client::new();

for version in root {
let request = client.get(format!("https://raw.githubusercontent.com/LGUG2Z/komorebi/refs/tags/v{version}/komorebi.example.json")).header("User-Agent", "komorebi-backwards-compat-test").build().unwrap();
versions.push((version, client.execute(request).unwrap().text().unwrap()));
}

for version in docs {
let request = client.get(format!("https://raw.githubusercontent.com/LGUG2Z/komorebi/refs/tags/v{version}/docs/komorebi.example.json")).header("User-Agent", "komorebi-backwards-compat-test").build().unwrap();
versions.push((version, client.execute(request).unwrap().text().unwrap()));
}

for (version, config) in versions {
println!("{version}");
StaticConfig::read_raw(&config).unwrap();
}
}
}
78 changes: 78 additions & 0 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3660,3 +3660,81 @@ impl WindowManager {
}
}
}

#[cfg(test)]
mod tests {
use crate::monitor;
use crate::window_manager::WindowManager;
use crate::Rect;
use crate::WindowManagerEvent;
use crate::DATA_DIR;
use crossbeam_channel::bounded;
use crossbeam_channel::Receiver;
use crossbeam_channel::Sender;
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_path = DATA_DIR.join(&socket_name);
let mut wm = WindowManager::new(receiver, Some(socket_path.clone())).unwrap();
let m = monitor::new(
0,
Rect::default(),
Rect::default(),
"TestMonitor".to_string(),
"TestDevice".to_string(),
"TestDeviceID".to_string(),
Some("TestMonitorID".to_string()),
);

// a new monitor should have a single workspace
assert_eq!(m.workspaces().len(), 1);

// the next index on the monitor should be the not-yet-created second workspace
let new_workspace_index = m.new_workspace_idx();
assert_eq!(new_workspace_index, 1);

// add the monitor to the window manager
wm.monitors_mut().push_back(m);

{
// focusing a workspace which doesn't yet exist should create it
let monitor = wm.focused_monitor_mut().unwrap();
monitor.focus_workspace(new_workspace_index).unwrap();
assert_eq!(monitor.workspaces().len(), 2);
}
assert_eq!(wm.focused_workspace_idx().unwrap(), 1);

{
// focusing a workspace many indices ahead should create all workspaces
// required along the way
let monitor = wm.focused_monitor_mut().unwrap();
monitor.focus_workspace(new_workspace_index + 2).unwrap();
assert_eq!(monitor.workspaces().len(), 4);
}
assert_eq!(wm.focused_workspace_idx().unwrap(), 3);

// we should be able to successfully focus an existing workspace too
wm.focus_workspace(0).unwrap();
assert_eq!(wm.focused_workspace_idx().unwrap(), 0);

std::fs::remove_file(socket_path).unwrap();
}
}
29 changes: 0 additions & 29 deletions komorebi/tests/compat.rs

This file was deleted.

86 changes: 0 additions & 86 deletions komorebi/tests/window_manager.rs

This file was deleted.

0 comments on commit 7600238

Please # to comment.