182 lines
8.8 KiB
Python
182 lines
8.8 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from app.services.engine_service import load_engine, validate_cdsl
|
|
from app.settings import get_settings
|
|
|
|
|
|
class ProfileSchemaTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
cls.settings = get_settings()
|
|
cls.engine = load_engine(cls.settings)
|
|
cls.schema = json.loads((cls.settings.engine_root / "profile_schema.json").read_text(encoding="utf-8"))
|
|
cls.cdsl_schema = json.loads(
|
|
(cls.settings.engine_root / cls.schema["cdsl_json_schema_file"]).read_text(encoding="utf-8")
|
|
)
|
|
|
|
def test_schema_and_registered_profiles_stay_in_sync(self) -> None:
|
|
self.assertEqual(set(self.schema["runtime_supported_profiles"]), set(self.engine.SHAPE_GENERATORS))
|
|
|
|
def test_legacy_profile_macros_require_explicit_adapter_lowering(self) -> None:
|
|
legacy = {
|
|
"schema": "cad.cdsl.llm.v1", "schema_version": "1.1.0", "kind": "part", "part_id": "legacy-rectangle",
|
|
"meta": {"unit": "mm"},
|
|
"geometry": {"sketches": [{
|
|
"id": "base", "workplane": {"origin_mm": [0, 0, 0], "x_dir": [1, 0, 0], "normal": [0, 0, 1]},
|
|
"profile": {"type": "rectangle", "center": [0, 0], "width_mm": 10, "height_mm": 10},
|
|
}]},
|
|
"features": [{"id": "base_add", "atomic_id": "extrude_add_blind", "depends_on": [], "sketch_id": "base", "params": {"distance_mm": 2}}],
|
|
}
|
|
with self.assertRaisesRegex(ValueError, "CDSL schema violation"):
|
|
validate_cdsl(legacy, self.engine)
|
|
from cdsl_importer.legacy_profile_adapter import lower_legacy_profiles
|
|
|
|
lowered = lower_legacy_profiles(legacy)
|
|
self.assertEqual(lowered["geometry"]["sketches"][0]["profile"]["type"], "analytic_contours")
|
|
validate_cdsl(lowered, self.engine)
|
|
|
|
def test_schema_and_executable_atomic_operations_stay_in_sync(self) -> None:
|
|
self.assertNotIn("runtime_supported_atomic_ids", self.schema)
|
|
self.assertNotIn("feature_atomic_ids", self.schema)
|
|
self.assertEqual(set(self.engine.SUPPORTED_ATOMIC_IDS), set(self.schema["operation_contracts"]))
|
|
self.assertEqual(
|
|
set(self.engine.SUPPORTED_ATOMIC_IDS),
|
|
set(self.engine.materialized_feature_contracts(self.schema)),
|
|
)
|
|
self.assertEqual(
|
|
set(self.cdsl_schema["$defs"]["feature_atomic_ids"]["enum"]),
|
|
set(self.engine.SUPPORTED_ATOMIC_IDS),
|
|
)
|
|
|
|
def test_machine_schema_and_human_contract_stay_in_sync(self) -> None:
|
|
self.assertEqual(
|
|
set(self.cdsl_schema["$defs"]["profile_type"]["enum"]),
|
|
set(self.schema["runtime_supported_profiles"]),
|
|
)
|
|
self.assertEqual(set(self.schema["profiles"]), set(self.schema["runtime_supported_profiles"]))
|
|
|
|
def test_rejects_unsupported_atomic_operation_before_rebuild(self) -> None:
|
|
cdsl = {
|
|
"schema": "cad.cdsl.llm.v1",
|
|
"part_id": "invalid-extrude",
|
|
"features": [{
|
|
"id": "f01", "atomic_id": "extrude", "depends_on": [],
|
|
"params": {"depth_mm": 10}, "sketch_id": "s01",
|
|
}],
|
|
"geometry": {"sketches": [{
|
|
"id": "s01", "workplane": {}, "profile": {"type": "circle", "radius_mm": 5},
|
|
}]},
|
|
}
|
|
with self.assertRaisesRegex(ValueError, "features\\[0\\]\\.atomic_id"):
|
|
validate_cdsl(cdsl, self.engine)
|
|
|
|
def test_semantic_validator_preserves_deferred_features_but_runtime_checks_current_capabilities(self) -> None:
|
|
cdsl = {
|
|
"schema": "cad.cdsl.llm.v1", "schema_version": "1.1.0", "part_id": "deferred-fillet",
|
|
"meta": {"unit": "mm"},
|
|
"features": [{
|
|
"id": "f01", "atomic_id": "fillet", "depends_on": [], "params": {"radius_mm": 1},
|
|
"execution_status": "deferred",
|
|
}],
|
|
"geometry": {"sketches": [{
|
|
"id": "s01", "workplane": {"origin_mm": [0, 0, 0], "x_dir": [1, 0, 0], "normal": [0, 0, 1]},
|
|
"profile": {"type": "circle", "radius_mm": 5},
|
|
}]},
|
|
}
|
|
result = self.engine.validate_semantic_cdsl(cdsl)
|
|
self.assertEqual(result["deferred_feature_ids"], ["f01"])
|
|
with self.assertRaisesRegex(ValueError, "missing_selector"):
|
|
validate_cdsl(cdsl, self.engine)
|
|
|
|
def test_rejects_bare_hole_coordinate_arrays_before_rebuild(self) -> None:
|
|
cdsl = {
|
|
"schema": "cad.cdsl.llm.v1",
|
|
"part_id": "invalid-hole-position",
|
|
"features": [{
|
|
"id": "f01", "atomic_id": "hole_blind", "depends_on": [], "sketch_id": "s01",
|
|
"params": {"diameter_mm": 6, "depth_mm": 10, "positions": [[0, 0]]},
|
|
}],
|
|
"geometry": {"sketches": [{
|
|
"id": "s01",
|
|
"workplane": {"origin_mm": [0, 0, 0], "x_dir": [1, 0, 0], "normal": [0, 0, 1]},
|
|
"profile": {"type": "circle", "radius_mm": 20},
|
|
}]},
|
|
}
|
|
with self.assertRaisesRegex(ValueError, "positions\\[0\\].*not of type 'object'"):
|
|
validate_cdsl(cdsl, self.engine)
|
|
|
|
def test_cdsl_only_rebuild_preserves_its_actual_failure(self) -> None:
|
|
cdsl = {
|
|
"schema": "cad.cdsl.llm.v1",
|
|
"part_id": "invalid-extrude",
|
|
"features": [{
|
|
"id": "f01", "atomic_id": "extrude", "depends_on": [],
|
|
"params": {"depth_mm": 10}, "sketch_id": "s01",
|
|
}],
|
|
"geometry": {"sketches": [{
|
|
"id": "s01", "workplane": {}, "profile": {"type": "circle", "radius_mm": 5},
|
|
}]},
|
|
}
|
|
with tempfile.TemporaryDirectory() as temporary_directory:
|
|
out_step = Path(temporary_directory) / "model.step"
|
|
with self.assertRaisesRegex(RuntimeError, "CDSL-only rebuild failed: unsupported atomic_id: extrude"):
|
|
self.engine.run_rebuild(cdsl, out_step)
|
|
|
|
def test_hole_uses_inverse_host_face_normal_for_a_concave_l_bracket(self) -> None:
|
|
"""A local top face can be below the global body centre on an L part.
|
|
|
|
The drill must still enter its host face rather than point out into
|
|
empty space, otherwise the runtime reports a successful no-op hole.
|
|
"""
|
|
cdsl = {
|
|
"schema": "cad.cdsl.llm.v1", "schema_version": "1.1.0", "kind": "part", "part_id": "l-bracket-hole",
|
|
"meta": {"unit": "mm"},
|
|
"geometry": {"sketches": [
|
|
{
|
|
"id": "base", "workplane": {"origin_mm": [0, 0, 0], "x_dir": [1, 0, 0], "normal": [0, 0, 1]},
|
|
"profile": {"type": "polygon", "vertices": [[-25, 0], [25, 0], [25, 80], [-25, 80]]},
|
|
},
|
|
{
|
|
"id": "upright", "workplane": {"origin_mm": [0, 8, 0], "x_dir": [1, 0, 0], "normal": [0, -1, 0]},
|
|
"profile": {"type": "polygon", "vertices": [[-25, 0], [25, 0], [25, 60], [-25, 60]]},
|
|
},
|
|
]},
|
|
"features": [
|
|
{"id": "base_add", "atomic_id": "extrude_add_blind", "depends_on": [], "params": {"distance_mm": 8}, "sketch_id": "base"},
|
|
{"id": "upright_add", "atomic_id": "extrude_add_blind", "depends_on": ["base_add"], "params": {"distance_mm": 8}, "sketch_id": "upright"},
|
|
{
|
|
"id": "top_holes", "atomic_id": "hole_blind", "depends_on": ["upright_add"],
|
|
"params": {
|
|
"diameter_mm": 8, "depth_mm": 10,
|
|
"positions": [{"mm": [-15, 50, 0]}, {"mm": [15, 50, 0]}],
|
|
"host_face": {"frame": {"origin_mm": [0, 0, 8], "x_dir": [1, 0, 0], "y_dir": [0, 1, 0], "normal": [0, 0, 1]}},
|
|
},
|
|
},
|
|
],
|
|
}
|
|
validate_cdsl(cdsl, self.engine)
|
|
with tempfile.TemporaryDirectory() as temporary_directory:
|
|
result = self.engine.run_cdsl_only(cdsl, Path(temporary_directory) / "l-bracket.step")
|
|
self.assertAlmostEqual(float(result["volume_mm3"]), 52800 - 2 * 3.141592653589793 * 4 * 4 * 8, places=4)
|
|
self.assertEqual(int(result["solid_count"]), 1)
|
|
|
|
def test_all_official_samples_match_the_engine_schema(self) -> None:
|
|
from cdsl_importer.legacy_profile_adapter import lower_legacy_profiles
|
|
|
|
samples = sorted(self.settings.library_root.glob("samples/**/model.cdsl.json"))
|
|
self.assertGreater(len(samples), 0)
|
|
for sample_path in samples:
|
|
with self.subTest(sample=sample_path.parent.name):
|
|
legacy_sample = json.loads(sample_path.read_text(encoding="utf-8"))
|
|
lowered = lower_legacy_profiles(legacy_sample)
|
|
validate_cdsl(lowered, self.engine)
|
|
self.assertTrue(self.engine.analyze_cdsl(lowered).runtime_eligible)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|