From fec9b1e3da7fc23c092db2507117a39080d26b48 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Oct 2026 12:37:03 -0700 Subject: [PATCH] feat(nix): the query scenario is shared and asserts values Each komorebic query is compared with the daemon's state, before and after a change of focus and layout, and a resize that was saved is undone by loading it, with quick-save-resize and with save-resize. Platform gets temp_path for the file of save-resize. --- docs/dev/suite-ledger.md | 2 +- nix/tests/vm/lib/komorebi_vm/core/platform.py | 5 + .../lib/komorebi_vm/core/scenarios/queries.py | 103 ++++++++++++++++++ nix/tests/vm/lib/komorebi_vm/kde_platform.py | 3 + .../platforms/kde/extras/commands.py | 32 +----- nix/tests/vm/lib/tests/fake_platform.py | 42 ++++++- nix/tests/vm/lib/tests/test_moved.py | 31 +++++- nix/tests/vm/lib/tests/test_tiling.py | 3 + nix/tests/vm/suites/commands/test.py | 4 +- 9 files changed, 188 insertions(+), 37 deletions(-) create mode 100644 nix/tests/vm/lib/komorebi_vm/core/scenarios/queries.py diff --git a/docs/dev/suite-ledger.md b/docs/dev/suite-ledger.md index f763d978..7a6839f5 100644 --- a/docs/dev/suite-ledger.md +++ b/docs/dev/suite-ledger.md @@ -49,7 +49,7 @@ The suite had its own layout loop (section 2) and then called the shared `layout | 3b, `minimize`, `unmanage`, `manage` | shared `handover`; the check that the unmanaged window is centered is now "inside the work area and not on a tile of the layout" | | 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) | +| 4, `komorebic query ...`, `quick-save-resize`, `save-resize` | shared `queries`: each answer is compared with the daemon's state, before and after a focus and a layout change, and a saved resize is undone by loading it (was: exit status only) | | 5, every window is where the daemon last asked; a clean stop | extra `at_the_end` | The extra's layout loop still goes through five, three, two and one windows, for the daemon's log diff --git a/nix/tests/vm/lib/komorebi_vm/core/platform.py b/nix/tests/vm/lib/komorebi_vm/core/platform.py index a97864a0..f6254e68 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/platform.py +++ b/nix/tests/vm/lib/komorebi_vm/core/platform.py @@ -132,6 +132,11 @@ class Platform(Protocol): it succeeded or not.""" ... + def temp_path(self, name: str) -> str: + """A path, on the machine the daemon runs on, of a file `komorebic` and the daemon may + write and read (`save-resize` takes one), in the form `komorebic` is given it.""" + ... + def state(self) -> Snapshot: """The daemon's state.""" ... diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/queries.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/queries.py new file mode 100644 index 00000000..44ef0c35 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/queries.py @@ -0,0 +1,103 @@ +"""`komorebic query` answers with what the daemon's state says, and the resize a workspace has can +be saved and loaded.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.geometry import Rect +from komorebi_vm.core.platform import Platform +from komorebi_vm.core.scenarios.commands import NOT_A_COMMAND +from komorebi_vm.core.snapshot import Snapshot + +# the layouts that `change-layout` takes by a name that is the lower case of the one a query gives +BY_NAME = {"BSP": "bsp", "Columns": "columns", "Rows": "rows"} + + +def _expected(state: Snapshot) -> dict[str, str]: + """What each query that depends on the focus should answer, from the daemon's own state.""" + monitor = state.monitors[state.focused_monitor] + workspace = state.workspace(monitor=state.focused_monitor) + expected = { + "focused-monitor-index": str(state.focused_monitor), + "focused-workspace-index": str(monitor.focused_workspace), + "focused-workspace-name": workspace.name or str(monitor.focused_workspace), + "focused-workspace-layout": workspace.layout, + } + if workspace.containers: + container = workspace.containers[workspace.focused_container] + expected["focused-container-index"] = str(workspace.focused_container) + expected["focused-window-index"] = str( + workspace.focused_windows[workspace.focused_container] + ) + expected["focused-container-kind"] = "Stack" if len(container) > 1 else "Single" + return expected + + +def _query(platform: Platform, checks: Checks, state: Snapshot) -> None: + for query, want in _expected(state).items(): + status, out = platform.komorebic_status("query", query) + if status != 0 and NOT_A_COMMAND in out: + checks.skip(f"query {query}", "the port has no such query") + continue + checks.check(f"query {query} answers {want!r}", status == 0 and out.strip() == want) + + +def _layout(platform: Platform) -> list[Rect]: + return platform.state().workspace().latest_layout + + +def _round_trip( + platform: Platform, checks: Checks, name: str, save: tuple[str, ...], load: tuple[str, ...] +) -> None: + """Saves the resize, resizes the focused container, loads it, and checks that the layout is + the one it was.""" + before = _layout(platform) + status, _ = platform.komorebic_status(*save) + platform.komorebic("resize-axis", "horizontal", "increase") + resized = platform.wait_for(lambda: _layout(platform) != before, timeout=10) + status_load, _ = platform.komorebic_status(*load) + checks.check( + f"{name}: a resize is undone by loading what was saved", + status == 0 + and resized + and status_load == 0 + and platform.wait_for(lambda: _layout(platform) == before, timeout=10), + ) + + +def run(platform: Platform, checks: Checks) -> None: + """Asks every query and checks the answer against the daemon's state, after changing the + focus and the layout so that the answers are not the ones of a fresh daemon, and saves and + loads the resize both ways. + + The focused workspace has two containers or more. The focus and the layout are as they were at + the end. + """ + _query(platform, checks, platform.state()) + status, out = platform.komorebic_status("query", "version") + checks.check("query version answers", status == 0 and out.strip() != "") + + workspace = platform.state().workspace() + if len(workspace.containers) < 2: + checks.skip("queries after a change, and the saved resize", "fewer than two containers") + return + + platform.komorebic("cycle-focus", "next") + platform.wait_for( + lambda: platform.state().workspace().focused_container != workspace.focused_container, + timeout=10, + ) + _query(platform, checks, platform.state()) + platform.komorebic("cycle-focus", "previous") + + if workspace.layout in BY_NAME: + other = "columns" if workspace.layout == "Rows" else "rows" + platform.komorebic("change-layout", other) + platform.wait_for(lambda: platform.state().workspace().layout != workspace.layout) + _query(platform, checks, platform.state()) + platform.komorebic("change-layout", BY_NAME[workspace.layout]) + platform.wait_for(lambda: platform.state().workspace().layout == workspace.layout) + + _round_trip( + platform, checks, "quick-save-resize", ("quick-save-resize",), ("quick-load-resize",) + ) + path = platform.temp_path("e2e-resize.json") + _round_trip(platform, checks, "save-resize", ("save-resize", path), ("load-resize", path)) diff --git a/nix/tests/vm/lib/komorebi_vm/kde_platform.py b/nix/tests/vm/lib/komorebi_vm/kde_platform.py index 696d2308..fa987d7c 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde_platform.py +++ b/nix/tests/vm/lib/komorebi_vm/kde_platform.py @@ -89,6 +89,9 @@ class KdePlatform: def komorebic_status(self, *args: str) -> tuple[int, str]: return self.kde.run(shlex.join(["komorebic", *args])) + def temp_path(self, name: str) -> str: + return f"/tmp/{name}" + def state(self) -> Snapshot: return snapshot.parse(self.kde.state()) 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 c71f6655..80c86b9c 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,7 +6,7 @@ windows and that every command is understood and leaves the daemon healthy, as t sees it; what is here reads KWin (`kdotool`) and the daemon's log. The suite runs `setup`, `layouts_by_count`, the shared `layouts` and `sweep`, -`replace_configuration`, the shared `handover`, `title_rule` and `prints`, `queries` and +`replace_configuration`, the shared `handover`, `title_rule`, `prints` and `queries`, and `at_the_end`. The daemon runs on a session with two outputs, and `clients` are five windows on the first. """ @@ -28,16 +28,6 @@ LAYOUTS = ( "right-main-vertical-stack", "scrolling", ) -QUERIES = ( - "focused-monitor-index", - "focused-workspace-index", - "focused-container-index", - "focused-window-index", - "focused-workspace-name", - "focused-workspace-layout", - "focused-container-kind", - "version", -) def _live_windows(platform: KdePlatform) -> list[str]: @@ -153,26 +143,6 @@ def replace_configuration(platform: KdePlatform, checks: Checks, clients: list[C ) -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 - for query in QUERIES: - status, out = kde.run(f"komorebic query {query}") - checks.check(f"query {query} answers", status == 0 and out.strip() != "") - checks.check( - "quick-save-resize and quick-load-resize", - kde.run("komorebic quick-save-resize && komorebic quick-load-resize")[0] == 0, - ) - checks.check( - "save-resize and load-resize", - kde.run( - "komorebic save-resize /tmp/e2e-resize.json" - " && komorebic load-resize /tmp/e2e-resize.json" - )[0] - == 0, - ) - - def at_the_end(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: """The daemon has every window KWin has, each is where the daemon last asked for it, and a clean stop leaves the daemon not running.""" diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index d38073cb..40c288c2 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -67,6 +67,8 @@ class FakePlatform: honours_ignore: bool = True, unmanages: bool = True, manages: bool = True, + loads_resize: bool = True, + answers_queries: bool = True, ) -> None: self.panel = panel """The height of a panel along the bottom of the first display, which the work area leaves @@ -75,6 +77,10 @@ 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.loads_resize = loads_resize + self.answers_queries = answers_queries + self.saved: dict[str, list[Rect]] = {} + """The tiles of each resize that `save-resize` saved, by its path.""" self.unmanages = unmanages self.manages = manages self.let_go: list[str] = [] @@ -439,6 +445,9 @@ class FakePlatform: def exe_id(self, program: str) -> str: return program + def temp_path(self, name: str) -> str: + return f"/tmp/{name}" + def os_window(self, client: Client) -> OsWindow | None: if client.title == self.maximized: return OsWindow(client.title, self.tiled_area, focused=True) @@ -514,8 +523,8 @@ class FakePlatform: self.containers.pop(self.focus) self.focus = max(0, min(self.focus, len(self.containers) - 1)) self.spaces[int(number)].append([window]) - case ("change-layout", _): - pass + case ("change-layout", name): + self.layout_name = "BSP" if name == "bsp" else name.title() case ("send-to-monitor", _): pass case ("move-to-monitor", number): @@ -585,6 +594,16 @@ class FakePlatform: case ("flip-layout", "horizontal"): self.flipped = not self.flipped self._relayout() + case ("query", name): + return self._answer(name) + case ("quick-save-resize",): + self.saved["quick"] = list(self.tiles) + case ("quick-load-resize",): + self._load_resize("quick") + case ("save-resize", path): + self.saved[path] = list(self.tiles) + case ("load-resize", path): + self._load_resize(path) case ("minimize",): self.minimize(Client(self.containers[self.focus][0])) case ("unmanage",): @@ -622,6 +641,25 @@ class FakePlatform: raise AssertionError(f"the fake does not know komorebic {args}") return "" + def _load_resize(self, key: str) -> None: + if self.loads_resize and key in self.saved: + self.tiles = list(self.saved[key]) + + def _answer(self, name: str) -> str: + if not self.answers_queries: + return "0" + stack = self.containers[self.focus] if self.containers else [] + return { + "focused-monitor-index": "0", + "focused-workspace-index": str(self.current), + "focused-container-index": str(self.focus), + "focused-window-index": "0", + "focused-workspace-name": self.names[self.current] or str(self.current), + "focused-workspace-layout": self.layout_name, + "focused-container-kind": "Stack" if len(stack) > 1 else "Single", + "version": "0.1.0", + }[name] + def _swap(self, other: int) -> None: if not self.moves: return diff --git a/nix/tests/vm/lib/tests/test_moved.py b/nix/tests/vm/lib/tests/test_moved.py index 1f546252..bec8a5e6 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, title_rule +from komorebi_vm.core.scenarios import commands, handover, queries, title_rule from tests.fake_platform import FakePlatform @@ -89,3 +89,32 @@ def test_a_layout_that_overlaps_two_windows_fails_commands_layouts() -> None: clients = opened(platform, checks) commands.layouts(platform, checks, clients) assert any("grid with 2 windows" in f for f in checks.failures), checks.failures + + +def test_a_daemon_that_answers_queries_and_loads_resizes_passes_queries() -> None: + platform = FakePlatform() + checks = Checks() + opened(platform, checks) + base = checks.passed + queries.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed - base == 1 + 7 * 3 + 2 + assert platform.state().workspace().layout == "Columns" + + +def test_a_daemon_that_answers_the_same_for_every_query_fails_queries() -> None: + platform = FakePlatform(answers_queries=False) + checks = Checks() + opened(platform, checks) + queries.run(platform, checks) + assert any("query focused-workspace-layout" in f for f in checks.failures) + assert any("query focused-container-kind" in f for f in checks.failures) + + +def test_a_daemon_that_does_not_load_a_resize_fails_queries() -> None: + platform = FakePlatform(loads_resize=False) + checks = Checks() + opened(platform, checks) + queries.run(platform, checks) + assert any("quick-save-resize: a resize is undone" in f for f in checks.failures) + assert any("save-resize: a resize is undone" 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 8c077cfb..89877758 100644 --- a/nix/tests/vm/lib/tests/test_tiling.py +++ b/nix/tests/vm/lib/tests/test_tiling.py @@ -79,6 +79,9 @@ class FakePlatform: def exe_id(self, program: str) -> str: return program + def temp_path(self, name: str) -> str: + return f"/tmp/{name}" + def minimize(self, client: Client) -> None: pass diff --git a/nix/tests/vm/suites/commands/test.py b/nix/tests/vm/suites/commands/test.py index d5e45af3..9b6bb901 100644 --- a/nix/tests/vm/suites/commands/test.py +++ b/nix/tests/vm/suites/commands/test.py @@ -11,7 +11,7 @@ import shlex from komorebi_vm import Checks, Kde -from komorebi_vm.core.scenarios import commands, handover, title_rule +from komorebi_vm.core.scenarios import commands, handover, queries, 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 @@ -48,7 +48,7 @@ run_suite( shared(handover.run, *args), shared(title_rule.run, platform, checks), shared(commands.prints, platform, checks), - platform_only("kde", kde_commands.queries, *args), + shared(queries.run, platform, checks), platform_only("kde", kde_commands.at_the_end, *args), )