"""Aiming arithmetic (CONTRACT-sessions.md §3.3). An agent says "click 40 pixels into this window"; the virtual pointer wants a fraction of its output. Everything between is this one function, and the case that breaks in the field is the last one: a window on an output that is not at the layout origin. """ from __future__ import annotations import pytest from hhstreamd.pointer import window_to_absolute #: A 1280x720 output at the origin, and a 640x480 window centred in it. ORIGIN_MONITOR = (0, 0, 1280, 720) CENTRED_WINDOW = (320, 120, 640, 480) def test_window_centre_is_the_window_centre_not_the_monitor_centre(): # (320 + 320) / 1280 = 0.5 and (120 + 240) / 720 = 0.5 — the window happens # to be centred, so this also pins down the easy reading of the maths. assert window_to_absolute((320, 240), CENTRED_WINDOW, ORIGIN_MONITOR) == (0.5, 0.5) def test_an_off_centre_window_centre_is_not_the_monitor_centre(): window = (0, 0, 640, 480) assert window_to_absolute((320, 240), window, ORIGIN_MONITOR) == (0.25, 1 / 3) @pytest.mark.parametrize( "corner, expected", [ ((0, 0), (320 / 1280, 120 / 720)), # top-left ((640, 0), (960 / 1280, 120 / 720)), # top-right ((0, 480), (320 / 1280, 600 / 720)), # bottom-left ((640, 480), (960 / 1280, 600 / 720)), # bottom-right ], ) def test_all_four_window_corners(corner, expected): assert window_to_absolute(corner, CENTRED_WINDOW, ORIGIN_MONITOR) == expected def test_the_far_edge_is_inside(): """``(w, h)`` is the bottom-right corner; rejecting it makes it unaimable.""" assert window_to_absolute((640, 480), CENTRED_WINDOW, ORIGIN_MONITOR) == (0.75, 600 / 720) @pytest.mark.parametrize("point", [(-1, 0), (0, -1), (641, 0), (0, 481), (641, 481)]) def test_a_point_outside_the_window_raises(point): with pytest.raises(ValueError, match="outside window"): window_to_absolute(point, CENTRED_WINDOW, ORIGIN_MONITOR) def test_a_window_on_the_second_monitor_uses_that_monitors_frame(): """The offset case. Two 1280x720 outputs side by side; the window is on the right-hand one, so its global x starts at 1280. Without subtracting the monitor origin every coordinate would exceed 1.0, clamp, and quietly aim at the far right edge — a click that happens, in the wrong place. """ monitor = (1280, 0, 1280, 720) window = (1280 + 320, 120, 640, 480) assert window_to_absolute((320, 240), window, monitor) == (0.5, 0.5) assert window_to_absolute((0, 0), window, monitor) == (320 / 1280, 120 / 720) assert window_to_absolute((640, 480), window, monitor) == (960 / 1280, 600 / 720) def test_a_vertically_stacked_monitor_uses_its_own_origin(): monitor = (0, 720, 1280, 720) window = (0, 720, 1280, 720) assert window_to_absolute((640, 360), window, monitor) == (0.5, 0.5) assert window_to_absolute((0, 0), window, monitor) == (0.0, 0.0) def test_a_window_hanging_off_the_monitor_clamps_rather_than_overflowing(): """Hyprland will place a window partly off-screen; 1.0 is the honest answer for "as far as this output goes".""" window = (1000, 600, 640, 480) assert window_to_absolute((640, 480), window, ORIGIN_MONITOR) == (1.0, 1.0) # Negative global coordinates clamp at the other end. assert window_to_absolute((0, 0), (-100, -50, 640, 480), ORIGIN_MONITOR) == (0.0, 0.0) def test_a_degenerate_window_or_monitor_raises(): with pytest.raises(ValueError, match="window has no area"): window_to_absolute((0, 0), (0, 0, 0, 480), ORIGIN_MONITOR) with pytest.raises(ValueError, match="monitor has no area"): window_to_absolute((0, 0), CENTRED_WINDOW, (0, 0, 1280, 0))