refactor(kde): match window rules with the shared komorebi-config::rules

KDE's should_act and should_act_individual become thin wrappers over
komorebi_config::rules (RuleQuirks::UNIX: a `.exe` in a rule id is
ignored, as before); their signatures and the should_act_window wrappers
are unchanged, so no call site moves. The 12 pure matching tests of
komorebi-for-kde/komorebi/tests/rule_matching.rs move to
crates/komorebi-config/tests/rule_matching.rs with their assertions
untouched (they call same-signature local wrappers over the shared
function); the manage-rule rescue test, which needs the fake backend, stays
in the KDE file.

No behaviour change: the function bodies were identical (docs/dev/
rule-matching.md). Verified: cargo test -p komorebi-config --test
rule_matching (12 passed); KDE cargo test -p komorebi-linux --lib (292
passed) and --test rule_matching (1 passed), command_sweep, rules; cargo
check --all-targets clean. KDE clippy -D warnings fails on 4 pre-existing
lints in process_command.rs and kwin/mod.rs (not touched here). Not run:
VM suites.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent b3bcf4a2c3
commit ac168c9657
3 files changed
+516 -716

No files matched your search

@@ -0,0 +1,481 @@
//! How a rule matches a window: the kinds (title, class, exe, path), every matching strategy,
//! composite rules and the last rule that matches winning. This is the one implementation all
//! three daemons call (`komorebi_config::rules`), so what the `rules`, `rules_class`, `rules_exe`,
//! `title_rule` and `startup_rules` end-to-end scenarios assert about WHICH window a rule catches
//! is asserted here once, with no window manager. These tests are the ones the KDE daemon had
//! (`komorebi-for-kde/komorebi/tests/rule_matching.rs`), moved with the function; they run the
//! KDE/macOS input shape (`RuleQuirks::UNIX`, a window with a list of classes).
//!
//! Not covered here, and so still end-to-end: that the daemon applies the rule at the right moment
//! (when the window opens, not when it is renamed later), that the window then lands on the right
//! workspace and is hidden or shown, what the OS reports as a window's title, class and
//! executable, and that an ignore rule is overridden by a manage rule (the daemon's
//! `should_manage`; KDE has `rule_matching.rs` for that).
use komorebi_config::matching::IdWithIdentifier;
use komorebi_config::matching::MatchingRule;
use komorebi_config::matching::MatchingStrategy;
use komorebi_config::rules::RuleQuirks;
use komorebi_config::rules::RuleSubject;
use komorebi_protocol::types::ApplicationIdentifier;
use regex::Regex;
use std::collections::HashMap;
fn should_act_individual(
title: &str,
exe_name: &str,
classes: &[&str],
path: &str,
rule: &IdWithIdentifier,
regex: &HashMap<String, Regex>,
) -> bool {
komorebi_config::rules::should_act_individual(
&RuleSubject {
title,
exe_name,
classes,
path,
},
RuleQuirks::UNIX,
rule,
regex,
)
}
fn should_act(
title: &str,
exe_name: &str,
classes: &[&str],
path: &str,
rules: &[MatchingRule],
regex: &HashMap<String, Regex>,
) -> Option<MatchingRule> {
komorebi_config::rules::should_act(
&RuleSubject {
title,
exe_name,
classes,
path,
},
RuleQuirks::UNIX,
rules,
regex,
)
}
fn should_act_window(
title: &str,
exe_name: &str,
class: &str,
path: &str,
rules: &[MatchingRule],
regex: &HashMap<String, Regex>,
) -> Option<MatchingRule> {
should_act(title, exe_name, &[class], path, rules, regex)
}
fn id(
kind: ApplicationIdentifier,
id: &str,
strategy: Option<MatchingStrategy>,
) -> IdWithIdentifier {
IdWithIdentifier {
kind,
id: id.to_string(),
matching_strategy: strategy,
}
}
fn simple(kind: ApplicationIdentifier, text: &str, strategy: MatchingStrategy) -> MatchingRule {
MatchingRule::Simple(id(kind, text, Some(strategy)))
}
fn no_regex() -> HashMap<String, Regex> {
HashMap::new()
}
/// The window every test asks about.
const TITLE: &str = "komorebi-e2e: rule target";
const EXE: &str = "kwrite";
const CLASS: &str = "org.kde.kwrite";
const PATH: &str = "/usr/bin/kwrite";
fn matches(rule: &IdWithIdentifier) -> bool {
should_act_individual(TITLE, EXE, &[CLASS], PATH, rule, &no_regex())
}
fn kinds() -> [ApplicationIdentifier; 4] {
[
ApplicationIdentifier::Title,
ApplicationIdentifier::Class,
ApplicationIdentifier::Exe,
ApplicationIdentifier::Path,
]
}
fn text_of(kind: ApplicationIdentifier) -> &'static str {
match kind {
ApplicationIdentifier::Title => TITLE,
ApplicationIdentifier::Class => CLASS,
ApplicationIdentifier::Exe => EXE,
ApplicationIdentifier::Path => PATH,
}
}
// mirrors: rules / a window an initial rule matches goes to the workspace the rule names (by title)
// mirrors: rules_class / a window a class rule matches goes to the workspace the rule names
// mirrors: rules_exe / a window an exe rule matches goes to the workspace the rule names
#[test]
fn equals_matches_the_whole_text_of_each_kind_and_nothing_else() {
for kind in kinds() {
let text = text_of(kind);
assert!(
matches(&id(kind, text, Some(MatchingStrategy::Equals))),
"{kind:?}"
);
assert!(
!matches(&id(
kind,
&text[..text.len() - 1],
Some(MatchingStrategy::Equals)
)),
"{kind:?}: a prefix is not equal"
);
assert!(
!matches(&id(
kind,
&format!("{text}x"),
Some(MatchingStrategy::Equals)
)),
"{kind:?}: a longer text is not equal"
);
}
}
// mirrors: rules / a window of another title stays (the rule does not catch it)
// mirrors: rules_class / a window of another class stays on the first workspace
// mirrors: rules_exe / a window of another program stays on the first workspace
#[test]
fn each_kind_looks_at_its_own_part_of_the_window_only() {
// the title of the window as the id of a class rule, and so on: no kind reads another's text
for kind in kinds() {
for other in kinds().into_iter().filter(|o| *o != kind) {
assert!(
!matches(&id(kind, text_of(other), Some(MatchingStrategy::Equals))),
"a {kind:?} rule matched the {other:?} of the window"
);
}
}
}
#[test]
fn starts_with_ends_with_and_contains_match_the_part_they_name() {
for kind in kinds() {
let text = text_of(kind);
let head = &text[..4];
let tail = &text[text.len() - 4..];
let middle = &text[2..text.len() - 2];
for (strategy, yes, no) in [
(MatchingStrategy::StartsWith, head, tail),
(MatchingStrategy::EndsWith, tail, head),
(MatchingStrategy::Contains, middle, "not-in-the-window"),
] {
assert!(
matches(&id(kind, yes, Some(strategy.clone()))),
"{kind:?} {strategy}"
);
assert!(
!matches(&id(kind, no, Some(strategy.clone()))),
"{kind:?} {strategy}"
);
}
}
}
#[test]
fn every_negated_strategy_is_the_opposite_of_its_positive_one() {
for kind in kinds() {
let text = text_of(kind);
let head = &text[..4];
let tail = &text[text.len() - 4..];
for (positive, negative, text) in [
(
MatchingStrategy::Equals,
MatchingStrategy::DoesNotEqual,
text,
),
(
MatchingStrategy::StartsWith,
MatchingStrategy::DoesNotStartWith,
head,
),
(
MatchingStrategy::EndsWith,
MatchingStrategy::DoesNotEndWith,
tail,
),
(
MatchingStrategy::Contains,
MatchingStrategy::DoesNotContain,
head,
),
] {
for probe in [text, "zzzz-not-there"] {
let p = matches(&id(kind, probe, Some(positive.clone())));
let n = matches(&id(kind, probe, Some(negative.clone())));
assert_ne!(p, n, "{kind:?}: {positive} and {negative} for {probe:?}");
}
}
}
}
// mirrors: rules / a window a rule by the name of a workspace matches goes to that workspace
#[test]
fn a_regex_rule_matches_with_the_compiled_regex_and_nothing_without_one() {
let pattern = "^komorebi-e2e: .* target$";
let rule = id(
ApplicationIdentifier::Title,
pattern,
Some(MatchingStrategy::Regex),
);
let mut compiled = HashMap::new();
compiled.insert(pattern.to_string(), Regex::new(pattern).unwrap());
assert!(should_act_individual(
TITLE,
EXE,
&[CLASS],
PATH,
&rule,
&compiled
));
assert!(!should_act_individual(
"another",
EXE,
&[CLASS],
PATH,
&rule,
&compiled
));
// the daemon compiles every regex when it loads the rules: an id it has not compiled matches nothing
assert!(!should_act_individual(
TITLE,
EXE,
&[CLASS],
PATH,
&rule,
&no_regex()
));
}
#[test]
fn a_regex_rule_can_catch_any_of_the_classes_of_a_window() {
let pattern = "^org\\.kde\\.";
let rule = id(
ApplicationIdentifier::Class,
pattern,
Some(MatchingStrategy::Regex),
);
let mut compiled = HashMap::new();
compiled.insert(pattern.to_string(), Regex::new(pattern).unwrap());
assert!(should_act_individual(
TITLE,
EXE,
&["x", CLASS],
PATH,
&rule,
&compiled
));
assert!(!should_act_individual(
TITLE,
EXE,
&["x", "y"],
PATH,
&rule,
&compiled
));
}
// mirrors: rules_exe / a window an exe rule matches goes to the workspace the rule names
// (a rule written the Windows way, `kwrite.exe`, names the same program)
#[test]
fn an_exe_suffix_in_a_rule_id_is_ignored() {
assert!(matches(&id(
ApplicationIdentifier::Exe,
"kwrite.exe",
Some(MatchingStrategy::Equals)
)));
assert!(matches(&id(ApplicationIdentifier::Exe, "kwrite.exe", None)));
}
#[test]
fn a_rule_without_a_strategy_or_with_legacy_is_the_old_one() {
for strategy in [None, Some(MatchingStrategy::Legacy)] {
// title and class: starts or ends with the id
assert!(matches(&id(
ApplicationIdentifier::Title,
"komorebi-e2e",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Title,
"rule target",
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Title,
"e2e: rule",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Class,
"org.kde",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Class,
"kwrite",
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Class,
"kde.kw",
strategy.clone()
)));
// exe and path: equal to the id
assert!(matches(&id(
ApplicationIdentifier::Exe,
EXE,
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Exe,
"kwri",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Path,
PATH,
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Path,
"/usr/bin",
strategy.clone()
)));
}
}
// mirrors: rules / a window an ignore rule matches is not managed (which window)
#[test]
fn a_simple_rule_is_matched_by_one_condition_and_a_composite_by_all_of_them() {
let title = id(
ApplicationIdentifier::Title,
TITLE,
Some(MatchingStrategy::Equals),
);
let class = id(
ApplicationIdentifier::Class,
CLASS,
Some(MatchingStrategy::Equals),
);
let other = id(
ApplicationIdentifier::Exe,
"not-kwrite",
Some(MatchingStrategy::Equals),
);
let both = MatchingRule::Composite(vec![title.clone(), class.clone()]);
let one_wrong = MatchingRule::Composite(vec![title.clone(), other.clone()]);
let regex = no_regex();
assert_eq!(
should_act(
TITLE,
EXE,
&[CLASS],
PATH,
std::slice::from_ref(&both),
&regex
),
Some(both.clone())
);
assert_eq!(
should_act(TITLE, EXE, &[CLASS], PATH, &[one_wrong], &regex),
None
);
assert_eq!(
should_act(
TITLE,
EXE,
&[CLASS],
PATH,
&[MatchingRule::Simple(title.clone())],
&regex
),
Some(MatchingRule::Simple(title))
);
}
// mirrors: rules / a rule that is not initial sends the window back at the next event (which
// of two rules wins)
#[test]
fn of_two_rules_that_match_the_last_one_is_the_answer() {
let first = simple(
ApplicationIdentifier::Class,
CLASS,
MatchingStrategy::Equals,
);
let last = simple(
ApplicationIdentifier::Title,
"komorebi-e2e",
MatchingStrategy::StartsWith,
);
let rules = [first.clone(), last.clone()];
assert_eq!(
should_act_window(TITLE, EXE, CLASS, PATH, &rules, &no_regex()),
Some(last)
);
assert_eq!(
should_act_window(
TITLE,
EXE,
CLASS,
PATH,
std::slice::from_ref(&first),
&no_regex()
),
Some(first)
);
}
// mirrors: rules / once the rules are cleared, a window they matched stays on the workspace in view
#[test]
fn no_rules_match_nothing() {
assert_eq!(
should_act_window(TITLE, EXE, CLASS, PATH, &[], &no_regex()),
None
);
}
// mirrors: title_rule / a window the ignore rule matches by its title is left alone
// mirrors: title_rule / it gives itself another title (the same rule no longer matches)
#[test]
fn a_rule_by_title_follows_the_title_it_is_asked_about() {
let rule = simple(
ApplicationIdentifier::Title,
"secret",
MatchingStrategy::Contains,
);
let ask = |title: &str| {
should_act_window(
title,
EXE,
CLASS,
PATH,
std::slice::from_ref(&rule),
&no_regex(),
)
};
assert!(ask("a secret window").is_some());
assert!(ask("a renamed window").is_none());
}
+31 -305
View File
@@ -24,17 +24,18 @@ use crate::MANAGE_IDENTIFIERS;
use crate::PERMAIGNORE_CLASSES;
use crate::REGEX_IDENTIFIERS;
use crate::backend::backend;
use crate::core::ApplicationIdentifier;
use crate::core::Rect;
use crate::core::config_generation::IdWithIdentifier;
use crate::core::config_generation::MatchingRule;
use crate::core::config_generation::MatchingStrategy;
use crate::window_manager_event::WindowManagerEvent;
use color_eyre::eyre;
use color_eyre::eyre::OptionExt;
use komorebi_backend::WindowId;
pub use komorebi_config::aspect_ratio::AspectRatio;
pub use komorebi_config::aspect_ratio::PredefinedAspectRatio;
use komorebi_config::rules;
use komorebi_config::rules::RuleQuirks;
use komorebi_config::rules::RuleSubject;
use regex::Regex;
use serde::Deserialize;
use serde::Serialize;
@@ -568,10 +569,6 @@ fn window_is_eligible(
true
}
/// Returns the last of `identifiers` that matches a window with this title, executable, classes
/// and path, if one does: a simple rule matches when its one condition does
/// ([`should_act_individual`]), and a composite rule when all of its conditions do
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
/// [`should_act`] for a window that has one class
pub fn should_act_window(
title: &str,
@@ -610,6 +607,9 @@ pub fn should_act_window_individual(
)
}
/// Returns the last of `identifiers` that matches a window with this title, executable, classes
/// and path, if one does (the shared evaluation, [`komorebi_config::rules::should_act`], with a
/// `.exe` in a rule id ignored)
pub fn should_act(
title: &str,
exe_name: &str,
@@ -618,52 +618,22 @@ pub fn should_act(
identifiers: &[MatchingRule],
regex_identifiers: &HashMap<String, Regex>,
) -> Option<MatchingRule> {
let mut matching_rule = None;
for rule in identifiers {
match rule {
MatchingRule::Simple(identifier) => {
if should_act_individual(
title,
exe_name,
classes,
path,
identifier,
regex_identifiers,
) {
matching_rule = Some(rule.clone());
};
}
MatchingRule::Composite(identifiers) => {
let mut composite_results = vec![];
for identifier in identifiers {
composite_results.push(should_act_individual(
title,
exe_name,
classes,
path,
identifier,
regex_identifiers,
));
}
if composite_results.iter().all(|&x| x) {
matching_rule = Some(rule.clone());
}
}
}
}
matching_rule
rules::should_act(
&RuleSubject {
title,
exe_name,
classes,
path,
},
RuleQuirks::UNIX,
identifiers,
regex_identifiers,
)
}
/// Returns true if and only if the condition `identifier` holds for a window with this title,
/// executable, classes and path
///
/// The condition compares the title, the executable, any of the classes or the path with its id,
/// by its matching strategy. Without one, or with `Legacy`, a title or a class matches when it
/// starts or ends with the id, and an executable or a path when it is the id. A `Regex` id is
/// looked up in `regex_identifiers`, and an id with no compiled regex there matches nothing. A
/// `.exe` in the id is ignored.
/// executable, classes and path (the shared evaluation,
/// [`komorebi_config::rules::should_act_individual`], with a `.exe` in the id ignored)
pub fn should_act_individual(
title: &str,
exe_name: &str,
@@ -672,263 +642,17 @@ pub fn should_act_individual(
identifier: &IdWithIdentifier,
regex_identifiers: &HashMap<String, Regex>,
) -> bool {
let mut should_act = false;
let mut identifier = identifier.clone();
identifier.id = identifier.id.replace(".exe", "");
match identifier.matching_strategy {
None | Some(MatchingStrategy::Legacy) => match identifier.kind {
ApplicationIdentifier::Title => {
if title.starts_with(&identifier.id) || title.ends_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if class.starts_with(&identifier.id) || class.ends_with(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if exe_name.eq(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if path.eq(&identifier.id) {
should_act = true;
}
}
rules::should_act_individual(
&RuleSubject {
title,
exe_name,
classes,
path,
},
Some(MatchingStrategy::Equals) => match identifier.kind {
ApplicationIdentifier::Title => {
if title.eq(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if class.eq(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if exe_name.eq(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if path.eq(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::DoesNotEqual) => match identifier.kind {
ApplicationIdentifier::Title => {
if !title.eq(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if !class.eq(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if !exe_name.eq(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if !path.eq(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::StartsWith) => match identifier.kind {
ApplicationIdentifier::Title => {
if title.starts_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if class.starts_with(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if exe_name.starts_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if path.starts_with(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::DoesNotStartWith) => match identifier.kind {
ApplicationIdentifier::Title => {
if !title.starts_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if !class.starts_with(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if !exe_name.starts_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if !path.starts_with(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::EndsWith) => match identifier.kind {
ApplicationIdentifier::Title => {
if title.ends_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if class.ends_with(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if exe_name.ends_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if path.ends_with(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::DoesNotEndWith) => match identifier.kind {
ApplicationIdentifier::Title => {
if !title.ends_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if !class.ends_with(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if !exe_name.ends_with(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if !path.ends_with(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::Contains) => match identifier.kind {
ApplicationIdentifier::Title => {
if title.contains(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if class.contains(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if exe_name.contains(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if path.contains(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::DoesNotContain) => match identifier.kind {
ApplicationIdentifier::Title => {
if !title.contains(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Class => {
for class in classes {
if !class.contains(&identifier.id) {
should_act = true;
}
}
}
ApplicationIdentifier::Exe => {
if !exe_name.contains(&identifier.id) {
should_act = true;
}
}
ApplicationIdentifier::Path => {
if !path.contains(&identifier.id) {
should_act = true;
}
}
},
Some(MatchingStrategy::Regex) => match identifier.kind {
ApplicationIdentifier::Title => {
if let Some(re) = regex_identifiers.get(&identifier.id)
&& re.is_match(title)
{
should_act = true;
}
}
ApplicationIdentifier::Class => {
if let Some(re) = regex_identifiers.get(&identifier.id) {
for class in classes {
if re.is_match(class) {
should_act = true;
}
}
}
}
ApplicationIdentifier::Exe => {
if let Some(re) = regex_identifiers.get(&identifier.id)
&& re.is_match(exe_name)
{
should_act = true;
}
}
ApplicationIdentifier::Path => {
if let Some(re) = regex_identifiers.get(&identifier.id)
&& re.is_match(path)
{
should_act = true;
}
}
},
}
should_act
RuleQuirks::UNIX,
identifier,
regex_identifiers,
)
}
#[cfg(test)]
@@ -937,6 +661,8 @@ mod tests {
use crate::backend::fake::Command;
use crate::backend::fake::FakeBackend;
use crate::backend::with_backend;
use crate::core::ApplicationIdentifier;
use crate::core::config_generation::MatchingStrategy;
use std::sync::Arc;
fn fake_with(id: u32) -> Arc<FakeBackend> {
@@ -1,426 +1,19 @@
//! How a rule matches a window: the kinds (title, class, exe, path), every matching strategy,
//! composite rules, the last rule that matches winning, and a manage rule rescuing a window an
//! ignore rule covers. These are the pure functions every rule-driven behaviour of the daemon
//! goes through, so what the `rules`, `rules_class`, `rules_exe`, `manage_rules`, `title_rule`
//! and `startup_rules` end-to-end scenarios assert about WHICH window a rule catches is asserted
//! here with no window manager.
//!
//! Not covered here, and so still end-to-end: that the daemon applies the rule at the right moment
//! (when the window opens, not when it is renamed later), that the window then lands on the right
//! workspace and is hidden or shown, and what the OS reports as a window's title, class and
//! executable.
//! A manage rule rescuing a window an ignore rule covers, through the KDE daemon. Which window a
//! rule matches (kinds, strategies, composite, last wins) is tested once for all daemons in
//! `crates/komorebi-config/tests/rule_matching.rs`; what stays here is the daemon's `should_manage`
//! on the fake backend.
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
use komorebi::core::ApplicationIdentifier;
use komorebi::core::SocketMessage;
use komorebi::core::config_generation::IdWithIdentifier;
use komorebi::core::config_generation::MatchingRule;
use komorebi::core::config_generation::MatchingStrategy;
use komorebi::window::Window;
use komorebi::window::should_act;
use komorebi::window::should_act_individual;
use komorebi::window::should_act_window;
use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use regex::Regex;
use std::collections::HashMap;
use std::sync::Arc;
fn id(
kind: ApplicationIdentifier,
id: &str,
strategy: Option<MatchingStrategy>,
) -> IdWithIdentifier {
IdWithIdentifier {
kind,
id: id.to_string(),
matching_strategy: strategy,
}
}
fn simple(kind: ApplicationIdentifier, text: &str, strategy: MatchingStrategy) -> MatchingRule {
MatchingRule::Simple(id(kind, text, Some(strategy)))
}
fn no_regex() -> HashMap<String, Regex> {
HashMap::new()
}
/// The window every test asks about.
const TITLE: &str = "komorebi-e2e: rule target";
const EXE: &str = "kwrite";
const CLASS: &str = "org.kde.kwrite";
const PATH: &str = "/usr/bin/kwrite";
fn matches(rule: &IdWithIdentifier) -> bool {
should_act_individual(TITLE, EXE, &[CLASS], PATH, rule, &no_regex())
}
fn kinds() -> [ApplicationIdentifier; 4] {
[
ApplicationIdentifier::Title,
ApplicationIdentifier::Class,
ApplicationIdentifier::Exe,
ApplicationIdentifier::Path,
]
}
fn text_of(kind: ApplicationIdentifier) -> &'static str {
match kind {
ApplicationIdentifier::Title => TITLE,
ApplicationIdentifier::Class => CLASS,
ApplicationIdentifier::Exe => EXE,
ApplicationIdentifier::Path => PATH,
}
}
// mirrors: rules / a window an initial rule matches goes to the workspace the rule names (by title)
// mirrors: rules_class / a window a class rule matches goes to the workspace the rule names
// mirrors: rules_exe / a window an exe rule matches goes to the workspace the rule names
#[test]
fn equals_matches_the_whole_text_of_each_kind_and_nothing_else() {
for kind in kinds() {
let text = text_of(kind);
assert!(
matches(&id(kind, text, Some(MatchingStrategy::Equals))),
"{kind:?}"
);
assert!(
!matches(&id(
kind,
&text[..text.len() - 1],
Some(MatchingStrategy::Equals)
)),
"{kind:?}: a prefix is not equal"
);
assert!(
!matches(&id(
kind,
&format!("{text}x"),
Some(MatchingStrategy::Equals)
)),
"{kind:?}: a longer text is not equal"
);
}
}
// mirrors: rules / a window of another title stays (the rule does not catch it)
// mirrors: rules_class / a window of another class stays on the first workspace
// mirrors: rules_exe / a window of another program stays on the first workspace
#[test]
fn each_kind_looks_at_its_own_part_of_the_window_only() {
// the title of the window as the id of a class rule, and so on: no kind reads another's text
for kind in kinds() {
for other in kinds().into_iter().filter(|o| *o != kind) {
assert!(
!matches(&id(kind, text_of(other), Some(MatchingStrategy::Equals))),
"a {kind:?} rule matched the {other:?} of the window"
);
}
}
}
#[test]
fn starts_with_ends_with_and_contains_match_the_part_they_name() {
for kind in kinds() {
let text = text_of(kind);
let head = &text[..4];
let tail = &text[text.len() - 4..];
let middle = &text[2..text.len() - 2];
for (strategy, yes, no) in [
(MatchingStrategy::StartsWith, head, tail),
(MatchingStrategy::EndsWith, tail, head),
(MatchingStrategy::Contains, middle, "not-in-the-window"),
] {
assert!(
matches(&id(kind, yes, Some(strategy.clone()))),
"{kind:?} {strategy}"
);
assert!(
!matches(&id(kind, no, Some(strategy.clone()))),
"{kind:?} {strategy}"
);
}
}
}
#[test]
fn every_negated_strategy_is_the_opposite_of_its_positive_one() {
for kind in kinds() {
let text = text_of(kind);
let head = &text[..4];
let tail = &text[text.len() - 4..];
for (positive, negative, text) in [
(
MatchingStrategy::Equals,
MatchingStrategy::DoesNotEqual,
text,
),
(
MatchingStrategy::StartsWith,
MatchingStrategy::DoesNotStartWith,
head,
),
(
MatchingStrategy::EndsWith,
MatchingStrategy::DoesNotEndWith,
tail,
),
(
MatchingStrategy::Contains,
MatchingStrategy::DoesNotContain,
head,
),
] {
for probe in [text, "zzzz-not-there"] {
let p = matches(&id(kind, probe, Some(positive.clone())));
let n = matches(&id(kind, probe, Some(negative.clone())));
assert_ne!(p, n, "{kind:?}: {positive} and {negative} for {probe:?}");
}
}
}
}
// mirrors: rules / a window a rule by the name of a workspace matches goes to that workspace
#[test]
fn a_regex_rule_matches_with_the_compiled_regex_and_nothing_without_one() {
let pattern = "^komorebi-e2e: .* target$";
let rule = id(
ApplicationIdentifier::Title,
pattern,
Some(MatchingStrategy::Regex),
);
let mut compiled = HashMap::new();
compiled.insert(pattern.to_string(), Regex::new(pattern).unwrap());
assert!(should_act_individual(
TITLE,
EXE,
&[CLASS],
PATH,
&rule,
&compiled
));
assert!(!should_act_individual(
"another",
EXE,
&[CLASS],
PATH,
&rule,
&compiled
));
// the daemon compiles every regex when it loads the rules: an id it has not compiled matches nothing
assert!(!should_act_individual(
TITLE,
EXE,
&[CLASS],
PATH,
&rule,
&no_regex()
));
}
#[test]
fn a_regex_rule_can_catch_any_of_the_classes_of_a_window() {
let pattern = "^org\\.kde\\.";
let rule = id(
ApplicationIdentifier::Class,
pattern,
Some(MatchingStrategy::Regex),
);
let mut compiled = HashMap::new();
compiled.insert(pattern.to_string(), Regex::new(pattern).unwrap());
assert!(should_act_individual(
TITLE,
EXE,
&["x", CLASS],
PATH,
&rule,
&compiled
));
assert!(!should_act_individual(
TITLE,
EXE,
&["x", "y"],
PATH,
&rule,
&compiled
));
}
// mirrors: rules_exe / a window an exe rule matches goes to the workspace the rule names
// (a rule written the Windows way, `kwrite.exe`, names the same program)
#[test]
fn an_exe_suffix_in_a_rule_id_is_ignored() {
assert!(matches(&id(
ApplicationIdentifier::Exe,
"kwrite.exe",
Some(MatchingStrategy::Equals)
)));
assert!(matches(&id(ApplicationIdentifier::Exe, "kwrite.exe", None)));
}
#[test]
fn a_rule_without_a_strategy_or_with_legacy_is_the_old_one() {
for strategy in [None, Some(MatchingStrategy::Legacy)] {
// title and class: starts or ends with the id
assert!(matches(&id(
ApplicationIdentifier::Title,
"komorebi-e2e",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Title,
"rule target",
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Title,
"e2e: rule",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Class,
"org.kde",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Class,
"kwrite",
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Class,
"kde.kw",
strategy.clone()
)));
// exe and path: equal to the id
assert!(matches(&id(
ApplicationIdentifier::Exe,
EXE,
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Exe,
"kwri",
strategy.clone()
)));
assert!(matches(&id(
ApplicationIdentifier::Path,
PATH,
strategy.clone()
)));
assert!(!matches(&id(
ApplicationIdentifier::Path,
"/usr/bin",
strategy.clone()
)));
}
}
// mirrors: rules / a window an ignore rule matches is not managed (which window)
#[test]
fn a_simple_rule_is_matched_by_one_condition_and_a_composite_by_all_of_them() {
let title = id(
ApplicationIdentifier::Title,
TITLE,
Some(MatchingStrategy::Equals),
);
let class = id(
ApplicationIdentifier::Class,
CLASS,
Some(MatchingStrategy::Equals),
);
let other = id(
ApplicationIdentifier::Exe,
"not-kwrite",
Some(MatchingStrategy::Equals),
);
let both = MatchingRule::Composite(vec![title.clone(), class.clone()]);
let one_wrong = MatchingRule::Composite(vec![title.clone(), other.clone()]);
let regex = no_regex();
assert_eq!(
should_act(
TITLE,
EXE,
&[CLASS],
PATH,
std::slice::from_ref(&both),
&regex
),
Some(both.clone())
);
assert_eq!(
should_act(TITLE, EXE, &[CLASS], PATH, &[one_wrong], &regex),
None
);
assert_eq!(
should_act(
TITLE,
EXE,
&[CLASS],
PATH,
&[MatchingRule::Simple(title.clone())],
&regex
),
Some(MatchingRule::Simple(title))
);
}
// mirrors: rules / a rule that is not initial sends the window back at the next event (which
// of two rules wins)
#[test]
fn of_two_rules_that_match_the_last_one_is_the_answer() {
let first = simple(
ApplicationIdentifier::Class,
CLASS,
MatchingStrategy::Equals,
);
let last = simple(
ApplicationIdentifier::Title,
"komorebi-e2e",
MatchingStrategy::StartsWith,
);
let rules = [first.clone(), last.clone()];
assert_eq!(
should_act_window(TITLE, EXE, CLASS, PATH, &rules, &no_regex()),
Some(last)
);
assert_eq!(
should_act_window(TITLE, EXE, CLASS, PATH, &[first.clone()], &no_regex()),
Some(first)
);
}
// mirrors: rules / once the rules are cleared, a window they matched stays on the workspace in view
#[test]
fn no_rules_match_nothing() {
assert_eq!(
should_act_window(TITLE, EXE, CLASS, PATH, &[], &no_regex()),
None
);
}
// mirrors: title_rule / a window the ignore rule matches by its title is left alone
// mirrors: title_rule / it gives itself another title (the same rule no longer matches)
#[test]
fn a_rule_by_title_follows_the_title_it_is_asked_about() {
let rule = simple(
ApplicationIdentifier::Title,
"secret",
MatchingStrategy::Contains,
);
let ask =
|title: &str| should_act_window(title, EXE, CLASS, PATH, &[rule.clone()], &no_regex());
assert!(ask("a secret window").is_some());
assert!(ask("a renamed window").is_none());
}
fn added(id: u32) -> WindowManagerEvent {
WindowManagerEvent::Show(
SystemNotification::KWin(KWinNotification::WindowAdded),