"""Container plans: the exact argv lists, and the validation that guards them. The argv lists are asserted whole rather than probed for substrings. These commands are the only thing this module produces, nobody here runs them, and a test that accepted "contains the output name" would pass on a command that silently changed shape under a refactor. """ from __future__ import annotations import pytest from hhstreamd.containers import ( ContainerPlan, app_id_for, contains_output, container_plan, output_name, quarantine_argv, ) def test_create_is_the_four_commands_in_order() -> None: plan = container_plan("foot-aaaaaa", 1000, 1280, 720, "hh-foot-aaaaaa") assert plan.create == [ ["hyprctl", "output", "create", "headless", "hh-foot-aaaaaa"], [ "hyprctl", "keyword", "workspace", "1000, monitor:hh-foot-aaaaaa, default:true, persistent:true", ], [ "hyprctl", "keyword", "windowrule", "match:initial_class ^hh-foot-aaaaaa$, workspace 1000, monitor hh-foot-aaaaaa", ], ["hyprctl", "keyword", "monitor", "hh-foot-aaaaaa,1280x720,auto,1"], ] def test_teardown_removes_the_output_and_nothing_else() -> None: plan = container_plan("foot-aaaaaa", 1000, 1280, 720, "hh-foot-aaaaaa") assert plan.teardown == [["hyprctl", "output", "remove", "hh-foot-aaaaaa"]] def test_plan_describes_what_it_built() -> None: plan = container_plan("x1", 1007, 1920, 1080, "org.gnome.Nautilus") assert isinstance(plan, ContainerPlan) assert (plan.output, plan.workspace, plan.mode) == ("hh-x1", 1007, (1920, 1080)) def test_a_foreign_class_is_matched_as_given() -> None: # The rule has to match what the toolkit reports, which is not always the # id the launcher stamped. plan = container_plan("x1", 1001, 800, 600, "org.gnome.Nautilus") assert plan.create[2] == [ "hyprctl", "keyword", "windowrule", "match:initial_class ^org.gnome.Nautilus$, workspace 1001, monitor hh-x1", ] def test_identical_arguments_give_byte_identical_commands() -> None: first = container_plan("foot-aaaaaa", 1000, 1280, 720, "hh-foot-aaaaaa") second = container_plan("foot-aaaaaa", 1000, 1280, 720, "hh-foot-aaaaaa") assert first == second assert first.create == second.create assert first.teardown == second.teardown def test_output_name_and_app_id_are_the_same_string() -> None: assert output_name("foot-aaaaaa") == "hh-foot-aaaaaa" assert app_id_for("foot-aaaaaa") == "hh-foot-aaaaaa" @pytest.mark.parametrize( "name", [ "hh-foot-aaaaaa", "hh-x", "hh-1000", ], ) def test_contains_output_claims_our_own_names(name: str) -> None: assert contains_output(name) is True @pytest.mark.parametrize( "name", [ "HEADLESS-1", # the compositor's own "eDP-1", "hh", "hh-", # no session id: not something the broker ever created "hh-Foot", # upper case is outside the minted alphabet "hh-foot\n", # a trailing newline is not a name, it is two lines "hh-foot aaaa", " hh-foot", "xhh-foot", "", ], ) def test_contains_output_refuses_everything_else(name: str) -> None: # A scan removes what this says is ours, so a false positive deletes an # output the broker did not create. assert contains_output(name) is False @pytest.mark.parametrize( "bad", ["", "Foot", "foot_aaaa", "foot aaaa", "hh-foot;rm", "foot\n", "../x", "é"], ) def test_a_session_id_outside_the_alphabet_is_refused(bad: str) -> None: with pytest.raises(ValueError) as exc: output_name(bad) assert repr(bad) in str(exc.value) assert "[a-z0-9-]" in str(exc.value) with pytest.raises(ValueError) as app_exc: app_id_for(bad) assert repr(bad) in str(app_exc.value) with pytest.raises(ValueError) as plan_exc: container_plan(bad, 1000, 1280, 720, "hh-x") assert repr(bad) in str(plan_exc.value) @pytest.mark.parametrize("width,height", [(0, 720), (1280, 0), (-1, 720), (1280, -720)]) def test_a_non_positive_mode_is_refused(width: int, height: int) -> None: with pytest.raises(ValueError) as exc: container_plan("x1", 1000, width, height, "hh-x1") message = str(exc.value) offender = width if width < 1 else height assert repr(offender) in message assert ("width" in message) or ("height" in message) assert ">= 1" in message def test_a_fractional_mode_is_refused() -> None: # 1280.5 would reach the compositor as "1280.5x720"; a mode is pixels. with pytest.raises(ValueError) as exc: container_plan("x1", 1000, 1280.5, 720, "hh-x1") # type: ignore[arg-type] assert "width" in str(exc.value) assert "is not an int" in str(exc.value) def test_a_boolean_workspace_is_refused() -> None: # True is an int in Python and would quietly become workspace 1. with pytest.raises(ValueError) as exc: container_plan("x1", True, 1280, 720, "hh-x1") # type: ignore[arg-type] assert "workspace" in str(exc.value) assert "is not an int" in str(exc.value) @pytest.mark.parametrize("workspace", [0, -1]) def test_a_non_positive_workspace_is_refused(workspace: int) -> None: with pytest.raises(ValueError) as exc: container_plan("x1", workspace, 1280, 720, "hh-x1") assert "workspace" in str(exc.value) assert repr(workspace) in str(exc.value) def test_an_empty_app_id_is_refused() -> None: with pytest.raises(ValueError) as exc: container_plan("x1", 1000, 1280, 720, "") assert "app id" in str(exc.value) assert "''" in str(exc.value) @pytest.mark.parametrize( "bad", [ "hh-x1, workspace 1001", # a forged second rule field "hh-x1$, monitor eDP-1", "hh x1", "hh-x1\nkeyword monitor eDP-1,disable", "^hh-x1$", "hh-x1'", ], ) def test_an_app_id_that_could_forge_a_rule_field_is_refused(bad: str) -> None: with pytest.raises(ValueError) as exc: container_plan("x1", 1000, 1280, 720, bad) assert repr(bad) in str(exc.value) assert "[A-Za-z0-9._+-]" in str(exc.value) def test_quarantine_argv_is_the_catch_all_keyword() -> None: argv = quarantine_argv("special:quarantine") # Bare argv lists, not a ContainerPlan: a quarantine rule has no output, # no numbered workspace and no mode to put in one (CONTRACT v2 §3.2). assert argv == [ [ "hyprctl", "keyword", "windowrule", "match:initial_class .*, workspace special:quarantine", ] ] def test_the_quarantine_rule_matches_what_a_container_rule_does_not() -> None: """The backstop and a container rule must not be able to collide. The catch-all is what every unplaced window hits, so it has to be the same shape as a per-session rule with a wider match — otherwise the two are evaluated differently and the ordering the caller gets right stops mattering. """ (backstop,) = quarantine_argv("special:quarantine") container = container_plan("x1", 1000, 1280, 720, "hh-x1").create[2] assert backstop[:3] == container[:3] == ["hyprctl", "keyword", "windowrule"] assert backstop[3].startswith("match:initial_class .*,") assert container[3].startswith("match:initial_class ^hh-x1$,") @pytest.mark.parametrize("disabled", ["", " ", "\n"]) def test_quarantine_argv_is_none_when_disabled(disabled: str) -> None: assert quarantine_argv(disabled) is None @pytest.mark.parametrize("bad", ["special:quarantine, monitor eDP-1", "a b", "q\n1", "q'"]) def test_a_quarantine_workspace_that_could_forge_a_field_is_refused(bad: str) -> None: with pytest.raises(ValueError) as exc: quarantine_argv(bad) assert "quarantine_workspace" in str(exc.value) assert repr(bad.strip()) in str(exc.value)