diff --git a/docs/dev/suite-ledger.md b/docs/dev/suite-ledger.md index c7a0867d..0bef194a 100644 --- a/docs/dev/suite-ledger.md +++ b/docs/dev/suite-ledger.md @@ -122,10 +122,10 @@ keeps the part that is KWin's: a workspace is a virtual desktop. | 1, it is on the parking desktop while desktop 1 shows; on desktop 2 once shown, not parked; tiled where asked | extra `desktops` | | 1, the other window is where it opened (desktop 1) | extra `desktops` | | 2, the user takes the window back; an initial rule does not send it again | shared `rules` | -| 2, a rule by the name of a workspace (`chat`) sends a window; desktop 3 when shown | extra `named_rule` (the shared scenarios use indexes) | -| 2, a new window is an event that applies the rules again | extra `named_rule` (a candidate for the shared `rules`) | +| 2, a rule by the name of a workspace (`chat`) sends a window; desktop 3 when shown | shared `rules` (the window goes to the named workspace); extra `named_rule` (parked, desktop 3) | +| 2, a new window is an event that applies the rules again | shared `rules` (a window that opens sends the named-rule window back) | | 2, `enforce-workspace-rules` sends the initial rule's window again | shared `rules` | -| 3, the rules are cleared; a window that opens stays on the workspace in front | shared `rules`; extra `cleared` (the daemon's global state has no rule; desktop of the new window) | +| 3, the rules are cleared; a window that opens stays on the workspace in front | shared `rules` (also: the daemon's global state has no rule); extra `cleared` (desktop of the new window) | | 4, a `komorebi.json` rule, with a window open before the daemon | shared `startup_rules` | The extras open windows of their own and close them, so that the shared scenarios start from a diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules.py index 685ede96..a2bb17c0 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules.py +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/rules.py @@ -1,5 +1,7 @@ """Rules that match a window by its title decide where it goes, or that it is left alone.""" +import json + from komorebi_vm.core.checks import Checks from komorebi_vm.core.platform import Platform from komorebi_vm.core.scenarios.support import QUIET @@ -10,12 +12,13 @@ def _on(platform: Platform, title: str) -> int | None: def run(platform: Platform, checks: Checks) -> None: - """Gives the daemon three rules, opens a window that each matches, and checks where it went. + """Gives the daemon four rules, opens a window that each matches, and checks where it went. An initial rule sends a window when it opens, so that moving it back leaves it there. A rule - that is not initial sends it again whenever the rules are enforced, and an ignore rule leaves - it alone. The daemon must be running, on the first workspace, which is the one in view, with - no window. + that is not initial sends it again whenever the rules are enforced, and at the next event (a + window that opens); the same by the name of a workspace. An ignore rule leaves it alone. Once + the rules are cleared the daemon's global state has none. The daemon must be running, on the + first workspace, which is the one in view, with no window. """ platform.komorebic("ensure-workspaces", "0", "3") @@ -72,7 +75,34 @@ def run(platform: Platform, checks: Checks) -> None: and _on(platform, "left-alone") is None, ) + platform.komorebic("workspace-name", "0", "2", "chat") + platform.komorebic("named-workspace-rule", "title", "sent-named", "chat") + named = platform.spawn("sent-named") + checks.check( + "a window a rule by the name of a workspace matches goes to that workspace", + platform.wait_for(lambda: _on(platform, "sent-named") == 2, timeout=20), + ) + platform.komorebic("focus-workspace", "2") + # the always rule's window is on that workspace too: the focus is on the one to move + platform.activate(named) + platform.komorebic("move-to-workspace", "0") + platform.wait_for(lambda: _on(platform, "sent-named") == 0, timeout=20) + # a window that opens is an event, and a rule that is not initial is applied at every event + poke = platform.spawn("sent-poke") + checks.check( + "a rule that is not initial sends the window back at the next event, a window that opens", + platform.wait_for(lambda: _on(platform, "sent-named") == 2, timeout=30), + ) + platform.komorebic("clear-all-workspace-rules") + state = json.loads(platform.komorebic("global-state")) + if "workspace_rules" in state: + checks.check( + "once the rules are cleared the daemon's global state has none", + not state["workspace_rules"], + ) + else: + checks.skip("the global state has no rule", "the port's global state has no such field") platform.komorebic("focus-workspace", "0") # the title is the window's name here, so the first window of it goes before the next opens platform.close(once) @@ -83,5 +113,5 @@ def run(platform: Platform, checks: Checks) -> None: platform.wait_for(lambda: _on(platform, "sent-once") == 0, timeout=20) and not platform.wait_for(lambda: _on(platform, "sent-once") != 0, timeout=QUIET), ) - for client in (always, ignored, later): + for client in (always, ignored, later, named, poke): platform.close(client) diff --git a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py index c5fcea11..edb9942d 100644 --- a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py +++ b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py @@ -3,14 +3,11 @@ A workspace is a virtual desktop of KWin, so a window that a rule sends to another workspace is on that workspace's desktop, and out of sight on the parking desktop while the desktop in front is showing. The shared scenarios check where the daemon puts the window and that it is put away; these -check it on KWin's desktops, the rules by the name of a workspace, and a rule that is applied at -the next event. +check it on KWin's desktops, also for the rules by the name of a workspace. The suite runs the shared scenarios, then `desktops`, `named_rule` and `cleared`. """ -import json - from komorebi_vm.core.checks import Checks from komorebi_vm.core.platform import Client from komorebi_vm.kde_platform import KdePlatform @@ -73,9 +70,9 @@ def desktops(platform: KdePlatform, checks: Checks, clients: list[Client]) -> No def named_rule(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: - """A rule by the name of a workspace (`chat`) sends a window to it, on desktop 3, and a window - that opens is an event that applies the rules again, so the window comes back after the user - moves it. Needs `desktops` to have run.""" + """A rule by the name of a workspace (`chat`) sends a window to it, which is out of sight, and + on desktop 3 once shown. The shared `rules` checks the daemon's side, and that the rule is + applied again at the next event. Needs `desktops` to have run.""" kde = platform.kde always = platform.spawn("sent-always one") checks.check( @@ -90,26 +87,20 @@ def named_rule(platform: KdePlatform, checks: Checks, clients: list[Client]) -> ), ) kde.komorebic("move-to-workspace", "0") - # a new window is an event, and every event looks at the rules first - platform.spawn("poke one") - checks.check( - "the rule sends the window back at the next event", - kde.wait_for(lambda: _on(platform, always.title) == 2, timeout=30), - ) + kde.wait_for(lambda: _on(platform, always.title) == 0 and kde.desk(kde.wid(always.title)) == 1) kde.komorebic("focus-workspace", "0") -OPENED = ("stays here", "sent-once one", "sent-always one", "poke one", "sent-once two") +OPENED = ("stays here", "sent-once one", "sent-always one", "sent-once two") def cleared(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: - """Once every rule is cleared the daemon's global state has none, and a window that opens stays - on the desktop in front. The windows of these extras are closed after, and the workspace in - front is the first, so that the shared scenarios find the daemon as they expect.""" + """Once every rule is cleared a window that opens stays on the desktop in front. The shared + `rules` checks the workspace in view, and that the daemon's global state has no rule. The + windows of these extras are closed after, and the workspace in front is the first, so that the + shared scenarios find the daemon as they expect.""" kde = platform.kde kde.komorebic("clear-all-workspace-rules") - rules = json.loads(kde.komorebic("global-state"))["workspace_rules"] - checks.check("no rule is left", rules == []) later = platform.spawn("sent-once two") checks.check( "a window that opens now is on the desktop that is in front", diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index 1ef85f3a..044d6a2b 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -5,6 +5,7 @@ real daemon does with windows laid out left to right, so that a scenario can be that is right, and against ones that are wrong in one way. """ +import json from typing import Any from PIL import Image @@ -70,6 +71,8 @@ class FakePlatform: loads_resize: bool = True, answers_queries: bool = True, matches_class: bool = True, + applies_rules_on_event: bool = True, + clears_rules: bool = True, ) -> None: self.panel = panel """The height of a panel along the bottom of the first display, which the work area leaves @@ -78,6 +81,8 @@ class FakePlatform: """A daemon that tiles on the whole display, under the panel.""" self.second = second """The rectangle of the second display, when it is not one of the first's size.""" + self.applies_rules_on_event = applies_rules_on_event + self.clears_rules = clears_rules self.matches_class = matches_class self.loads_resize = loads_resize self.answers_queries = answers_queries @@ -286,6 +291,8 @@ class FakePlatform: self.containers.append([title]) if len(self.containers) > len(self.tiles): self._relayout() + if self.applies_rules_on_event: + self.komorebic("enforce-workspace-rules") return Client(title) def _place(self, title: str) -> None: @@ -539,11 +546,18 @@ class FakePlatform: top = self.containers[self.focus][0] self.monocle = None if self.monocle else top case ("ensure-workspaces", _, _) | ("clear-all-workspace-rules",): - if args[0] == "clear-all-workspace-rules": + if args[0] == "clear-all-workspace-rules" and self.clears_rules: self.class_rules.clear() self.exe_rules.clear() self.rules.clear() self.initial_rules.clear() + case ("global-state",): + rules = [*self.rules, *self.initial_rules, *self.class_rules, *self.exe_rules] + return json.dumps({"workspace_rules": rules}) + case ("workspace-name", _, index, name): + self.names[int(index)] = name + case ("named-workspace-rule", "title", title, name): + self.rules[title] = self.names.index(name) case ("initial-workspace-rule", "title", title, _, number): self.initial_rules[title] = int(number) case ("workspace-rule", "title", title, _, number): diff --git a/nix/tests/vm/lib/tests/test_moved.py b/nix/tests/vm/lib/tests/test_moved.py index 279f4d4f..5bbca9a9 100644 --- a/nix/tests/vm/lib/tests/test_moved.py +++ b/nix/tests/vm/lib/tests/test_moved.py @@ -3,7 +3,7 @@ are wrong in one way.""" from komorebi_vm.core.checks import Checks from komorebi_vm.core.geometry import Rect -from komorebi_vm.core.scenarios import commands, handover, placement, queries, title_rule +from komorebi_vm.core.scenarios import commands, handover, placement, queries, rules, title_rule from tests.fake_platform import FakePlatform @@ -126,3 +126,24 @@ def test_a_daemon_that_ignores_the_class_of_a_placement_rule_fails_placement() - placement.run(platform, checks) assert [f for f in checks.failures if "by the class" in f], checks.failures assert checks.failed == 1 + + +def test_a_daemon_that_sends_a_window_back_on_the_next_event_passes_rules() -> None: + platform = FakePlatform() + checks = Checks() + rules.run(platform, checks) + assert checks.failed == 0, checks.failures + + +def test_a_daemon_that_only_applies_rules_when_asked_fails_rules() -> None: + platform = FakePlatform(applies_rules_on_event=False) + checks = Checks() + rules.run(platform, checks) + assert any("at the next event" in f for f in checks.failures), checks.failures + + +def test_a_daemon_that_keeps_its_rules_when_cleared_fails_rules() -> None: + platform = FakePlatform(clears_rules=False) + checks = Checks() + rules.run(platform, checks) + assert any("global state has none" in f for f in checks.failures), checks.failures diff --git a/nix/tests/vm/suites/workspace-rules/test.py b/nix/tests/vm/suites/workspace-rules/test.py index cebe72ea..547f0ef7 100644 --- a/nix/tests/vm/suites/workspace-rules/test.py +++ b/nix/tests/vm/suites/workspace-rules/test.py @@ -6,7 +6,8 @@ # and the rules of a `komorebi.json` for a window that was open before the daemon. What only a real # KWin can tell is in `komorebi_vm.platforms.kde.extras.workspace_rules`: that the window is on the # desktop of its workspace and out of sight on the parking desktop while another desktop shows, -# the rules by the name of a workspace, and a rule applied at the next event. +# also for a rule by the name of a workspace. That a rule is applied at the next event is a check +# of the shared `rules`. # `komorebi/tests/workspace_rules.rs` checks the same against the fake. from komorebi_vm import Checks, Kde from komorebi_vm.core.scenarios import rules, startup_rules