update tests/integration/test_hunter_strategies.py
Browse files
tests/integration/test_hunter_strategies.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import re
|
| 3 |
+
from unittest.mock import patch, MagicMock, AsyncMock
|
| 4 |
+
from aioresponses import aioresponses
|
| 5 |
+
from app.hunter.strategies.github import GitHubStrategy
|
| 6 |
+
from app.hunter.strategies.ai import AIStrategy
|
| 7 |
+
from app.hunter.strategies.search import SearchStrategy
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.mark.asyncio
|
| 11 |
+
async def test_github_strategy():
|
| 12 |
+
strategy = GitHubStrategy()
|
| 13 |
+
|
| 14 |
+
with aioresponses() as m:
|
| 15 |
+
# Mock GitHub Search API
|
| 16 |
+
m.get(
|
| 17 |
+
re.compile(r"^https://api\.github\.com/search/code.*$"),
|
| 18 |
+
payload={
|
| 19 |
+
"items": [
|
| 20 |
+
{
|
| 21 |
+
"html_url": "https://github.com/user/repo/blob/main/proxy.txt",
|
| 22 |
+
"name": "proxy.txt",
|
| 23 |
+
}
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
repeat=True,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
urls = await strategy.discover()
|
| 30 |
+
|
| 31 |
+
assert len(urls) > 0
|
| 32 |
+
# Check raw conversion
|
| 33 |
+
assert "raw.githubusercontent.com" in urls[0]
|
| 34 |
+
assert "/blob/" not in urls[0]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@pytest.mark.asyncio
|
| 38 |
+
async def test_ai_strategy():
|
| 39 |
+
strategy = AIStrategy()
|
| 40 |
+
|
| 41 |
+
# Create mock response structure
|
| 42 |
+
mock_choice = MagicMock()
|
| 43 |
+
mock_choice.message.content = (
|
| 44 |
+
"Sure! https://pastebin.com/raw/abcd and https://github.com/raw/xyz"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
mock_response = MagicMock()
|
| 48 |
+
mock_response.choices = [mock_choice]
|
| 49 |
+
|
| 50 |
+
# Mock g4f components
|
| 51 |
+
with (
|
| 52 |
+
patch("app.hunter.strategies.ai.AsyncClient") as mock_client_cls,
|
| 53 |
+
patch("app.hunter.strategies.ai.HAS_G4F", True),
|
| 54 |
+
patch("app.hunter.strategies.ai.RetryProvider", MagicMock()),
|
| 55 |
+
patch("app.hunter.strategies.ai.PollinationsAI", MagicMock()),
|
| 56 |
+
patch("app.hunter.strategies.ai.BlackboxPro", MagicMock()),
|
| 57 |
+
):
|
| 58 |
+
mock_client = AsyncMock()
|
| 59 |
+
mock_client_cls.return_value = mock_client
|
| 60 |
+
|
| 61 |
+
# Set return value for create
|
| 62 |
+
mock_client.chat.completions.create.return_value = mock_response
|
| 63 |
+
|
| 64 |
+
urls = await strategy.discover()
|
| 65 |
+
|
| 66 |
+
# Debug info if fails
|
| 67 |
+
if len(urls) != 2:
|
| 68 |
+
print(f"\nDEBUG: discover returned {urls}")
|
| 69 |
+
print(f"DEBUG: AsyncClient patched: {mock_client_cls.called}")
|
| 70 |
+
print(f"DEBUG: create called: {mock_client.chat.completions.create.called}")
|
| 71 |
+
|
| 72 |
+
assert len(urls) == 2
|
| 73 |
+
assert "https://pastebin.com/raw/abcd" in urls
|
| 74 |
+
assert "https://github.com/raw/xyz" in urls
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@pytest.mark.asyncio
|
| 78 |
+
async def test_search_strategy():
|
| 79 |
+
strategy = SearchStrategy()
|
| 80 |
+
|
| 81 |
+
# Mock DB storage get_random_proxy
|
| 82 |
+
with patch(
|
| 83 |
+
"app.hunter.strategies.search.db_storage.get_random_proxy",
|
| 84 |
+
new_callable=AsyncMock,
|
| 85 |
+
) as mock_get_proxy:
|
| 86 |
+
mock_proxy = MagicMock()
|
| 87 |
+
mock_proxy.protocol = "http"
|
| 88 |
+
mock_proxy.ip = "1.1.1.1"
|
| 89 |
+
mock_proxy.port = 8080
|
| 90 |
+
mock_get_proxy.return_value = mock_proxy
|
| 91 |
+
|
| 92 |
+
# Mock aiohttp request to DuckDuckGo
|
| 93 |
+
with aioresponses() as m:
|
| 94 |
+
# Mock the POST request to html.duckduckgo.com
|
| 95 |
+
m.post(
|
| 96 |
+
"https://html.duckduckgo.com/html/",
|
| 97 |
+
body='<html><body><a class="result__a" href="/l/?kh=-1&uddg=https%3A%2F%2Fpastebin.com%2Fraw%2Ffound">Link</a></body></html>',
|
| 98 |
+
repeat=True,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# We also need to mock get_db to yield a fake session
|
| 102 |
+
with patch("app.hunter.strategies.search.get_db") as mock_get_db:
|
| 103 |
+
# Create an async generator mock
|
| 104 |
+
async def async_gen():
|
| 105 |
+
yield MagicMock()
|
| 106 |
+
|
| 107 |
+
mock_get_db.return_value = async_gen()
|
| 108 |
+
|
| 109 |
+
urls = await strategy.discover()
|
| 110 |
+
|
| 111 |
+
assert len(urls) > 0
|
| 112 |
+
assert "https://pastebin.com/raw/found" in urls
|