Files
Scrapling/tests/ai/test_ai_mcp.py
T
Karim shoair a356dd2f2b refactor(mcp)!: Cleaning and unifying functions to async
- `get()` now delegates to `bulk_get([url])[0]` (was a separate sync implementation)
- `fetch()` now delegates to `bulk_fetch([url])[0]` (eliminated duplicate fetcher call)
- `stealthy_fetch()` now delegates to `bulk_stealthy_fetch([url])[0]` (same)
- Replaced 6x repeated `_content_translator(Convertor._extract_content(...), page)` with a single `_translate_response()` helper
- Removed unused imports (`Fetcher`, `DynamicFetcher`, `StealthyFetcher`, `Generator`)
2026-03-27 19:30:58 +02:00

82 lines
2.9 KiB
Python

import pytest
import pytest_httpbin
from scrapling.core.ai import ScraplingMCPServer, ResponseModel, _normalize_credentials
@pytest_httpbin.use_class_based_httpbin
class TestMCPServer:
"""Test MCP server functionality"""
@pytest.fixture(scope="class")
def test_url(self, httpbin):
return f"{httpbin.url}/html"
@pytest.fixture
def server(self):
return ScraplingMCPServer()
@pytest.mark.asyncio
async def test_get_tool(self, server, test_url):
"""Test the get tool method"""
result = await server.get(url=test_url, extraction_type="markdown")
assert isinstance(result, ResponseModel)
assert result.status == 200
assert result.url == test_url
@pytest.mark.asyncio
async def test_bulk_get_tool(self, server, test_url):
"""Test the bulk_get tool method"""
results = await server.bulk_get(urls=(test_url, test_url), extraction_type="html")
assert len(results) == 2
assert all(isinstance(r, ResponseModel) for r in results)
@pytest.mark.asyncio
async def test_fetch_tool(self, server, test_url):
"""Test the fetch tool method"""
result = await server.fetch(url=test_url, headless=True)
assert isinstance(result, ResponseModel)
assert result.status == 200
@pytest.mark.asyncio
async def test_bulk_fetch_tool(self, server, test_url):
"""Test the bulk_fetch tool method"""
result = await server.bulk_fetch(urls=(test_url, test_url), headless=True)
assert all(isinstance(r, ResponseModel) for r in result)
@pytest.mark.asyncio
async def test_stealthy_fetch_tool(self, server, test_url):
"""Test the stealthy_fetch tool method"""
result = await server.stealthy_fetch(url=test_url, headless=True)
assert isinstance(result, ResponseModel)
assert result.status == 200
@pytest.mark.asyncio
async def test_bulk_stealthy_fetch_tool(self, server, test_url):
"""Test the bulk_stealthy_fetch tool method"""
result = await server.bulk_stealthy_fetch(urls=(test_url, test_url), headless=True)
assert all(isinstance(r, ResponseModel) for r in result)
class TestNormalizeCredentials:
"""Test the _normalize_credentials helper"""
def test_none_returns_none(self):
assert _normalize_credentials(None) is None
def test_empty_dict_returns_none(self):
assert _normalize_credentials({}) is None
def test_valid_credentials_returns_tuple(self):
result = _normalize_credentials({"username": "user", "password": "pass"})
assert result == ("user", "pass")
def test_missing_password_raises(self):
with pytest.raises(ValueError, match="password"):
_normalize_credentials({"username": "user"})
def test_missing_username_raises(self):
with pytest.raises(ValueError, match="username"):
_normalize_credentials({"password": "pass"})