Skip to content

Commit f670605

Browse files
committed
šŸ› fix(network): eliminate test flakiness with reliable patching and parametrization
Refactor network initialization and tests to ensure stable performance and better coverage. Changes: - Move `settings` import to top-level in `browser_utils/initialization/network.py` to prevent patching issues - Consolidation of multiple test cases into a single parametrized test in `tests/browser_utils/initialization/test_network.py` - Switch from `patch` to `patch.object(settings, ...)` for more reliable configuration mocking - Improve test isolation and clean up redundant mock resets These changes address intermittent failures caused by improper patching of the configuration settings in an async environment.
1 parent 7406c40 commit f670605

2 files changed

Lines changed: 28 additions & 60 deletions

File tree

ā€Žbrowser_utils/initialization/network.pyā€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from playwright.async_api import BrowserContext as AsyncBrowserContext
77

8+
from config import settings
9+
810
from .scripts import add_init_scripts_to_context
911

1012
logger = logging.getLogger("AIStudioProxyServer")
@@ -13,8 +15,6 @@
1315
async def setup_network_interception_and_scripts(context: AsyncBrowserContext):
1416
"""Setup network interception and script injection"""
1517
try:
16-
from config import settings
17-
1818
# Check for network interception toggle
1919
if settings.NETWORK_INTERCEPTION_ENABLED:
2020
# Setup network interception

ā€Žtests/browser_utils/initialization/test_network.pyā€Ž

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,28 @@
1313
_setup_model_list_interception,
1414
setup_network_interception_and_scripts,
1515
)
16+
from config import settings
1617

1718

1819
@pytest.mark.asyncio
19-
async def test_setup_disabled():
20-
"""Test early return when network interception disabled"""
21-
mock_context = AsyncMock()
22-
23-
with (
24-
patch("config.settings.NETWORK_INTERCEPTION_ENABLED", False),
25-
patch("config.settings.ENABLE_SCRIPT_INJECTION", False),
26-
):
27-
await setup_network_interception_and_scripts(mock_context)
28-
29-
mock_context.route.assert_not_called()
30-
31-
32-
@pytest.mark.asyncio
33-
async def test_setup_enabled():
34-
"""Test setup when both flags enabled"""
35-
mock_context = AsyncMock()
36-
37-
with (
38-
patch("config.settings.NETWORK_INTERCEPTION_ENABLED", True),
39-
patch("config.settings.ENABLE_SCRIPT_INJECTION", True),
40-
patch(
41-
"browser_utils.initialization.network._setup_model_list_interception"
42-
) as mock_setup,
43-
patch(
44-
"browser_utils.initialization.network.add_init_scripts_to_context"
45-
) as mock_scripts,
46-
):
47-
await setup_network_interception_and_scripts(mock_context)
48-
mock_setup.assert_called_once_with(mock_context)
49-
mock_scripts.assert_called_once_with(mock_context)
50-
51-
52-
@pytest.mark.asyncio
53-
async def test_setup_granular_control():
54-
"""Test granular control of network interception and script injection"""
20+
@pytest.mark.parametrize(
21+
"interception_enabled, scripts_enabled, should_intercept, should_scripts",
22+
[
23+
(False, False, False, False),
24+
(True, True, True, True),
25+
(True, False, True, False),
26+
(False, True, False, True),
27+
],
28+
)
29+
async def test_setup_network_and_scripts_combinations(
30+
interception_enabled, scripts_enabled, should_intercept, should_scripts
31+
):
32+
"""Test all combinations of network interception and script injection toggles"""
5533
mock_context = AsyncMock()
5634

57-
# Case 1: Network enabled, Scripts disabled
5835
with (
59-
patch("config.settings.NETWORK_INTERCEPTION_ENABLED", True),
60-
patch("config.settings.ENABLE_SCRIPT_INJECTION", False),
36+
patch.object(settings, "NETWORK_INTERCEPTION_ENABLED", interception_enabled),
37+
patch.object(settings, "ENABLE_SCRIPT_INJECTION", scripts_enabled),
6138
patch(
6239
"browser_utils.initialization.network._setup_model_list_interception"
6340
) as mock_setup,
@@ -66,26 +43,16 @@ async def test_setup_granular_control():
6643
) as mock_scripts,
6744
):
6845
await setup_network_interception_and_scripts(mock_context)
69-
mock_setup.assert_called_once_with(mock_context)
70-
mock_scripts.assert_not_called()
7146

72-
mock_setup.reset_mock()
73-
mock_scripts.reset_mock()
47+
if should_intercept:
48+
mock_setup.assert_called_once_with(mock_context)
49+
else:
50+
mock_setup.assert_not_called()
7451

75-
# Case 2: Network disabled, Scripts enabled
76-
with (
77-
patch("config.settings.NETWORK_INTERCEPTION_ENABLED", False),
78-
patch("config.settings.ENABLE_SCRIPT_INJECTION", True),
79-
patch(
80-
"browser_utils.initialization.network._setup_model_list_interception"
81-
) as mock_setup,
82-
patch(
83-
"browser_utils.initialization.network.add_init_scripts_to_context"
84-
) as mock_scripts,
85-
):
86-
await setup_network_interception_and_scripts(mock_context)
87-
mock_setup.assert_not_called()
88-
mock_scripts.assert_called_once_with(mock_context)
52+
if should_scripts:
53+
mock_scripts.assert_called_once_with(mock_context)
54+
else:
55+
mock_scripts.assert_not_called()
8956

9057

9158
@pytest.mark.asyncio
@@ -133,7 +100,8 @@ async def test_setup_exception_handling():
133100
mock_context = AsyncMock()
134101

135102
with (
136-
patch("config.settings.ENABLE_SCRIPT_INJECTION", True),
103+
patch.object(settings, "ENABLE_SCRIPT_INJECTION", True),
104+
patch.object(settings, "NETWORK_INTERCEPTION_ENABLED", True),
137105
patch(
138106
"browser_utils.initialization.network._setup_model_list_interception",
139107
side_effect=RuntimeError("Route setup failed"),

0 commit comments

Comments
Ā (0)