from __future__ import annotations import hashlib import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] VIEWER_ROOT = ROOT / "tools" / "board-viewer" sys.path.insert(0, str(VIEWER_ROOT)) from board_viewer.photo import PhotoOverlayError, prepare_photo_overlay, solve_affine_matrix # noqa: E402 CONTROL_POINTS = [ {"image": {"x": 0, "y": 0}, "board": {"x": 10, "y": -5}}, {"image": {"x": 100, "y": 0}, "board": {"x": 20, "y": -5}}, {"image": {"x": 0, "y": 50}, "board": {"x": 10, "y": 5}}, ] def test_affine_matrix_maps_image_pixels_to_view_coordinates() -> None: matrix = solve_affine_matrix( CONTROL_POINTS, board_bounds={"x": 0, "y": -10, "width": 30, "height": 20}, ) assert matrix == pytest.approx([0.1, 0.0, 0.0, 0.2, 10.0, 5.0]) def test_affine_matrix_rejects_collinear_control_points() -> None: points = [ {"image": {"x": 0, "y": 0}, "board": {"x": 0, "y": 0}}, {"image": {"x": 10, "y": 10}, "board": {"x": 10, "y": 10}}, {"image": {"x": 20, "y": 20}, "board": {"x": 20, "y": 20}}, ] with pytest.raises(PhotoOverlayError, match="non-collinear"): solve_affine_matrix(points, board_bounds={"x": 0, "y": 0, "width": 20, "height": 20}) def test_prepare_photo_overlay_checks_hash_copies_asset_and_emits_matrix(tmp_path: Path) -> None: photo = tmp_path / "reference" / "board-photo.png" photo.parent.mkdir(parents=True) photo.write_bytes(b"fixture-photo") digest = hashlib.sha256(b"fixture-photo").hexdigest() assets = tmp_path / "generated" assets.mkdir() prepared = prepare_photo_overlay( { "path": "reference/board-photo.png", "sha256": digest, "control_points": CONTROL_POINTS, }, repository_root=tmp_path, assets_dir=assets, board_bounds={"x": 0, "y": -10, "width": 30, "height": 20}, ) assert prepared is not None assert prepared.data == { "asset": "board-photo.png", "sha256": digest, "matrix": pytest.approx([0.1, 0.0, 0.0, 0.2, 10.0, 5.0]), "control_point_count": 3, } assert prepared.asset_path.read_bytes() == b"fixture-photo" assert prepared.source_path == photo def test_prepare_photo_overlay_rejects_hash_mismatch_and_path_escape(tmp_path: Path) -> None: photo = tmp_path / "photo.png" photo.write_bytes(b"fixture-photo") assets = tmp_path / "generated" assets.mkdir() with pytest.raises(PhotoOverlayError, match="SHA-256"): prepare_photo_overlay( {"path": "photo.png", "sha256": "0" * 64, "control_points": CONTROL_POINTS}, repository_root=tmp_path, assets_dir=assets, board_bounds={"x": 0, "y": -10, "width": 30, "height": 20}, ) with pytest.raises(PhotoOverlayError, match="inside repository"): prepare_photo_overlay( {"path": "../outside.png", "sha256": "0" * 64, "control_points": CONTROL_POINTS}, repository_root=tmp_path, assets_dir=assets, board_bounds={"x": 0, "y": -10, "width": 30, "height": 20}, ) def test_prepare_photo_overlay_accepts_missing_optional_configuration(tmp_path: Path) -> None: assert prepare_photo_overlay( None, repository_root=tmp_path, assets_dir=tmp_path, board_bounds={"x": 0, "y": 0, "width": 1, "height": 1}, ) is None