import os
import unittest
from unittest.mock import AsyncMock, patch
import server
VALID_XML = """
"""
BROKEN_STRUCTURAL_XML = VALID_XML.replace(
"",
"""
""",
)
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()