"""fusion_import.manifest(manifest.json のスキーマとローダ)の仕様ベーステスト.
仕様出典(Fusion 取り込みの確定事項):
- manifest.json: design, version, units="mm", bodies[{name, occurrence_path, top_occurrence, stl,
aabb_mm(ワールド mm), transform(16 要素・行優先・平行移動 mm のオカレンスのワールド変換)}]
- transform は任意(省略時は単位行列=後方互換),16 要素かつ最下行 (0,0,0,1) を検証
- transform_matrix() で 4x4
"""
from __future__ import annotations
import json
import numpy as np
import pytest
from pydantic import ValidationError
from sm_tias_lightsim.fusion_import.manifest import FusionManifest, ManifestBody, load_manifest
def _body(**kw) -> dict:
data = {
"name": "LED基板カバー",
"occurrence_path": "LEDユニット:1",
"top_occurrence": "LEDユニット:1",
"stl": "stl/000.stl",
"aabb_mm": [[0.0, 0.0, 0.0], [1.0, 2.0, 3.0]],
}
data.update(kw)
return data
def _manifest(**kw) -> dict:
data = {"design": "SmTIASv3", "version": 7, "units": "mm", "bodies": [_body()]}
data.update(kw)
return data
def test_transform_omitted_means_identity():
body = ManifestBody.model_validate(_body())
assert body.transform is None
np.testing.assert_array_equal(body.transform_matrix(), np.eye(4))
def test_transform_is_row_major_with_translation_in_last_column():
rot_tr = [0.0, -1.0, 0.0, 65.0,
1.0, 0.0, 0.0, -10.0,
0.0, 0.0, 1.0, -66.0,
0.0, 0.0, 0.0, 1.0]
m = ManifestBody.model_validate(_body(transform=rot_tr)).transform_matrix()
assert m.shape == (4, 4)
np.testing.assert_array_equal(m[:3, 3], [65.0, -10.0, -66.0])
# 局所 X → 世界 Y(行優先なら m[1, 0] = 1)
np.testing.assert_allclose(m @ [1.0, 0.0, 0.0, 1.0], [65.0, -9.0, -66.0, 1.0])
@pytest.mark.parametrize("n", [0, 9, 12, 15, 17])
def test_transform_with_wrong_length_is_rejected(n):
with pytest.raises(ValidationError):
ManifestBody.model_validate(_body(transform=[0.0] * n))
def test_transform_with_non_homogeneous_bottom_row_is_rejected():
t = list(np.eye(4).flatten())
t[12] = 1.0
with pytest.raises(ValidationError):
ManifestBody.model_validate(_body(transform=t))
t = list(np.eye(4).flatten())
t[15] = 2.0
with pytest.raises(ValidationError):
ManifestBody.model_validate(_body(transform=t))
def test_root_body_defaults_to_empty_occurrence():
body = ManifestBody.model_validate(
{"name": "前側", "stl": "stl/a.stl", "aabb_mm": [[0, 0, 0], [1, 1, 1]]})
assert body.top_occurrence == ""
assert body.occurrence_path == ""
@pytest.mark.parametrize("units", ["cm", "m", "inch"])
def test_units_other_than_mm_are_rejected(units):
with pytest.raises(ValidationError):
FusionManifest.model_validate(_manifest(units=units))
def test_empty_bodies_rejected():
with pytest.raises(ValidationError):
FusionManifest.model_validate(_manifest(bodies=[]))
def test_missing_required_body_fields_rejected():
for key in ("name", "stl", "aabb_mm"):
b = _body()
del b[key]
with pytest.raises(ValidationError):
FusionManifest.model_validate(_manifest(bodies=[b]))
def test_version_is_optional():
m = _manifest()
del m["version"]
assert FusionManifest.model_validate(m).version is None
def test_load_manifest_reads_utf8_json(tmp_path):
path = tmp_path / "manifest.json"
path.write_text(json.dumps(_manifest(), ensure_ascii=False), encoding="utf-8")
m = load_manifest(path)
assert m.design == "SmTIASv3"
assert m.bodies[0].name == "LED基板カバー"
assert m.bodies[0].aabb_mm == ((0.0, 0.0, 0.0), (1.0, 2.0, 3.0))
def test_load_manifest_missing_file_raises(tmp_path):
with pytest.raises(FileNotFoundError):
load_manifest(tmp_path / "nope.json")
def test_load_manifest_invalid_schema_raises(tmp_path):
path = tmp_path / "manifest.json"
path.write_text(json.dumps(_manifest(units="cm")), encoding="utf-8")
with pytest.raises(ValidationError):
load_manifest(path)