"""Host and Blender stroke configuration must stay in sync.""" from __future__ import annotations import ast import json from pathlib import Path import pytest import yaml from orisys_sensor_design import stroke_pattern from orisys_sensor_design.manifest import repo_root SCRIPT = repo_root() / "blender" / "scripts" / "camera_grid_stroke.py" def _constant(name: str): tree = ast.parse(SCRIPT.read_text(encoding="utf-8")) for node in tree.body: if isinstance(node, ast.Assign) and any( isinstance(target, ast.Name) and target.id == name for target in node.targets ): return ast.literal_eval(node.value) raise AssertionError(name) def test_generator_contract(): assert _constant("DEFAULT_PARAMS") == stroke_pattern.STROKE_DEFAULTS assert _constant("MODE") == stroke_pattern.STROKE_MODE @pytest.mark.parametrize("sensor", ["finger", "finger-xynova"]) def test_revision_settings(sensor): path = repo_root() / "sensors" / sensor / "r01" / "design.yaml" block = yaml.safe_load(path.read_text(encoding="utf-8"))["stroke_pattern"] assert stroke_pattern.resolve_stroke_settings(block) == stroke_pattern.STROKE_DEFAULTS @pytest.mark.parametrize("block", [ {"width_min_mm": 0.2}, {"width_ratio": 0}, {"width_ratio": 0.9}, {"length_ratio": 1}, {"seed": 1.5}, {"proposals": 0}, {"orientation_mode": "unknown"}, ]) def test_invalid_settings(block): assert stroke_pattern.validate_stroke_block(block) with pytest.raises(stroke_pattern.StrokePatternConfigError): stroke_pattern.resolve_stroke_settings(block) def test_params_round_trip(tmp_path: Path): settings = stroke_pattern.resolve_stroke_settings({"seed": 13}) path = stroke_pattern.write_stroke_params(settings, tmp_path) assert json.loads(path.read_text(encoding="utf-8")) == settings