|
| 1 | +""" |
| 2 | +MIT License |
| 3 | +Copyright (c) 2025 Disguise Technologies ltd |
| 4 | +""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from unittest.mock import AsyncMock, patch |
| 8 | + |
| 9 | +from designer_plugin.d3sdk.function import d3function |
| 10 | +from designer_plugin.d3sdk.session import D3AsyncSession, D3Session |
| 11 | +from designer_plugin.models import PluginPayload, PluginResponse, PluginStatus |
| 12 | + |
| 13 | + |
| 14 | +# Register a module so D3Function._available_d3functions knows about it. |
| 15 | +@d3function("lazy_test_module") |
| 16 | +def _lazy_test_fn() -> str: |
| 17 | + return "hello world" |
| 18 | + |
| 19 | + |
| 20 | +def _make_response() -> PluginResponse: |
| 21 | + return PluginResponse( |
| 22 | + status=PluginStatus(code=0, message="OK", details=[]), |
| 23 | + returnValue=None, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def _module_payload() -> PluginPayload: |
| 28 | + """Payload that references a registered @d3function module.""" |
| 29 | + return PluginPayload(moduleName="lazy_test_module", script="return _lazy_test_fn()") |
| 30 | + |
| 31 | + |
| 32 | +def _script_payload() -> PluginPayload: |
| 33 | + """Payload with no module (equivalent to @d3pythonscript).""" |
| 34 | + return PluginPayload(moduleName=None, script="return 42") |
| 35 | + |
| 36 | + |
| 37 | +class TestD3SessionLazyRegistration: |
| 38 | + """Lazy registration behaviour for the synchronous D3Session.""" |
| 39 | + |
| 40 | + def test_registered_modules_starts_empty(self): |
| 41 | + session = D3Session("localhost", 80) |
| 42 | + assert session.registered_modules == set() |
| 43 | + |
| 44 | + def test_module_registered_on_first_execute(self): |
| 45 | + session = D3Session("localhost", 80) |
| 46 | + with ( |
| 47 | + patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, |
| 48 | + patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), |
| 49 | + ): |
| 50 | + session.execute(_module_payload()) |
| 51 | + mock_reg.assert_called_once() |
| 52 | + assert "lazy_test_module" in session.registered_modules |
| 53 | + |
| 54 | + def test_module_not_re_registered_on_second_execute(self): |
| 55 | + session = D3Session("localhost", 80) |
| 56 | + with ( |
| 57 | + patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, |
| 58 | + patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), |
| 59 | + ): |
| 60 | + session.execute(_module_payload()) |
| 61 | + session.execute(_module_payload()) |
| 62 | + mock_reg.assert_called_once() |
| 63 | + |
| 64 | + def test_no_registration_for_script_payload(self): |
| 65 | + """Payloads without a moduleName must never trigger registration.""" |
| 66 | + session = D3Session("localhost", 80) |
| 67 | + with ( |
| 68 | + patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, |
| 69 | + patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), |
| 70 | + ): |
| 71 | + session.execute(_script_payload()) |
| 72 | + mock_reg.assert_not_called() |
| 73 | + |
| 74 | + def test_context_module_not_re_registered_lazily(self): |
| 75 | + """A module pre-registered via context_modules must not be registered again in execute().""" |
| 76 | + with ( |
| 77 | + patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, |
| 78 | + patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), |
| 79 | + ): |
| 80 | + with D3Session("localhost", 80, {"lazy_test_module"}) as session: |
| 81 | + assert "lazy_test_module" in session.registered_modules |
| 82 | + session.execute(_module_payload()) |
| 83 | + mock_reg.assert_called_once() # only from __enter__, not from execute() |
| 84 | + |
| 85 | + def test_registered_modules_updated_after_execute(self): |
| 86 | + session = D3Session("localhost", 80) |
| 87 | + assert "lazy_test_module" not in session.registered_modules |
| 88 | + with ( |
| 89 | + patch("designer_plugin.d3sdk.session.d3_api_register_module"), |
| 90 | + patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), |
| 91 | + ): |
| 92 | + session.execute(_module_payload()) |
| 93 | + assert "lazy_test_module" in session.registered_modules |
| 94 | + |
| 95 | + |
| 96 | +class TestD3AsyncSessionLazyRegistration: |
| 97 | + """Lazy registration behaviour for the asynchronous D3AsyncSession.""" |
| 98 | + |
| 99 | + def test_registered_modules_starts_empty(self): |
| 100 | + session = D3AsyncSession("localhost", 80) |
| 101 | + assert session.registered_modules == set() |
| 102 | + |
| 103 | + def test_module_registered_on_first_execute(self): |
| 104 | + async def run(): |
| 105 | + session = D3AsyncSession("localhost", 80) |
| 106 | + with ( |
| 107 | + patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, |
| 108 | + patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, |
| 109 | + ): |
| 110 | + mock_exec.return_value = _make_response() |
| 111 | + await session.execute(_module_payload()) |
| 112 | + mock_reg.assert_called_once() |
| 113 | + assert "lazy_test_module" in session.registered_modules |
| 114 | + |
| 115 | + asyncio.run(run()) |
| 116 | + |
| 117 | + def test_module_not_re_registered_on_second_execute(self): |
| 118 | + async def run(): |
| 119 | + session = D3AsyncSession("localhost", 80) |
| 120 | + with ( |
| 121 | + patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, |
| 122 | + patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, |
| 123 | + ): |
| 124 | + mock_exec.return_value = _make_response() |
| 125 | + await session.execute(_module_payload()) |
| 126 | + await session.execute(_module_payload()) |
| 127 | + mock_reg.assert_called_once() |
| 128 | + |
| 129 | + asyncio.run(run()) |
| 130 | + |
| 131 | + def test_no_registration_for_script_payload(self): |
| 132 | + """Payloads without a moduleName must never trigger registration.""" |
| 133 | + async def run(): |
| 134 | + session = D3AsyncSession("localhost", 80) |
| 135 | + with ( |
| 136 | + patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, |
| 137 | + patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, |
| 138 | + ): |
| 139 | + mock_exec.return_value = _make_response() |
| 140 | + await session.execute(_script_payload()) |
| 141 | + mock_reg.assert_not_called() |
| 142 | + |
| 143 | + asyncio.run(run()) |
| 144 | + |
| 145 | + def test_context_module_not_re_registered_lazily(self): |
| 146 | + """A module pre-registered via context_modules must not be registered again in execute().""" |
| 147 | + async def run(): |
| 148 | + with ( |
| 149 | + patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, |
| 150 | + patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, |
| 151 | + ): |
| 152 | + mock_exec.return_value = _make_response() |
| 153 | + async with D3AsyncSession("localhost", 80, {"lazy_test_module"}) as session: |
| 154 | + assert "lazy_test_module" in session.registered_modules |
| 155 | + await session.execute(_module_payload()) |
| 156 | + mock_reg.assert_called_once() |
| 157 | + |
| 158 | + asyncio.run(run()) |
| 159 | + |
| 160 | + def test_registered_modules_updated_after_execute(self): |
| 161 | + async def run(): |
| 162 | + session = D3AsyncSession("localhost", 80) |
| 163 | + assert "lazy_test_module" not in session.registered_modules |
| 164 | + with ( |
| 165 | + patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock), |
| 166 | + patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, |
| 167 | + ): |
| 168 | + mock_exec.return_value = _make_response() |
| 169 | + await session.execute(_module_payload()) |
| 170 | + assert "lazy_test_module" in session.registered_modules |
| 171 | + |
| 172 | + asyncio.run(run()) |
0 commit comments