diff --git a/nix/tests/vm/lib/komorebi_vm/core/platform.py b/nix/tests/vm/lib/komorebi_vm/core/platform.py index d20d264c..5d6414bc 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/platform.py +++ b/nix/tests/vm/lib/komorebi_vm/core/platform.py @@ -114,8 +114,12 @@ class Platform(Protocol): """The daemon's state.""" ... - def spawn(self, title: str) -> Client: - """Opens a window with this title and waits until the platform has it.""" + def spawn(self, title: str, app_id: str | None = None) -> Client: + """Opens a window with this title and waits until the platform has it. + + `app_id` is the class of the window (its app id on Wayland, its window class on Windows) + for a rule that matches by it; the platform's own when there is none. + """ ... def os_window(self, client: Client) -> OsWindow | None: diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules_class.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules_class.py new file mode 100644 index 00000000..bf3fb8c4 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules_class.py @@ -0,0 +1,49 @@ +"""Rules that match a window by its class (its app id on Wayland) decide where it goes, or that it +is left alone.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Platform + + +def run(platform: Platform, checks: Checks) -> None: + """Gives the daemon a workspace rule and an ignore rule by class, opens a window of each + class and one of neither, and checks where each went. + + The daemon must be running, on the first workspace, which is the one in view, with no window, + and with workspaces 0 and 1. The rules are cleared afterwards. + """ + platform.komorebic("ensure-workspaces", "0", "2") + platform.komorebic("workspace-rule", "class", "e2e-sent", "0", "1") + platform.komorebic("ignore-rule", "class", "e2e-left") + + sent = platform.spawn("by-class-sent", app_id="e2e-sent") + checks.check( + "a window a class rule matches goes to the workspace the rule names", + platform.wait_for(lambda: platform.state().workspace_of(sent.title) == 1, timeout=20), + ) + checks.check( + "... where it is put away while another workspace is in view", + platform.wait_for(lambda: platform.hidden(sent)), + ) + + left = platform.spawn("by-class-left", app_id="e2e-left") + checks.check( + "a window an ignore rule by class matches is not managed", + not platform.wait_for( + lambda: platform.state().workspace_of(left.title) is not None, timeout=3 + ), + ) + + plain = platform.spawn("by-class-plain", app_id="e2e-other") + checks.check( + "a window of another class stays on the first workspace", + platform.wait_for(lambda: platform.state().workspace_of(plain.title) == 0, timeout=20), + ) + + platform.komorebic("clear-all-workspace-rules") + for client in (sent, left, plain): + platform.close(client) + checks.check( + "the three windows are closed", + platform.wait_for(lambda: platform.state().managed() == 0, timeout=20), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/kde_platform.py b/nix/tests/vm/lib/komorebi_vm/kde_platform.py index 98fbb437..b4f35208 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde_platform.py +++ b/nix/tests/vm/lib/komorebi_vm/kde_platform.py @@ -74,8 +74,9 @@ class KdePlatform: def state(self) -> Snapshot: return snapshot.parse(self.kde.state()) - def spawn(self, title: str) -> Client: - self.kde.spawn(f"foot -T {shlex.quote(title)} sleep 3000") + def spawn(self, title: str, app_id: str | None = None) -> Client: + name = f"-a {shlex.quote(app_id)} " if app_id else "" + self.kde.spawn(f"foot {name}-T {shlex.quote(title)} sleep 3000") self.kde.wait_for(lambda: self.kde.wid(title) != "", timeout=30) return Client(title) diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index 89ea544c..676c9f06 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -80,6 +80,8 @@ class FakePlatform: self.display_of: dict[str, int] = {} self.forgets_displays = False self.floats: list[str] = [] + self.class_rules: dict[str, int] = {} + self.ignored_classes: set[str] = set() self.tiling = True self.paused = False self.flipped = False @@ -144,7 +146,13 @@ class FakePlatform: """The titles in view, in the order of the tiles.""" return [stack[0] for stack in self.containers] - def spawn(self, title: str) -> Client: + def spawn(self, title: str, app_id: str | None = None) -> Client: + if app_id in self.ignored_classes: + self.unmanaged.append(title) + return Client(title) + if app_id in self.class_rules: + self.spaces[self.class_rules[app_id]].append([title]) + return Client(title) if self.paused: self.unmanaged.append(title) return Client(title) @@ -379,12 +387,17 @@ class FakePlatform: self.monocle = None if self.monocle else top case ("ensure-workspaces", _, _) | ("clear-all-workspace-rules",): if args[0] == "clear-all-workspace-rules": + self.class_rules.clear() self.rules.clear() self.initial_rules.clear() case ("initial-workspace-rule", "title", title, _, number): self.initial_rules[title] = int(number) case ("workspace-rule", "title", title, _, number): self.rules[title] = int(number) + case ("workspace-rule", "class", name, _, number): + self.class_rules[name] = int(number) + case ("ignore-rule", "class", name): + self.ignored_classes.add(name) case ("ignore-rule", "title", title): self.ignored.add(title) case ("move-to-workspace", number): diff --git a/nix/tests/vm/lib/tests/test_focus_moving.py b/nix/tests/vm/lib/tests/test_focus_moving.py index aeb9520c..4656c405 100644 --- a/nix/tests/vm/lib/tests/test_focus_moving.py +++ b/nix/tests/vm/lib/tests/test_focus_moving.py @@ -25,6 +25,7 @@ from komorebi_vm.core.scenarios import ( resize, resize_commands, rules, + rules_class, scrolling, stacks, startup_rules, @@ -433,6 +434,25 @@ def test_a_daemon_that_loses_the_focused_window_off_screen_fails_scrolling() -> assert checks.failed > 0 +def test_a_daemon_that_is_right_passes_rules_class() -> None: + platform = FakePlatform() + checks = Checks() + rules_class.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_ignores_class_rules_fails_rules_class() -> None: + class Deaf(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if "class" in args else super().komorebic(*args) + + platform = Deaf() + checks = Checks() + rules_class.run(platform, checks) + assert checks.failed > 0 + + def test_a_command_the_port_lacks_is_skipped_by_name() -> None: platform = FakePlatform() checks = Checks() diff --git a/nix/tests/vm/lib/tests/test_tiling.py b/nix/tests/vm/lib/tests/test_tiling.py index 8aa4e6ba..10a5fd83 100644 --- a/nix/tests/vm/lib/tests/test_tiling.py +++ b/nix/tests/vm/lib/tests/test_tiling.py @@ -21,7 +21,7 @@ class FakePlatform: def wait_for(self, predicate, timeout: float = 10) -> bool: # noqa: ANN001 return predicate() - def spawn(self, title: str) -> Client: + def spawn(self, title: str, app_id: str | None = None) -> Client: self.opened.append(title) return Client(title) diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index 220ae828..c94e565f 100644 --- a/nix/tests/vm/suites/core-e2e/test.py +++ b/nix/tests/vm/suites/core-e2e/test.py @@ -33,6 +33,7 @@ from komorebi_vm.core.scenarios import ( promoting, resize_commands, rules, + rules_class, scrolling, stacks, startup_rules, @@ -484,6 +485,9 @@ for title in ("neutral2", "neutral3"): checks.check("no window is left", kde.wait_for(lambda: kde.managed_all() == 0, timeout=20)) platform.komorebic("focus-workspace", "0") rules.run(platform, checks) +checks.check("no window is left", kde.wait_for(lambda: kde.managed_all() == 0, timeout=20)) +print("== 12. rules by class") +rules_class.run(platform, checks) print("== 12a. where a window that opens goes, from the placement rules of the config") placement.run(platform, checks)