-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_agent_runtime.py
More file actions
816 lines (619 loc) · 28.3 KB
/
test_agent_runtime.py
File metadata and controls
816 lines (619 loc) · 28.3 KB
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
"""
Tests for AgentRuntime.
These tests verify the AgentRuntime works correctly with the new
BrowserBackend-based architecture.
"""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from sentience.agent_runtime import AgentRuntime
from sentience.models import EvaluateJsRequest, SnapshotOptions, TabInfo
from sentience.verification import AssertContext, AssertOutcome
class MockBackend:
"""Mock BrowserBackend implementation for testing."""
def __init__(self) -> None:
self._url = "https://example.com"
self.eval_results: dict[str, any] = {}
async def get_url(self) -> str:
return self._url
async def eval(self, expression: str) -> any:
return self.eval_results.get(expression)
async def refresh_page_info(self):
pass
async def call(self, function_declaration: str, args=None):
pass
async def get_layout_metrics(self):
pass
async def screenshot_png(self) -> bytes:
return b""
async def mouse_move(self, x: float, y: float) -> None:
pass
async def mouse_click(self, x: float, y: float, button="left", click_count=1) -> None:
pass
async def wheel(self, delta_y: float, x=None, y=None) -> None:
pass
async def type_text(self, text: str) -> None:
pass
async def wait_ready_state(self, state="interactive", timeout_ms=15000) -> None:
pass
async def list_tabs(self):
return [
TabInfo(tab_id="tab-1", url="https://example.com", is_active=True),
TabInfo(tab_id="tab-2", url="https://example.com/2", is_active=False),
]
async def open_tab(self, url: str):
return TabInfo(tab_id="tab-new", url=url, is_active=True)
async def switch_tab(self, tab_id: str):
return TabInfo(tab_id=tab_id, url="https://example.com/2", is_active=True)
async def close_tab(self, tab_id: str):
return TabInfo(tab_id=tab_id, url="https://example.com/2", is_active=False)
class MockTracer:
"""Mock Tracer for testing."""
def __init__(self) -> None:
self.events: list[dict] = []
def emit(self, event_type: str, data: dict, step_id: str | None = None) -> None:
self.events.append(
{
"type": event_type,
"data": data,
"step_id": step_id,
}
)
class TestAgentRuntimeInit:
"""Tests for AgentRuntime initialization."""
def test_init_with_backend(self) -> None:
"""Test basic initialization with backend."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
assert runtime.backend is backend
assert runtime.tracer is tracer
assert runtime.step_id is None
# 0-based step ids: first begin_step() will produce "step-0"
assert runtime.step_index == -1
assert runtime.last_snapshot is None
assert runtime.is_task_done is False
def test_init_with_snapshot_options(self) -> None:
"""Test initialization with custom snapshot options."""
backend = MockBackend()
tracer = MockTracer()
options = SnapshotOptions(limit=100, goal="test goal")
runtime = AgentRuntime(backend=backend, tracer=tracer, snapshot_options=options)
assert runtime._snapshot_options.limit == 100
assert runtime._snapshot_options.goal == "test goal"
def test_init_with_api_key(self) -> None:
"""Test initialization with API key enables use_api."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(
backend=backend,
tracer=tracer,
sentience_api_key="sk_test_key",
)
assert runtime._snapshot_options.sentience_api_key == "sk_test_key"
assert runtime._snapshot_options.use_api is True
def test_init_with_api_key_and_options(self) -> None:
"""Test API key merges with provided options."""
backend = MockBackend()
tracer = MockTracer()
options = SnapshotOptions(limit=50)
runtime = AgentRuntime(
backend=backend,
tracer=tracer,
snapshot_options=options,
sentience_api_key="sk_pro_key",
)
assert runtime._snapshot_options.limit == 50
assert runtime._snapshot_options.sentience_api_key == "sk_pro_key"
assert runtime._snapshot_options.use_api is True
@pytest.mark.asyncio
async def test_evaluate_js_success(self) -> None:
backend = MockBackend()
tracer = MockTracer()
backend.eval_results["1 + 1"] = 2
runtime = AgentRuntime(backend=backend, tracer=tracer)
result = await runtime.evaluate_js(EvaluateJsRequest(code="1 + 1"))
assert result.ok is True
assert result.value == 2
assert result.text == "2"
@pytest.mark.asyncio
async def test_evaluate_js_truncate(self) -> None:
backend = MockBackend()
tracer = MockTracer()
backend.eval_results["long"] = "x" * 50
runtime = AgentRuntime(backend=backend, tracer=tracer)
result = await runtime.evaluate_js(EvaluateJsRequest(code="long", max_output_chars=10))
assert result.ok is True
assert result.truncated is True
assert result.text == "x" * 10 + "..."
@pytest.mark.asyncio
async def test_tab_operations(self) -> None:
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
tabs = await runtime.list_tabs()
assert tabs.ok is True
assert len(tabs.tabs) == 2
opened = await runtime.open_tab("https://example.com/new")
assert opened.ok is True
assert opened.tab is not None
switched = await runtime.switch_tab("tab-2")
assert switched.ok is True
closed = await runtime.close_tab("tab-2")
assert closed.ok is True
class TestAgentRuntimeGetUrl:
"""Tests for get_url method."""
@pytest.mark.asyncio
async def test_get_url(self) -> None:
"""Test get_url returns URL from backend."""
backend = MockBackend()
backend._url = "https://test.example.com/page"
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
url = await runtime.get_url()
assert url == "https://test.example.com/page"
assert runtime._cached_url == "https://test.example.com/page"
class TestAgentRuntimeBeginStep:
"""Tests for begin_step method."""
def test_begin_step_generates_step_id(self) -> None:
"""Test begin_step generates a step_id in 'step-N' format."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
step_id = runtime.begin_step(goal="Test step")
assert step_id is not None
assert step_id == "step-0" # First step should be step-0
def test_begin_step_id_matches_index(self) -> None:
"""Test step_id format matches step_index for Studio compatibility."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
step_id_1 = runtime.begin_step(goal="Step 1")
assert step_id_1 == "step-0"
assert runtime.step_index == 0
step_id_2 = runtime.begin_step(goal="Step 2")
assert step_id_2 == "step-1"
assert runtime.step_index == 1
# With explicit index
step_id_10 = runtime.begin_step(goal="Step 10", step_index=10)
assert step_id_10 == "step-10"
assert runtime.step_index == 10
def test_begin_step_increments_index(self) -> None:
"""Test begin_step auto-increments step_index."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Step 1")
assert runtime.step_index == 0
runtime.begin_step(goal="Step 2")
assert runtime.step_index == 1
def test_begin_step_explicit_index(self) -> None:
"""Test begin_step with explicit step_index."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Custom step", step_index=10)
assert runtime.step_index == 10
def test_begin_step_clears_assertions(self) -> None:
"""Test begin_step clears previous assertions."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
# Add some assertions
runtime._assertions_this_step = [{"label": "old", "passed": True}]
runtime.begin_step(goal="New step")
assert runtime._assertions_this_step == []
class TestAgentRuntimeAssertions:
"""Tests for assertion methods."""
def test_assert_passing(self) -> None:
"""Test assert_ with passing predicate."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
# Create a passing predicate
def passing_predicate(ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=True, reason="Matched", details={})
result = runtime.assert_(passing_predicate, label="test_label")
assert result is True
assert len(runtime._assertions_this_step) == 1
assert runtime._assertions_this_step[0]["label"] == "test_label"
assert runtime._assertions_this_step[0]["passed"] is True
def test_assert_failing(self) -> None:
"""Test assert_ with failing predicate."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
def failing_predicate(ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=False, reason="Not matched", details={})
result = runtime.assert_(failing_predicate, label="fail_label")
assert result is False
assert runtime._assertions_this_step[0]["passed"] is False
def test_assert_emits_event(self) -> None:
"""Test assert_ emits verification event."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
def predicate(ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=True, reason="OK", details={"key": "value"})
runtime.assert_(predicate, label="test_emit")
assert len(tracer.events) == 1
event = tracer.events[0]
assert event["type"] == "verification"
assert event["data"]["kind"] == "assert"
assert event["data"]["passed"] is True
assert event["data"]["label"] == "test_emit"
def test_assert_done_marks_task_complete(self) -> None:
"""Test assert_done marks task as done on success."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
def passing_predicate(ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=True, reason="Done", details={})
result = runtime.assert_done(passing_predicate, label="task_complete")
assert result is True
assert runtime.is_task_done is True
assert runtime._task_done_label == "task_complete"
def test_assert_done_does_not_mark_on_failure(self) -> None:
"""Test assert_done doesn't mark task done on failure."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
def failing_predicate(ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=False, reason="Not done", details={})
result = runtime.assert_done(failing_predicate, label="task_incomplete")
assert result is False
assert runtime.is_task_done is False
@pytest.mark.asyncio
async def test_check_eventually_records_final_only(self) -> None:
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
# Two failing snapshots, then success
snaps = [
MagicMock(url="https://example.com", elements=[]),
MagicMock(url="https://example.com", elements=[]),
MagicMock(url="https://example.com/done", elements=[]),
]
async def fake_snapshot(**_kwargs):
runtime.last_snapshot = snaps.pop(0)
return runtime.last_snapshot
runtime.snapshot = AsyncMock(side_effect=fake_snapshot) # type: ignore[method-assign]
def pred(ctx: AssertContext) -> AssertOutcome:
ok = (ctx.url or "").endswith("/done")
return AssertOutcome(
passed=ok,
reason="" if ok else "not done",
details={"selector": "text~'Done'", "reason_code": "ok" if ok else "no_match"},
)
handle = runtime.check(pred, label="eventually_done")
ok = await handle.eventually(timeout_s=2.0, poll_s=0.0)
assert ok is True
# Only the final record is accumulated for step_end
assert len(runtime._assertions_this_step) == 1
assert runtime._assertions_this_step[0]["label"] == "eventually_done"
assert runtime._assertions_this_step[0]["passed"] is True
assert runtime._assertions_this_step[0].get("final") is True
# But attempts emitted multiple verification events
assert len(tracer.events) >= 3
assert all(e["type"] == "verification" for e in tracer.events)
@pytest.mark.asyncio
async def test_check_eventually_snapshot_exhausted_min_confidence(self) -> None:
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
low_diag = MagicMock()
low_diag.confidence = 0.1
low_diag.model_dump = lambda: {"confidence": 0.1}
snaps = [
MagicMock(url="https://example.com", elements=[], diagnostics=low_diag),
MagicMock(url="https://example.com", elements=[], diagnostics=low_diag),
]
async def fake_snapshot(**_kwargs):
runtime.last_snapshot = snaps.pop(0)
return runtime.last_snapshot
runtime.snapshot = AsyncMock(side_effect=fake_snapshot) # type: ignore[method-assign]
def pred(_ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=True, reason="would pass", details={})
handle = runtime.check(pred, label="min_confidence_gate")
ok = await handle.eventually(
timeout_s=5.0,
poll_s=0.0,
min_confidence=0.7,
max_snapshot_attempts=2,
)
assert ok is False
# Only the final record is accumulated for step_end
assert len(runtime._assertions_this_step) == 1
details = runtime._assertions_this_step[0]["details"]
assert details["reason_code"] == "snapshot_exhausted"
@pytest.mark.asyncio
async def test_check_eventually_vision_fallback_on_exhaustion(self) -> None:
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
low_diag = MagicMock()
low_diag.confidence = 0.1
low_diag.model_dump = lambda: {"confidence": 0.1}
async def fake_snapshot(**_kwargs):
runtime.last_snapshot = MagicMock(
url="https://example.com", elements=[], diagnostics=low_diag
)
return runtime.last_snapshot
runtime.snapshot = AsyncMock(side_effect=fake_snapshot) # type: ignore[method-assign]
class VisionProviderStub:
def supports_vision(self) -> bool:
return True
def generate_with_image(self, *_args, **_kwargs):
return MagicMock(content="YES")
def pred(_ctx: AssertContext) -> AssertOutcome:
return AssertOutcome(passed=False, reason="should not run", details={})
handle = runtime.check(pred, label="vision_fallback_check")
ok = await handle.eventually(
timeout_s=5.0,
poll_s=0.0,
min_confidence=0.7,
max_snapshot_attempts=1,
vision_provider=VisionProviderStub(),
)
assert ok is True
assert len(runtime._assertions_this_step) == 1
rec = runtime._assertions_this_step[0]
assert rec.get("vision_fallback") is True
assert rec["details"]["reason_code"] == "vision_fallback_pass"
class TestAgentRuntimeAssertionHelpers:
"""Tests for assertion helper methods."""
def test_all_assertions_passed_empty(self) -> None:
"""Test all_assertions_passed with no assertions."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
assert runtime.all_assertions_passed() is True
def test_all_assertions_passed_true(self) -> None:
"""Test all_assertions_passed when all pass."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._assertions_this_step = [
{"passed": True},
{"passed": True},
]
assert runtime.all_assertions_passed() is True
def test_all_assertions_passed_false(self) -> None:
"""Test all_assertions_passed when one fails."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._assertions_this_step = [
{"passed": True},
{"passed": False},
]
assert runtime.all_assertions_passed() is False
def test_required_assertions_passed(self) -> None:
"""Test required_assertions_passed ignores optional failures."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._assertions_this_step = [
{"passed": True, "required": True},
{"passed": False, "required": False}, # Optional failure
]
assert runtime.required_assertions_passed() is True
def test_required_assertions_failed(self) -> None:
"""Test required_assertions_passed fails on required failure."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._assertions_this_step = [
{"passed": True, "required": True},
{"passed": False, "required": True}, # Required failure
]
assert runtime.required_assertions_passed() is False
class TestAgentRuntimeFlushAssertions:
"""Tests for flush_assertions method."""
def test_flush_assertions(self) -> None:
"""Test flush_assertions returns and clears assertions."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._assertions_this_step = [
{"label": "a", "passed": True},
{"label": "b", "passed": False},
]
assertions = runtime.flush_assertions()
assert len(assertions) == 2
assert assertions[0]["label"] == "a"
assert runtime._assertions_this_step == []
class TestAgentRuntimeGetAssertionsForStepEnd:
"""Tests for get_assertions_for_step_end method."""
def test_get_assertions_basic(self) -> None:
"""Test get_assertions_for_step_end returns assertions."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._assertions_this_step = [{"label": "test", "passed": True}]
result = runtime.get_assertions_for_step_end()
assert "assertions" in result
assert len(result["assertions"]) == 1
assert "task_done" not in result
def test_get_assertions_with_task_done(self) -> None:
"""Test get_assertions_for_step_end includes task_done."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._task_done = True
runtime._task_done_label = "completed"
result = runtime.get_assertions_for_step_end()
assert result["task_done"] is True
assert result["task_done_label"] == "completed"
class TestAgentRuntimeResetTaskDone:
"""Tests for reset_task_done method."""
def test_reset_task_done(self) -> None:
"""Test reset_task_done clears task state."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._task_done = True
runtime._task_done_label = "was_done"
runtime.reset_task_done()
assert runtime.is_task_done is False
assert runtime._task_done_label is None
class TestAgentRuntimeContext:
"""Tests for _ctx method."""
def test_ctx_with_snapshot(self) -> None:
"""Test _ctx uses snapshot URL."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime.begin_step(goal="Test")
# Mock snapshot with URL
mock_snapshot = MagicMock()
mock_snapshot.url = "https://snapshot-url.com"
runtime.last_snapshot = mock_snapshot
ctx = runtime._ctx()
assert ctx.url == "https://snapshot-url.com"
assert ctx.snapshot is mock_snapshot
assert ctx.step_id == runtime.step_id
def test_ctx_fallback_to_cached_url(self) -> None:
"""Test _ctx falls back to cached URL."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
runtime._cached_url = "https://cached-url.com"
runtime.begin_step(goal="Test")
ctx = runtime._ctx()
assert ctx.url == "https://cached-url.com"
assert ctx.snapshot is None
class TestAgentRuntimeFromSentienceBrowser:
"""Tests for from_sentience_browser factory method."""
@pytest.mark.asyncio
async def test_from_sentience_browser_creates_runtime(self) -> None:
"""Test from_sentience_browser creates runtime with legacy support."""
mock_browser = MagicMock()
mock_page = MagicMock()
mock_page.url = "https://example.com"
tracer = MockTracer()
with patch("sentience.backends.playwright_backend.PlaywrightBackend") as MockPWBackend:
mock_backend_instance = MagicMock()
MockPWBackend.return_value = mock_backend_instance
runtime = await AgentRuntime.from_sentience_browser(
browser=mock_browser,
page=mock_page,
tracer=tracer,
)
assert runtime.backend is mock_backend_instance
assert runtime._legacy_browser is mock_browser
assert runtime._legacy_page is mock_page
MockPWBackend.assert_called_once_with(mock_page)
@pytest.mark.asyncio
async def test_from_sentience_browser_with_api_key(self) -> None:
"""Test from_sentience_browser passes API key."""
mock_browser = MagicMock()
mock_page = MagicMock()
tracer = MockTracer()
with patch("sentience.backends.playwright_backend.PlaywrightBackend"):
runtime = await AgentRuntime.from_sentience_browser(
browser=mock_browser,
page=mock_page,
tracer=tracer,
sentience_api_key="sk_test",
)
assert runtime._snapshot_options.sentience_api_key == "sk_test"
assert runtime._snapshot_options.use_api is True
class TestAgentRuntimeFromPlaywrightPage:
"""Tests for from_playwright_page factory method."""
def test_from_playwright_page_creates_runtime(self) -> None:
"""Test from_playwright_page creates runtime with PlaywrightBackend."""
mock_page = MagicMock()
tracer = MockTracer()
with patch("sentience.backends.playwright_backend.PlaywrightBackend") as MockPWBackend:
mock_backend_instance = MagicMock()
MockPWBackend.return_value = mock_backend_instance
runtime = AgentRuntime.from_playwright_page(page=mock_page, tracer=tracer)
assert runtime.backend is mock_backend_instance
assert not hasattr(runtime, "_legacy_browser")
assert not hasattr(runtime, "_legacy_page")
MockPWBackend.assert_called_once_with(mock_page)
def test_from_playwright_page_with_api_key(self) -> None:
"""Test from_playwright_page passes API key."""
mock_page = MagicMock()
tracer = MockTracer()
with patch("sentience.backends.playwright_backend.PlaywrightBackend"):
runtime = AgentRuntime.from_playwright_page(
page=mock_page,
tracer=tracer,
sentience_api_key="sk_test",
)
assert runtime._snapshot_options.sentience_api_key == "sk_test"
assert runtime._snapshot_options.use_api is True
class TestAgentRuntimeSnapshot:
"""Tests for snapshot method."""
@pytest.mark.asyncio
async def test_snapshot_with_legacy_browser(self) -> None:
"""Test snapshot uses legacy browser when available."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
# Set up legacy browser
mock_browser = MagicMock()
mock_page = MagicMock()
mock_snapshot = MagicMock()
mock_browser.snapshot = AsyncMock(return_value=mock_snapshot)
runtime._legacy_browser = mock_browser
runtime._legacy_page = mock_page
result = await runtime.snapshot(limit=30)
mock_browser.snapshot.assert_called_once_with(mock_page, limit=30)
assert result is mock_snapshot
assert runtime.last_snapshot is mock_snapshot
class TestAgentRuntimeEndStep:
@pytest.mark.asyncio
async def test_end_step_aliases_emit_step_end(self) -> None:
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
with patch.object(runtime, "emit_step_end", new_callable=AsyncMock) as emit_mock:
emit_mock.return_value = {"ok": True}
out = await runtime.end_step(action="noop")
emit_mock.assert_awaited_once_with(action="noop")
assert out == {"ok": True}
@pytest.mark.asyncio
async def test_snapshot_with_backend(self) -> None:
"""Test snapshot uses backend-agnostic snapshot."""
backend = MockBackend()
tracer = MockTracer()
runtime = AgentRuntime(backend=backend, tracer=tracer)
mock_snapshot = MagicMock()
with patch("sentience.backends.snapshot.snapshot", new_callable=AsyncMock) as mock_snap_fn:
mock_snap_fn.return_value = mock_snapshot
result = await runtime.snapshot(goal="test goal")
mock_snap_fn.assert_called_once()
call_args = mock_snap_fn.call_args
assert call_args[0][0] is backend
assert call_args[1]["options"].goal == "test goal"
assert result is mock_snapshot
assert runtime.last_snapshot is mock_snapshot
@pytest.mark.asyncio
async def test_snapshot_merges_options(self) -> None:
"""Test snapshot merges default and call-specific options."""
backend = MockBackend()
tracer = MockTracer()
default_options = SnapshotOptions(limit=100, screenshot=True)
runtime = AgentRuntime(
backend=backend,
tracer=tracer,
snapshot_options=default_options,
)
with patch("sentience.backends.snapshot.snapshot", new_callable=AsyncMock) as mock_snap_fn:
mock_snap_fn.return_value = MagicMock()
await runtime.snapshot(goal="override goal")
call_args = mock_snap_fn.call_args
options = call_args[1]["options"]
assert options.limit == 100 # From default
assert options.screenshot is True # From default
assert options.goal == "override goal" # From call