From 8b64b3373a512b57b5284c364fe65544e8ea383b Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Oct 2026 12:33:01 -0700 Subject: [PATCH] feat(nix): the title rule scenario is shared, and a window can rename itself Platform.spawn takes rename_to and after: the window gives itself another title after some seconds (a terminal escape on KDE). The scenario ignores a title, and checks that the window is left alone while it has it, and tiled once it has another. --- docs/dev/suite-ledger.md | 2 +- nix/tests/vm/lib/komorebi_vm/core/platform.py | 13 ++++- .../komorebi_vm/core/scenarios/title_rule.py | 48 +++++++++++++++++++ nix/tests/vm/lib/komorebi_vm/kde_platform.py | 16 ++++++- .../platforms/kde/extras/commands.py | 44 +---------------- nix/tests/vm/lib/tests/fake_platform.py | 38 ++++++++++++++- nix/tests/vm/lib/tests/test_focus_moving.py | 2 +- nix/tests/vm/lib/tests/test_moved.py | 38 +++++++++++++++ nix/tests/vm/lib/tests/test_tiling.py | 9 +++- nix/tests/vm/suites/commands/test.py | 4 +- 10 files changed, 162 insertions(+), 52 deletions(-) create mode 100644 nix/tests/vm/lib/komorebi_vm/core/scenarios/title_rule.py create mode 100644 nix/tests/vm/lib/tests/test_moved.py diff --git a/docs/dev/suite-ledger.md b/docs/dev/suite-ledger.md index e9c2a917..fed48ad9 100644 --- a/docs/dev/suite-ledger.md +++ b/docs/dev/suite-ledger.md @@ -47,7 +47,7 @@ The suite had its own layout loop (section 2) and then called the shared `layout | 3, the layouts and the sweep of commands | shared `layouts` and `sweep` | | 3, `replace-configuration` made the window manager from the file | extra `replace_configuration` | | 3b, `minimize`, `unmanage`, `manage` | extra `handover` | -| 3c, a title rule decides when the window has its title | extra `title_rule` | +| 3c, a title rule decides when the window has its title | shared `title_rule` (the window renames itself through `spawn(rename_to=, after=)`). Not kept: the daemon-log line "should not be managed" for the first title, and `healthy` after it | | 4, every command that only prints | shared `prints` | | 4, `komorebic query ...`, `quick-save-resize`, `save-resize` | extra `queries` (candidates for the shared list) | | 5, every window is where the daemon last asked; a clean stop | extra `at_the_end` | diff --git a/nix/tests/vm/lib/komorebi_vm/core/platform.py b/nix/tests/vm/lib/komorebi_vm/core/platform.py index 5eca15f0..a97864a0 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/platform.py +++ b/nix/tests/vm/lib/komorebi_vm/core/platform.py @@ -136,7 +136,14 @@ class Platform(Protocol): """The daemon's state.""" ... - def spawn(self, title: str, app_id: str | None = None, exe: str | None = None) -> Client: + def spawn( + self, + title: str, + app_id: str | None = None, + exe: str | None = None, + rename_to: str | None = None, + after: float = 0, + ) -> Client: """Opens a window with this title and waits until the platform has it, and raises if it never does: a scenario that waits for a window to stay unmanaged must be sure it is there. @@ -148,6 +155,10 @@ class Platform(Protocol): `exe` is the name of the program that opens it, without an extension (`e2e-exe` is `e2e-exe.exe` on Windows and `e2e-exe` on Linux), for a rule that matches by it. The platform makes a program of that name, and `exe_id` spells the id of a rule for it. + + `rename_to` is a title the window gives itself `after` seconds after it opens (a terminal + escape on KDE, a title update on Windows), as a program that sets its title late does. The + window is then known to the platform by the new title: `Client(rename_to)`. """ ... diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/title_rule.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/title_rule.py new file mode 100644 index 00000000..37b47d26 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/title_rule.py @@ -0,0 +1,48 @@ +"""A rule that matches a title decides when the window has its title.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform + +PENDING = "pending-rule" +RENAMED = "renamed-rule" + + +def run(platform: Platform, checks: Checks) -> None: + """A window that opens with a title the ignore rule matches is left alone, and tiled once it + gives itself another title. + + The daemon runs with its windows tiled; the window is closed at the end, and the rule stays. + """ + platform.komorebic("ignore-rule", "title", PENDING) + before = platform.state().managed() + + # the window renames itself after the daemon has had time to decide about the first title + platform.spawn(PENDING, rename_to=RENAMED, after=6) + renamed = Client(RENAMED) + checks.check( + "a window the ignore rule matches by its title is left alone", + not platform.wait_for(lambda: platform.state().managed() != before, timeout=2) + and platform.os_window(Client(PENDING)) is not None, + ) + checks.check( + "once it has a title the rule does not match, it is tiled", + platform.wait_for( + lambda: ( + platform.os_window(renamed) is not None and platform.state().managed() == before + 1 + ), + timeout=30, + ), + ) + window = platform.os_window(renamed) + checks.check( + "... and inside the work area", + window is not None and window.frame.inside(platform.work_area(), 2), + ) + platform.close(renamed) + checks.check( + "closed, it leaves the daemon", + platform.wait_for( + lambda: platform.os_window(renamed) is None and platform.state().managed() == before, + timeout=10, + ), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/kde_platform.py b/nix/tests/vm/lib/komorebi_vm/kde_platform.py index ab1cba8a..696d2308 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde_platform.py +++ b/nix/tests/vm/lib/komorebi_vm/kde_platform.py @@ -92,7 +92,14 @@ class KdePlatform: def state(self) -> Snapshot: return snapshot.parse(self.kde.state()) - def spawn(self, title: str, app_id: str | None = None, exe: str | None = None) -> Client: + def spawn( + self, + title: str, + app_id: str | None = None, + exe: str | None = None, + rename_to: str | None = None, + after: float = 0, + ) -> Client: name = f"-a {shlex.quote(app_id)} " if app_id else "" program = "foot" if exe is not None: @@ -105,7 +112,12 @@ class KdePlatform: self.kde.sh( f"mkdir -p {BIN} && {{ test -x {program} || cp -L $(command -v foot) {program}; }}" ) - self.kde.spawn(f"{program} {name}-T {shlex.quote(title)} sleep 3000") + command = "sleep 3000" + if rename_to is not None: + # foot takes the title from the terminal escape OSC 0 + script = f'sleep {after}; printf "\\033]0;{rename_to}\\007"; sleep 3000' + command = f"sh -c {shlex.quote(script)}" + self.kde.spawn(f"{program} {name}-T {shlex.quote(title)} {command}") if not self.kde.wait_for(lambda: self.kde.wid(title) != "", timeout=30): raise AssertionError(f"no window titled {title!r} appeared") return Client(title) diff --git a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/commands.py b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/commands.py index 3a675d8c..e8584d7d 100644 --- a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/commands.py +++ b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/commands.py @@ -6,12 +6,11 @@ command is understood and leaves the daemon healthy, as the operating system see reads KWin (`kdotool`) and the daemon's log. The suite runs `setup`, `layouts_by_count`, the shared `layouts` and `sweep`, -`replace_configuration`, `handover`, `title_rule`, the shared `prints`, `queries` and `at_the_end`. +`replace_configuration`, `handover`, the shared `title_rule` and `prints`, `queries` and +`at_the_end`. The daemon runs on a session with two outputs, and `clients` are five windows on the first. """ -import shlex - from komorebi_vm import daemon from komorebi_vm.core.checks import Checks from komorebi_vm.core.platform import Client @@ -217,45 +216,6 @@ def handover(platform: KdePlatform, checks: Checks, clients: list[Client]) -> No ) -def title_rule(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: - """A rule that matches a title decides when the window has its title: a window that opens - with a title the ignore rule matches is left alone, and tiled once it has another.""" - kde = platform.kde - kde.komorebic("ignore-rule", "title", "pending-rule") - before_all = kde.managed_all() - - def ignored(title: str) -> bool: - """True once the daemon has logged that it leaves the window titled `title` alone.""" - return any( - "should not be managed" in line and f"title: {title}," in line - for line in kde.komorebi_log().splitlines() - ) - - # foot is titled pending-rule when it opens, and gives itself another title after 4 seconds - rename = 'sleep 4; printf "\\033]0;renamed-rule\\007"; sleep 3000' - kde.spawn(f"foot -T pending-rule sh -c {shlex.quote(rename)}") - checks.check( - "a window the ignore rule matches by its title is left alone", - kde.wait_for(lambda: ignored("pending-rule"), timeout=20) - and kde.managed_all() == before_all, - ) - checks.check( - "once it has a title the rule does not match, it is tiled", - kde.wait_for( - lambda: kde.wid("renamed-rule") != "" and kde.managed_all() == before_all + 1, - timeout=20, - ) - and healthy(platform) == 0, - ) - # the daemon's log names a window by the title it had when it was focused, which for this one - # is the first: close it, so that the last check finds every window it looks for in the log - kde.close(kde.wid("renamed-rule")) - checks.check( - "closed, it leaves komorebi", - kde.wait_for(lambda: kde.wid("renamed-rule") == "" and healthy(platform) == 0, timeout=10), - ) - - def queries(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: """`komorebic query` answers, and the resize commands that save and load are understood.""" kde = platform.kde diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index 583b1472..063fa61f 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -63,6 +63,8 @@ class FakePlatform: panel: int = 0, ignores_panel: bool = False, second: Rect | None = None, + follows_renames: bool = True, + honours_ignore: bool = True, ) -> None: self.panel = panel """The height of a panel along the bottom of the first display, which the work area leaves @@ -71,6 +73,11 @@ 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.follows_renames = follows_renames + self.honours_ignore = honours_ignore + self.clock = 0.0 + self.renames: list[tuple[float, str, str]] = [] + """The windows that will give themselves a title: when, the old one, the new one.""" self.tiles = self._base() self.has_pointer = pointer self.follows_pointer = follows_pointer @@ -166,8 +173,26 @@ class FakePlatform: return def wait_for(self, predicate: Any, timeout: float = 10) -> bool: + """Time passes by `timeout`, and the windows that were to rename themselves have.""" + self.clock += timeout + for entry in [r for r in self.renames if r[0] <= self.clock]: + self.renames.remove(entry) + self._rename(entry[1], entry[2]) return bool(predicate()) + def _rename(self, old: str, new: str) -> None: + for table in (self.exes, self.classes): + if old in table: + table[new] = table.pop(old) + if old not in self.unmanaged or not self.follows_renames: + for stack in [s for cs in self.spaces for s in cs]: + stack[:] = [new if t == old else t for t in stack] + self.unmanaged[:] = [new if t == old else t for t in self.unmanaged] + return + self.unmanaged.remove(old) + self.containers.append([new]) + self._relayout() + def start_komorebi(self) -> None: """A new daemon knows nothing: every window is tiled on the first workspace.""" everyone = [t for containers in self.spaces for stack in containers for t in stack] @@ -197,7 +222,16 @@ 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, app_id: str | None = None, exe: str | None = None) -> Client: + def spawn( + self, + title: str, + app_id: str | None = None, + exe: str | None = None, + rename_to: str | None = None, + after: float = 0, + ) -> Client: + if rename_to is not None: + self.renames.append((self.clock + after, title, rename_to)) self.exes[title] = exe or "foot" self.classes[title] = app_id or "" forced = ( @@ -224,7 +258,7 @@ class FakePlatform: if self.paused: self.unmanaged.append(title) return Client(title) - if any(title.startswith(i) for i in self.ignored): + if self.honours_ignore and any(title.startswith(i) for i in self.ignored): self.unmanaged.append(title) elif title in self.initial_rules: self.spaces[self.initial_rules.pop(title)].append([title]) diff --git a/nix/tests/vm/lib/tests/test_focus_moving.py b/nix/tests/vm/lib/tests/test_focus_moving.py index 72f1915b..466cd75a 100644 --- a/nix/tests/vm/lib/tests/test_focus_moving.py +++ b/nix/tests/vm/lib/tests/test_focus_moving.py @@ -575,7 +575,7 @@ def test_a_daemon_that_shows_what_a_window_was_given_passes_state_fidelity() -> def test_a_daemon_that_loses_the_class_fails_state_fidelity() -> None: class Forgetful(FakePlatform): - def spawn(self, title, app_id=None, exe=None): # noqa: ANN001, ANN202 + def spawn(self, title, app_id=None, exe=None, rename_to=None, after=0): # noqa: ANN001, ANN202 client = super().spawn(title, app_id=None, exe=exe) return client diff --git a/nix/tests/vm/lib/tests/test_moved.py b/nix/tests/vm/lib/tests/test_moved.py new file mode 100644 index 00000000..8543edf8 --- /dev/null +++ b/nix/tests/vm/lib/tests/test_moved.py @@ -0,0 +1,38 @@ +"""The scenarios that moved out of the KDE extras, against a daemon that is right and ones that +are wrong in one way.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.scenarios import title_rule +from tests.fake_platform import FakePlatform + + +def opened(platform: FakePlatform, checks: Checks): # noqa: ANN201 + from komorebi_vm.core.scenarios import tiling + + return tiling.run(platform, checks) + + +def test_a_daemon_that_follows_a_rename_passes_title_rule() -> None: + platform = FakePlatform() + checks = Checks() + opened(platform, checks) + base = checks.passed + title_rule.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed - base == 4 + + +def test_a_daemon_that_manages_an_ignored_title_fails_title_rule() -> None: + platform = FakePlatform(honours_ignore=False) + checks = Checks() + opened(platform, checks) + title_rule.run(platform, checks) + assert any("left alone" in f for f in checks.failures) + + +def test_a_daemon_that_misses_the_rename_fails_title_rule() -> None: + platform = FakePlatform(follows_renames=False) + checks = Checks() + opened(platform, checks) + title_rule.run(platform, checks) + assert any("it is tiled" in f for f in checks.failures) diff --git a/nix/tests/vm/lib/tests/test_tiling.py b/nix/tests/vm/lib/tests/test_tiling.py index 283f705f..8c077cfb 100644 --- a/nix/tests/vm/lib/tests/test_tiling.py +++ b/nix/tests/vm/lib/tests/test_tiling.py @@ -21,7 +21,14 @@ class FakePlatform: def wait_for(self, predicate, timeout: float = 10) -> bool: # noqa: ANN001 return predicate() - def spawn(self, title: str, app_id: str | None = None, exe: str | None = None) -> Client: + def spawn( + self, + title: str, + app_id: str | None = None, + exe: str | None = None, + rename_to: str | None = None, + after: float = 0, + ) -> Client: self.opened.append(title) return Client(title) diff --git a/nix/tests/vm/suites/commands/test.py b/nix/tests/vm/suites/commands/test.py index dcf1dc08..14946e83 100644 --- a/nix/tests/vm/suites/commands/test.py +++ b/nix/tests/vm/suites/commands/test.py @@ -10,7 +10,7 @@ import shlex from komorebi_vm import Checks, Kde -from komorebi_vm.core.scenarios import commands +from komorebi_vm.core.scenarios import commands, title_rule from komorebi_vm.core.suite import platform_only, run_suite, shared from komorebi_vm.kde_platform import KdePlatform from komorebi_vm.platforms.kde.extras import commands as kde_commands @@ -45,7 +45,7 @@ run_suite( shared(commands.sweep, *args, CONFIG), platform_only("kde", kde_commands.replace_configuration, *args), platform_only("kde", kde_commands.handover, *args), - platform_only("kde", kde_commands.title_rule, *args), + shared(title_rule.run, platform, checks), shared(commands.prints, platform, checks), platform_only("kde", kde_commands.queries, *args), platform_only("kde", kde_commands.at_the_end, *args),