test_photo_overlay.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from __future__ import annotations
  2. import hashlib
  3. import sys
  4. from pathlib import Path
  5. import pytest
  6. ROOT = Path(__file__).resolve().parents[2]
  7. VIEWER_ROOT = ROOT / "tools" / "board-viewer"
  8. sys.path.insert(0, str(VIEWER_ROOT))
  9. from board_viewer.photo import PhotoOverlayError, prepare_photo_overlay, solve_affine_matrix # noqa: E402
  10. CONTROL_POINTS = [
  11. {"image": {"x": 0, "y": 0}, "board": {"x": 10, "y": -5}},
  12. {"image": {"x": 100, "y": 0}, "board": {"x": 20, "y": -5}},
  13. {"image": {"x": 0, "y": 50}, "board": {"x": 10, "y": 5}},
  14. ]
  15. def test_affine_matrix_maps_image_pixels_to_view_coordinates() -> None:
  16. matrix = solve_affine_matrix(
  17. CONTROL_POINTS,
  18. board_bounds={"x": 0, "y": -10, "width": 30, "height": 20},
  19. )
  20. assert matrix == pytest.approx([0.1, 0.0, 0.0, 0.2, 10.0, 5.0])
  21. def test_affine_matrix_rejects_collinear_control_points() -> None:
  22. points = [
  23. {"image": {"x": 0, "y": 0}, "board": {"x": 0, "y": 0}},
  24. {"image": {"x": 10, "y": 10}, "board": {"x": 10, "y": 10}},
  25. {"image": {"x": 20, "y": 20}, "board": {"x": 20, "y": 20}},
  26. ]
  27. with pytest.raises(PhotoOverlayError, match="non-collinear"):
  28. solve_affine_matrix(points, board_bounds={"x": 0, "y": 0, "width": 20, "height": 20})
  29. def test_prepare_photo_overlay_checks_hash_copies_asset_and_emits_matrix(tmp_path: Path) -> None:
  30. photo = tmp_path / "reference" / "board-photo.png"
  31. photo.parent.mkdir(parents=True)
  32. photo.write_bytes(b"fixture-photo")
  33. digest = hashlib.sha256(b"fixture-photo").hexdigest()
  34. assets = tmp_path / "generated"
  35. assets.mkdir()
  36. prepared = prepare_photo_overlay(
  37. {
  38. "path": "reference/board-photo.png",
  39. "sha256": digest,
  40. "control_points": CONTROL_POINTS,
  41. },
  42. repository_root=tmp_path,
  43. assets_dir=assets,
  44. board_bounds={"x": 0, "y": -10, "width": 30, "height": 20},
  45. )
  46. assert prepared is not None
  47. assert prepared.data == {
  48. "asset": "board-photo.png",
  49. "sha256": digest,
  50. "matrix": pytest.approx([0.1, 0.0, 0.0, 0.2, 10.0, 5.0]),
  51. "control_point_count": 3,
  52. }
  53. assert prepared.asset_path.read_bytes() == b"fixture-photo"
  54. assert prepared.source_path == photo
  55. def test_prepare_photo_overlay_rejects_hash_mismatch_and_path_escape(tmp_path: Path) -> None:
  56. photo = tmp_path / "photo.png"
  57. photo.write_bytes(b"fixture-photo")
  58. assets = tmp_path / "generated"
  59. assets.mkdir()
  60. with pytest.raises(PhotoOverlayError, match="SHA-256"):
  61. prepare_photo_overlay(
  62. {"path": "photo.png", "sha256": "0" * 64, "control_points": CONTROL_POINTS},
  63. repository_root=tmp_path,
  64. assets_dir=assets,
  65. board_bounds={"x": 0, "y": -10, "width": 30, "height": 20},
  66. )
  67. with pytest.raises(PhotoOverlayError, match="inside repository"):
  68. prepare_photo_overlay(
  69. {"path": "../outside.png", "sha256": "0" * 64, "control_points": CONTROL_POINTS},
  70. repository_root=tmp_path,
  71. assets_dir=assets,
  72. board_bounds={"x": 0, "y": -10, "width": 30, "height": 20},
  73. )
  74. def test_prepare_photo_overlay_accepts_missing_optional_configuration(tmp_path: Path) -> None:
  75. assert prepare_photo_overlay(
  76. None,
  77. repository_root=tmp_path,
  78. assets_dir=tmp_path,
  79. board_bounds={"x": 0, "y": 0, "width": 1, "height": 1},
  80. ) is None