diff --git a/e2e/lib/komorebi_vm/core/scenarios/startup_rules.py b/e2e/lib/komorebi_vm/core/scenarios/startup_rules.py new file mode 100644 index 00000000..157d38fb --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/startup_rules.py @@ -0,0 +1,58 @@ +"""Workspace rules from the config: a window that was open before the daemon, and one after.""" + +from typing import Any + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Platform + + +def _config() -> dict[str, Any]: + rule = {"kind": "Title", "id": "stays here", "matching_strategy": "StartsWith"} + return { + "monitors": [ + { + "workspaces": [ + {"name": "web", "layout": "BSP"}, + {"name": "code", "layout": "BSP", "workspace_rules": [rule]}, + ] + } + ] + } + + +def run(platform: Platform, checks: Checks) -> None: + """Opens a window, starts the daemon with a rule that sends windows by their title to the + second workspace, and checks where that window and one that opens afterwards are. + + The daemon must be running with no window open. The rule is in the config file, not made with + `komorebic`, so that what is checked is how the file is read at start. + """ + before = platform.spawn("stays here, from before") + platform.restart_with(_config()) + checks.check( + "a window that was open when the daemon started is on the workspace the file names", + platform.wait_for(lambda: platform.state().workspace_of(before.title) == 1, timeout=30), + ) + checks.check( + "... where it is put away while the first workspace is in view", + platform.wait_for(lambda: platform.hidden(before)), + ) + after = platform.spawn("stays here, from after") + checks.check( + "a window that opens after the start goes to that workspace too", + platform.wait_for(lambda: platform.state().workspace_of(after.title) == 1, timeout=30), + ) + other = platform.spawn("elsewhere") + checks.check( + "a window no rule names stays on the first", + platform.wait_for(lambda: platform.state().workspace_of(other.title) == 0, timeout=30), + ) + platform.komorebic("focus-workspace", "1") + checks.check( + "and the windows of the rule are in view when their workspace is", + platform.wait_for(lambda: not platform.hidden(before) and not platform.hidden(after)), + ) + platform.komorebic("focus-workspace", "0") + for client in (before, after, other): + platform.close(client) + platform.wait_for(lambda: platform.state().managed() == 0, timeout=20) diff --git a/e2e/lib/tests/fake_platform.py b/e2e/lib/tests/fake_platform.py index 3ee24cca..e0a4dec8 100644 --- a/e2e/lib/tests/fake_platform.py +++ b/e2e/lib/tests/fake_platform.py @@ -68,6 +68,7 @@ class FakePlatform: self.rules: dict[str, int] = {} self.ignored: set[str] = set() self.placing = False + self.prefix_rules: list[tuple[str, int]] = [] self.unmanaged: list[str] = [] self.display_count = 2 self.display_of: dict[str, int] = {} @@ -133,6 +134,8 @@ class FakePlatform: self.spaces[self.initial_rules.pop(title)].append([title]) elif title in self.rules: self.spaces[self.rules[title]].append([title]) + elif (target := self._by_prefix(title)) is not None: + self.spaces[target].append([title]) elif self.placing: self._place(title) else: @@ -159,12 +162,34 @@ class FakePlatform: self.containers.insert(index, [title]) self.focus = index + def _by_prefix(self, title: str) -> int | None: + for prefix, space in self.prefix_rules: + if title.startswith(prefix): + return space + return None + def restart_with(self, config: dict[str, Any]) -> None: self.start_komorebi() - workspace = config["monitors"][0]["workspaces"][0] - self.placement = workspace.get("initial_window_placement_rules") + workspaces = config["monitors"][0]["workspaces"] + self.placement = workspaces[0].get("initial_window_placement_rules") + self.prefix_rules = [ + (rule["id"], index) + for index, workspace in enumerate(workspaces) + for rule in workspace.get("workspace_rules", []) + if rule["matching_strategy"] == "StartsWith" + ] + for title in [t for stack in self.spaces[0] for t in stack]: + target = self._by_prefix(title) + if target not in (None, 0): + self._find_remove(title) + self.spaces[target].append([title]) + self._relayout() self.placing = True + def _find_remove(self, title: str) -> None: + space, index = self._find(title) + self.spaces[space].pop(index) + def _find(self, title: str) -> tuple[int, int]: for s, containers in enumerate(self.spaces): for i, stack in enumerate(containers): diff --git a/e2e/lib/tests/test_focus_moving.py b/e2e/lib/tests/test_focus_moving.py index 9b595b5b..662aaea6 100644 --- a/e2e/lib/tests/test_focus_moving.py +++ b/e2e/lib/tests/test_focus_moving.py @@ -13,6 +13,7 @@ from komorebi_vm.core.scenarios import ( resize, rules, stacks, + startup_rules, tiling, workspaces, ) @@ -318,3 +319,22 @@ def test_a_case_a_port_cannot_start_is_a_known_failure_and_the_rest_run() -> Non assert checks.failed == 0, checks.failures assert checks.passed == len(placement.CASES) - 1 assert len(checks.known) == 1 and "exited at start" in checks.known[0] + + +def test_a_daemon_that_reads_the_rules_of_its_file_passes_startup_rules() -> None: + platform = FakePlatform() + checks = Checks() + startup_rules.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed == 5 + + +def test_a_daemon_that_ignores_the_rules_of_its_file_fails_startup_rules() -> None: + class Ignores(FakePlatform): + def restart_with(self, config: dict) -> None: + self.start_komorebi() + + platform = Ignores() + checks = Checks() + startup_rules.run(platform, checks) + assert checks.failed >= 3, checks.failures