-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_commit.py
More file actions
746 lines (616 loc) · 31.7 KB
/
test_commit.py
File metadata and controls
746 lines (616 loc) · 31.7 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
"""Tests for CommitBuilder."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from pierre_storage import GitStorage
from pierre_storage.version import get_user_agent
from pierre_storage.errors import RefUpdateError
class TestCommitBuilder:
"""Tests for CommitBuilder operations."""
@pytest.mark.asyncio
async def test_create_commit_with_string_file(self, git_storage_options: dict) -> None:
"""Test creating commit with string file."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"abc123","tree_sha":"def456","target_branch":"main","pack_bytes":1024,"blob_count":1},"result":{"success":true,"status":"ok","branch":"main","old_sha":"000000","new_sha":"abc123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="Add README",
author={"name": "Test", "email": "test@example.com"},
)
.add_file_from_string("README.md", "# Hello World")
.send()
)
assert result is not None
assert result["commit_sha"] == "abc123"
assert result["tree_sha"] == "def456"
assert result["target_branch"] == "main"
assert result["ref_update"]["branch"] == "main"
assert result["ref_update"]["new_sha"] == "abc123"
@pytest.mark.asyncio
async def test_create_commit_with_bytes(self, git_storage_options: dict) -> None:
"""Test creating commit with byte content."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"xyz789","tree_sha":"uvw456","target_branch":"main","pack_bytes":2048,"blob_count":1},"result":{"success":true,"status":"ok","branch":"main","old_sha":"abc123","new_sha":"xyz789"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="Add binary file",
author={"name": "Test", "email": "test@example.com"},
)
.add_file("data.bin", b"\x00\x01\x02\x03")
.send()
)
assert result is not None
assert result["commit_sha"] == "xyz789"
@pytest.mark.asyncio
async def test_create_commit_with_multiple_files(self, git_storage_options: dict) -> None:
"""Test creating commit with multiple files."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"multi123","tree_sha":"multi456","target_branch":"main","pack_bytes":4096,"blob_count":3},"result":{"success":true,"status":"ok","branch":"main","old_sha":"old123","new_sha":"multi123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="Multiple files",
author={"name": "Test", "email": "test@example.com"},
)
.add_file_from_string("README.md", "# Project")
.add_file_from_string("package.json", '{"name":"test"}')
.add_file("data.bin", b"\x00\x01")
.send()
)
assert result is not None
assert result["blob_count"] == 3
@pytest.mark.asyncio
async def test_create_commit_with_delete(self, git_storage_options: dict) -> None:
"""Test creating commit with file deletion."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"del123","tree_sha":"del456","target_branch":"main","pack_bytes":512,"blob_count":0},"result":{"success":true,"status":"ok","branch":"main","old_sha":"old123","new_sha":"del123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="Delete old file",
author={"name": "Test", "email": "test@example.com"},
)
.delete_path("old-file.txt")
.send()
)
assert result is not None
assert result["commit_sha"] == "del123"
@pytest.mark.asyncio
async def test_create_commit_with_expected_head(self, git_storage_options: dict) -> None:
"""Test creating commit with expected head SHA."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"new123","tree_sha":"new456","target_branch":"main","pack_bytes":1024,"blob_count":1},"result":{"success":true,"status":"ok","branch":"main","old_sha":"expected123","new_sha":"new123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
expected_head_sha="expected123",
commit_message="Safe update",
author={"name": "Test", "email": "test@example.com"},
)
.add_file_from_string("file.txt", "content")
.send()
)
assert result is not None
assert result["ref_update"]["old_sha"] == "expected123"
@pytest.mark.asyncio
async def test_create_commit_ref_update_failed(self, git_storage_options: dict) -> None:
"""Test handling ref update failure."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"fail123","tree_sha":"fail456","target_branch":"main","pack_bytes":1024,"blob_count":1},"result":{"success":false,"status":"rejected","reason":"conflict","branch":"main","old_sha":"old123","new_sha":"fail123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
with pytest.raises(RefUpdateError) as exc_info:
await (
repo.create_commit(
target_branch="main",
commit_message="Should fail",
author={"name": "Test", "email": "test@example.com"},
)
.add_file_from_string("file.txt", "content")
.send()
)
assert exc_info.value.status == "rejected"
assert (
exc_info.value.reason == "rejected"
) # reason defaults to status when not provided
@pytest.mark.asyncio
async def test_create_commit_with_custom_encoding(self, git_storage_options: dict) -> None:
"""Test creating commit with custom text encoding."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"enc123","tree_sha":"enc456","target_branch":"main","pack_bytes":1024,"blob_count":1},"result":{"success":true,"status":"ok","branch":"main","old_sha":"000000","new_sha":"enc123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="Latin-1 file",
author={"name": "Test", "email": "test@example.com"},
)
.add_file_from_string("file.txt", "café", encoding="latin-1")
.send()
)
assert result is not None
assert result["commit_sha"] == "enc123"
@pytest.mark.asyncio
async def test_create_commit_with_committer(self, git_storage_options: dict) -> None:
"""Test creating commit with separate committer."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"com123","tree_sha":"com456","target_branch":"main","pack_bytes":1024,"blob_count":1},"result":{"success":true,"status":"ok","branch":"main","old_sha":"000000","new_sha":"com123"}}'
)
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
# Mock stream() to return an async context manager
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
client_instance.stream = MagicMock(return_value=stream_context)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="Authored by one, committed by another",
author={"name": "Author", "email": "author@example.com"},
committer={"name": "Committer", "email": "committer@example.com"},
)
.add_file_from_string("file.txt", "content")
.send()
)
assert result is not None
assert result["commit_sha"] == "com123"
@pytest.mark.asyncio
async def test_create_commit_with_base_branch(self, git_storage_options: dict) -> None:
"""Test creating commit with base_branch metadata."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"deadbeef","tree_sha":"cafebabe","target_branch":"feature/one","pack_bytes":1,"blob_count":1},"result":{"success":true,"status":"ok","branch":"feature/one","old_sha":"0000000000000000000000000000000000000000","new_sha":"deadbeef"}}'
)
# Capture the request to verify base_branch is included
captured_body = None
def capture_stream(*args, **kwargs):
nonlocal captured_body
content = kwargs.get("content")
async def capture_content():
nonlocal captured_body
if content:
chunks = []
async for chunk in content:
chunks.append(chunk)
captured_body = b"".join(chunks).decode("utf-8")
# Create stream context that will capture content
stream_context = MagicMock()
async def aenter_handler(*args, **kwargs):
await capture_content()
return stream_response
stream_context.__aenter__ = AsyncMock(side_effect=aenter_handler)
stream_context.__aexit__ = AsyncMock(return_value=None)
return stream_context
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
client_instance.stream = capture_stream
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="feature/one",
base_branch="main",
expected_head_sha="abc123",
commit_message="branch off main",
author={"name": "Author", "email": "author@example.com"},
)
.add_file_from_string("docs/base.txt", "hello")
.send()
)
assert result is not None
assert result["commit_sha"] == "deadbeef"
# Verify metadata includes base_branch
assert captured_body is not None
import json
metadata_line = captured_body.split("\n")[0]
metadata = json.loads(metadata_line)["metadata"]
assert metadata["base_branch"] == "main"
assert metadata["expected_head_sha"] == "abc123"
assert metadata["target_branch"] == "feature/one"
@pytest.mark.asyncio
async def test_create_commit_base_branch_without_expected_head(
self, git_storage_options: dict
) -> None:
"""Test creating commit with base_branch but without expected_head_sha."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
# Mock the streaming response
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"abc123","tree_sha":"def456","target_branch":"feature/one","pack_bytes":1,"blob_count":1},"result":{"success":true,"status":"ok","branch":"feature/one","old_sha":"0000000000000000000000000000000000000000","new_sha":"abc123"}}'
)
# Capture the request to verify base_branch is included
captured_body = None
def capture_stream(*args, **kwargs):
nonlocal captured_body
content = kwargs.get("content")
async def capture_content():
nonlocal captured_body
if content:
chunks = []
async for chunk in content:
chunks.append(chunk)
captured_body = b"".join(chunks).decode("utf-8")
# Create stream context that will capture content
stream_context = MagicMock()
async def aenter_handler(*args, **kwargs):
await capture_content()
return stream_response
stream_context.__aenter__ = AsyncMock(side_effect=aenter_handler)
stream_context.__aexit__ = AsyncMock(return_value=None)
return stream_context
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
client_instance.stream = capture_stream
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="feature/one",
base_branch="main",
commit_message="branch off",
author={"name": "Author", "email": "author@example.com"},
)
.add_file_from_string("docs/base.txt", "hello")
.send()
)
assert result is not None
assert result["commit_sha"] == "abc123"
# Verify metadata includes base_branch but not expected_head_sha
assert captured_body is not None
import json
metadata_line = captured_body.split("\n")[0]
metadata = json.loads(metadata_line)["metadata"]
assert metadata["base_branch"] == "main"
assert "expected_head_sha" not in metadata
@pytest.mark.asyncio
async def test_create_commit_ephemeral_flags_included_in_metadata(
self, git_storage_options: dict
) -> None:
"""Ensure ephemeral options are forwarded in metadata."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"eph123","tree_sha":"eph456","target_branch":"feature/demo","pack_bytes":1,"blob_count":1},"result":{"success":true,"status":"ok","branch":"feature/demo","old_sha":"0000000000000000000000000000000000000000","new_sha":"eph123"}}'
)
captured_body = None
def capture_stream(*args, **kwargs):
nonlocal captured_body
content = kwargs.get("content")
async def capture_content():
nonlocal captured_body
if content:
chunks = []
async for chunk in content:
chunks.append(chunk)
captured_body = b"".join(chunks).decode("utf-8")
stream_context = MagicMock()
async def aenter_handler(*args, **kwargs):
await capture_content()
return stream_response
stream_context.__aenter__ = AsyncMock(side_effect=aenter_handler)
stream_context.__aexit__ = AsyncMock(return_value=None)
return stream_context
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
client_instance.stream = capture_stream
repo = await storage.create_repo(id="test-repo")
await (
repo.create_commit(
target_branch="feature/demo",
base_branch="feature/base",
ephemeral=True,
ephemeral_base=True,
commit_message="ephemeral commit",
author={"name": "Author", "email": "author@example.com"},
)
.add_file_from_string("docs/file.txt", "hello")
.send()
)
assert captured_body is not None
import json
metadata_line = captured_body.split("\n")[0]
metadata = json.loads(metadata_line)["metadata"]
assert metadata["ephemeral"] is True
assert metadata["ephemeral_base"] is True
assert metadata["base_branch"] == "feature/base"
@pytest.mark.asyncio
async def test_create_commit_ephemeral_base_requires_base_branch(
self, git_storage_options: dict
) -> None:
"""ephemeral_base should require base_branch."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
repo = await storage.create_repo(id="test-repo")
with pytest.raises(ValueError) as exc_info:
repo.create_commit(
target_branch="feature/demo",
commit_message="missing base branch",
ephemeral_base=True,
author={"name": "Author", "email": "author@example.com"},
)
assert "ephemeral_base requires base_branch" in str(exc_info.value)
@pytest.mark.asyncio
async def test_create_commit_base_branch_rejects_refs_prefix(
self, git_storage_options: dict
) -> None:
"""Test that base_branch with refs/ prefix is rejected."""
storage = GitStorage(git_storage_options)
create_response = MagicMock()
create_response.status_code = 200
create_response.is_success = True
create_response.json.return_value = {"repo_id": "test-repo"}
with patch("httpx.AsyncClient") as mock_client:
client_instance = mock_client.return_value.__aenter__.return_value
client_instance.post = AsyncMock(return_value=create_response)
repo = await storage.create_repo(id="test-repo")
with pytest.raises(ValueError) as exc_info:
repo.create_commit(
target_branch="feature/two",
base_branch="refs/heads/main",
expected_head_sha="abc123",
commit_message="branch",
author={"name": "Author", "email": "author@example.com"},
)
assert "must not include refs/ prefix" in str(exc_info.value)
@pytest.mark.asyncio
async def test_create_commit_includes_agent_header(self, git_storage_options: dict) -> None:
"""Test that createCommit includes Code-Storage-Agent header."""
from unittest.mock import AsyncMock, MagicMock, patch
storage = GitStorage(git_storage_options)
mock_response = MagicMock()
mock_response.json = AsyncMock(
return_value={"repo_id": "test-repo", "url": "https://example.com/repo.git"}
)
mock_response.status_code = 200
mock_response.is_success = True
# Mock streaming response for commit
stream_response = MagicMock()
stream_response.is_success = True
stream_response.status_code = 200
stream_response.aread = AsyncMock(
return_value=b'{"commit":{"commit_sha":"abc123","tree_sha":"def456","target_branch":"main","pack_bytes":1024,"blob_count":1},"result":{"success":true,"status":"ok","branch":"main","old_sha":"000000","new_sha":"abc123"}}'
)
captured_headers = None
with patch("httpx.AsyncClient") as mock_client:
# Setup create repo mock
mock_client.return_value.__aenter__.return_value.post = AsyncMock(
return_value=mock_response
)
# Setup stream mock
stream_context = MagicMock()
stream_context.__aenter__ = AsyncMock(return_value=stream_response)
stream_context.__aexit__ = AsyncMock(return_value=None)
def capture_stream(*args, **kwargs):
nonlocal captured_headers
captured_headers = kwargs.get("headers")
return stream_context
mock_client.return_value.__aenter__.return_value.stream = capture_stream
repo = await storage.create_repo(id="test-repo")
await (
repo.create_commit(
target_branch="main",
commit_message="Test",
author={"name": "Author", "email": "author@example.com"},
)
.add_file_from_string("test.txt", "test")
.send()
)
# Verify headers include Code-Storage-Agent
assert captured_headers is not None
assert "Code-Storage-Agent" in captured_headers
assert captured_headers["Code-Storage-Agent"] == get_user_agent()
@pytest.mark.asyncio
async def test_send_importable_on_python39(self, git_storage_options: dict) -> None:
"""Regression test: parenthesized async-with syntax is Python 3.10+ only.
Before the fix, commit.py used:
async with (
httpx.AsyncClient() as client,
client.stream(...) as response,
):
That form raises SyntaxError on Python 3.9 at import time, making the
entire module unusable despite pyproject.toml declaring requires-python>=3.9.
The fix is nested async-with statements, which are valid back to Python 3.1.
This test confirms send() completes successfully, which would be unreachable
on Python 3.9 because the module would never import.
"""
import sys
import pierre_storage.commit # must be importable without SyntaxError
assert pierre_storage.commit is not None, (
"pierre_storage.commit failed to import — likely a SyntaxError "
f"from parenthesized async-with on Python {sys.version_info}"
)
storage = GitStorage(git_storage_options)
stream_response = MagicMock()
stream_response.is_success = True
stream_response.aread = AsyncMock(
return_value=(
b'{"commit":{"commit_sha":"aaa","tree_sha":"bbb",'
b'"target_branch":"main","pack_bytes":10,"blob_count":1},'
b'"result":{"success":true,"status":"ok","branch":"main",'
b'"old_sha":"000","new_sha":"aaa"}}'
)
)
with patch("httpx.AsyncClient") as mock_client:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.is_success = True
mock_response.json.return_value = {"repo_id": "test-repo"}
mock_client.return_value.__aenter__.return_value.post = AsyncMock(
return_value=mock_response
)
stream_ctx = MagicMock()
stream_ctx.__aenter__ = AsyncMock(return_value=stream_response)
stream_ctx.__aexit__ = AsyncMock(return_value=None)
mock_client.return_value.__aenter__.return_value.stream = MagicMock(
return_value=stream_ctx
)
repo = await storage.create_repo(id="test-repo")
result = await (
repo.create_commit(
target_branch="main",
commit_message="test",
author={"name": "A", "email": "a@example.com"},
)
.add_file_from_string("f.txt", "hello")
.send()
)
assert result["commit_sha"] == "aaa"