Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : SPIDER — Universal Web Intelligence Engine7# File : tests/unit/browser/conftest.py8# Module : Tests9# Repository : https://git.spboucher.ai/spider10# Project Website : Not specified11# Created : 2026-12-0412# Last Modified : 2026-12-0413#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development : dev@spboucher.ai23# Support : support@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Fixtures of the browser engine tests: one local page server and one browser pool for the session."""42from __future__ import annotations4344import pytest45import pytest_asyncio46from browser_testlib import UA_SUFFIX, pageserver4748from spider.contracts import CaptureOptions, RenderRequest495051@pytest_asyncio.fixture(scope="session", loop_scope="session")52async def server():53 srv = await pageserver.PageServer().start()54 yield srv55 await srv.stop()565758@pytest_asyncio.fixture(scope="session", loop_scope="session")59async def pool():60 from spider.browser import BrowserPool61 async with BrowserPool() as p:62 yield p636465@pytest.fixture66def make_request(server, tmp_path):67 """Build a RenderRequest for a path of the local server; captures are minimal unless asked."""68 counter = {"n": 0}6970 def build(path: str, *, host: str = "127.0.0.1", profile: str = "desktop", timeout_s: float = 60,71 max_states: int = 20, max_actions: int = 40, allowed_hosts: list[str] | None = None,72 proxy: str | None = None, **capture) -> RenderRequest:73 counter["n"] += 174 capture.setdefault("full_page", False)75 capture.setdefault("webp_derivatives", False)76 return RenderRequest(url=server.url(path, host=host), profile=profile, capture=CaptureOptions(**capture),77 out_dir=str(tmp_path / f"render-{counter['n']}"), timeout_s=timeout_s,78 max_states=max_states, max_actions=max_actions, user_agent_suffix=UA_SUFFIX,79 allowed_hosts=allowed_hosts or [], proxy=proxy)80 return build818283@pytest_asyncio.fixture(loop_scope="session")84async def open_page(pool, server):85 """Open a plain page (instrumented like a render, same desktop profile) to measure things independently."""86 from spider.browser import stability8788 contexts = []8990 async def _open(path: str, engine: str = "chromium"):91 slot = await pool._acquire(engine, None)92 await pool._release(slot)93 context = await slot.browser.new_context(viewport={"width": 1280, "height": 800}, device_scale_factor=1)94 contexts.append(context)95 await stability.install(context)96 page = await context.new_page()97 await page.goto(server.url(path))98 await stability.wait_stable(page, quiet_ms=200, max_ms=3000)99 return page100101 yield _open102 for context in contexts:103 await context.close()104105