Spaces:
Sleeping

XCos_MCP / test_remote_validation.py
xcos-bot
Deploy snapshot from 23aafd1a (verified archive)
39e01c4
Raw History Blame Contribute Delete
3.07 kB
import os
import unittest
from unittest.mock import AsyncMock, patch
import server
VALID_XML = """<?xml version="1.0" ?>
<XcosDiagram>
<mxGraphModel as="model">
<root>
<mxCell id="0:1:0"/>
<mxCell id="0:2:0" parent="0:1:0"/>
<BasicBlock id="B1" interfaceFunctionName="CONST_f">
<ExplicitOutputPort id="P1" dataLines="1" dataColumns="1" ordering="1"/>
</BasicBlock>
</root>
</mxGraphModel>
</XcosDiagram>
"""
BROKEN_STRUCTURAL_XML = VALID_XML.replace(
"</root>",
"""
<BasicLink id="L1" parent="0:2:0">
<SourcePort reference="P1"/>
<DestinationPort reference="MISSING_PORT"/>
</BasicLink>
</root>
""",
)
class RemoteValidationTests(unittest.IsolatedAsyncioTestCase):
def test_detect_validation_mode_prefers_remote_url(self):
with patch.dict(
os.environ,
{
"XCOS_VALIDATION_MODE": "",
"XCOS_SCILAB_SERVICE_URL": "https://example.invalid",
"SCILAB_BIN": "",
},
clear=False,
):
self.assertEqual(server.detect_validation_mode(), "remote")
async def test_run_verification_uses_remote_validator_when_enabled(self):
remote_result = {
"success": True,
"origin": "scilab-remote-service",
"warnings": ["remote warning"],
"task_id": "remote-job-1",
"auto_fixed_mux_to_scalar": False,
}
with patch.object(server, "detect_validation_mode", return_value="remote"):
with patch.object(
server,
"run_remote_scilab_validation",
AsyncMock(return_value=remote_result),
) as remote_mock:
result = await server.run_verification(VALID_XML)
remote_mock.assert_awaited_once()
self.assertTrue(result["success"])
self.assertIn("scilab-remote-service", result["origin"])
self.assertTrue(result["structural_check"]["success"])
self.assertIn("remote warning", result.get("warnings") or [])
async def test_run_verification_remote_short_circuits_on_structural_error(self):
with patch.object(server, "detect_validation_mode", return_value="remote"):
with patch.object(server, "run_remote_scilab_validation", AsyncMock()) as remote_mock:
result = await server.run_verification(BROKEN_STRUCTURAL_XML)
remote_mock.assert_not_awaited()
self.assertFalse(result["success"])
self.assertEqual(result["origin"], "structural-validator")
self.assertGreater(len(result.get("errors") or []), 0)
def test_infer_validation_code_maps_remote_origin(self):
code = server.infer_validation_code(
{
"success": False,
"origin": "hybrid (structural-python + scilab-remote-service)",
}
)
self.assertEqual(code, "SCILAB_REMOTE_FAILED")
if __name__ == "__main__":
unittest.main()