File size: 1,684 Bytes
4a04219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""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]