forked from CoplayDev/unity-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Auto-select sole Unity instance via PluginHub/stdio and add tests #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
dsarno
wants to merge
1
commit into
feature/gameobject-enhancements
from
codex/clarify-issue-with-custom-tools-in-repo-givkr2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import asyncio | ||
| import sys | ||
| import types | ||
| from types import SimpleNamespace | ||
|
|
||
| from .test_helpers import DummyContext | ||
|
|
||
|
|
||
| class DummyMiddlewareContext: | ||
| def __init__(self, ctx): | ||
| self.fastmcp_context = ctx | ||
|
|
||
|
|
||
| def test_auto_selects_single_instance_via_pluginhub(monkeypatch): | ||
| plugin_hub = types.ModuleType("transport.plugin_hub") | ||
|
|
||
| class PluginHub: | ||
| @classmethod | ||
| def is_configured(cls) -> bool: | ||
| return True | ||
|
|
||
| @classmethod | ||
| async def get_sessions(cls): | ||
| raise AssertionError("get_sessions should be stubbed in test") | ||
|
|
||
| plugin_hub.PluginHub = PluginHub | ||
| monkeypatch.setitem(sys.modules, "transport.plugin_hub", plugin_hub) | ||
| unity_transport = types.ModuleType("transport.unity_transport") | ||
| unity_transport._current_transport = lambda: "http" | ||
| monkeypatch.setitem(sys.modules, "transport.unity_transport", unity_transport) | ||
| monkeypatch.delitem(sys.modules, "transport.unity_instance_middleware", raising=False) | ||
|
|
||
| from transport.unity_instance_middleware import UnityInstanceMiddleware, PluginHub as ImportedPluginHub | ||
| assert ImportedPluginHub is plugin_hub.PluginHub | ||
|
|
||
| monkeypatch.setenv("UNITY_MCP_TRANSPORT", "http") | ||
|
|
||
| middleware = UnityInstanceMiddleware() | ||
| ctx = DummyContext() | ||
| ctx.client_id = "client-1" | ||
| middleware_context = DummyMiddlewareContext(ctx) | ||
|
|
||
| call_count = {"sessions": 0} | ||
|
|
||
| async def fake_get_sessions(): | ||
| call_count["sessions"] += 1 | ||
| return SimpleNamespace( | ||
| sessions={ | ||
| "session-1": SimpleNamespace(project="Ramble", hash="deadbeef"), | ||
| } | ||
| ) | ||
|
|
||
| monkeypatch.setattr(plugin_hub.PluginHub, "get_sessions", fake_get_sessions) | ||
|
|
||
| selected = asyncio.run(middleware._maybe_autoselect_instance(ctx)) | ||
|
|
||
| assert selected == "Ramble@deadbeef" | ||
| assert middleware.get_active_instance(ctx) == "Ramble@deadbeef" | ||
| assert call_count["sessions"] == 1 | ||
|
|
||
| asyncio.run(middleware._inject_unity_instance(middleware_context)) | ||
|
|
||
| assert ctx.get_state("unity_instance") == "Ramble@deadbeef" | ||
| assert call_count["sessions"] == 1 | ||
|
|
||
|
|
||
| def test_auto_selects_single_instance_via_stdio(monkeypatch): | ||
| plugin_hub = types.ModuleType("transport.plugin_hub") | ||
|
|
||
| class PluginHub: | ||
| @classmethod | ||
| def is_configured(cls) -> bool: | ||
| return False | ||
|
|
||
| plugin_hub.PluginHub = PluginHub | ||
| monkeypatch.setitem(sys.modules, "transport.plugin_hub", plugin_hub) | ||
| unity_transport = types.ModuleType("transport.unity_transport") | ||
| unity_transport._current_transport = lambda: "stdio" | ||
| monkeypatch.setitem(sys.modules, "transport.unity_transport", unity_transport) | ||
| monkeypatch.delitem(sys.modules, "transport.unity_instance_middleware", raising=False) | ||
|
|
||
| from transport.unity_instance_middleware import UnityInstanceMiddleware, PluginHub as ImportedPluginHub | ||
| assert ImportedPluginHub is plugin_hub.PluginHub | ||
|
|
||
| monkeypatch.setenv("UNITY_MCP_TRANSPORT", "stdio") | ||
|
|
||
| middleware = UnityInstanceMiddleware() | ||
| ctx = DummyContext() | ||
| ctx.client_id = "client-1" | ||
| middleware_context = DummyMiddlewareContext(ctx) | ||
|
|
||
| class PoolStub: | ||
| def discover_all_instances(self, force_refresh=False): | ||
| assert force_refresh is True | ||
| return [SimpleNamespace(id="UnityMCPTests@cc8756d4")] | ||
|
|
||
| unity_connection = types.ModuleType("transport.legacy.unity_connection") | ||
| unity_connection.get_unity_connection_pool = lambda: PoolStub() | ||
| monkeypatch.setitem(sys.modules, "transport.legacy.unity_connection", unity_connection) | ||
|
|
||
| selected = asyncio.run(middleware._maybe_autoselect_instance(ctx)) | ||
|
|
||
| assert selected == "UnityMCPTests@cc8756d4" | ||
| assert middleware.get_active_instance(ctx) == "UnityMCPTests@cc8756d4" | ||
|
|
||
| asyncio.run(middleware._inject_unity_instance(middleware_context)) | ||
|
|
||
| assert ctx.get_state("unity_instance") == "UnityMCPTests@cc8756d4" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Missing stub for
PluginHub._resolve_session_id. When_inject_unity_instanceis called with PluginHub configured, it will attempt to call_resolve_session_id(active_instance)on line 187 of the middleware. While the exception handling prevents the test from failing, the test should properly stub this method to avoid relying on exception handling and to ensure the happy path is tested.Prompt To Fix With AI