123 lines
5.8 KiB
Python
123 lines
5.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_schema_and_executable_atomic_operations_stay_in_sync(self) -> None:
|
|
self.assertEqual(set(self.schema["runtime_supported_atomic_ids"]), set(self.engine.SUPPORTED_ATOMIC_IDS))
|
|
self.assertTrue(set(self.engine.SUPPORTED_ATOMIC_IDS).issubset(self.schema["feature_atomic_ids"]))
|
|
self.assertEqual(
|
|
set(self.cdsl_schema["$defs"]["feature_atomic_ids"]["enum"]),
|
|
set(self.schema["feature_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["profiles"]) - {"complex_arc_shape", "unknown_shape"},
|
|
)
|
|
self.assertEqual(set(self.cdsl_schema["$defs"]["motif_type"]["enum"]), set(self.schema["motif_types"]))
|
|
self.assertEqual(set(self.cdsl_schema["$defs"]["layout_type"]["enum"]), set(self.schema["layout_types"]))
|
|
|
|
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_product_revision_path_uses_the_cdsl_only_entry_point(self) -> None:
|
|
source = (Path(__file__).resolve().parents[2] / "backend" / "app" / "services" / "engine_service.py").read_text(encoding="utf-8")
|
|
build_revision_source = source[source.index("def build_revision"):]
|
|
self.assertIn("engine.run_cdsl_only(cdsl_copy, step_path)", build_revision_source)
|
|
self.assertNotIn("engine.run_rebuild(cdsl_copy, step_path)", build_revision_source)
|
|
|
|
def test_all_official_samples_match_the_engine_schema(self) -> None:
|
|
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):
|
|
validate_cdsl(json.loads(sample_path.read_text(encoding="utf-8")), self.engine)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|