forked from trycua/cua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_interfaces.py
More file actions
945 lines (717 loc) · 36.8 KB
/
Copy pathtest_interfaces.py
File metadata and controls
945 lines (717 loc) · 36.8 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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
"""Integration tests for every sandbox interface method across all guest OS types.
Imports exclusively from `cua` (the meta-package). Tests auto-skip via
skip_if_unsupported() when the required local runtime is absent.
Interface coverage:
sb.shell — Shell.run()
sb.mouse — click, right_click, double_click, move, scroll, mouse_down, mouse_up, drag
sb.keyboard — type, keypress, key_down, key_up
sb.screen — screenshot, screenshot_base64, size
sb.clipboard — get, set
sb.tunnel — forward (single, multi, await form)
sb.terminal — create, send_input, resize, close
sb.window — get_active_title
sb.mobile — tap, long_press, double_tap, type_text, swipe, scroll_up/down/left/right,
fling, gesture, pinch_in, pinch_out, key, home, back, recents, power,
volume_up, volume_down, enter, backspace, wake, notifications,
close_notifications
Guest OS coverage:
Linux container (Image.linux()) — shell, clipboard, tunnel, terminal
Linux VM (Image.linux(kind="vm")) — + screen, mouse, keyboard, window
macOS VM (Image.macos()) — shell, screen, mouse, keyboard, clipboard,
tunnel, terminal, window
Windows VM (Image.windows()) — shell, screen, mouse, keyboard, clipboard,
tunnel, terminal, window
Android VM (Image.android()) — shell, screen, mobile.*
Run with:
pytest tests/test_interfaces.py -v
pytest tests/test_interfaces.py -v -k "linux and shell"
"""
from __future__ import annotations
import asyncio
import base64
import httpx
import pytest
from cua import Image, Sandbox, skip_if_unsupported
# ── helpers ──────────────────────────────────────────────────────────────────
async def _shell(sb, cmd: str, timeout: int = 30) -> str:
r = await sb.shell.run(cmd, timeout=timeout)
assert r.success, f"Command failed (rc={r.returncode}): {r.stderr}"
return r.stdout.strip()
async def _start_http_server(sb, port: int) -> None:
"""Start a minimal Python HTTP echo server inside the sandbox.
Uses python3 with POSIX background operator on Linux/macOS.
macOS VMs ship python3; Linux containers have it in the base image.
"""
server_code = (
"import http.server as h, socketserver as ss; "
f"ss.TCPServer(('0.0.0.0',{port}),"
"type('H',(h.BaseHTTPRequestHandler,),"
"{'do_GET':lambda s:(s.send_response(200),s.send_header('Content-Type','text/plain')"
",s.end_headers(),s.wfile.write(b'ok'))})"
").serve_forever()"
)
await sb.shell.run(f'python3 -c "{server_code}" &', timeout=5)
await asyncio.sleep(1)
# ═══════════════════════════════════════════════════════════════════════════
# Shell — Linux container
# ═══════════════════════════════════════════════════════════════════════════
class TestShellLinux:
_IMAGE = Image.linux()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_run_success(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo hello")
assert r.success
assert r.returncode == 0
assert "hello" in r.stdout
async def test_run_failure_returncode(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("false")
assert not r.success
assert r.returncode != 0
async def test_run_stderr(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("ls /no_such_dir_xyz 2>&1; true")
# At minimum the command ran
assert r.returncode is not None
async def test_run_pipeline(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo hello | tr a-z A-Z")
assert r.success
assert "HELLO" in r.stdout
async def test_run_multiline_output(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("printf 'a\\nb\\nc\\n'")
assert r.success
assert r.stdout.strip().splitlines() == ["a", "b", "c"]
async def test_run_sequential_state(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r1 = await sb.shell.run("echo persistent > /tmp/st.txt")
assert r1.success
r2 = await sb.shell.run("cat /tmp/st.txt")
assert r2.success
assert "persistent" in r2.stdout
async def test_run_timeout(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
with pytest.raises(BaseException): # noqa: B017
await sb.shell.run("sleep 60", timeout=2)
async def test_commandresult_fields(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo out; echo err >&2; exit 0")
assert isinstance(r.stdout, str)
assert isinstance(r.stderr, str)
assert isinstance(r.returncode, int)
assert r.success is True
class TestShellMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_run_success(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo hello macos")
assert r.success
assert "hello" in r.stdout
async def test_run_pipeline(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo hello | tr a-z A-Z")
assert r.success
assert "HELLO" in r.stdout
class TestShellWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_run_success(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo hello windows")
assert r.success
assert "hello" in r.stdout
async def test_run_failure(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("exit 1")
assert not r.success
class TestShellAndroid:
_IMAGE = Image.android()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_run_adb_shell(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("echo hello android")
assert r.success
assert "hello" in r.stdout
async def test_run_getprop(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
r = await sb.shell.run("getprop ro.build.version.release")
assert r.success
assert r.stdout.strip() # non-empty version string
# ═══════════════════════════════════════════════════════════════════════════
# Clipboard — Linux container, macOS, Windows
# ═══════════════════════════════════════════════════════════════════════════
class TestClipboardLinux:
_IMAGE = Image.linux()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_set_and_get(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.clipboard.set("hello clipboard")
text = await sb.clipboard.get()
assert text == "hello clipboard"
async def test_overwrite(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.clipboard.set("first")
await sb.clipboard.set("second")
assert await sb.clipboard.get() == "second"
async def test_unicode(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
payload = "héllo wörld 🌸"
await sb.clipboard.set(payload)
assert await sb.clipboard.get() == payload
async def test_multiline(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
payload = "line1\nline2\nline3"
await sb.clipboard.set(payload)
assert await sb.clipboard.get() == payload
async def test_empty_string(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.clipboard.set("non-empty")
await sb.clipboard.set("")
assert await sb.clipboard.get() == ""
class TestClipboardMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_set_and_get(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.clipboard.set("macos clipboard")
assert await sb.clipboard.get() == "macos clipboard"
async def test_unicode(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
payload = "café ☕"
await sb.clipboard.set(payload)
assert await sb.clipboard.get() == payload
class TestClipboardWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_set_and_get(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.clipboard.set("windows clipboard")
assert await sb.clipboard.get() == "windows clipboard"
# ═══════════════════════════════════════════════════════════════════════════
# Screen — Linux VM, macOS, Windows, Android
# ═══════════════════════════════════════════════════════════════════════════
_LINUX_VM = Image.linux(kind="vm")
class TestScreenLinuxVM:
def setup_method(self):
skip_if_unsupported(_LINUX_VM)
async def test_screenshot_returns_bytes(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
data = await sb.screen.screenshot()
assert isinstance(data, bytes) and len(data) > 0
async def test_screenshot_png_magic(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
data = await sb.screen.screenshot(format="png")
assert data[:8] == b"\x89PNG\r\n\x1a\n"
async def test_screenshot_jpeg(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
data = await sb.screen.screenshot(format="jpeg", quality=80)
assert data[:2] == b"\xff\xd8"
async def test_screenshot_base64(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
b64 = await sb.screen.screenshot_base64()
assert isinstance(b64, str)
assert len(base64.b64decode(b64)) > 0
async def test_size(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
assert isinstance(w, int) and w >= 640
assert isinstance(h, int) and h >= 480
class TestScreenMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_screenshot_returns_bytes(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
data = await sb.screen.screenshot()
assert isinstance(data, bytes) and len(data) > 0
async def test_screenshot_png_magic(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
data = await sb.screen.screenshot(format="png")
assert data[:8] == b"\x89PNG\r\n\x1a\n"
async def test_screenshot_base64(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
b64 = await sb.screen.screenshot_base64()
decoded = base64.b64decode(b64)
assert len(decoded) > 0
async def test_size(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
assert w >= 640 and h >= 480
class TestScreenWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_screenshot_returns_bytes(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
data = await sb.screen.screenshot()
assert isinstance(data, bytes) and len(data) > 0
async def test_size(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
assert w > 0 and h > 0
class TestScreenAndroid:
_IMAGE = Image.android()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_screenshot_returns_bytes(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
data = await sb.screen.screenshot()
assert isinstance(data, bytes) and len(data) > 0
async def test_size(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
assert w > 0 and h > 0
# ═══════════════════════════════════════════════════════════════════════════
# Mouse — Linux VM, macOS, Windows
# ═══════════════════════════════════════════════════════════════════════════
class TestMouseLinuxVM:
def setup_method(self):
skip_if_unsupported(_LINUX_VM)
async def test_click(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.click(w // 2, h // 2) # must not raise
async def test_right_click(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.right_click(w // 2, h // 2)
async def test_double_click(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.double_click(w // 2, h // 2)
async def test_move(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.mouse.move(100, 100)
await sb.mouse.move(200, 200)
async def test_scroll(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.scroll(w // 2, h // 2, scroll_y=3)
async def test_mouse_down_and_up(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.mouse_down(w // 2, h // 2)
await sb.mouse.mouse_up(w // 2, h // 2)
async def test_drag(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.drag(100, h // 2, 300, h // 2)
class TestMouseMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_click(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.click(w // 2, h // 2)
async def test_right_click(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.right_click(w // 2, h // 2)
async def test_double_click(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.double_click(w // 2, h // 2)
async def test_move(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mouse.move(100, 100)
async def test_scroll(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.scroll(w // 2, h // 2, scroll_y=3)
async def test_mouse_down_and_up(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.mouse_down(w // 2, h // 2)
await sb.mouse.mouse_up(w // 2, h // 2)
async def test_drag(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.drag(100, h // 2, 300, h // 2)
class TestMouseWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_click(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.click(w // 2, h // 2)
async def test_right_click(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.right_click(w // 2, h // 2)
async def test_double_click(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.double_click(w // 2, h // 2)
async def test_move(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mouse.move(100, 100)
async def test_scroll(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.scroll(w // 2, h // 2, scroll_y=3)
async def test_mouse_down_and_up(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.mouse_down(w // 2, h // 2)
await sb.mouse.mouse_up(w // 2, h // 2)
async def test_drag(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mouse.drag(100, h // 2, 300, h // 2)
# ═══════════════════════════════════════════════════════════════════════════
# Keyboard — Linux VM, macOS, Windows
# ═══════════════════════════════════════════════════════════════════════════
class TestKeyboardLinuxVM:
def setup_method(self):
skip_if_unsupported(_LINUX_VM)
async def test_type(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.keyboard.type("hello") # must not raise
async def test_keypress_single(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.keyboard.keypress("enter")
async def test_keypress_combination(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.keyboard.keypress(["ctrl", "a"])
async def test_keypress_string_list(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.keyboard.keypress(["shift", "tab"])
async def test_key_down_and_up(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.keyboard.key_down("shift")
await sb.keyboard.key_up("shift")
async def test_type_unicode(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
await sb.keyboard.type("héllo")
class TestKeyboardMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_type(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.type("hello macos")
async def test_keypress_single(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.keypress("enter")
async def test_keypress_combination(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.keypress(["cmd", "a"])
async def test_key_down_and_up(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.key_down("shift")
await sb.keyboard.key_up("shift")
class TestKeyboardWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_type(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.type("hello windows")
async def test_keypress_single(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.keypress("enter")
async def test_keypress_combination(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.keypress(["ctrl", "a"])
async def test_key_down_and_up(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.keyboard.key_down("shift")
await sb.keyboard.key_up("shift")
# ═══════════════════════════════════════════════════════════════════════════
# Tunnel — Linux container, macOS, Windows
# ═══════════════════════════════════════════════════════════════════════════
class TestTunnelLinux:
_IMAGE = Image.linux()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_forward_single_context_manager(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await _start_http_server(sb, 8888)
async with sb.tunnel.forward(8888) as t:
assert t.host
assert t.port > 0
assert t.sandbox_port == 8888
assert t.url.startswith("http://")
async with httpx.AsyncClient() as client:
r = await client.get(t.url, timeout=5)
assert r.status_code == 200
async def test_tunnel_info_repr(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await _start_http_server(sb, 8889)
async with sb.tunnel.forward(8889) as t:
assert "8889" in repr(t)
async def test_forward_multiple_ports(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await _start_http_server(sb, 8890)
await _start_http_server(sb, 8891)
async with sb.tunnel.forward(8890, 8891) as tunnels:
assert 8890 in tunnels
assert 8891 in tunnels
async with httpx.AsyncClient() as client:
for t in tunnels.values():
r = await client.get(t.url, timeout=5)
assert r.status_code == 200
async def test_forward_await_form(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await _start_http_server(sb, 8892)
t = await sb.tunnel.forward(8892)
try:
async with httpx.AsyncClient() as client:
r = await client.get(t.url, timeout=5)
assert r.status_code == 200
finally:
await t.close()
async def test_forward_no_ports_raises(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
with pytest.raises(ValueError):
sb.tunnel.forward()
class TestTunnelMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_forward_single(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await _start_http_server(sb, 8888)
async with sb.tunnel.forward(8888) as t:
assert t.sandbox_port == 8888
async with httpx.AsyncClient() as client:
r = await client.get(t.url, timeout=5)
assert r.status_code == 200
class TestTunnelWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_forward_single(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await _start_http_server(sb, 8888)
async with sb.tunnel.forward(8888) as t:
async with httpx.AsyncClient() as client:
r = await client.get(t.url, timeout=5)
assert r.status_code == 200
# ═══════════════════════════════════════════════════════════════════════════
# Terminal (PTY) — Linux container, macOS, Windows
# ═══════════════════════════════════════════════════════════════════════════
class TestTerminalLinux:
_IMAGE = Image.linux()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_create_returns_pid(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create()
assert "pid" in info and isinstance(info["pid"], int)
await sb.terminal.close(info["pid"])
async def test_create_custom_shell(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create(shell="sh")
assert info["pid"] > 0
await sb.terminal.close(info["pid"])
async def test_create_custom_dimensions(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create(cols=120, rows=40)
assert info.get("cols") == 120
assert info.get("rows") == 40
await sb.terminal.close(info["pid"])
async def test_send_input(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create()
await sb.terminal.send_input(info["pid"], "echo hello\n")
await asyncio.sleep(0.3)
await sb.terminal.close(info["pid"])
async def test_resize(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create(cols=80, rows=24)
await sb.terminal.resize(info["pid"], cols=160, rows=48)
await sb.terminal.close(info["pid"])
async def test_close_returns_exit_code_or_none(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create()
await sb.terminal.send_input(info["pid"], "exit 0\n")
await asyncio.sleep(0.3)
result = await sb.terminal.close(info["pid"])
assert result is None or isinstance(result, int)
async def test_multiple_sessions_independent(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
a = await sb.terminal.create()
b = await sb.terminal.create()
assert a["pid"] != b["pid"]
await sb.terminal.close(a["pid"])
await sb.terminal.close(b["pid"])
class TestTerminalMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_create_and_close(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create()
assert info["pid"] > 0
await sb.terminal.send_input(info["pid"], "echo hi\n")
await asyncio.sleep(0.3)
await sb.terminal.close(info["pid"])
async def test_resize(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create(cols=80, rows=24)
await sb.terminal.resize(info["pid"], 120, 40)
await sb.terminal.close(info["pid"])
class TestTerminalWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_create_and_close(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create(shell="cmd.exe")
assert info["pid"] > 0
await sb.terminal.close(info["pid"])
async def test_send_input(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
info = await sb.terminal.create(shell="cmd.exe")
await sb.terminal.send_input(info["pid"], "echo hello\r\n")
await asyncio.sleep(0.5)
await sb.terminal.close(info["pid"])
# ═══════════════════════════════════════════════════════════════════════════
# Window — Linux VM, macOS, Windows
# ═══════════════════════════════════════════════════════════════════════════
class TestWindowLinuxVM:
def setup_method(self):
skip_if_unsupported(_LINUX_VM)
async def test_get_active_title_returns_string(self):
async with Sandbox.ephemeral(_LINUX_VM, local=True) as sb:
title = await sb.window.get_active_title()
assert isinstance(title, str)
class TestWindowMacOS:
_IMAGE = Image.macos()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_get_active_title_returns_string(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
title = await sb.window.get_active_title()
assert isinstance(title, str)
class TestWindowWindows:
_IMAGE = Image.windows()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def test_get_active_title_returns_string(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
title = await sb.window.get_active_title()
assert isinstance(title, str)
# ═══════════════════════════════════════════════════════════════════════════
# Mobile — Android only
# ═══════════════════════════════════════════════════════════════════════════
class TestMobileAndroid:
_IMAGE = Image.android()
def setup_method(self):
skip_if_unsupported(self._IMAGE)
async def _sb(self):
return Sandbox.ephemeral(self._IMAGE, local=True)
# Touch
async def test_tap(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.tap(w // 2, h // 2)
async def test_long_press(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.long_press(w // 2, h // 2, duration_ms=500)
async def test_double_tap(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.double_tap(w // 2, h // 2)
async def test_type_text(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.type_text("hello")
# Gestures
async def test_swipe(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.swipe(w // 2, h * 3 // 4, w // 2, h // 4)
async def test_scroll_up(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.scroll_up(w // 2, h // 2)
async def test_scroll_down(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.scroll_down(w // 2, h // 2)
async def test_scroll_left(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.scroll_left(w // 2, h // 2)
async def test_scroll_right(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.scroll_right(w // 2, h // 2)
async def test_fling(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.fling(w // 2, h * 3 // 4, w // 2, h // 4)
async def test_pinch_in(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.pinch_in(w // 2, h // 2, spread=200)
async def test_pinch_out(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
await sb.mobile.pinch_out(w // 2, h // 2, spread=200)
async def test_gesture_two_finger(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
w, h = await sb.screen.size()
cx, cy = w // 2, h // 2
await sb.mobile.gesture(
(cx - 20, cy),
(cx - 200, cy),
(cx + 20, cy),
(cx + 200, cy),
)
async def test_gesture_invalid_raises(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
with pytest.raises(ValueError):
await sb.mobile.gesture((100, 100), (200, 200), (300, 300))
# Hardware keys
async def test_key_home(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.home()
async def test_key_back(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.back()
async def test_key_recents(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.recents()
async def test_key_volume_up(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.volume_up()
async def test_key_volume_down(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.volume_down()
async def test_key_enter(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.enter()
async def test_key_backspace(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.backspace()
async def test_key_power(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.power()
async def test_key_arbitrary(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.key(3) # HOME keycode
# System
async def test_wake(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.wake()
async def test_notifications(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.notifications()
async def test_close_notifications(self):
async with Sandbox.ephemeral(self._IMAGE, local=True) as sb:
await sb.mobile.close_notifications()