-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_async_ops.py
More file actions
1237 lines (1015 loc) · 46.9 KB
/
test_async_ops.py
File metadata and controls
1237 lines (1015 loc) · 46.9 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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Comprehensive tests for async client classes."""
from __future__ import annotations
import io
import tarfile
from types import SimpleNamespace
from pathlib import Path
from unittest.mock import AsyncMock
import pytest
from tests.sdk.conftest import (
MockAgentView,
MockDevboxView,
MockObjectView,
MockScorerView,
MockScenarioView,
MockSnapshotView,
MockBlueprintView,
create_mock_httpx_response,
)
from runloop_api_client.sdk import (
AsyncAgent,
AsyncDevbox,
AsyncScorer,
AsyncAgentOps,
AsyncScenario,
AsyncSnapshot,
AsyncBlueprint,
AsyncDevboxOps,
AsyncScorerOps,
AsyncRunloopSDK,
AsyncScenarioOps,
AsyncSnapshotOps,
AsyncBlueprintOps,
AsyncStorageObject,
AsyncStorageObjectOps,
)
from runloop_api_client.lib.polling import PollingConfig
class TestAsyncDevboxOps:
"""Tests for AsyncDevboxOps class."""
@pytest.mark.asyncio
async def test_create(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test create method."""
mock_async_client.devboxes.create_and_await_running = AsyncMock(return_value=devbox_view)
ops = AsyncDevboxOps(mock_async_client)
devbox = await ops.create(
name="test-devbox",
metadata={"key": "value"},
polling_config=PollingConfig(timeout_seconds=60.0),
)
assert isinstance(devbox, AsyncDevbox)
assert devbox.id == "dev_123"
mock_async_client.devboxes.create_and_await_running.assert_awaited_once()
@pytest.mark.asyncio
async def test_create_from_blueprint_id(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test create_from_blueprint_id method."""
mock_async_client.devboxes.create_and_await_running = AsyncMock(return_value=devbox_view)
ops = AsyncDevboxOps(mock_async_client)
devbox = await ops.create_from_blueprint_id(
"bp_123",
name="test-devbox",
)
assert isinstance(devbox, AsyncDevbox)
call_kwargs = mock_async_client.devboxes.create_and_await_running.call_args[1]
assert call_kwargs["blueprint_id"] == "bp_123"
@pytest.mark.asyncio
async def test_create_from_blueprint_name(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test create_from_blueprint_name method."""
mock_async_client.devboxes.create_and_await_running = AsyncMock(return_value=devbox_view)
ops = AsyncDevboxOps(mock_async_client)
devbox = await ops.create_from_blueprint_name(
"my-blueprint",
name="test-devbox",
)
assert isinstance(devbox, AsyncDevbox)
call_kwargs = mock_async_client.devboxes.create_and_await_running.call_args[1]
assert call_kwargs["blueprint_name"] == "my-blueprint"
@pytest.mark.asyncio
async def test_create_from_snapshot(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test create_from_snapshot method."""
mock_async_client.devboxes.create_and_await_running = AsyncMock(return_value=devbox_view)
ops = AsyncDevboxOps(mock_async_client)
devbox = await ops.create_from_snapshot(
"snap_123",
name="test-devbox",
)
assert isinstance(devbox, AsyncDevbox)
call_kwargs = mock_async_client.devboxes.create_and_await_running.call_args[1]
assert call_kwargs["snapshot_id"] == "snap_123"
def test_from_id(self, mock_async_client: AsyncMock) -> None:
"""Test from_id method."""
ops = AsyncDevboxOps(mock_async_client)
devbox = ops.from_id("dev_123")
assert isinstance(devbox, AsyncDevbox)
assert devbox.id == "dev_123"
# Verify from_id does not wait for running status
if hasattr(mock_async_client.devboxes, "await_running"):
assert not mock_async_client.devboxes.await_running.called
@pytest.mark.asyncio
async def test_list_empty(self, mock_async_client: AsyncMock) -> None:
"""Test list method with empty results."""
page = SimpleNamespace(devboxes=[])
mock_async_client.devboxes.list = AsyncMock(return_value=page)
ops = AsyncDevboxOps(mock_async_client)
devboxes = await ops.list(limit=10, status="running")
assert len(devboxes) == 0
mock_async_client.devboxes.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_single(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test list method with single result."""
page = SimpleNamespace(devboxes=[devbox_view])
mock_async_client.devboxes.list = AsyncMock(return_value=page)
ops = AsyncDevboxOps(mock_async_client)
devboxes = await ops.list(
limit=10,
status="running",
starting_after="dev_000",
)
assert len(devboxes) == 1
assert isinstance(devboxes[0], AsyncDevbox)
assert devboxes[0].id == "dev_123"
mock_async_client.devboxes.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_multiple(self, mock_async_client: AsyncMock) -> None:
"""Test list method with multiple results."""
devbox_view1 = MockDevboxView(id="dev_001", name="devbox-1")
devbox_view2 = MockDevboxView(id="dev_002", name="devbox-2")
page = SimpleNamespace(devboxes=[devbox_view1, devbox_view2])
mock_async_client.devboxes.list = AsyncMock(return_value=page)
ops = AsyncDevboxOps(mock_async_client)
devboxes = await ops.list(limit=10, status="running")
assert len(devboxes) == 2
assert isinstance(devboxes[0], AsyncDevbox)
assert isinstance(devboxes[1], AsyncDevbox)
assert devboxes[0].id == "dev_001"
assert devboxes[1].id == "dev_002"
mock_async_client.devboxes.list.assert_awaited_once()
class TestAsyncSnapshotOps:
"""Tests for AsyncSnapshotOps class."""
@pytest.mark.asyncio
async def test_list_empty(self, mock_async_client: AsyncMock) -> None:
"""Test list method with empty results."""
page = SimpleNamespace(snapshots=[])
mock_async_client.devboxes.disk_snapshots.list = AsyncMock(return_value=page)
ops = AsyncSnapshotOps(mock_async_client)
snapshots = await ops.list(devbox_id="dev_123", limit=10)
assert len(snapshots) == 0
mock_async_client.devboxes.disk_snapshots.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_single(self, mock_async_client: AsyncMock, snapshot_view: MockSnapshotView) -> None:
"""Test list method with single result."""
page = SimpleNamespace(snapshots=[snapshot_view])
mock_async_client.devboxes.disk_snapshots.list = AsyncMock(return_value=page)
ops = AsyncSnapshotOps(mock_async_client)
snapshots = await ops.list(
devbox_id="dev_123",
limit=10,
starting_after="snap_000",
)
assert len(snapshots) == 1
assert isinstance(snapshots[0], AsyncSnapshot)
assert snapshots[0].id == "snap_123"
mock_async_client.devboxes.disk_snapshots.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_multiple(self, mock_async_client: AsyncMock) -> None:
"""Test list method with multiple results."""
snapshot_view1 = MockSnapshotView(id="snap_001", name="snapshot-1")
snapshot_view2 = MockSnapshotView(id="snap_002", name="snapshot-2")
page = SimpleNamespace(snapshots=[snapshot_view1, snapshot_view2])
mock_async_client.devboxes.disk_snapshots.list = AsyncMock(return_value=page)
ops = AsyncSnapshotOps(mock_async_client)
snapshots = await ops.list(devbox_id="dev_123", limit=10)
assert len(snapshots) == 2
assert isinstance(snapshots[0], AsyncSnapshot)
assert isinstance(snapshots[1], AsyncSnapshot)
assert snapshots[0].id == "snap_001"
assert snapshots[1].id == "snap_002"
mock_async_client.devboxes.disk_snapshots.list.assert_awaited_once()
def test_from_id(self, mock_async_client: AsyncMock) -> None:
"""Test from_id method."""
ops = AsyncSnapshotOps(mock_async_client)
snapshot = ops.from_id("snap_123")
assert isinstance(snapshot, AsyncSnapshot)
assert snapshot.id == "snap_123"
class TestAsyncBlueprintOps:
"""Tests for AsyncBlueprintOps class."""
@pytest.mark.asyncio
async def test_create(self, mock_async_client: AsyncMock, blueprint_view: MockBlueprintView) -> None:
"""Test create method."""
mock_async_client.blueprints.create_and_await_build_complete = AsyncMock(return_value=blueprint_view)
ops = AsyncBlueprintOps(mock_async_client)
blueprint = await ops.create(
name="test-blueprint",
polling_config=PollingConfig(timeout_seconds=60.0),
)
assert isinstance(blueprint, AsyncBlueprint)
assert blueprint.id == "bp_123"
mock_async_client.blueprints.create_and_await_build_complete.assert_awaited_once()
def test_from_id(self, mock_async_client: AsyncMock) -> None:
"""Test from_id method."""
ops = AsyncBlueprintOps(mock_async_client)
blueprint = ops.from_id("bp_123")
assert isinstance(blueprint, AsyncBlueprint)
assert blueprint.id == "bp_123"
@pytest.mark.asyncio
async def test_list_empty(self, mock_async_client: AsyncMock) -> None:
"""Test list method with empty results."""
page = SimpleNamespace(blueprints=[])
mock_async_client.blueprints.list = AsyncMock(return_value=page)
ops = AsyncBlueprintOps(mock_async_client)
blueprints = await ops.list(limit=10)
assert len(blueprints) == 0
mock_async_client.blueprints.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_single(self, mock_async_client: AsyncMock, blueprint_view: MockBlueprintView) -> None:
"""Test list method with single result."""
page = SimpleNamespace(blueprints=[blueprint_view])
mock_async_client.blueprints.list = AsyncMock(return_value=page)
ops = AsyncBlueprintOps(mock_async_client)
blueprints = await ops.list(
limit=10,
name="test",
starting_after="bp_000",
)
assert len(blueprints) == 1
assert isinstance(blueprints[0], AsyncBlueprint)
assert blueprints[0].id == "bp_123"
mock_async_client.blueprints.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_multiple(self, mock_async_client: AsyncMock) -> None:
"""Test list method with multiple results."""
blueprint_view1 = MockBlueprintView(id="bp_001", name="blueprint-1")
blueprint_view2 = MockBlueprintView(id="bp_002", name="blueprint-2")
page = SimpleNamespace(blueprints=[blueprint_view1, blueprint_view2])
mock_async_client.blueprints.list = AsyncMock(return_value=page)
ops = AsyncBlueprintOps(mock_async_client)
blueprints = await ops.list(limit=10)
assert len(blueprints) == 2
assert isinstance(blueprints[0], AsyncBlueprint)
assert isinstance(blueprints[1], AsyncBlueprint)
assert blueprints[0].id == "bp_001"
assert blueprints[1].id == "bp_002"
mock_async_client.blueprints.list.assert_awaited_once()
class TestAsyncStorageObjectOps:
"""Tests for AsyncStorageObjectOps class."""
@pytest.mark.asyncio
async def test_create(self, mock_async_client: AsyncMock, object_view: MockObjectView) -> None:
"""Test create method."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.create(name="test.txt", content_type="text", metadata={"key": "value"})
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
assert obj.upload_url == "https://upload.example.com/obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="test.txt",
content_type="text",
metadata={"key": "value"},
)
def test_from_id(self, mock_async_client: AsyncMock) -> None:
"""Test from_id method."""
ops = AsyncStorageObjectOps(mock_async_client)
obj = ops.from_id("obj_123")
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
assert obj.upload_url is None
@pytest.mark.asyncio
async def test_list_empty(self, mock_async_client: AsyncMock) -> None:
"""Test list method with empty results."""
page = SimpleNamespace(objects=[])
mock_async_client.objects.list = AsyncMock(return_value=page)
ops = AsyncStorageObjectOps(mock_async_client)
objects = await ops.list(limit=10)
assert len(objects) == 0
mock_async_client.objects.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_single(self, mock_async_client: AsyncMock, object_view: MockObjectView) -> None:
"""Test list method with single result."""
page = SimpleNamespace(objects=[object_view])
mock_async_client.objects.list = AsyncMock(return_value=page)
ops = AsyncStorageObjectOps(mock_async_client)
objects = await ops.list(
content_type="text",
limit=10,
name="test",
search="query",
starting_after="obj_000",
state="READ_ONLY",
)
assert len(objects) == 1
assert isinstance(objects[0], AsyncStorageObject)
assert objects[0].id == "obj_123"
mock_async_client.objects.list.assert_awaited_once_with(
content_type="text",
limit=10,
name="test",
search="query",
starting_after="obj_000",
state="READ_ONLY",
)
@pytest.mark.asyncio
async def test_list_multiple(self, mock_async_client: AsyncMock) -> None:
"""Test list method with multiple results."""
object_view1 = MockObjectView(id="obj_001", name="object-1")
object_view2 = MockObjectView(id="obj_002", name="object-2")
page = SimpleNamespace(objects=[object_view1, object_view2])
mock_async_client.objects.list = AsyncMock(return_value=page)
ops = AsyncStorageObjectOps(mock_async_client)
objects = await ops.list(limit=10)
assert len(objects) == 2
assert isinstance(objects[0], AsyncStorageObject)
assert isinstance(objects[1], AsyncStorageObject)
assert objects[0].id == "obj_001"
assert objects[1].id == "obj_002"
mock_async_client.objects.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_upload_from_file(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""Test upload_from_file method."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
temp_file = tmp_path / "test_file.txt"
temp_file.write_text("test content")
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_file(temp_file, name="test.txt")
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="test.txt",
content_type="text",
metadata=None,
ttl_ms=None,
)
http_client.put.assert_awaited_once_with(object_view.upload_url, content=b"test content")
mock_async_client.objects.complete.assert_awaited_once()
@pytest.mark.asyncio
async def test_upload_from_text(self, mock_async_client: AsyncMock, object_view: MockObjectView) -> None:
"""Test upload_from_text method."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_text("test content", name="test.txt", metadata={"key": "value"})
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="test.txt",
content_type="text",
metadata={"key": "value"},
ttl_ms=None,
)
http_client.put.assert_awaited_once_with(object_view.upload_url, content="test content")
mock_async_client.objects.complete.assert_awaited_once()
@pytest.mark.asyncio
async def test_upload_from_bytes(self, mock_async_client: AsyncMock, object_view: MockObjectView) -> None:
"""Test upload_from_bytes method."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_bytes(b"test content", name="test.bin", content_type="binary")
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="test.bin",
content_type="binary",
metadata=None,
ttl_ms=None,
)
http_client.put.assert_awaited_once_with(object_view.upload_url, content=b"test content")
mock_async_client.objects.complete.assert_awaited_once()
@pytest.mark.asyncio
async def test_upload_from_file_missing_path(self, mock_async_client: AsyncMock, tmp_path: Path) -> None:
"""upload_from_file should raise when file cannot be read."""
ops = AsyncStorageObjectOps(mock_async_client)
missing_file = tmp_path / "missing.txt"
with pytest.raises(OSError, match="Failed to read file"):
await ops.upload_from_file(missing_file)
def test_as_build_context(self, mock_async_client: AsyncMock, object_view: MockObjectView) -> None:
"""as_build_context should return the correct dict shape."""
obj = AsyncStorageObject(mock_async_client, object_view.id, upload_url=None)
assert obj.as_build_context() == {
"object_id": object_view.id,
"type": "object",
}
@pytest.mark.asyncio
async def test_upload_from_dir(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""Test upload_from_dir method."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
# Create a temporary directory with some files
test_dir = tmp_path / "test_directory"
test_dir.mkdir()
(test_dir / "file1.txt").write_text("content1")
(test_dir / "file2.txt").write_text("content2")
subdir = test_dir / "subdir"
subdir.mkdir()
(subdir / "file3.txt").write_text("content3")
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_dir(test_dir, name="archive.tar.gz", metadata={"key": "value"})
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="archive.tar.gz",
content_type="tgz",
metadata={"key": "value"},
ttl_ms=None,
)
# Verify that put was called with tarball content
http_client.put.assert_awaited_once()
call_args = http_client.put.call_args
assert call_args[0][0] == object_view.upload_url
# Verify it's a valid gzipped tarball
uploaded_content = call_args[1]["content"]
with tarfile.open(fileobj=io.BytesIO(uploaded_content), mode="r:gz") as tar:
members = tar.getmembers()
member_names = [m.name for m in members]
# Should contain our test files (may include directory entries)
assert any("file1.txt" in name for name in member_names)
assert any("file2.txt" in name for name in member_names)
assert any("file3.txt" in name for name in member_names)
mock_async_client.objects.complete.assert_awaited_once()
@pytest.mark.asyncio
async def test_upload_from_dir_with_inline_ignore_patterns(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""upload_from_dir should respect inline ignore patterns."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
test_dir = tmp_path / "ctx"
test_dir.mkdir()
(test_dir / "keep.txt").write_text("keep", encoding="utf-8")
(test_dir / "ignore.log").write_text("ignore", encoding="utf-8")
build_dir = test_dir / "build"
build_dir.mkdir()
(build_dir / "ignored.txt").write_text("ignored", encoding="utf-8")
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
client = AsyncStorageObjectOps(mock_async_client)
# Tar filter: drop logs and anything under build/
def ignore_logs_and_build(ti: tarfile.TarInfo) -> tarfile.TarInfo | None:
if ti.name.endswith(".log") or ti.name.startswith("build/"):
return None
return ti
obj = await client.upload_from_dir(test_dir, ignore=ignore_logs_and_build)
assert isinstance(obj, AsyncStorageObject)
uploaded_content = http_client.put.call_args[1]["content"]
with tarfile.open(fileobj=io.BytesIO(uploaded_content), mode="r:gz") as tar:
names = {m.name for m in tar.getmembers()}
assert "keep.txt" in names
assert "ignore.log" not in names
assert not any(name.startswith("build/") for name in names)
@pytest.mark.asyncio
async def test_upload_from_dir_default_name(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""Test upload_from_dir uses directory name by default."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
test_dir = tmp_path / "my_folder"
test_dir.mkdir()
(test_dir / "file.txt").write_text("content")
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_dir(test_dir)
assert isinstance(obj, AsyncStorageObject)
# Name should be directory name + .tar.gz
mock_async_client.objects.create.assert_awaited_once_with(
name="my_folder.tar.gz",
content_type="tgz",
metadata=None,
ttl_ms=None,
)
@pytest.mark.asyncio
async def test_upload_from_dir_with_ttl(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""Test upload_from_dir with TTL."""
from datetime import timedelta
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
test_dir = tmp_path / "temp_dir"
test_dir.mkdir()
(test_dir / "file.txt").write_text("temporary content")
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_dir(test_dir, ttl=timedelta(hours=2))
assert isinstance(obj, AsyncStorageObject)
mock_async_client.objects.create.assert_awaited_once_with(
name="temp_dir.tar.gz",
content_type="tgz",
metadata=None,
ttl_ms=7200000, # 2 hours = 7200 seconds = 7200000 milliseconds
)
@pytest.mark.asyncio
async def test_upload_from_dir_empty_directory(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""Test upload_from_dir with empty directory."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
test_dir = tmp_path / "empty_dir"
test_dir.mkdir()
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
obj = await ops.upload_from_dir(test_dir)
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="empty_dir.tar.gz",
content_type="tgz",
metadata=None,
ttl_ms=None,
)
http_client.put.assert_awaited_once()
mock_async_client.objects.complete.assert_awaited_once()
@pytest.mark.asyncio
async def test_upload_from_dir_with_string_path(
self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path
) -> None:
"""Test upload_from_dir with string path instead of Path object."""
mock_async_client.objects.create = AsyncMock(return_value=object_view)
mock_async_client.objects.complete = AsyncMock(return_value=object_view)
test_dir = tmp_path / "string_path_dir"
test_dir.mkdir()
(test_dir / "file.txt").write_text("content")
http_client = AsyncMock()
mock_response = create_mock_httpx_response()
http_client.put = AsyncMock(return_value=mock_response)
mock_async_client._client = http_client
ops = AsyncStorageObjectOps(mock_async_client)
# Pass string path instead of Path object
obj = await ops.upload_from_dir(str(test_dir))
assert isinstance(obj, AsyncStorageObject)
assert obj.id == "obj_123"
mock_async_client.objects.create.assert_awaited_once_with(
name="string_path_dir.tar.gz",
content_type="tgz",
metadata=None,
ttl_ms=None,
)
http_client.put.assert_awaited_once()
mock_async_client.objects.complete.assert_awaited_once()
class TestAsyncScorerOps:
"""Tests for AsyncScorerOps class."""
@pytest.mark.asyncio
async def test_create(self, mock_async_client: AsyncMock, scorer_view: MockScorerView) -> None:
"""Test create method."""
mock_async_client.scenarios.scorers.create = AsyncMock(return_value=scorer_view)
ops = AsyncScorerOps(mock_async_client)
scorer = await ops.create(
bash_script="echo 'score=1.0'",
type="test_scorer",
)
assert isinstance(scorer, AsyncScorer)
assert scorer.id == "scorer_123"
mock_async_client.scenarios.scorers.create.assert_awaited_once()
def test_from_id(self, mock_async_client: AsyncMock) -> None:
"""Test from_id method."""
ops = AsyncScorerOps(mock_async_client)
scorer = ops.from_id("scorer_123")
assert isinstance(scorer, AsyncScorer)
assert scorer.id == "scorer_123"
@pytest.mark.asyncio
async def test_list_empty(self, mock_async_client: AsyncMock) -> None:
"""Test list method with empty results."""
async def async_iter():
return
yield # Make this a generator
mock_async_client.scenarios.scorers.list = AsyncMock(return_value=async_iter())
ops = AsyncScorerOps(mock_async_client)
scorers = await ops.list(limit=10)
assert len(scorers) == 0
mock_async_client.scenarios.scorers.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_single(self, mock_async_client: AsyncMock, scorer_view: MockScorerView) -> None:
"""Test list method with single result."""
async def async_iter():
yield scorer_view
mock_async_client.scenarios.scorers.list = AsyncMock(return_value=async_iter())
ops = AsyncScorerOps(mock_async_client)
scorers = await ops.list(
limit=10,
starting_after="scorer_000",
)
assert len(scorers) == 1
assert isinstance(scorers[0], AsyncScorer)
assert scorers[0].id == "scorer_123"
mock_async_client.scenarios.scorers.list.assert_awaited_once()
@pytest.mark.asyncio
async def test_list_multiple(self, mock_async_client: AsyncMock) -> None:
"""Test list method with multiple results."""
scorer_view1 = MockScorerView(id="scorer_001", type="scorer-1")
scorer_view2 = MockScorerView(id="scorer_002", type="scorer-2")
async def async_iter():
yield scorer_view1
yield scorer_view2
mock_async_client.scenarios.scorers.list = AsyncMock(return_value=async_iter())
ops = AsyncScorerOps(mock_async_client)
scorers = await ops.list(limit=10)
assert len(scorers) == 2
assert isinstance(scorers[0], AsyncScorer)
assert isinstance(scorers[1], AsyncScorer)
assert scorers[0].id == "scorer_001"
assert scorers[1].id == "scorer_002"
mock_async_client.scenarios.scorers.list.assert_awaited_once()
class TestAsyncAgentClient:
"""Tests for AsyncAgentClient class."""
@pytest.mark.asyncio
async def test_create(self, mock_async_client: AsyncMock, agent_view: MockAgentView) -> None:
"""Test create method."""
mock_async_client.agents.create = AsyncMock(return_value=agent_view)
client = AsyncAgentOps(mock_async_client)
agent = await client.create(
name="test-agent",
version="1.2.3",
)
assert isinstance(agent, AsyncAgent)
assert agent.id == "agent_123"
mock_async_client.agents.create.assert_called_once()
def test_from_id(self, mock_async_client: AsyncMock) -> None:
"""Test from_id method."""
client = AsyncAgentOps(mock_async_client)
agent = client.from_id("agent_123")
assert isinstance(agent, AsyncAgent)
assert agent.id == "agent_123"
@pytest.mark.asyncio
async def test_list(self, mock_async_client: AsyncMock) -> None:
"""Test list method."""
# Create three agent views with different data
agent_view_1 = MockAgentView(
id="agent_001",
name="first-agent",
create_time_ms=1234567890000,
is_public=False,
source=None,
)
agent_view_2 = MockAgentView(
id="agent_002",
name="second-agent",
create_time_ms=1234567891000,
is_public=True,
source={"type": "git", "git": {"repository": "https://github.com/example/repo"}},
)
agent_view_3 = MockAgentView(
id="agent_003",
name="third-agent",
create_time_ms=1234567892000,
is_public=False,
source={"type": "npm", "npm": {"package_name": "example-package"}},
)
page = SimpleNamespace(agents=[agent_view_1, agent_view_2, agent_view_3])
mock_async_client.agents.list = AsyncMock(return_value=page)
# Mock retrieve to return the corresponding agent_view when called
async def mock_retrieve(agent_id: str):
if agent_id == "agent_001":
return agent_view_1
elif agent_id == "agent_002":
return agent_view_2
elif agent_id == "agent_003":
return agent_view_3
return None
mock_async_client.agents.retrieve = AsyncMock(side_effect=mock_retrieve)
client = AsyncAgentOps(mock_async_client)
agents = await client.list(
limit=10,
starting_after="agent_000",
)
# Verify we got three agents
assert len(agents) == 3
assert all(isinstance(agent, AsyncAgent) for agent in agents)
# Verify the agent IDs
assert agents[0].id == "agent_001"
assert agents[1].id == "agent_002"
assert agents[2].id == "agent_003"
# Test that get_info() retrieves the AgentView for the first agent
info = await agents[0].get_info()
assert info.id == "agent_001"
assert info.name == "first-agent"
assert info.create_time_ms == 1234567890000
assert info.is_public is False
assert info.source is None
# Test that get_info() retrieves the AgentView for the second agent
info = await agents[1].get_info()
assert info.id == "agent_002"
assert info.name == "second-agent"
assert info.create_time_ms == 1234567891000
assert info.is_public is True
assert info.source == {"type": "git", "git": {"repository": "https://github.com/example/repo"}}
# Test that get_info() retrieves the AgentView for the third agent
info = await agents[2].get_info()
assert info.id == "agent_003"
assert info.name == "third-agent"
assert info.create_time_ms == 1234567892000
assert info.is_public is False
assert info.source == {"type": "npm", "npm": {"package_name": "example-package"}}
# Verify that agents.retrieve was called three times (once for each get_info)
assert mock_async_client.agents.retrieve.call_count == 3
mock_async_client.agents.list.assert_called_once()
@pytest.mark.asyncio
async def test_create_from_npm(self, mock_async_client: AsyncMock, agent_view: MockAgentView) -> None:
"""Test create_from_npm factory method."""
mock_async_client.agents.create = AsyncMock(return_value=agent_view)
client = AsyncAgentOps(mock_async_client)
agent = await client.create_from_npm(
name="test-agent",
package_name="@runloop/example-agent",
version="1.2.3",
)
assert isinstance(agent, AsyncAgent)
assert agent.id == "agent_123"
mock_async_client.agents.create.assert_awaited_once_with(
source={
"type": "npm",
"npm": {
"package_name": "@runloop/example-agent",
},
},
name="test-agent",
version="1.2.3",
)
@pytest.mark.asyncio
async def test_create_from_npm_with_all_options(
self, mock_async_client: AsyncMock, agent_view: MockAgentView
) -> None:
"""Test create_from_npm factory method with all optional parameters."""
mock_async_client.agents.create = AsyncMock(return_value=agent_view)
client = AsyncAgentOps(mock_async_client)
agent = await client.create_from_npm(
name="test-agent",
package_name="@runloop/example-agent",
registry_url="https://registry.example.com",
agent_setup=["npm install", "npm run setup"],
version="1.2.3",
extra_headers={"X-Custom": "header"},
)
assert isinstance(agent, AsyncAgent)
assert agent.id == "agent_123"
mock_async_client.agents.create.assert_awaited_once_with(
source={
"type": "npm",
"npm": {
"package_name": "@runloop/example-agent",
"registry_url": "https://registry.example.com",
"agent_setup": ["npm install", "npm run setup"],
},
},
name="test-agent",
version="1.2.3",
extra_headers={"X-Custom": "header"},
)
@pytest.mark.asyncio
async def test_create_from_npm_raises_when_source_provided(self, mock_async_client: AsyncMock) -> None:
"""Test create_from_npm raises ValueError when source is provided in params."""
client = AsyncAgentOps(mock_async_client)
with pytest.raises(ValueError, match="Cannot specify 'source' when using create_from_npm"):
await client.create_from_npm(
name="test-agent",
package_name="@runloop/example-agent",
version="1.2.3",
source={"type": "git", "git": {"repository": "https://github.com/example/repo"}},
)
@pytest.mark.asyncio
async def test_create_from_pip(self, mock_async_client: AsyncMock, agent_view: MockAgentView) -> None:
"""Test create_from_pip factory method."""
mock_async_client.agents.create = AsyncMock(return_value=agent_view)
client = AsyncAgentOps(mock_async_client)
agent = await client.create_from_pip(
name="test-agent",
package_name="runloop-example-agent",
version="1.2.3",
)
assert isinstance(agent, AsyncAgent)
assert agent.id == "agent_123"
mock_async_client.agents.create.assert_awaited_once_with(
source={
"type": "pip",
"pip": {
"package_name": "runloop-example-agent",
},
},
name="test-agent",
version="1.2.3",
)
@pytest.mark.asyncio
async def test_create_from_pip_with_all_options(
self, mock_async_client: AsyncMock, agent_view: MockAgentView
) -> None:
"""Test create_from_pip factory method with all optional parameters."""
mock_async_client.agents.create = AsyncMock(return_value=agent_view)
client = AsyncAgentOps(mock_async_client)
agent = await client.create_from_pip(