Files
robotex/tests/unit/conftest.py
T
protokeyandClaude Opus 5.5 f21483489a feat: url текущей страницы в ответе задачи (#116)
SPA меняет адрес без навигации (страница созданной заявки и т.п.): ответ v1/v2 отдаёт page.url
после действий, клиент 0.2.1 - Result.url. Документация и unit-тесты.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 22:02:08 +04:00

83 lines
2.0 KiB
Python

"""Сервисы api и worker без браузера: Chrome — FakeSession (окружение — tests/harness.py)."""
import pytest
from api import actions
from tests.harness import WORKER_ID, run_env # noqa: F401
from worker import windows
class FakeContext:
def __init__(self):
self.ops = []
async def cookies(self):
return [{"name": "cf_clearance", "value": "secret-cookie"}]
async def clear_cookies(self):
self.ops.append("clear")
async def add_cookies(self, cookies):
self.ops.append(("add", cookies))
class FakePage:
url = "https://example.com/after-actions"
def __init__(self, context):
self.context = context
async def screenshot(self, type):
return b"png"
async def inner_html(self, selector):
return "<body>page</body>"
async def evaluate(self, js):
return "UA/engine"
class FakeSession:
"""Вместо AsyncStealthySession: запоминает kwargs и вызовы, page_action выполняет на FakePage."""
instances: list["FakeSession"] = []
error: Exception | None = None
def __init__(self, **kwargs):
self.kwargs = kwargs
self.context = FakeContext()
self.calls = []
self.closed = False
FakeSession.instances.append(self)
async def start(self):
pass
async def close(self):
self.closed = True
async def fetch(self, url, page_action, **kwargs):
self.calls.append(("fetch", url))
if FakeSession.error:
raise FakeSession.error
return await page_action(FakePage(self.context))
async def run(self, page_action, **kwargs):
self.calls.append(("run", None))
return await page_action(FakePage(self.context))
def fake_execute(result=None, exc=None):
async def execute(*args, **kwargs):
if exc:
raise exc
return result if result is not None else []
return execute
@pytest.fixture
def env(monkeypatch, tmp_path):
FakeSession.instances, FakeSession.error = [], None
monkeypatch.setattr(windows, "AsyncStealthySession", FakeSession)
monkeypatch.setattr(actions, "execute", fake_execute())
with run_env(monkeypatch, tmp_path) as harness:
yield harness