test(core): add the review's gaps: floating hook order, placement rules, hook defaults, key sets

Follow-up to the review of komorebi-core (S/review/core.md, F2 and F4 and its
untested-behaviours list); most of the list was already pinned, this adds the rest:
- the after_floating_removed hook is pinned to run BEFORE the window is taken out
  (it sees both windows and the old focus index), today's KDE/macOS order, although
  the trait doc says after; the stub now logs len and focus for the hook.
- resolve_placement_index with Rules maps (Single and Many matching, Secondary,
  no match falls back after the focused container); the stub gained a match id and a
  path (a window without a path never matches Rules: it falls back).
- the JSON key set of a default workspace, and of Windows and macOS monitors per extra.
- the documented defaults of every optional hook on a platform that overrides nothing
  (the KDE-shaped test platform), and that the default place hooks make update_shared fail.
- Monitor remove_workspace_by_idx at the length, and an old monitor file with a
  non-empty ring of workspaces missing optional keys.

Verified: cargo test -p komorebi-core 215 passed, 1 ignored (the known bug), and with
--features schemars 217 passed, 1 ignored; clippy --all-targets has no warning in the
crate. No non-test source changed.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent 38f1295251
commit fa3ec1ba2e
5 files changed
+238 -8

No files matched your search

+21 -7
View File
@@ -68,6 +68,8 @@ struct Settings {
known_names: HashMap<usize, String>,
/// how much `adjust_layout` shrinks a tile
adjust: i32,
/// the id the placement rule matchers answer true for
match_id: Option<String>,
}
thread_local! {
@@ -135,6 +137,10 @@ pub fn set_known_workspace_name(idx: usize, name: &str) {
settings(|s| s.known_names.insert(idx, name.to_string()));
}
pub fn set_match_id(id: &str) {
settings(|s| s.match_id = Some(id.to_string()));
}
pub fn set_adjust(by: i32) {
settings(|s| s.adjust = by);
}
@@ -207,7 +213,7 @@ impl WorkspaceWindow for StubWindow {
}
fn path(&self) -> Option<String> {
None
Some(format!("/bin/exe{}", self.id))
}
}
@@ -244,9 +250,13 @@ impl WorkspacePlatform for StubPlatform {
_: &str,
_: &str,
_: &str,
_: &[MatchingRule],
rules: &[MatchingRule],
) -> Option<MatchingRule> {
None
let id = settings(|s| s.match_id.clone())?;
rules
.iter()
.find(|rule| matches!(rule, MatchingRule::Simple(r) if r.id == id))
.cloned()
}
fn should_act_window_individual(
@@ -254,9 +264,9 @@ impl WorkspacePlatform for StubPlatform {
_: &str,
_: &str,
_: &str,
_: &IdWithIdentifier,
rule: &IdWithIdentifier,
) -> bool {
false
settings(|s| s.match_id.clone()).is_some_and(|id| id == rule.id)
}
fn scrolling_idx_for_direction(_: &Workspace<Self>, _: OperationDirection) -> Option<usize> {
@@ -271,8 +281,12 @@ impl WorkspacePlatform for StubPlatform {
push(format!("note_scrolling_departure {idx}"));
}
fn after_floating_removed(_: &mut Workspace<Self>) {
push("after_floating_removed".to_string());
fn after_floating_removed(workspace: &mut Workspace<Self>) {
push(format!(
"after_floating_removed len={} focus={}",
workspace.floating_windows().len(),
workspace.focused_floating_window_idx()
));
}
fn unmaximize_window(window: &StubWindow) -> eyre::Result<()> {
@@ -325,3 +325,39 @@ fn a_monitor_with_workspaces_round_trips_through_json() {
assert_eq!(back, monitor);
assert_eq!(serde_json::to_value(&back).unwrap(), json);
}
#[test]
fn removing_the_workspace_at_the_length_focuses_the_last_one_and_removes_nothing() {
fresh();
let mut monitor = monitor(3);
assert!(monitor.remove_workspace_by_idx(3).is_none());
assert_eq!(monitor.workspaces().len(), 3);
assert_eq!(monitor.focused_workspace_idx(), 2);
}
#[test]
fn an_old_monitor_file_with_workspaces_and_without_optional_keys_loads() {
fresh();
let workspace = serde_json::to_value(Workspace::<StubPlatform>::default()).unwrap();
let mut workspace = workspace;
for key in ["layout_options_rules", "name", "layout_flip", "wallpaper"] {
workspace.as_object_mut().unwrap().remove(key);
}
let monitor: Monitor<StubPlatform> = serde_json::from_value(serde_json::json!({
"id": 1, "device": "d",
"size": { "left": 0, "top": 0, "right": 1, "bottom": 1 },
"work_area_size": { "left": 0, "top": 0, "right": 1, "bottom": 1 },
"work_area_offset": null, "window_based_work_area_offset": null,
"window_based_work_area_offset_limit": 1,
"workspaces": { "elements": [workspace.clone(), workspace], "focused": 1 },
"container_padding": null, "workspace_padding": null,
"wallpaper": null, "floating_layer_behaviour": null
}))
.unwrap();
assert_eq!(monitor.workspaces().len(), 2);
assert_eq!(monitor.focused_workspace_idx(), 1);
assert!(monitor.workspaces()[0].layout_options_rules.is_empty());
}
@@ -559,3 +559,86 @@ fn the_fixtures_are_the_ones_the_e2e_library_has() {
);
}
}
#[test]
fn a_platform_that_overrides_no_optional_hook_gets_the_documented_defaults() {
use komorebi_layouts::DefaultLayout;
use komorebi_layouts::Layout;
let workspace = komorebi_core::Workspace::<Kde>::default();
assert_eq!(
Kde::primary_container_idx(&Layout::Default(DefaultLayout::BSP)).unwrap(),
0
);
assert!(
Kde::unmaximize_window(&KdeWindow {
id: 1,
json: Value::Null
})
.is_ok()
);
let () = Kde::hide_spot_on_display(&workspace);
assert_eq!(Kde::default_padding(), (None, None));
let mut workspace = workspace;
assert!(Kde::before_hide_containers(&mut workspace).is_none());
assert!(matches!(
Kde::begin_update(&mut workspace).unwrap(),
komorebi_core::Flow::Run(None)
));
assert_eq!(
Kde::end_update(&mut workspace, 0, 0, 0).unwrap(),
komorebi_core::Pass::Done
);
assert!(!Kde::adopt_widths_before_layout(&workspace));
assert_eq!(
Kde::scrolling_layouts(&mut workspace, &Default::default(), 0),
(vec![], vec![])
);
}
#[test]
fn the_default_place_hooks_fail_so_a_platform_must_override_them_to_use_update_shared() {
let mut workspace = komorebi_core::Workspace::<Kde>::default();
workspace
.new_container_for_window(KdeWindow {
id: 1,
json: Value::Null,
})
.unwrap();
workspace.globals.work_area = komorebi_layouts::Rect {
left: 0,
top: 0,
right: 100,
bottom: 100,
};
assert!(workspace.update_shared().is_err());
}
#[test]
fn the_json_keys_of_a_monitor_per_platform_extra() {
fn keys(written: &Value) -> Vec<String> {
let mut keys: Vec<String> = written["elements"][0]
.as_object()
.unwrap()
.keys()
.cloned()
.collect();
keys.sort();
keys
}
let windows = keys(&round_trip::<Windows>(&json(KFW_TILED)).1);
let macos = keys(&round_trip::<Macos>(&json(MACOS_TILED)).1);
assert!(windows.starts_with(&[
"container_padding".to_string(),
"device".to_string(),
"device_id".to_string()
]));
for key in ["name", "device_id", "workspace_names"] {
assert!(windows.contains(&key.to_string()), "{key}");
}
assert!(!windows.contains(&"last_focused_workspace".to_string()));
assert!(macos.contains(&"window_hiding_position".to_string()));
assert!(!macos.contains(&"name".to_string()));
}
@@ -667,3 +667,85 @@ fn only_the_default_layouts_are_the_scrolling_layout_when_it_is_scrolling() {
workspace.layout = Layout::Default(DefaultLayout::Scrolling);
assert!(workspace.is_scrolling());
}
#[test]
fn a_placement_rule_that_matches_the_window_decides_where_it_goes() {
use komorebi_config::placement::InitialWindowPlacementRules;
fresh();
let rules = |json: serde_json::Value| -> InitialWindowPlacementRules {
serde_json::from_value(json).unwrap()
};
let single = serde_json::json!({ "Last": { "kind": "Exe", "id": "exe9" } });
let many = serde_json::json!({ "Last": [{ "kind": "Exe", "id": "other" }, { "kind": "Exe", "id": "exe9" }] });
let secondary = serde_json::json!({ "Secondary": { "kind": "Exe", "id": "exe9" } });
for (json, expected) in [(single, 3), (many, 3), (secondary, 1)] {
let mut workspace = workspace_of(&[1, 2, 3]);
workspace.focus_container(0);
workspace.initial_window_placement_rules = Some(rules(json.clone()));
// no rule matches this window: after the focused container
assert_eq!(workspace.resolve_placement_index(&w(9)), 1, "{json}");
set_match_id("exe9");
assert_eq!(workspace.resolve_placement_index(&w(9)), expected, "{json}");
fresh();
}
}
#[test]
fn the_hooks_a_platform_does_not_override_do_nothing_to_the_workspace() {
fresh();
let mut workspace = workspace_of(&[1, 2]);
workspace.floating_windows_mut().push_back(w(5));
workspace.floating_windows_mut().push_back(w(6));
workspace.focus_floating_window(1);
// the stub overrides after_floating_removed only to log: the focus index is the workspace's
// own business, and the default platform of the other tests keeps it
workspace.remove_floating_window(6);
assert_eq!(workspace.focused_floating_window_idx(), 1);
}
#[test]
fn the_json_keys_of_a_default_workspace_are_the_ones_every_daemon_prints() {
fresh();
let value = serde_json::to_value(Workspace::<StubPlatform>::default()).unwrap();
let mut keys: Vec<&str> = value
.as_object()
.unwrap()
.keys()
.map(String::as_str)
.collect();
keys.sort_unstable();
assert_eq!(
keys,
[
"apply_window_based_work_area_offset",
"container_padding",
"containers",
"float_override",
"floating_layer_behaviour",
"floating_windows",
"latest_layout",
"layer",
"layout",
"layout_flip",
"layout_options",
"layout_options_rules",
"layout_rules",
"maximized_window",
"monocle_container",
"name",
"resize_dimensions",
"tile",
"wallpaper",
"window_container_behaviour",
"window_container_behaviour_rules",
"work_area_offset",
"work_area_offset_rules",
"workspace_padding",
]
);
}
+16 -1
View File
@@ -284,7 +284,7 @@ fn a_floating_window_is_removed_by_id_and_the_hook_runs_before_it_goes() {
let removed = workspace.remove_floating_window(8).unwrap();
assert_eq!(removed.id, 8);
assert_eq!(log(), ["after_floating_removed"]);
assert_eq!(log(), ["after_floating_removed len=2 focus=0"]);
assert!(workspace.remove_floating_window(8).is_none());
// nothing is logged for a window that is not there
assert!(log().is_empty());
@@ -517,3 +517,18 @@ fn an_empty_container_among_the_containers_is_hidden_without_a_call() {
assert_eq!(log(), ["before_hide_containers", "hide 1 @100"]);
}
#[test]
fn the_floating_removal_hook_runs_before_the_window_is_taken_out() {
fresh();
let mut workspace = Workspace::<StubPlatform>::default();
workspace.floating_windows_mut().push_back(w(1));
workspace.floating_windows_mut().push_back(w(2));
workspace.focus_floating_window(1);
assert_eq!(workspace.remove_floating_window(2).map(|w| w.id), Some(2));
// pins today's order (the KDE and macOS order): the hook sees both windows and the old
// focus, although the trait doc says "after a floating window was taken out"
assert_eq!(log(), ["after_floating_removed len=2 focus=1"]);
}