Files
komorebi/nix/tests/vm/lib/tests/fake_platform.py
T
LGUG2Z 0f73ba3a55 feat(nix): the stacks scenario, and Platform.hidden
hidden says that a window exists but the daemon has put it away: the others
of a stack, here, and later those of another workspace or behind a monocle.
It is a window on the parking desktop on KDE and will be a cloaked one on
Windows. scenarios.stacks stacks the focused window onto its neighbor,
cycles the stack, checks that the window in view has the focus and fills
the tile the first had, and takes the stack apart.

The scenario's fake daemon now models stacks. Running it on KDE found a bug
in Snapshot: focused_title took the first window of the focused container,
and a stack's focused window is the one its ring names. Snapshot keeps that
index now. core-e2e runs the scenario after the others: 98 checks pass on
KVM.
2026-10-30 12:02:55 -07:00

143 lines
5.3 KiB
Python

"""A desktop in memory with a daemon that follows the commands of the scenarios.
Three tiles in a row, a window in each. `focus`, `cycle-focus`, `move` and `cycle-move` do what the
real daemon does with windows laid out left to right, so that a scenario can be run against a daemon
that is right, and against ones that are wrong in one way.
"""
from typing import Any
from PIL import Image
from komorebi_vm.core.geometry import Rect
from komorebi_vm.core.platform import Client, OsWindow
from komorebi_vm.core.snapshot import Snapshot, parse
TILES = [Rect(0, 0, 640, 1080), Rect(640, 0, 640, 1080), Rect(1280, 0, 640, 1080)]
class FakePlatform:
tolerance = 24
def __init__(self, *, moves: bool = True, focus_follows_move: bool = True) -> None:
self.containers: list[list[str]] = []
"""The stack of each container, in the order of the tiles; the first is in view."""
self.focus = 0
self.moves = moves
self.focus_follows_move = focus_follows_move
def wait_for(self, predicate: Any, timeout: float = 10) -> bool:
return bool(predicate())
def start_komorebi(self) -> None:
pass
@property
def order(self) -> list[str]:
"""The titles in view, in the order of the tiles."""
return [stack[0] for stack in self.containers]
def spawn(self, title: str) -> Client:
self.containers.append([title])
return Client(title)
def _container(self, title: str) -> int:
return next(i for i, stack in enumerate(self.containers) if title in stack)
def hidden(self, client: Client) -> bool:
stack = self.containers[self._container(client.title)]
return stack[0] != client.title
def work_area(self) -> Rect:
return Rect(0, 0, 1920, 1080)
def activate(self, client: Client) -> None:
self.focus = self._container(client.title)
stack = self.containers[self.focus]
stack.insert(0, stack.pop(stack.index(client.title)))
def screenshot(self, name: str) -> Image.Image:
return Image.new("RGB", (4, 4))
def os_window(self, client: Client) -> OsWindow | None:
i = self._container(client.title)
return OsWindow(
client.title,
TILES[i],
focused=i == self.focus and self.containers[i][0] == client.title,
)
def komorebic(self, *args: str) -> str:
n = len(self.containers)
match args:
case ("focus", "right"):
self.focus = min(self.focus + 1, n - 1)
case ("focus", "left"):
self.focus = max(self.focus - 1, 0)
case ("cycle-focus", "next"):
self.focus = (self.focus + 1) % n
case ("cycle-focus", "previous"):
self.focus = (self.focus - 1) % n
case ("move", "right"):
self._swap(min(self.focus + 1, n - 1))
case ("cycle-move", "previous"):
self._swap((self.focus - 1) % n)
case ("stack", "left"):
if self.focus > 0:
stack = self.containers.pop(self.focus)
self.focus -= 1
self.containers[self.focus][:0] = stack
case ("cycle-stack", "next" | "previous" as way):
stack = self.containers[self.focus]
stack.append(stack.pop(0)) if way == "next" else stack.insert(0, stack.pop())
case ("unstack",):
stack = self.containers.pop(self.focus)
self.containers[self.focus : self.focus] = [[t] for t in stack]
case _:
raise AssertionError(f"the fake does not know komorebic {args}")
return ""
def _swap(self, other: int) -> None:
if not self.moves:
return
self.containers[self.focus], self.containers[other] = (
self.containers[other],
self.containers[self.focus],
)
if self.focus_follows_move:
self.focus = other
def state(self) -> Snapshot:
def window(title: str, tile: Rect) -> dict[str, Any]:
return {
"id": hash(title) % 1000,
"rect": {"left": tile.x, "top": tile.y, "right": tile.w, "bottom": tile.h},
"details": {"title": title, "exe": "", "class": "", "path": ""},
}
workspace = {
"name": None,
"containers": {
"elements": [
{
"id": str(i),
"locked": False,
"windows": {"elements": [window(t, TILES[i]) for t in stack], "focused": 0},
}
for i, stack in enumerate(self.containers)
],
"focused": self.focus,
},
"monocle_container": None,
"maximized_window": None,
"floating_windows": {"elements": [], "focused": 0},
"layout": {"Default": "Columns"},
"tile": True,
"latest_layout": [
{"left": r.x, "top": r.y, "right": r.w, "bottom": r.h} for r in TILES
],
}
size = {"left": 0, "top": 0, "right": 1920, "bottom": 1080}
monitor = {"id": 1, "size": size, "workspaces": {"elements": [workspace], "focused": 0}}
return parse({"monitors": {"elements": [monitor], "focused": 0}})