from __future__ import annotations import unittest from app.simulation.components.experimental.library import LIBRARY from app.simulation.core.base import AlgebraicComponent from app.simulation.core.catalog import ComponentDisplaySpec, PortDisplaySpec from app.simulation.core.medium import IdealGasMedium from app.simulation.core.ports import PortDefinition from app.simulation.registry import ( COMPONENT_LIBRARY_REGISTRY, COMPONENT_MODEL_REGISTRY, discover_component_registries, validate_component_model_class, ) class ComponentRegistryTests(unittest.TestCase): def test_manifest_is_the_only_public_model_source(self) -> None: libraries, models = discover_component_registries() self.assertEqual(libraries, COMPONENT_LIBRARY_REGISTRY) self.assertEqual(models, COMPONENT_MODEL_REGISTRY) self.assertEqual( tuple(models), ("cylinder", "tank", "pipe", "orifice", "tee"), ) self.assertNotIn( "app.simulation.components.experimental.flow.pipe:Pipe", LIBRARY.models, ) def test_every_public_model_owns_its_catalog_contract(self) -> None: for model_type, spec in COMPONENT_MODEL_REGISTRY.items(): with self.subTest(model_type=model_type): declarations = spec.component_class.__dict__ self.assertIn("MODEL_VERSION", declarations) self.assertIn("DISPLAY", declarations) self.assertIn("create", declarations) self.assertEqual(spec.display.library_id, LIBRARY.id) self.assertIn( spec.display.category_id, LIBRARY.category_by_id, ) def test_display_ports_must_match_physical_ports(self) -> None: class BrokenDisplayComponent(AlgebraicComponent): MODEL_TYPE = "broken_display" MODEL_VERSION = "1.0.0" PORTS = ( PortDefinition.pneumatic( "port_a", nominal_role="bidirectional", ), ) PARAMETERS = () RESULT_VARIABLES = () DISPLAY = ComponentDisplaySpec( label="错误显示模型", library_id="experimental", category_id="flow", symbol="generic", ports=(PortDisplaySpec("port_b", "right"),), ) @classmethod def create( cls, *, name: str, medium: IdealGasMedium, parameters, ): return cls(name) with self.assertRaisesRegex( ValueError, "display ports must exactly match", ): validate_component_model_class( BrokenDisplayComponent, library=LIBRARY, ) if __name__ == "__main__": unittest.main()