"""Fixtures for Playwright E2E tests. Starts a real Marionette server (with temp paths) and provides the base URL. """ import threading import time import pytest import uvicorn from marionette.main import create_app # Use a high port to avoid conflicts TEST_PORT = 18042 class _ServerThread: """Runs a uvicorn server in a background thread.""" def __init__(self, app, port: int): self.config = uvicorn.Config(app, host="127.0.0.1", port=port, log_level="warning") self.server = uvicorn.Server(self.config) self.thread = threading.Thread(target=self.server.run, daemon=True) def start(self): self.thread.start() # Wait for the server to be ready for _ in range(50): if self.server.started: break time.sleep(0.1) def stop(self): self.server.should_exit = True self.thread.join(timeout=5) @pytest.fixture(scope="session") def _test_server(tmp_path_factory): """Start a Marionette server for the entire test session.""" tmp = tmp_path_factory.mktemp("e2e") app, marionette = create_app( registry_path=tmp / "registry.json", dataset_root=tmp / "datasets", ) srv = _ServerThread(app, TEST_PORT) srv.start() yield f"http://127.0.0.1:{TEST_PORT}", marionette srv.stop() @pytest.fixture(scope="session") def base_url(_test_server): """Return the base URL of the test server (session-scoped for pytest-playwright).""" return _test_server[0] @pytest.fixture(scope="session") def test_marionette(_test_server): """Return the Marionette instance backing the test server.""" return _test_server[1]