Skip to content

Commit 1a26e19

Browse files
author
SentienceDEV
committed
fix tests
1 parent 559671c commit 1a26e19

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

tests/unit/conftest.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
pure unit/contract tests without requiring Playwright.
1010
1111
IMPORTANT:
12-
- These stubs are only active during pytest runs (via conftest import order).
12+
- These stubs are ONLY loaded when Playwright is NOT installed.
13+
- When Playwright IS installed, real Playwright is used for all tests.
1314
- Integration/E2E tests that need real Playwright should install Playwright and will
1415
typically run in separate environments.
1516
"""
@@ -20,52 +21,61 @@
2021
import types
2122

2223

23-
def _ensure_module(name: str) -> types.ModuleType:
24-
if name in sys.modules:
25-
return sys.modules[name]
26-
mod = types.ModuleType(name)
27-
sys.modules[name] = mod
28-
return mod
24+
def _ensure_playwright_stubs() -> None:
25+
"""
26+
Provide minimal `playwright.*` stubs so the SDK can be imported in environments
27+
where Playwright isn't installed (e.g., constrained CI/sandbox).
2928
29+
This is only intended to support pure unit/contract tests that don't actually
30+
launch browsers.
31+
"""
3032

31-
# Create top-level playwright module and submodules
32-
playwright_mod = _ensure_module("playwright")
33-
async_api_mod = _ensure_module("playwright.async_api")
34-
sync_api_mod = _ensure_module("playwright.sync_api")
33+
def _ensure_module(name: str) -> types.ModuleType:
34+
if name in sys.modules:
35+
return sys.modules[name]
36+
mod = types.ModuleType(name)
37+
sys.modules[name] = mod
38+
return mod
3539

40+
# Create top-level playwright module and submodules
41+
playwright_mod = _ensure_module("playwright")
42+
async_api_mod = _ensure_module("playwright.async_api")
43+
sync_api_mod = _ensure_module("playwright.sync_api")
3644

37-
class _Dummy:
38-
"""Placeholder type used for Playwright classes in unit tests."""
45+
class _Dummy:
46+
"""Placeholder type used for Playwright classes in unit tests."""
3947

48+
# Minimal symbols imported by `sentience.browser`
49+
async_api_mod.BrowserContext = _Dummy
50+
async_api_mod.Browser = _Dummy
51+
async_api_mod.Page = _Dummy
52+
async_api_mod.Playwright = _Dummy
53+
async_api_mod.PlaywrightContextManager = _Dummy
4054

41-
# Minimal symbols imported by `sentience.browser`
42-
async_api_mod.BrowserContext = _Dummy
43-
async_api_mod.Browser = _Dummy
44-
async_api_mod.Page = _Dummy
45-
async_api_mod.Playwright = _Dummy
46-
async_api_mod.PlaywrightContextManager = _Dummy
55+
async def _async_playwright():
56+
raise RuntimeError("Playwright is not available in this unit-test environment.")
4757

58+
async_api_mod.async_playwright = _async_playwright
4859

49-
async def _async_playwright():
50-
raise RuntimeError("Playwright is not available in this unit-test environment.")
60+
sync_api_mod.BrowserContext = _Dummy
61+
sync_api_mod.Browser = _Dummy
62+
sync_api_mod.Page = _Dummy
63+
sync_api_mod.Playwright = _Dummy
64+
sync_api_mod.PlaywrightContextManager = _Dummy
5165

66+
def _sync_playwright():
67+
raise RuntimeError("Playwright is not available in this unit-test environment.")
5268

53-
async_api_mod.async_playwright = _async_playwright
69+
sync_api_mod.sync_playwright = _sync_playwright
5470

55-
sync_api_mod.BrowserContext = _Dummy
56-
sync_api_mod.Browser = _Dummy
57-
sync_api_mod.Page = _Dummy
58-
sync_api_mod.Playwright = _Dummy
59-
sync_api_mod.PlaywrightContextManager = _Dummy
71+
# Expose submodules on the top-level module for completeness
72+
playwright_mod.async_api = async_api_mod
73+
playwright_mod.sync_api = sync_api_mod
6074

6175

62-
def _sync_playwright():
63-
raise RuntimeError("Playwright is not available in this unit-test environment.")
64-
65-
66-
sync_api_mod.sync_playwright = _sync_playwright
67-
68-
69-
# Expose submodules on the top-level module for completeness
70-
playwright_mod.async_api = async_api_mod
71-
playwright_mod.sync_api = sync_api_mod
76+
# Only load stubs if Playwright is NOT available
77+
# This prevents overwriting real Playwright when it IS installed
78+
try:
79+
import playwright # noqa: F401
80+
except ImportError:
81+
_ensure_playwright_stubs()

0 commit comments

Comments
 (0)