Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/routers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ async def execute_tool(name: str, arguments: dict, context: dict) -> tuple[str,
return f"Available models:\n{lines}", None

elif name == "unload_models":
await client.post(f"{MODLY_API}/model/unload-all")
r = await client.post(f"{MODLY_API}/model/unload-all")
r.raise_for_status()
return "All 3D generation models have been unloaded from VRAM.", None

elif name == "get_mesh_info":
Expand Down
53 changes: 53 additions & 0 deletions api/tests/test_agent_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import asyncio
import unittest
from unittest import mock

import httpx

import routers.agent as agent


class _MockClientFactory:
"""Builds real AsyncClients wired to a MockTransport, so execute_tool talks to
a fake Modly API instead of the network."""

def __init__(self, handler) -> None:
self._handler = handler
self._real = httpx.AsyncClient

def __call__(self, *args, **kwargs):
kwargs["transport"] = httpx.MockTransport(self._handler)
return self._real(*args, **kwargs)


def _run_tool(name: str, handler) -> tuple[str, object]:
factory = _MockClientFactory(handler)
with mock.patch.object(agent.httpx, "AsyncClient", factory):
return asyncio.run(agent.execute_tool(name, {}, {}))


class UnloadModelsErrorTests(unittest.TestCase):
"""unload_models must report a failed unload, not claim success (like every
other POST tool and like the MCP server's modly_unload_models)."""

def test_http_error_is_surfaced(self) -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(500, text="boom")

text, payload = _run_tool("unload_models", handler)
# Before the fix the response was discarded and the success string was
# returned even on a 500; now the shared HTTPStatusError handler runs.
self.assertTrue(text.startswith("API error 500"), text)
self.assertIsNone(payload)

def test_success_still_reports_unloaded(self) -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json={"ok": True})

text, payload = _run_tool("unload_models", handler)
self.assertIn("unloaded", text.lower())
self.assertIsNone(payload)


if __name__ == "__main__":
unittest.main()