import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "tools" / "voxel-model-eval")) import core def test_extract_json_plain(): assert core.extract_json('{"name":"a","boxes":[]}') == {"name": "a", "boxes": []} def test_extract_json_with_trailing_prose(): assert core.extract_json('{"name":"a","boxes":[]} done') == {"name": "a", "boxes": []} def test_extract_json_malformed_returns_none(): assert core.extract_json("not json at all") is None def test_voxelize_centering_and_count(): # a 2x1x1 box centered at origin -> two cells straddling x=0 cells = core.voxelize([{"x": 0, "y": 0, "z": 0, "w": 2, "h": 1, "d": 1, "color": "#fff"}]) assert set(cells.keys()) == {(0, 0, 0), (1, 0, 0)} def test_voxelize_dedupe_last_writer_wins(): boxes = [ {"x": 0, "y": 0, "z": 0, "w": 1, "h": 1, "d": 1, "color": "#111"}, {"x": 0, "y": 0, "z": 0, "w": 1, "h": 1, "d": 1, "color": "#222"}, ] cells = core.voxelize(boxes) assert len(cells) == 1 and cells[(0, 0, 0)] == "#222" def test_metrics_grounded_and_symmetry(): # symmetric pair on the ground obj = {"boxes": [ {"x": -1, "y": 0, "z": 0, "w": 1, "h": 1, "d": 1, "color": "#fff"}, {"x": 1, "y": 0, "z": 0, "w": 1, "h": 1, "d": 1, "color": "#fff"}, ]} m = core.metrics(obj) assert m["grounded"] is True assert m["symmetry"] == 1.0 assert m["in_bounds_pct"] == 100 def test_metrics_empty(): m = core.metrics({"boxes": []}) assert m["boxes"] == 0 and m["grounded"] is False