diff --git a/docs/dev/vm-tests.md b/docs/dev/vm-tests.md index 6bd044a1..7b41cb04 100644 --- a/docs/dev/vm-tests.md +++ b/docs/dev/vm-tests.md @@ -146,9 +146,10 @@ the run. - `Snapshot` (`core/snapshot.py`) is the daemon's state in one shape for every port: `snapshot.parse(json)` reads the `komorebic state` of kfde and of kfw into monitors, workspaces, containers and windows. What only one port's state has - goes in `Snapshot.extra`. Its unit tests use fixtures written from the two - ports' serializers; recorded states replace them once a run on each port - exists. + goes in `Snapshot.extra`. Its unit tests read states recorded from a real run of each + port (`tests/fixtures`: kfde's from `core-e2e`, which writes them into its output, and + kfw's from a Windows guest), and a scene written from the serializers for the shapes + that no run has recorded. - `Platform` (`core/platform.py`) is what a scenario needs of a platform: start the daemon, run `komorebic`, read the `Snapshot`, open a window, ask whether the daemon put it away (`hidden`), ask the operating system for its frame diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py index 249a61da..57c963c4 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py @@ -39,6 +39,8 @@ def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: ) platform.komorebic("focus-workspace", "1") + platform.wait_for(lambda: _in_view(platform, clients) == [last.title]) + print(f" in view after the switch: {_in_view(platform, clients)}") checks.check( "after 'focus-workspace 1' only the window sent there is in view", platform.wait_for(lambda: _in_view(platform, clients) == [last.title]), diff --git a/nix/tests/vm/lib/tests/fixtures/kfde-stack.json b/nix/tests/vm/lib/tests/fixtures/kfde-stack.json new file mode 100644 index 00000000..b97af438 --- /dev/null +++ b/nix/tests/vm/lib/tests/fixtures/kfde-stack.json @@ -0,0 +1,164 @@ +{ + "monitors": { + "elements": [ + { + "id": 1230296105, + "device": "Virtual-0", + "serial_number_id": "Virtual-0", + "workspaces": { + "elements": [ + { + "name": null, + "containers": { + "elements": [ + { + "id": "maAEzGA_0nRWS6PpcDLlI", + "locked": false, + "windows": { + "elements": [ + { + "id": 4, + "rect": { + "left": 20, + "top": 20, + "right": 925, + "bottom": 1040 + }, + "details": { + "title": "neutral1", + "exe": "foot", + "class": "foot", + "path": "/nix/store/0gkg75w7bmwd7klmf23qy8m697abypvc-foot-1.28.0/bin/foot" + } + }, + { + "id": 6, + "rect": { + "left": 20, + "top": 20, + "right": 924, + "bottom": 1037 + }, + "details": { + "title": "neutral3", + "exe": "foot", + "class": "foot", + "path": "/nix/store/0gkg75w7bmwd7klmf23qy8m697abypvc-foot-1.28.0/bin/foot" + } + } + ], + "focused": 1 + } + }, + { + "id": "z2cN_l7IEJBa53ymxMgvA", + "locked": false, + "windows": { + "elements": [ + { + "id": 5, + "rect": { + "left": 975, + "top": 20, + "right": 924, + "bottom": 1037 + }, + "details": { + "title": "neutral2", + "exe": "foot", + "class": "foot", + "path": "/nix/store/0gkg75w7bmwd7klmf23qy8m697abypvc-foot-1.28.0/bin/foot" + } + } + ], + "focused": 0 + } + } + ], + "focused": 0 + }, + "monocle_container": null, + "maximized_window": null, + "floating_windows": { + "elements": [], + "focused": 0 + }, + "layout": { + "Default": "BSP" + }, + "layout_options": null, + "layout_rules": [], + "layout_options_rules": [], + "layout_flip": null, + "workspace_padding": null, + "container_padding": null, + "latest_layout": [ + { + "left": 20, + "top": 20, + "right": 925, + "bottom": 1040 + }, + { + "left": 975, + "top": 20, + "right": 925, + "bottom": 1040 + } + ], + "resize_dimensions": [ + null, + null + ], + "tile": true, + "work_area_offset": null, + "work_area_offset_rules": [], + "apply_window_based_work_area_offset": true, + "window_container_behaviour": null, + "window_container_behaviour_rules": null, + "float_override": null, + "layer": "Tiling", + "floating_layer_behaviour": null, + "wallpaper": null + } + ], + "focused": 0 + }, + "size": { + "left": 0, + "top": 0, + "right": 1920, + "bottom": 1080 + }, + "work_area_offset": null, + "work_area_size": { + "left": 0, + "top": 0, + "right": 1920, + "bottom": 1080 + }, + "window_based_work_area_offset": null, + "window_based_work_area_offset_limit": 1, + "container_padding": null, + "workspace_padding": null, + "last_focused_workspace": 0, + "wallpaper": null, + "floating_layer_behaviour": null + } + ], + "focused": 0 + }, + "monitor_usr_idx_map": { + "0": 0 + }, + "is_paused": false, + "resize_delta": 50, + "new_window_behaviour": "Create", + "float_override": false, + "cross_monitor_move_behaviour": "Swap", + "monocle_focus_behaviour": "NoOp", + "unmanaged_window_operation_behaviour": "Op", + "work_area_offset": null, + "focus_follows_mouse": null, + "mouse_follows_focus": true +} \ No newline at end of file diff --git a/nix/tests/vm/lib/tests/fixtures/kfde-tiled.json b/nix/tests/vm/lib/tests/fixtures/kfde-tiled.json new file mode 100644 index 00000000..941b94e9 --- /dev/null +++ b/nix/tests/vm/lib/tests/fixtures/kfde-tiled.json @@ -0,0 +1,180 @@ +{ + "monitors": { + "elements": [ + { + "id": 1230296105, + "device": "Virtual-0", + "serial_number_id": "Virtual-0", + "workspaces": { + "elements": [ + { + "name": null, + "containers": { + "elements": [ + { + "id": "maAEzGA_0nRWS6PpcDLlI", + "locked": false, + "windows": { + "elements": [ + { + "id": 4, + "rect": { + "left": 20, + "top": 20, + "right": 925, + "bottom": 1040 + }, + "details": { + "title": "neutral1", + "exe": "foot", + "class": "foot", + "path": "/nix/store/0gkg75w7bmwd7klmf23qy8m697abypvc-foot-1.28.0/bin/foot" + } + } + ], + "focused": 0 + } + }, + { + "id": "f63LOWKm57v9SDinvNb_K", + "locked": false, + "windows": { + "elements": [ + { + "id": 6, + "rect": { + "left": 975, + "top": 20, + "right": 924, + "bottom": 504 + }, + "details": { + "title": "neutral3", + "exe": "foot", + "class": "foot", + "path": "/nix/store/0gkg75w7bmwd7klmf23qy8m697abypvc-foot-1.28.0/bin/foot" + } + } + ], + "focused": 0 + } + }, + { + "id": "z2cN_l7IEJBa53ymxMgvA", + "locked": false, + "windows": { + "elements": [ + { + "id": 5, + "rect": { + "left": 975, + "top": 555, + "right": 924, + "bottom": 504 + }, + "details": { + "title": "neutral2", + "exe": "foot", + "class": "foot", + "path": "/nix/store/0gkg75w7bmwd7klmf23qy8m697abypvc-foot-1.28.0/bin/foot" + } + } + ], + "focused": 0 + } + } + ], + "focused": 1 + }, + "monocle_container": null, + "maximized_window": null, + "floating_windows": { + "elements": [], + "focused": 0 + }, + "layout": { + "Default": "BSP" + }, + "layout_options": null, + "layout_rules": [], + "layout_options_rules": [], + "layout_flip": null, + "workspace_padding": null, + "container_padding": null, + "latest_layout": [ + { + "left": 20, + "top": 20, + "right": 925, + "bottom": 1040 + }, + { + "left": 975, + "top": 20, + "right": 925, + "bottom": 505 + }, + { + "left": 975, + "top": 555, + "right": 925, + "bottom": 505 + } + ], + "resize_dimensions": [ + null, + null, + null + ], + "tile": true, + "work_area_offset": null, + "work_area_offset_rules": [], + "apply_window_based_work_area_offset": true, + "window_container_behaviour": null, + "window_container_behaviour_rules": null, + "float_override": null, + "layer": "Tiling", + "floating_layer_behaviour": null, + "wallpaper": null + } + ], + "focused": 0 + }, + "size": { + "left": 0, + "top": 0, + "right": 1920, + "bottom": 1080 + }, + "work_area_offset": null, + "work_area_size": { + "left": 0, + "top": 0, + "right": 1920, + "bottom": 1080 + }, + "window_based_work_area_offset": null, + "window_based_work_area_offset_limit": 1, + "container_padding": null, + "workspace_padding": null, + "last_focused_workspace": 0, + "wallpaper": null, + "floating_layer_behaviour": null + } + ], + "focused": 0 + }, + "monitor_usr_idx_map": { + "0": 0 + }, + "is_paused": false, + "resize_delta": 50, + "new_window_behaviour": "Create", + "float_override": false, + "cross_monitor_move_behaviour": "Swap", + "monocle_focus_behaviour": "NoOp", + "unmanaged_window_operation_behaviour": "Op", + "work_area_offset": null, + "focus_follows_mouse": null, + "mouse_follows_focus": true +} \ No newline at end of file diff --git a/nix/tests/vm/lib/tests/fixtures/kfw-float.json b/nix/tests/vm/lib/tests/fixtures/kfw-float.json new file mode 100644 index 00000000..1f996b12 --- /dev/null +++ b/nix/tests/vm/lib/tests/fixtures/kfw-float.json @@ -0,0 +1,160 @@ +{ + "monitors": { + "elements": [ + { + "id": 65537, + "name": "DISPLAY1", + "device": "Default_Monitor", + "device_id": "Default_Monitor-4&183790ca&0&UID0", + "serial_number_id": null, + "size": { + "left": 0, + "top": 0, + "right": 1280, + "bottom": 800 + }, + "work_area_size": { + "left": 0, + "top": 0, + "right": 1280, + "bottom": 752 + }, + "work_area_offset": null, + "window_based_work_area_offset": null, + "window_based_work_area_offset_limit": 1, + "workspaces": { + "elements": [ + { + "name": null, + "containers": { + "elements": [ + { + "id": "lHDkMLvTxsJGQfBTiKnkt", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 262540, + "title": "win1", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + } + } + ], + "focused": 0 + } + }, + { + "id": "Zqsuvee3D_fegP33OgBnq", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 131234, + "title": "win2", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 657, + "top": 27, + "right": 596, + "bottom": 698 + } + } + ], + "focused": 0 + } + } + ], + "focused": 0 + }, + "monocle_container": null, + "maximized_window": null, + "floating_windows": { + "elements": [ + { + "hwnd": 327778, + "title": "win3", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + } + } + ], + "focused": 0 + }, + "layout": { + "Default": "BSP" + }, + "layout_options": null, + "layout_rules": [], + "layout_options_rules": [], + "work_area_offset_rules": [], + "layout_flip": null, + "workspace_padding": 10, + "container_padding": 10, + "latest_layout": [ + { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + }, + { + "left": 657, + "top": 27, + "right": 596, + "bottom": 698 + } + ], + "resize_dimensions": [ + null, + null + ], + "tile": true, + "work_area_offset": null, + "apply_window_based_work_area_offset": true, + "window_container_behaviour": null, + "window_container_behaviour_rules": null, + "float_override": null, + "layer": "Floating", + "floating_layer_behaviour": null, + "wallpaper": null + } + ], + "focused": 0 + }, + "workspace_names": {}, + "container_padding": null, + "workspace_padding": null, + "wallpaper": null, + "floating_layer_behaviour": null + } + ], + "focused": 0 + }, + "monitor_usr_idx_map": { + "0": 0 + }, + "is_paused": false, + "resize_delta": 50, + "new_window_behaviour": "Create", + "float_override": false, + "cross_monitor_move_behaviour": "Swap", + "monocle_focus_behaviour": "NoOp", + "unmanaged_window_operation_behaviour": "Op", + "work_area_offset": null, + "focus_follows_mouse": null, + "mouse_follows_focus": true, + "has_pending_raise_op": false, + "virtual_desktop_id": null +} diff --git a/nix/tests/vm/lib/tests/fixtures/kfw-stack.json b/nix/tests/vm/lib/tests/fixtures/kfw-stack.json new file mode 100644 index 00000000..2df13c18 --- /dev/null +++ b/nix/tests/vm/lib/tests/fixtures/kfw-stack.json @@ -0,0 +1,159 @@ +{ + "monitors": { + "elements": [ + { + "id": 65537, + "name": "DISPLAY1", + "device": "Default_Monitor", + "device_id": "Default_Monitor-4&183790ca&0&UID0", + "serial_number_id": null, + "size": { + "left": 0, + "top": 0, + "right": 1280, + "bottom": 800 + }, + "work_area_size": { + "left": 0, + "top": 0, + "right": 1280, + "bottom": 752 + }, + "work_area_offset": null, + "window_based_work_area_offset": null, + "window_based_work_area_offset_limit": 1, + "workspaces": { + "elements": [ + { + "name": null, + "containers": { + "elements": [ + { + "id": "lHDkMLvTxsJGQfBTiKnkt", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 262540, + "title": "win1", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + } + }, + { + "hwnd": 327778, + "title": "win3", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + } + } + ], + "focused": 1 + } + }, + { + "id": "Zqsuvee3D_fegP33OgBnq", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 131234, + "title": "win2", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 657, + "top": 27, + "right": 596, + "bottom": 698 + } + } + ], + "focused": 0 + } + } + ], + "focused": 0 + }, + "monocle_container": null, + "maximized_window": null, + "floating_windows": { + "elements": [], + "focused": 0 + }, + "layout": { + "Default": "BSP" + }, + "layout_options": null, + "layout_rules": [], + "layout_options_rules": [], + "work_area_offset_rules": [], + "layout_flip": null, + "workspace_padding": 10, + "container_padding": 10, + "latest_layout": [ + { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + }, + { + "left": 657, + "top": 27, + "right": 596, + "bottom": 698 + } + ], + "resize_dimensions": [ + null, + null + ], + "tile": true, + "work_area_offset": null, + "apply_window_based_work_area_offset": true, + "window_container_behaviour": null, + "window_container_behaviour_rules": null, + "float_override": null, + "layer": "Tiling", + "floating_layer_behaviour": null, + "wallpaper": null + } + ], + "focused": 0 + }, + "workspace_names": {}, + "container_padding": null, + "workspace_padding": null, + "wallpaper": null, + "floating_layer_behaviour": null + } + ], + "focused": 0 + }, + "monitor_usr_idx_map": { + "0": 0 + }, + "is_paused": false, + "resize_delta": 50, + "new_window_behaviour": "Create", + "float_override": false, + "cross_monitor_move_behaviour": "Swap", + "monocle_focus_behaviour": "NoOp", + "unmanaged_window_operation_behaviour": "Op", + "work_area_offset": null, + "focus_follows_mouse": null, + "mouse_follows_focus": true, + "has_pending_raise_op": false, + "virtual_desktop_id": null +} diff --git a/nix/tests/vm/lib/tests/fixtures/kfw-tiled.json b/nix/tests/vm/lib/tests/fixtures/kfw-tiled.json new file mode 100644 index 00000000..dc9de770 --- /dev/null +++ b/nix/tests/vm/lib/tests/fixtures/kfw-tiled.json @@ -0,0 +1,175 @@ +{ + "monitors": { + "elements": [ + { + "id": 65537, + "name": "DISPLAY1", + "device": "Default_Monitor", + "device_id": "Default_Monitor-4&183790ca&0&UID0", + "serial_number_id": null, + "size": { + "left": 0, + "top": 0, + "right": 1280, + "bottom": 800 + }, + "work_area_size": { + "left": 0, + "top": 0, + "right": 1280, + "bottom": 752 + }, + "work_area_offset": null, + "window_based_work_area_offset": null, + "window_based_work_area_offset_limit": 1, + "workspaces": { + "elements": [ + { + "name": null, + "containers": { + "elements": [ + { + "id": "lHDkMLvTxsJGQfBTiKnkt", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 262540, + "title": "win1", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + } + } + ], + "focused": 0 + } + }, + { + "id": "Zqsuvee3D_fegP33OgBnq", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 131234, + "title": "win2", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 657, + "top": 27, + "right": 596, + "bottom": 332 + } + } + ], + "focused": 0 + } + }, + { + "id": "fop08rEXwYLbkqzpy3nE9", + "locked": false, + "windows": { + "elements": [ + { + "hwnd": 327778, + "title": "win3", + "exe": "komorebi-testwin.exe", + "class": "KomorebiTestWin", + "rect": { + "left": 657, + "top": 393, + "right": 596, + "bottom": 332 + } + } + ], + "focused": 0 + } + } + ], + "focused": 2 + }, + "monocle_container": null, + "maximized_window": null, + "floating_windows": { + "elements": [], + "focused": 0 + }, + "layout": { + "Default": "BSP" + }, + "layout_options": null, + "layout_rules": [], + "layout_options_rules": [], + "work_area_offset_rules": [], + "layout_flip": null, + "workspace_padding": 10, + "container_padding": 10, + "latest_layout": [ + { + "left": 27, + "top": 27, + "right": 596, + "bottom": 698 + }, + { + "left": 657, + "top": 27, + "right": 596, + "bottom": 332 + }, + { + "left": 657, + "top": 393, + "right": 596, + "bottom": 332 + } + ], + "resize_dimensions": [ + null, + null, + null + ], + "tile": true, + "work_area_offset": null, + "apply_window_based_work_area_offset": true, + "window_container_behaviour": null, + "window_container_behaviour_rules": null, + "float_override": null, + "layer": "Tiling", + "floating_layer_behaviour": null, + "wallpaper": null + } + ], + "focused": 0 + }, + "workspace_names": {}, + "container_padding": null, + "workspace_padding": null, + "wallpaper": null, + "floating_layer_behaviour": null + } + ], + "focused": 0 + }, + "monitor_usr_idx_map": { + "0": 0 + }, + "is_paused": false, + "resize_delta": 50, + "new_window_behaviour": "Create", + "float_override": false, + "cross_monitor_move_behaviour": "Swap", + "monocle_focus_behaviour": "NoOp", + "unmanaged_window_operation_behaviour": "Op", + "work_area_offset": null, + "focus_follows_mouse": null, + "mouse_follows_focus": true, + "has_pending_raise_op": false, + "virtual_desktop_id": null +} diff --git a/nix/tests/vm/lib/tests/test_recorded.py b/nix/tests/vm/lib/tests/test_recorded.py new file mode 100644 index 00000000..cf3c5326 --- /dev/null +++ b/nix/tests/vm/lib/tests/test_recorded.py @@ -0,0 +1,92 @@ +"""The parser on what each port printed, recorded from a real run. + +`fixtures/kfde-*.json` are the `komorebic state` of kfde in a headless KWin (the `core-e2e` suite +writes them into its output); `fixtures/kfw-*.json` are the `komorebic state` of kfw's daemon on a +Windows 11 guest. Both are raw stdout, unedited: they hold only the test windows. What differs +between the two ports is what `Snapshot` exists to hide, so the checks below are the same for both. +""" + +import json +from pathlib import Path + +import pytest + +from komorebi_vm.core.snapshot import Snapshot, parse + +FIXTURES = Path(__file__).parent / "fixtures" +# a terminal cell, in pixels, which is how far a `foot` window can be short of the frame asked for +CELL = 24 + +# the port, and the titles of the three windows its scenario opened +PORTS = {"kfde": {"neutral1", "neutral2", "neutral3"}, "kfw": {"win1", "win2", "win3"}} + + +def recorded(name: str) -> Snapshot: + return parse(json.loads((FIXTURES / f"{name}.json").read_text())) + + +@pytest.mark.parametrize("port", PORTS) +def test_three_tiled_windows_are_three_containers_of_one(port: str) -> None: + snap = recorded(f"{port}-tiled") + assert sorted(t for c in snap.containers() for t in c) == sorted(PORTS[port]) + assert [len(c) for c in snap.containers()] == [1, 1, 1] + assert snap.managed() == 3 + assert snap.workspace().layout == "BSP" + assert snap.workspace().tile + + +@pytest.mark.parametrize("port", PORTS) +def test_the_layout_has_a_rectangle_for_each_container_inside_the_work_area(port: str) -> None: + snap = recorded(f"{port}-tiled") + layout = snap.workspace().latest_layout + assert len(layout) == 3 + area = snap.monitors[0].work_area + assert all(rect.inside(area) for rect in layout) + assert not any(a.overlaps(b) for i, a in enumerate(layout) for b in layout[i + 1 :]) + + +@pytest.mark.parametrize("port", PORTS) +def test_each_window_is_where_its_containers_rectangle_is_up_to_the_clients_rounding( + port: str, +) -> None: + """The same place, and the size no more than a terminal cell short: kfde's test windows are + `foot`, which sizes itself to whole cells (a frame of 924 by 504 for a layout of 925 by 505), + where kfw's test windows have no granularity.""" + snap = recorded(f"{port}-tiled") + workspace = snap.workspace() + for container, rect in zip(workspace.containers, workspace.latest_layout, strict=True): + frame = container[0].rect + assert frame is not None + assert (frame.x, frame.y) == (rect.x, rect.y) + assert 0 <= rect.w - frame.w < CELL + assert 0 <= rect.h - frame.h < CELL + + +@pytest.mark.parametrize("port", PORTS) +def test_the_focused_window_is_one_of_the_three(port: str) -> None: + assert recorded(f"{port}-tiled").focused_title() in PORTS[port] + + +@pytest.mark.parametrize("port", PORTS) +def test_a_stack_is_a_container_of_two_and_its_focused_window_is_the_one_the_ring_names( + port: str, +) -> None: + snap = recorded(f"{port}-stack") + assert sorted(len(c) for c in snap.containers()) == [1, 2] + stacked = next(c for c in snap.containers() if len(c) == 2) + assert snap.focused_title() in stacked + assert len(snap.workspace().latest_layout) == 2 + assert snap.managed() == 3 + + +def test_a_floating_window_is_not_in_a_container() -> None: + snap = recorded("kfw-float") + workspace = snap.workspace() + assert [w.title for w in workspace.floating] == ["win3"] + assert sorted(t for c in snap.containers() for t in c) == ["win1", "win2"] + assert snap.managed() == 3 + + +def test_what_only_kfw_has_is_kept_aside() -> None: + assert "has_pending_raise_op" in recorded("kfw-tiled").extra + assert "has_pending_raise_op" not in recorded("kfde-tiled").extra diff --git a/nix/tests/vm/lib/tests/test_snapshot.py b/nix/tests/vm/lib/tests/test_snapshot.py index 6d6d8aff..dd91d870 100644 --- a/nix/tests/vm/lib/tests/test_snapshot.py +++ b/nix/tests/vm/lib/tests/test_snapshot.py @@ -1,8 +1,8 @@ """The same scene, as each port prints it, reads as the same Snapshot. -The fixtures are written from the `Serialize` code of the two ports (kfde `komorebi/src/window.rs`, -kfw `komorebi/src/window.rs`), not recorded; a recorded state of each replaces them once a run on -each port exists. +The scenes are written from the `Serialize` code of the two ports (kfde `komorebi/src/window.rs`, +kfw `komorebi/src/window.rs`), so that shapes no run has recorded (a monocle, a stack in a +neighbor's place) can be tried; `test_recorded.py` has what each port really printed. """ from typing import Any diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index 5dee1fa1..34834fd3 100644 --- a/nix/tests/vm/suites/core-e2e/test.py +++ b/nix/tests/vm/suites/core-e2e/test.py @@ -76,12 +76,16 @@ def parked() -> list[str]: if PARK not in names: return [] park = names.index(PARK) + 1 - return sorted(t for t in ["win1", "win2"] if kde.wid(t) != "" and kde.desk(kde.wid(t)) == park) + return sorted( + t for t in ["win1", "win2"] if kde.wid(t) != "" and kde.desk(kde.wid(t)) == park + ) def on_a_workspace_desktop() -> bool: """Every window that is open is on a desktop that is a workspace, not on the parking one.""" - return all(kde.desk(kde.wid(t)) in (1, 2) for t in ["win1", "win2"] if kde.wid(t) != "") + return all( + kde.desk(kde.wid(t)) in (1, 2) for t in ["win1", "win2"] if kde.wid(t) != "" + ) def state_agrees() -> bool: @@ -108,14 +112,18 @@ checks.check( kde.wait_for(lambda: "loaded the KWin bridge script" in daemon_log(), timeout=30), ) checks.check("KWin runs the script", kde.script_loaded()) -checks.check("the daemon is up and answering komorebic", kde.wait_for(answering, timeout=30)) +checks.check( + "the daemon is up and answering komorebic", kde.wait_for(answering, timeout=30) +) print("== 2. three windows are tiled") titles = ["win1", "win2", "win3"] for title in titles: kde.spawn(f"foot -T {title} sleep 3000") kde.wait_for(lambda title=title: kde.wid(title) != "", timeout=30) - kde.wait_for(lambda title=title: title in daemon.window_ids(daemon_log()), timeout=10) + kde.wait_for( + lambda title=title: title in daemon.window_ids(daemon_log()), timeout=10 + ) settled = kde.wait_for(lambda: all(where_asked(t) for t in titles), timeout=20) report(titles) checks.check("every window is where the daemon asked for it", settled) @@ -125,14 +133,24 @@ for title, rect in zip(titles, rects, strict=True): checks.check("win1 and win2 do not overlap", not rects[0].overlaps(rects[1])) checks.check("win1 and win3 do not overlap", not rects[0].overlaps(rects[2])) checks.check("win2 and win3 do not overlap", not rects[1].overlaps(rects[2])) -checks.check("the windows share the screen: none is full width", all(r.w < W - 20 for r in rects)) -checks.check("KWin and the daemon agree on the focused window", kde.wait_for(focus_agrees)) -checks.check("komorebi manages the three windows", kde.wait_for(lambda: kde.managed() == 3)) -checks.check("komorebi's state focuses the window KWin does", kde.wait_for(state_agrees)) +checks.check( + "the windows share the screen: none is full width", all(r.w < W - 20 for r in rects) +) +checks.check( + "KWin and the daemon agree on the focused window", kde.wait_for(focus_agrees) +) +checks.check( + "komorebi manages the three windows", kde.wait_for(lambda: kde.managed() == 3) +) +checks.check( + "komorebi's state focuses the window KWin does", kde.wait_for(state_agrees) +) print("== 3. the focus moves by hand") kde.activate(kde.wid("win1")) -checks.check("KWin focused win1", kde.wait_for(lambda: kde.title(kde.active()) == "win1")) +checks.check( + "KWin focused win1", kde.wait_for(lambda: kde.title(kde.active()) == "win1") +) checks.check("... and the daemon followed", kde.wait_for(focus_agrees)) checks.check("... and its state says so", kde.wait_for(state_agrees)) @@ -142,10 +160,13 @@ checks.check( "after 'focus right' KWin has given the focus to another window", kde.wait_for(lambda: kde.title(kde.active()) not in ("", "win1")), ) -checks.check("... and komorebi's state focuses the same one", kde.wait_for(state_agrees)) +checks.check( + "... and komorebi's state focuses the same one", kde.wait_for(state_agrees) +) kde.komorebic("focus", "left") checks.check( - "'focus left' gives it back to win1", kde.wait_for(lambda: kde.title(kde.active()) == "win1") + "'focus left' gives it back to win1", + kde.wait_for(lambda: kde.title(kde.active()) == "win1"), ) checks.check("... and komorebi's state says so", kde.wait_for(state_agrees)) kde.komorebic("cycle-focus", "next") @@ -201,13 +222,17 @@ checks.check( checks.check( "komorebi's state has it as the monocle container", kde.wait_for( - lambda: kde.state()["monitors"]["elements"][0]["workspaces"]["elements"][0][ - "monocle_container" - ] - is not None + lambda: ( + kde.state()["monitors"]["elements"][0]["workspaces"]["elements"][0][ + "monocle_container" + ] + is not None + ) ), ) -checks.check("... and it still has the focus", kde.title(kde.active()) == kde.title(focused)) +checks.check( + "... and it still has the focus", kde.title(kde.active()) == kde.title(focused) +) kde.komorebic("toggle-monocle") checks.check( "toggling back puts it on its tile", @@ -225,7 +250,8 @@ kde.komorebic("focus", "right") checks.check( "with the cycle behaviour, focusing in a direction gives another window the work area", kde.wait_for( - lambda: kde.active() != focused and fills_the_work_area(kde.active()), timeout=20 + lambda: kde.active() != focused and fills_the_work_area(kde.active()), + timeout=20, ), ) kde.komorebic("toggle-monocle") @@ -270,7 +296,9 @@ report(titles) print("== 8. close") kde.close(kde.wid("win3")) -checks.check("KWin closed win3", kde.wait_for(lambda: kde.wid("win3") == "", timeout=10)) +checks.check( + "KWin closed win3", kde.wait_for(lambda: kde.wid("win3") == "", timeout=10) +) checks.check( "the two that are left share the screen again", kde.wait_for( @@ -282,8 +310,12 @@ checks.check( ), ) report(["win1", "win2"]) -checks.check("komorebi manages the two that are left", kde.wait_for(lambda: kde.managed() == 2)) -checks.check("KWin carried out every command", "the bridge could not do" not in daemon_log()) +checks.check( + "komorebi manages the two that are left", kde.wait_for(lambda: kde.managed() == 2) +) +checks.check( + "KWin carried out every command", "the bridge could not do" not in daemon_log() +) print("== 9. a clean stop undoes what a monocle did") kde.komorebic("toggle-monocle") @@ -293,7 +325,9 @@ checks.check( ) kde.komorebic("stop") checks.check("the daemon exited", kde.wait_for(lambda: not kde.komorebi_running())) -checks.check("the parking desktop is gone", kde.wait_for(lambda: PARK not in kde.desktop_names())) +checks.check( + "the parking desktop is gone", kde.wait_for(lambda: PARK not in kde.desktop_names()) +) checks.check( "and every window is on a workspace's desktop", kde.wait_for(on_a_workspace_desktop) ) @@ -301,7 +335,9 @@ checks.check( print("== 10. a crash with a monocle on leaves nothing stuck behind") kde.start_komorebi() checks.check("the daemon answers", kde.wait_for(answering, timeout=30)) -checks.check("it manages the two windows again", kde.wait_for(lambda: kde.managed() == 2)) +checks.check( + "it manages the two windows again", kde.wait_for(lambda: kde.managed() == 2) +) kde.komorebic("toggle-monocle") checks.check( "a window is parked and a parking desktop exists", @@ -318,53 +354,94 @@ checks.check( kde.wait_for(lambda: answering() and PARK not in kde.desktop_names()), ) checks.check("and brought the parked window back", kde.wait_for(on_a_workspace_desktop)) -checks.check("it manages the two windows again", kde.wait_for(lambda: kde.managed() == 2)) +checks.check( + "it manages the two windows again", kde.wait_for(lambda: kde.managed() == 2) +) kde.komorebic("toggle-monocle") checks.check( "after a crash and a restart a monocle still hides the other window", kde.wait_for(lambda: len(parked()) == 1), ) kde.komorebic("toggle-monocle") -checks.check("and turned off, nothing is out of sight", kde.wait_for(lambda: parked() == [])) +checks.check( + "and turned off, nothing is out of sight", kde.wait_for(lambda: parked() == []) +) print("== 11. KWin restarts under the daemon") kde.restart_kwin() checks.check( "the daemon loaded its script into the new KWin", - kde.wait_for(lambda: daemon_log().count("loaded the KWin bridge script") == 2, timeout=30), + kde.wait_for( + lambda: daemon_log().count("loaded the KWin bridge script") == 2, timeout=30 + ), ) checks.check( - "komorebi let go of the windows of the old KWin", kde.wait_for(lambda: kde.managed() == 0) + "komorebi let go of the windows of the old KWin", + kde.wait_for(lambda: kde.managed() == 0), ) kde.spawn("foot -T afterwards sleep 3000") kde.wait_for(lambda: kde.wid("afterwards") != "", timeout=30) -checks.check("a window opened in the new KWin is managed", kde.wait_for(lambda: kde.managed() == 1)) +checks.check( + "a window opened in the new KWin is managed", + kde.wait_for(lambda: kde.managed() == 1), +) checks.check( "... and tiled on its own", kde.wait_for( - lambda: where_asked("afterwards") and kde.rect(kde.wid("afterwards")).w > W - 100, + lambda: ( + where_asked("afterwards") and kde.rect(kde.wid("afterwards")).w > W - 100 + ), timeout=20, ), ) report(["afterwards"]) -print("== 12. tiling, focus, moving, stacks and workspaces, through the neutral platform") +print( + "== 12. tiling, focus, moving, stacks and workspaces, through the neutral platform" +) # the scenarios of komorebi_vm.core.scenarios are what runs on Windows too: it asks the # operating system where the windows are, and not the daemon's log kde.close(kde.wid("afterwards")) -checks.check("the window of the last section is closed", kde.wait_for(lambda: kde.managed() == 0)) +checks.check( + "the window of the last section is closed", kde.wait_for(lambda: kde.managed() == 0) +) platform = KdePlatform(kde, (W, H)) neutral = tiling.run(platform, checks, titles=("neutral1", "neutral2", "neutral3")) focus.run(platform, checks, neutral) moving.run(platform, checks, neutral) stacks.run(platform, checks, neutral) + +# what `komorebic state` prints, kept as a file of the run so that the unit tests of the neutral +# core (tests/fixtures) are of a real state and not of one written from the serializer +(machine.out_dir / "state-kfde-tiled.json").write_text( + json.dumps(kde.state(), indent=2) +) +platform.activate(neutral[-1]) +kde.komorebic("stack", "left") +checks.check( + "the windows are stacked", + kde.wait_for(lambda: sorted(map(len, kde.containers())) == [1, 2]), +) +(machine.out_dir / "state-kfde-stack.json").write_text( + json.dumps(kde.state(), indent=2) +) +kde.komorebic("unstack") +checks.check( + "and unstacked", + kde.wait_for(lambda: sorted(map(len, kde.containers())) == [1, 1, 1]), +) workspaces.run(platform, checks, neutral) print("== 13. komorebic stop") kde.komorebic("stop") checks.check("the daemon exited", kde.wait_for(lambda: not kde.komorebi_running())) -checks.check("its socket is gone", kde.run(f"test -e {kde.runtime}/komorebi/komorebi.sock")[0] != 0) -checks.check("the script is no longer in KWin", kde.wait_for(lambda: not kde.script_loaded())) +checks.check( + "its socket is gone", + kde.run(f"test -e {kde.runtime}/komorebi/komorebi.sock")[0] != 0, +) +checks.check( + "the script is no longer in KWin", kde.wait_for(lambda: not kde.script_loaded()) +) checks.check("the windows stay", kde.wid("neutral1") != "") kde.stop() diff --git a/scripts/sync-e2e-core b/scripts/sync-e2e-core index 67b353e3..63c8ff6c 100755 --- a/scripts/sync-e2e-core +++ b/scripts/sync-e2e-core @@ -4,7 +4,8 @@ # package komorebi_vm.core, and the unit tests that belong to it. The source is this repository until # the move is made, and the script goes with it. # -# scripts/sync-e2e-core DEST copy into DEST/komorebi_vm/core and DEST/tests +# scripts/sync-e2e-core DEST copy into DEST/komorebi_vm/core, DEST/tests and +# DEST/tests/fixtures # scripts/sync-e2e-core --check DEST say what differs, and exit 1 if anything does # # Nothing else of DEST is touched, and a file of DEST that is not here is reported and left alone. @@ -19,7 +20,8 @@ dest=${1:?usage: sync-e2e-core [--check] DEST} repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) lib=$repo/nix/tests/vm/lib -tests=(fake_platform.py test_checks.py test_pixels.py test_snapshot.py test_tiling.py test_focus_moving.py) +tests=(fake_platform.py test_checks.py test_pixels.py test_snapshot.py test_recorded.py test_tiling.py + test_focus_moving.py) status=0 sync() { @@ -33,6 +35,7 @@ sync() { } sync "$lib/komorebi_vm/core" "$dest/komorebi_vm/core" +sync "$lib/tests/fixtures" "$dest/tests/fixtures" for t in "${tests[@]}"; do if [ "$check" = 1 ]; then