"""The driver's XKB flags โ€” the only path a layout has to the uploaded keymap. This file exists where a `tests/sessions/unit/test_settings.py` case would have gone, and that is the point. The broker's TOML is read by `hhstreamd` for its lease numbers; `hhstream-pointer` is a different unit and never opens that file. So a `[keyboard]` table there would be a setting an operator could change with no effect at all, and a test asserting its default would be green while every injected key stayed on `us`. The layout therefore arrives as `--xkb-layout`, and these are the assertions about that flag: what it defaults to, that a bad value is exit **2**, and that a bad value is exit 2 **without the compositor being touched** โ€” because a layout xkbcommon will not compile is the operator's typo, and reporting it as exit 10 sends them to the wrong unit. """ from __future__ import annotations import pytest from hhstream_pointer import __main__ as entry class _Unreached(Exception): """Raised if the Wayland side is reached when it should not have been.""" class _RefusingSink: """A sink that fails the test if anything asks it to talk to a compositor. The review asked for an injected connect that can be asserted *not* to have been called; this is that, as a type rather than a flag, so a future code path that connects earlier fails loudly instead of silently reordering start-up. """ instances = 0 def __init__(self, runtime_dir, display_name=None) -> None: type(self).instances += 1 def connect(self) -> None: raise _Unreached("connect") def upload_keymap(self, text: str) -> None: raise _Unreached("upload_keymap") def close(self) -> None: pass @pytest.fixture def no_compositor(monkeypatch): _RefusingSink.instances = 0 monkeypatch.setattr(entry, "WaylandSink", _RefusingSink) return _RefusingSink # -- defaults --------------------------------------------------------------- def test_the_xkb_flags_default_to_the_frozen_values(): args = entry.build_parser().parse_args([]) assert args.xkb_layout == "us" assert args.xkb_model == "pc105" assert (args.xkb_variant, args.xkb_options) == ("", "") def test_the_defaults_are_the_ones_the_module_option_defaults_to(): # Stated as constants so the NixOS option and the hand-run binary cannot # drift into typing different characters. assert (entry.DEFAULT_LAYOUT, entry.DEFAULT_MODEL) == ("us", "pc105") def test_a_layout_is_taken_from_the_flag(): assert entry.build_parser().parse_args(["--xkb-layout", "de"]).xkb_layout == "de" def test_the_contract_command_line_still_parses(): """CONTRACT-sessions.md ยง3.4's exact line, plus the keymap flags.""" args = entry.build_parser().parse_args( [ "--socket", "/run/hhstream/pointer.sock", "--compositor-runtime-dir", "/run/hypr-stream", "--xkb-layout", "us", "--xkb-model", "pc105", "--xkb-variant", "", "--xkb-options", "", ] ) assert str(args.socket) == "/run/hhstream/pointer.sock" assert str(args.compositor_runtime_dir) == "/run/hypr-stream" assert args.xkb_layout == "us" # -- refusals, before any Wayland call -------------------------------------- def test_a_comma_separated_layout_exits_2_without_connecting(no_compositor, caplog): """v1 sends group 0 in every `modifiers`, so a second group is unreachable. xkbcommon accepts `us,ru` and hands back a two-group keymap, so nothing downstream would notice: a caller asking for Cyrillic would get Latin and no error. Refused here, and the NixOS option's type refuses it too โ€” this one is for the binary run by hand, which that type cannot reach. """ code = entry.main(["--xkb-layout", "us,ru"]) assert code == entry.EXIT_USAGE assert no_compositor.instances == 0, "the compositor must not be touched" def test_a_layout_xkbcommon_rejects_exits_2_without_connecting(no_compositor): code = entry.main(["--xkb-layout", "zzz-no-such-layout"]) assert code == entry.EXIT_USAGE assert no_compositor.instances == 0 def test_a_usable_layout_gets_as_far_as_the_compositor(no_compositor): """The other half: exit 2 must not be how a *good* layout fails. A test that only asserted the refusals would pass against a `main` that returned 2 for everything, so this one proves the good path reaches the Wayland side and fails there instead. """ with pytest.raises(_Unreached, match="connect"): entry.main(["--xkb-layout", "us"]) assert no_compositor.instances == 1 def test_the_exit_codes_match_the_brokers_table(): # Mirrored, not imported: the driver is a separate package on purpose # (see the module docstring in __main__). assert (entry.EXIT_OK, entry.EXIT_USAGE, entry.EXIT_COMPOSITOR) == (0, 2, 10)