Commit 619693bb authored by Kostas Chartsias's avatar Kostas Chartsias
Browse files

test: add basic unit tests

parent d0fa9c59
Loading
Loading
Loading
Loading

tests/__init__.py

0 → 100644
+1 −0
Original line number Diff line number Diff line
+5 −0
Original line number Diff line number Diff line
import pathlib

_SOURCE_DIR = pathlib.Path(__file__).resolve().parents[2] / "ai_agent"
if str(_SOURCE_DIR) not in __path__:
    __path__.append(str(_SOURCE_DIR))
+54 −0
Original line number Diff line number Diff line
import importlib.util
import pathlib
import sys
import unittest
from unittest.mock import AsyncMock, patch

import httpx


REPO_ROOT = pathlib.Path(__file__).resolve().parents[2]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

spec = importlib.util.spec_from_file_location("test_ai_agent_app", REPO_ROOT / "ai_agent" / "app.py")
ai_agent_app = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(ai_agent_app)
app = ai_agent_app.app


class AiAgentErrorCodeTests(unittest.IsolatedAsyncioTestCase):
    async def asyncSetUp(self) -> None:
        transport = httpx.ASGITransport(app=app)
        self.client = httpx.AsyncClient(transport=transport, base_url="http://test")

    async def asyncTearDown(self) -> None:
        await self.client.aclose()

    async def test_groq_query_missing_query_returns_400(self) -> None:
        response = await self.client.post("/groq-mcp", json={})

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), {"error": "No query provided"})

    async def test_groq_stream_missing_query_returns_400(self) -> None:
        response = await self.client.get("/groq-mcp/stream")

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), {"error": "No query provided"})

    async def test_groq_query_agent_failure_returns_500(self) -> None:
        agent = AsyncMock()
        agent.run.side_effect = RuntimeError("backend failure")

        with patch("ai_agent.routes.groq.create_groq_agent", new=AsyncMock(return_value=agent)):
            response = await self.client.post("/groq-mcp", json={"query": "hello"})

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.json(), {"error": "backend failure"})
        agent.close.assert_awaited_once()


if __name__ == "__main__":
    unittest.main()
+5 −0
Original line number Diff line number Diff line
import pathlib

_SOURCE_DIR = pathlib.Path(__file__).resolve().parents[2] / "mcp_module"
if str(_SOURCE_DIR) not in __path__:
    __path__.append(str(_SOURCE_DIR))
+40 −0
Original line number Diff line number Diff line
import importlib.util
import pathlib
import sys
import unittest

import httpx


REPO_ROOT = pathlib.Path(__file__).resolve().parents[2]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

spec = importlib.util.spec_from_file_location("test_mcp_module_app", REPO_ROOT / "mcp_module" / "app.py")
mcp_module_app = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(mcp_module_app)
app = mcp_module_app.app


class McpModuleErrorCodeTests(unittest.IsolatedAsyncioTestCase):
    async def asyncSetUp(self) -> None:
        transport = httpx.ASGITransport(app=app)
        self.client = httpx.AsyncClient(transport=transport, base_url="http://test")

    async def asyncTearDown(self) -> None:
        await self.client.aclose()

    async def test_unknown_route_returns_404(self) -> None:
        response = await self.client.get("/does-not-exist")

        self.assertEqual(response.status_code, 404)

    async def test_health_trailing_slash_returns_404(self) -> None:
        response = await self.client.get("/health/")

        self.assertEqual(response.status_code, 404)


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