File size: 22,165 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
"""
Integration tests for browser_utils/initialization/core.py.

These tests verify real browser initialization logic with minimal mocking:
- Uses REAL temp files for auth (no os.path.exists mocking)
- Uses REAL file I/O (storage state selection logic executes)
- Mocks ONLY external I/O (browser.new_context, page.goto, page.locator)

Focus: Test that storage state selection, page discovery, and readiness checks
work correctly with real files and real logic.
"""

import os
from unittest.mock import AsyncMock, MagicMock, patch

import pytest


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_storage_state_explicit_exists(temp_auth_file):
    """
    测试场景: 显式传入存在的认证文件路径
    策略: 使用真实临时文件,验证文件路径被正确使用
    """
    from browser_utils.initialization.core import initialize_page_logic

    # 验证文件确实存在(真实文件 I/O)
    assert temp_auth_file.exists()
    assert os.path.exists(str(temp_auth_file))

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    # Mock 页面状态
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Gemini 1.5 Pro")
    mock_page.locator = MagicMock(return_value=mock_locator)

    # Mock expect_async
    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        # 调用真实逻辑,传入真实文件路径
        await initialize_page_logic(
            mock_browser, storage_state_path=str(temp_auth_file)
        )

        # 验证: new_context 被调用时包含了真实文件路径
        call_args = mock_browser.new_context.call_args
        assert call_args is not None
        assert "storage_state" in call_args[1]
        assert call_args[1]["storage_state"] == str(temp_auth_file)


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_storage_state_explicit_missing(temp_auth_file_missing):
    """
    测试场景: 显式传入不存在的认证文件路径
    策略: 使用真实文件系统检查,验证 RuntimeError 被抛出
    """
    from browser_utils.initialization.core import initialize_page_logic

    # 验证文件确实不存在(真实文件 I/O)
    assert not temp_auth_file_missing.exists()
    assert not os.path.exists(str(temp_auth_file_missing))

    mock_browser = AsyncMock()

    with patch.dict("sys.modules", {"server": MagicMock()}):
        # 预期: 抛出 RuntimeError,因为文件不存在
        with pytest.raises(RuntimeError, match="Specified auth file does not exist"):
            await initialize_page_logic(
                mock_browser, storage_state_path=str(temp_auth_file_missing)
            )


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_headless_auth_exists(temp_auth_file):
    """
    测试场景: 无头模式下,ACTIVE_AUTH_JSON_PATH 指向存在的文件
    策略: 使用真实文件,验证无头模式正确加载认证
    """
    from browser_utils.initialization.core import initialize_page_logic

    assert temp_auth_file.exists()

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Gemini 2.0 Flash")
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch.dict(
            "os.environ",
            {
                "LAUNCH_MODE": "headless",
                "ACTIVE_AUTH_JSON_PATH": str(temp_auth_file),
                "AUTO_AUTH_ROTATION_ON_STARTUP": "false",
            },
        ),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        await initialize_page_logic(mock_browser)

        # 验证: storage_state 参数使用了真实文件路径
        call_args = mock_browser.new_context.call_args
        assert call_args[1]["storage_state"] == str(temp_auth_file)


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_debug_auth_exists(temp_auth_file):
    """
    测试场景: 调试模式下,ACTIVE_AUTH_JSON_PATH 指向存在的文件
    策略: 使用真实文件,验证调试模式正确加载认证
    """
    from browser_utils.initialization.core import initialize_page_logic

    assert temp_auth_file.exists()

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Model")
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch.dict(
            "os.environ",
            {"LAUNCH_MODE": "debug", "ACTIVE_AUTH_JSON_PATH": str(temp_auth_file)},
        ),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        await initialize_page_logic(mock_browser)

        call_args = mock_browser.new_context.call_args
        assert call_args[1]["storage_state"] == str(temp_auth_file)


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_debug_auth_missing_falls_back(temp_auth_file_missing):
    """
    测试场景: 调试模式下,ACTIVE_AUTH_JSON_PATH 指向不存在的文件
    策略: 使用真实文件系统,验证调试模式回退到不使用认证
    """
    from browser_utils.initialization.core import initialize_page_logic

    assert not temp_auth_file_missing.exists()

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Model")
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch.dict(
            "os.environ",
            {
                "LAUNCH_MODE": "debug",
                "ACTIVE_AUTH_JSON_PATH": str(temp_auth_file_missing),
            },
        ),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        await initialize_page_logic(mock_browser)

        # 验证: 调试模式下文件不存在时,不使用 storage_state
        call_args = mock_browser.new_context.call_args
        assert "storage_state" not in call_args[1]


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_proxy_settings_applied():
    """
    测试场景: 验证代理设置被正确传递到浏览器上下文
    策略: Mock browser I/O,验证 proxy 参数
    """
    from browser_utils.initialization.core import initialize_page_logic

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Model")
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    proxy_config = {"server": "http://127.0.0.1:8080"}

    # Create mock server state with proper proxy settings

    with (
        patch.dict("os.environ", {"LAUNCH_MODE": "debug"}),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch("api_utils.server_state.state") as mock_state,
    ):
        mock_state.PLAYWRIGHT_PROXY_SETTINGS = proxy_config
        await initialize_page_logic(mock_browser)

        # 验证: proxy 参数被正确传递
        call_args = mock_browser.new_context.call_args
        assert call_args[1]["proxy"] == proxy_config


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_page_discovery_existing_page():
    """
    测试场景: 浏览器上下文中已有页面,发现并复用
    策略: Mock browser I/O,验证页面发现逻辑
    """
    from browser_utils.initialization.core import initialize_page_logic

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)

    # 上下文中已有一个页面
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Model")
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_context.pages = [mock_page]

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch.dict("os.environ", {"LAUNCH_MODE": "debug"}),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        page, _ = await initialize_page_logic(mock_browser)

        # 验证: 返回的是现有页面,而不是新创建的
        assert page is mock_page
        # 验证: new_page 不应该被调用
        mock_context.new_page.assert_not_called()


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_login_url_transition():
    """
    测试场景: 页面从登录 URL 过渡到 AI Studio URL
    策略: Mock page.url 属性返回不同的值,验证 URL 检测逻辑
    """
    from unittest.mock import PropertyMock

    from browser_utils.initialization.core import initialize_page_logic

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    # 模拟 URL 过渡: login -> new_chat
    type(mock_page).url = PropertyMock(
        side_effect=[
            "https://accounts.google.com/signin",  # goto 后的 URL
            "https://aistudio.google.com/prompts/new_chat",  # wait_for_url 后
            "https://aistudio.google.com/prompts/new_chat",  # 最终检查
            "https://aistudio.google.com/prompts/new_chat",  # 模型名提取
        ]
    )

    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()
    mock_page.wait_for_url = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Model")
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch.dict("os.environ", {"LAUNCH_MODE": "debug", "SUPPRESS_LOGIN_WAIT": "0"}),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.core.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch("builtins.input", return_value=""),
        patch("builtins.print"),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        await initialize_page_logic(mock_browser)

        # 验证: wait_for_url 被调用,等待从登录页过渡
        mock_page.wait_for_url.assert_called()


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_model_name_extraction():
    """
    测试场景: 从页面中提取模型名称
    策略: Mock page.locator,验证模型名提取逻辑
    """
    from browser_utils.initialization.core import initialize_page_logic

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Mock 模型名选择器
    model_name = "Gemini 1.5 Pro Experimental"
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value=model_name)
    mock_page.locator = MagicMock(return_value=mock_locator)

    mock_expect = MagicMock()
    mock_expect.return_value.to_be_visible = AsyncMock()

    with (
        patch.dict("os.environ", {"LAUNCH_MODE": "debug"}),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None
        state.current_ai_studio_model_id = None

        _, is_new_page = await initialize_page_logic(mock_browser)

        # 验证: locator 被调用以提取模型名
        mock_page.locator.assert_called()
        # 验证: 模型名被设置到 server.current_ai_studio_model_id
        # (这个断言可能需要检查实际的赋值逻辑)


@pytest.mark.integration
@pytest.mark.asyncio
async def test_init_page_readiness_verification():
    """
    测试场景: 验证页面就绪状态(输入框可见)
    策略: Mock expect_async,验证页面就绪检查逻辑
    """
    from browser_utils.initialization.core import initialize_page_logic

    mock_browser = AsyncMock()
    mock_context = AsyncMock()
    mock_page = AsyncMock()

    mock_browser.new_context = AsyncMock(return_value=mock_context)
    mock_context.pages = []
    mock_context.new_page = AsyncMock(return_value=mock_page)

    mock_page.url = "https://aistudio.google.com/prompts/new_chat"
    mock_page.is_closed = MagicMock(return_value=False)
    mock_page.goto = AsyncMock()

    # Create locator mock with count() support
    mock_locator = MagicMock()
    mock_locator.count = AsyncMock(return_value=1)  # Element exists
    mock_locator.first = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="Model")
    mock_page.locator = MagicMock(return_value=mock_locator)

    # Mock expect_async 来验证页面就绪检查
    mock_expect = MagicMock()
    mock_assertion = MagicMock()
    mock_assertion.to_be_visible = AsyncMock()
    mock_expect.return_value = mock_assertion

    with (
        patch.dict("os.environ", {"LAUNCH_MODE": "debug"}),
        patch("browser_utils.initialization.core.expect_async", mock_expect),
        patch("playwright.async_api.expect", mock_expect),
        patch(
            "browser_utils.initialization.core.setup_network_interception_and_scripts",
            new_callable=AsyncMock,
        ),
        patch("browser_utils.initialization.core.setup_debug_listeners"),
        patch(
            "browser_utils.initialization.auth.wait_for_model_list_and_handle_auth_save",
            new_callable=AsyncMock,
        ),
        patch.dict("sys.modules", {"server": MagicMock()}),
    ):
        from api_utils.server_state import state

        state.PLAYWRIGHT_PROXY_SETTINGS = None

        await initialize_page_logic(mock_browser)

        # 验证: expect_async 被调用以检查输入框可见性
        mock_expect.assert_called()
        # 验证: to_be_visible 被等待
        mock_assertion.to_be_visible.assert_called()