Spaces:
Sleeping

File size: 3,069 Bytes
39e01c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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()