"""Сервисы 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: def __init__(self, context): self.context = context async def screenshot(self, type): return b"png" async def inner_html(self, selector): return "page" 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