"""Running container plans against a compositor-shaped socket. The compositor here is a real ``AF_UNIX`` listener laid out the way Hyprland lays one out, answering the commands Hyprland answers. That proves the wiring — the right commands, in the right order, and a record built from the reply rather than from the request. It does **not** prove Hyprland agrees: see the slice report for what is unverified. """ from __future__ import annotations from pathlib import Path import pytest from fakes import WINDOW_PID, FakeCompositor from hhstreamd import containers from hhstreamd.container_host import ContainerHost from hhstreamd.ipc import CompositorUnavailable @pytest.fixture def compositor(short_tmpdir: Path): fake = FakeCompositor(short_tmpdir) try: yield fake finally: fake.stop() def test_a_disabled_host_is_unavailable_and_says_why() -> None: """Turning containers off must be configuration, not a false compositor. ``available()`` is the whole mechanism: the manager already treats an unavailable host as "sessions without containers", which is exactly what turning them off means, so nothing else has to branch on it. It must also not ask a compositor this deployment never intended to talk to -- and its reason has to name the key, or a machine with containers disabled looks identical to one whose compositor is down and the operator goes hunting for a fault that is not there. """ host = ContainerHost( None, allocator=containers.WorkspaceAllocator(1000, 4), enabled=False ) assert host.available() is False assert "containers_enabled" in (host.last_error or "") def make_host(fake: FakeCompositor, *, slots: int = 4) -> ContainerHost: return ContainerHost( fake.runtime_dir, allocator=containers.WorkspaceAllocator(1000, slots), signature=fake.signature, ) def test_a_live_compositor_is_available_and_names_its_instance(compositor): host = make_host(compositor) assert host.available() is True # The signature goes in the record so a restarted broker re-attaches to # this instance rather than whichever one is there later. assert host.signature == compositor.signature def test_no_compositor_is_unavailable_and_says_why(short_tmpdir: Path): host = ContainerHost( short_tmpdir / "nothing-here", allocator=containers.WorkspaceAllocator(1000, 2), ) assert host.available() is False assert host.last_error and "nothing-here" in host.last_error def test_no_runtime_directory_at_all_is_unavailable_rather_than_an_error(short_tmpdir): """A broker with no `--compositor-runtime-dir` still has to start.""" host = ContainerHost(None, allocator=containers.WorkspaceAllocator(1000, 2)) assert host.available() is False assert "no compositor runtime directory" in (host.last_error or "") def test_create_runs_the_plan_in_order_and_the_output_appears(compositor): host = make_host(compositor) host.available() plan = host.create("foot-aaaaaa", "hh-foot-aaaaaa") assert plan.output == "hh-foot-aaaaaa" assert plan.workspace == 1000 # The plan's own order, which is load-bearing: the output has to exist # before a rule can name it, and the mode is set on an output that is # already there. queries = ("version", "clients", "monitors", "workspaces") assert [c for c in compositor.commands if not c.startswith(queries)] == [ "output create headless hh-foot-aaaaaa", "keyword workspace 1000, monitor:hh-foot-aaaaaa, default:true, persistent:true", "keyword windowrule match:initial_class ^hh-foot-aaaaaa$, workspace 1000, " "monitor hh-foot-aaaaaa", "keyword monitor hh-foot-aaaaaa,1280x720,auto,1", ] assert "hh-foot-aaaaaa" in compositor.outputs() assert compositor.workspaces_on("hh-foot-aaaaaa") == [1000] def test_the_quarantine_backstop_goes_in_before_any_container_rule(compositor): """Rules are applied in declaration order, so the catch-all must be first. Installed after a session's anchored rule, the catch-all would override it and sweep that session's own window into the special workspace — the exact leak containers exist to prevent. """ host = make_host(compositor) host.available() assert host.install_quarantine() is True host.create("foot-aaaaaa", "hh-foot-aaaaaa") rules = [c for c in compositor.commands if c.startswith("keyword windowrule")] assert rules[0] == "keyword windowrule match:initial_class .*, workspace special:quarantine" assert rules[1].startswith("keyword windowrule match:initial_class ^hh-foot-aaaaaa$") def test_a_blank_quarantine_workspace_installs_nothing(compositor): host = ContainerHost( compositor.runtime_dir, allocator=containers.WorkspaceAllocator(1000, 2), quarantine_workspace=" ", ) host.available() assert host.install_quarantine() is False assert not [c for c in compositor.commands if c.startswith("keyword")] def test_a_refused_command_is_reported_and_the_slot_is_given_back(compositor): """A half-built container must not also leak a workspace number.""" host = make_host(compositor) host.available() compositor.refuse.add("keyword monitor hh-foot-aaaaaa,1280x720,auto,1") with pytest.raises(CompositorUnavailable) as exc: host.create("foot-aaaaaa", "hh-foot-aaaaaa") assert "refused" in str(exc.value) assert host.allocator.in_use() == [] def test_read_back_reports_the_compositor_s_answer_not_ours(compositor): host = make_host(compositor) host.available() host.create("foot-aaaaaa", "hh-foot-aaaaaa") facts = host.read_back("hh-foot-aaaaaa") assert facts is not None assert facts.workspace == 1000 # The output is joined through the *workspace's* monitor name, because a # client reports its monitor as a numeric index and `ipc.Monitor` carries # no id to join that against (see the slice report). assert facts.output == "hh-foot-aaaaaa" assert facts.window["initial_class"] == "hh-foot-aaaaaa" assert facts.window["stable_id"] == f"hh-foot-aaaaaa:{WINDOW_PID}" # Titles contain commas; the client must not have lost the end of one. assert facts.window["title"] == "a window, with a comma in its title" def test_foreground_is_the_monitor_active_workspace_join(compositor): """Not the client's `visible` flag: "is anyone actually looking at this".""" host = make_host(compositor) host.available() host.create("foot-aaaaaa", "hh-foot-aaaaaa") assert host.read_back("hh-foot-aaaaaa").foreground is True compositor.focus("hh-foot-aaaaaa", 9999) assert host.read_back("hh-foot-aaaaaa").foreground is False def test_no_window_yet_is_none_and_not_an_invented_one(compositor): host = make_host(compositor) host.available() compositor.auto_window = False host.create("foot-aaaaaa", "hh-foot-aaaaaa") assert host.read_back("hh-foot-aaaaaa") is None def test_a_window_that_kept_its_own_class_is_still_found_by_pid(compositor): """An application that ignored the stamp is observed, not pretended away.""" host = make_host(compositor) host.available() compositor.auto_window = False host.create("foot-aaaaaa", "hh-foot-aaaaaa") compositor.add_window("firefox", 1, "HEADLESS-1", pid=777) facts = host.read_back("hh-foot-aaaaaa", pid=777) assert facts is not None assert facts.window["initial_class"] == "firefox" # On the shared output, not the container's: the rule did not bind, and # the caller is the one that has to say so. assert facts.output == "HEADLESS-1" def test_remove_takes_the_output_and_its_windows_with_it(compositor): host = make_host(compositor) host.available() plan = host.create("foot-aaaaaa", "hh-foot-aaaaaa") host.remove(plan.output) assert "hh-foot-aaaaaa" not in compositor.outputs() assert compositor.windows() == [] assert host.survivors("hh-foot-aaaaaa", plan.output) == [] def test_a_window_that_outlives_the_output_is_reported_as_a_survivor(compositor): """Teardown reports what is left rather than declaring success.""" host = make_host(compositor) host.available() host.create("foot-aaaaaa", "hh-foot-aaaaaa") # A second window of ours that landed somewhere else entirely. compositor.add_window("hh-foot-aaaaaa", 1, "HEADLESS-1", pid=999) host.remove("hh-foot-aaaaaa") survivors = host.survivors("hh-foot-aaaaaa", "hh-foot-aaaaaa") assert [s["pid"] for s in survivors] == [999] @pytest.mark.parametrize("name", ["HEADLESS-1", "hh-", "HH-x", "eDP-1", "hh-Foo"]) def test_remove_refuses_any_name_the_filter_rejects(compositor, name): """`contains_output` is the only thing between the scan and someone else's monitor, so the removal path asks it too.""" host = make_host(compositor) host.available() with pytest.raises(ValueError) as exc: host.remove(name) assert "not one this broker creates" in str(exc.value) def test_our_outputs_lists_only_names_this_broker_could_have_made(compositor): host = make_host(compositor) host.available() host.create("foot-aaaaaa", "hh-foot-aaaaaa") compositor.add_output("DP-1") compositor.add_output("hh-orphan-zzzzzz") assert host.our_outputs() == ["hh-foot-aaaaaa", "hh-orphan-zzzzzz"] def test_reserve_re_takes_an_adopted_slot_so_it_is_not_handed_out_twice(compositor): """After a restart the records name workspaces that are still in use.""" host = make_host(compositor) assert host.reserve([1002]) == [1002] assert host.allocator.in_use() == [1002] # The slots walked past on the way there were given back, so the next # session still gets the lowest free one. assert host.allocator.allocate() == 1000 def test_reserving_a_slot_this_allocator_already_holds_is_not_a_second_hunt(compositor): """Reconcile runs more than once; the second pass must be a no-op.""" host = make_host(compositor, slots=2) assert host.reserve([1001]) == [1001] assert host.reserve([1001]) == [1001] assert host.allocator.in_use() == [1001] assert host.allocator.allocate() == 1000 def test_reserve_is_a_no_op_for_a_session_that_never_had_a_slot(compositor): host = make_host(compositor) assert host.reserve([None, None]) == [] assert host.allocator.in_use() == [] def test_release_tolerates_a_slot_this_allocator_never_held(compositor): """Teardown runs for sessions started before the broker restarted too.""" host = make_host(compositor) host.release(1003) # must not raise: the application is already dead host.release(None) assert host.allocator.in_use() == [] def test_a_plan_entry_that_is_not_hyprctl_is_a_bug_and_says_so(compositor): host = make_host(compositor) host.available() with pytest.raises(ValueError) as exc: host.dispatch(["sh", "-c", "rm -rf /"]) assert "not a hyprctl plan entry" in str(exc.value)