FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/tests/basic/test_coder.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
12
(12)
Pull requests
7
(7)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
tests
/
basic
/
test_coder.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
1923 lines (1524 loc) · 72.9 KB
Breadcrumbs
cecli
/
tests
/
basic
/
test_coder.py
Copy path
File metadata and controls
1923 lines (1524 loc) · 72.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
base64
import
os
import
tempfile
import
uuid
from
pathlib
import
Path
from
unittest
.
mock
import
ANY
,
AsyncMock
,
MagicMock
,
patch
import
git
import
pytest
from
cecli
.
coders
import
Coder
from
cecli
.
coders
.
base_coder
import
FinishReasonLength
,
UnknownEditFormat
from
cecli
.
commands
import
SwitchCoderSignal
from
cecli
.
dump
import
dump
# noqa: F401
from
cecli
.
helpers
.
conversation
import
ConversationService
from
cecli
.
helpers
.
conversation
.
tags
import
MessageTag
from
cecli
.
io
import
InputOutput
from
cecli
.
mcp
import
McpServerManager
from
cecli
.
models
import
Model
from
cecli
.
repo
import
GitRepo
,
GitRepoProxy
from
cecli
.
sendchat
import
sanity_check_messages
from
cecli
.
utils
import
GitTemporaryDirectory
class
MockCoder
:
"""Simple mock coder class for tests."""
def
__init__
(
self
):
self
.
uuid
=
str
(
uuid
.
uuid4
())
class
TestCoder
:
@
pytest
.
fixture
(
autouse
=
True
)
def
setup
(
self
,
gpt35_model
):
self
.
uuid
=
str
(
uuid
.
uuid4
())
self
.
GPT35
=
gpt35_model
self
.
webbrowser_patcher
=
patch
(
"webbrowser.open"
)
self
.
mock_webbrowser
=
self
.
webbrowser_patcher
.
start
()
# Reset conversation system before each test
ConversationService
.
get_chunks
(
self
).
reset
()
# Reset per-root singletons for test isolation
from
cecli
.
helpers
.
file_system
import
FileSystemService
FileSystemService
.
reset_instance
()
GitRepoProxy
.
reset_instances
()
yield
# Cleanup after each test
self
.
webbrowser_patcher
.
stop
()
# Reset conversation system after each test as well
ConversationService
.
get_chunks
(
self
).
reset
()
async
def
test_stream_uses_active_model_streaming_support
(
self
):
"""self.stream must follow the active model's streaming capability."""
with
GitTemporaryDirectory
():
io
=
MagicMock
()
active_model
=
Model
(
self
.
GPT35
.
name
)
active_model
.
streaming
=
False
with
patch
.
object
(
Coder
,
"get_active_model"
,
return_value
=
active_model
):
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
stream
=
True
)
assert
coder
.
stream
is
False
async
def
test_allowed_to_edit
(
self
):
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname
=
Path
(
"added.txt"
)
fname
.
touch
()
repo
.
git
.
add
(
str
(
fname
))
fname
=
Path
(
"repo.txt"
)
fname
.
touch
()
repo
.
git
.
add
(
str
(
fname
))
repo
.
git
.
commit
(
"-m"
,
"init"
)
# YES!
# Use a completely mocked IO object instead of a real one
io
=
MagicMock
()
io
.
confirm_ask
=
AsyncMock
(
return_value
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
fnames
=
[
"added.txt"
])
assert
await
coder
.
allowed_to_edit
(
"added.txt"
)
assert
await
coder
.
allowed_to_edit
(
"repo.txt"
)
assert
await
coder
.
allowed_to_edit
(
"new.txt"
)
assert
"repo.txt"
in
str
(
coder
.
abs_fnames
)
assert
"new.txt"
in
str
(
coder
.
abs_fnames
)
assert
not
coder
.
need_commit_before_edits
async
def
test_allowed_to_edit_no
(
self
):
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname
=
Path
(
"added.txt"
)
fname
.
touch
()
repo
.
git
.
add
(
str
(
fname
))
fname
=
Path
(
"repo.txt"
)
fname
.
touch
()
repo
.
git
.
add
(
str
(
fname
))
repo
.
git
.
commit
(
"-m"
,
"init"
)
io
=
InputOutput
(
yes
=
False
)
io
.
confirm_ask
=
AsyncMock
(
return_value
=
False
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
fnames
=
[
"added.txt"
])
assert
await
coder
.
allowed_to_edit
(
"added.txt"
)
assert
not
await
coder
.
allowed_to_edit
(
"repo.txt"
)
assert
not
await
coder
.
allowed_to_edit
(
"new.txt"
)
assert
"repo.txt"
not
in
str
(
coder
.
abs_fnames
)
assert
"new.txt"
not
in
str
(
coder
.
abs_fnames
)
assert
not
coder
.
need_commit_before_edits
async
def
test_allowed_to_edit_dirty
(
self
):
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname
=
Path
(
"added.txt"
)
fname
.
touch
()
repo
.
git
.
add
(
str
(
fname
))
repo
.
git
.
commit
(
"-m"
,
"init"
)
# say NO
io
=
InputOutput
(
yes
=
False
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
fnames
=
[
"added.txt"
])
assert
await
coder
.
allowed_to_edit
(
"added.txt"
)
assert
not
coder
.
need_commit_before_edits
fname
.
write_text
(
"dirty!"
)
assert
await
coder
.
allowed_to_edit
(
"added.txt"
)
assert
coder
.
need_commit_before_edits
async
def
test_get_files_content
(
self
):
tempdir
=
Path
(
tempfile
.
mkdtemp
())
file1
=
tempdir
/
"file1.txt"
file2
=
tempdir
/
"file2.txt"
file1
.
touch
()
file2
.
touch
()
files
=
[
file1
,
file2
]
# Initialize the Coder object with the mocked IO and mocked repo
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
=
InputOutput
(),
fnames
=
files
)
content
=
coder
.
get_files_content
()
all_file_names
=
content
[
"chat_file_names"
]
|
content
[
"edit_file_names"
]
assert
"file1.txt"
in
all_file_names
assert
"file2.txt"
in
all_file_names
async
def
test_check_for_filename_mentions
(
self
):
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
mock_io
=
MagicMock
()
mock_io
.
confirm_ask
=
AsyncMock
(
return_value
=
True
)
fname1
=
Path
(
"file1.txt"
)
fname2
=
Path
(
"file2.py"
)
fname1
.
write_text
(
"one
\n
"
)
fname2
.
write_text
(
"two
\n
"
)
repo
.
git
.
add
(
str
(
fname1
))
repo
.
git
.
add
(
str
(
fname2
))
repo
.
git
.
commit
(
"-m"
,
"new"
)
mock_args
=
MagicMock
(
tui
=
False
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
mock_io
,
args
=
mock_args
)
await
coder
.
check_for_file_mentions
(
"Please check file1.txt and file2.py"
)
expected_files
=
set
(
[
str
(
Path
(
coder
.
root
)
/
fname1
),
str
(
Path
(
coder
.
root
)
/
fname2
),
]
)
assert
coder
.
abs_fnames
==
expected_files
async
def
test_check_for_ambiguous_filename_mentions_of_longer_paths
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
mock_args
=
MagicMock
(
tui
=
False
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
args
=
mock_args
)
fname
=
Path
(
"file1.txt"
)
fname
.
touch
()
other_fname
=
Path
(
"other"
)
/
"file1.txt"
other_fname
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
other_fname
.
touch
()
mock
=
MagicMock
()
mock
.
return_value
=
set
([
str
(
fname
),
str
(
other_fname
)])
coder
.
repo
.
get_tracked_files
=
mock
from
cecli
.
helpers
.
file_system
import
FileSystemService
FileSystemService
.
get_instance
().
build
()
await
coder
.
check_for_file_mentions
(
f"Please check
{
fname
}
!"
)
assert
coder
.
abs_fnames
==
{
str
(
fname
.
resolve
())}
async
def
test_skip_duplicate_basename_mentions
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
)
# Create files with same basename in different directories
fname1
=
Path
(
"dir1"
)
/
"file.txt"
fname2
=
Path
(
"dir2"
)
/
"file.txt"
fname3
=
Path
(
"dir3"
)
/
"unique.txt"
for
fname
in
[
fname1
,
fname2
,
fname3
]:
fname
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
fname
.
touch
()
# Add one file to chat
coder
.
add_rel_fname
(
str
(
fname1
))
# Mock get_tracked_files to return all files
mock
=
MagicMock
()
mock
.
return_value
=
set
([
str
(
fname1
),
str
(
fname2
),
str
(
fname3
)])
coder
.
repo
.
get_tracked_files
=
mock
from
cecli
.
helpers
.
file_system
import
FileSystemService
FileSystemService
.
get_instance
().
build
()
# Check that file mentions of a pure basename skips files with duplicate basenames
mentioned
=
coder
.
get_file_mentions
(
f"Check
{
fname2
.
name
}
and
{
fname3
}
"
)
assert
mentioned
==
{
str
(
fname3
)}
# Add a read-only file with same basename
coder
.
abs_read_only_fnames
.
add
(
str
(
fname2
.
resolve
()))
mentioned
=
coder
.
get_file_mentions
(
f"Check
{
fname1
}
and
{
fname3
}
"
)
assert
mentioned
==
{
str
(
fname3
)}
async
def
test_check_for_file_mentions_read_only
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
)
fname
=
Path
(
"readonly_file.txt"
)
fname
.
touch
()
coder
.
abs_read_only_fnames
.
add
(
str
(
fname
.
resolve
()))
mock
=
MagicMock
()
mock
.
return_value
=
set
([
str
(
fname
)])
coder
.
repo
.
get_tracked_files
=
mock
result
=
await
coder
.
check_for_file_mentions
(
f"Please check
{
fname
}
!"
)
assert
result
is
None
assert
coder
.
abs_fnames
==
set
()
async
def
test_check_for_file_mentions_with_mocked_confirm
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
)
io
.
confirm_ask
=
AsyncMock
(
side_effect
=
[
False
,
True
,
True
])
mock_args
=
MagicMock
(
tui
=
False
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
args
=
mock_args
)
coder
.
get_file_mentions
=
MagicMock
(
return_value
=
set
([
"file1.txt"
,
"file2.txt"
]))
await
coder
.
check_for_file_mentions
(
"Please check file1.txt for the info"
)
assert
io
.
confirm_ask
.
call_count
==
2
assert
len
(
coder
.
abs_fnames
)
==
1
assert
"file2.txt"
in
str
(
coder
.
abs_fnames
)
io
.
confirm_ask
.
reset_mock
()
await
coder
.
check_for_file_mentions
(
"Please check file1.txt and file2.txt again"
)
assert
io
.
confirm_ask
.
call_count
==
1
assert
len
(
coder
.
abs_fnames
)
==
1
assert
"file2.txt"
in
str
(
coder
.
abs_fnames
)
assert
"file1.txt"
in
coder
.
ignore_mentions
async
def
test_check_for_subdir_mention
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
mock_args
=
MagicMock
(
tui
=
False
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
args
=
mock_args
)
fname
=
Path
(
"other"
)
/
"file1.txt"
fname
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
fname
.
touch
()
mock
=
MagicMock
()
mock
.
return_value
=
set
([
str
(
fname
)])
coder
.
repo
.
get_tracked_files
=
mock
from
cecli
.
helpers
.
file_system
import
FileSystemService
FileSystemService
.
get_instance
().
build
()
await
coder
.
check_for_file_mentions
(
f"Please check `
{
fname
}
`"
)
assert
coder
.
abs_fnames
==
{
str
(
fname
.
resolve
())}
async
def
test_get_file_mentions_various_formats
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
)
# Create test files
test_files
=
[
"file1.txt"
,
"file2.py"
,
"dir/nested_file.js"
,
"dir/subdir/deep_file.html"
,
"file99.txt"
,
"special_chars!@#.md"
,
]
# Pre-format the Windows path to avoid backslash issues in f-string expressions
windows_path
=
test_files
[
2
].
replace
(
"/"
,
"
\\
"
)
win_path3
=
test_files
[
3
].
replace
(
"/"
,
"
\\
"
)
for
fname
in
test_files
:
fpath
=
Path
(
fname
)
fpath
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
fpath
.
touch
()
# Stage files and rebuild FileSystemService index
_gi
=
git
.
Repo
()
for
_f
in
test_files
:
_gi
.
git
.
add
(
_f
)
from
cecli
.
helpers
.
file_system
import
FileSystemService
FileSystemService
.
get_instance
().
build
()
# Mock get_addable_relative_files to return our test files
coder
.
get_addable_relative_files
=
MagicMock
(
return_value
=
set
(
test_files
))
# Test different mention formats
test_cases
=
[
# Simple plain text mentions
(
f"You should edit
{
test_files
[
0
]
}
first"
, {
test_files
[
0
]}),
# Multiple files in plain text
(
f"Edit both
{
test_files
[
0
]
}
and
{
test_files
[
1
]
}
"
,
{
test_files
[
0
],
test_files
[
1
]},
),
# Files in backticks
(
f"Check the file `
{
test_files
[
2
]
}
`"
, {
test_files
[
2
]}),
# Files in code blocks
(
f"```
\n
{
test_files
[
3
]
}
\n
```"
, {
test_files
[
3
]}),
# Files in code blocks with language specifier
# (
# f"```python\nwith open('{test_files[1]}', 'r') as f:\n"
# f" data = f.read()\n```",
# {test_files[1]},
# ),
# Files with Windows-style paths
(
f"Edit the file
{
windows_path
}
"
, {
test_files
[
2
]}),
# Files with different quote styles
(
f'Check "
{
test_files
[
5
]
}
" now'
, {
test_files
[
5
]}),
# All files in one complex message
(
(
f"First, edit `
{
test_files
[
0
]
}
`. Then modify
{
test_files
[
1
]
}
.
\n
"
f"```js
\n
// Update this file
\n
const file = '
{
test_files
[
2
]
}
';
\n
```
\n
"
f"Finally check
{
win_path3
}
"
),
{
test_files
[
0
],
test_files
[
1
],
test_files
[
2
],
test_files
[
3
]},
),
# Files mentioned in markdown bold format
(
f"You should check **
{
test_files
[
0
]
}
** for issues"
, {
test_files
[
0
]}),
(
f"Look at both **
{
test_files
[
1
]
}
** and **
{
test_files
[
2
]
}
**"
,
{
test_files
[
1
],
test_files
[
2
]},
),
(
f"The file **
{
win_path3
}
** needs updating"
,
{
test_files
[
3
]},
),
(
f"Files to modify:
\n
- **
{
test_files
[
0
]
}
**
\n
- **
{
test_files
[
4
]
}
**"
,
{
test_files
[
0
],
test_files
[
4
]},
),
]
for
content
,
expected_mentions
in
test_cases
:
mentioned_files
=
coder
.
get_file_mentions
(
content
)
assert
(
mentioned_files
==
expected_mentions
),
f"Failed to extract mentions from:
{
content
}
"
async
def
test_get_file_mentions_multiline_backticks
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
)
# Create test files
test_files
=
[
"swebench/harness/test_spec/python.py"
,
"swebench/harness/test_spec/javascript.py"
,
]
for
fname
in
test_files
:
fpath
=
Path
(
fname
)
fpath
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
fpath
.
touch
()
# Stage files and rebuild FileSystemService index
_gi
=
git
.
Repo
()
for
_f
in
test_files
:
_gi
.
git
.
add
(
_f
)
from
cecli
.
helpers
.
file_system
import
FileSystemService
FileSystemService
.
get_instance
().
build
()
# Mock get_addable_relative_files to return our test files
coder
.
get_addable_relative_files
=
MagicMock
(
return_value
=
set
(
test_files
))
# Input text with multiline backticked filenames
content
=
"""
Could you please **add the following files to the chat**?
1. `swebench/harness/test_spec/python.py`
2. `swebench/harness/test_spec/javascript.py`
Once I have these, I can show you precisely how to do the thing.
"""
expected_mentions
=
{
"swebench/harness/test_spec/python.py"
,
"swebench/harness/test_spec/javascript.py"
,
}
mentioned_files
=
coder
.
get_file_mentions
(
content
)
assert
(
mentioned_files
==
expected_mentions
),
f"Failed to extract mentions from multiline backticked content:
{
content
}
"
async
def
test_get_file_mentions_path_formats
(
self
):
with
GitTemporaryDirectory
():
io
=
InputOutput
(
pretty
=
False
,
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
)
# Test cases with different path formats
test_cases
=
[
# Unix paths in content, Unix paths in get_addable_relative_files
(
"Check file1.txt and dir/file2.txt"
, [
"file1.txt"
,
"dir/file2.txt"
]),
# Windows paths in content, Windows paths in get_addable_relative_files
(
"Check file1.txt and dir
\\
file2.txt"
, [
"file1.txt"
,
"dir
\\
file2.txt"
]),
# Unix paths in content, Windows paths in get_addable_relative_files
(
"Check file1.txt and dir/file2.txt"
, [
"file1.txt"
,
"dir
\\
file2.txt"
]),
# Windows paths in content, Unix paths in get_addable_relative_files
(
"Check file1.txt and dir
\\
file2.txt"
, [
"file1.txt"
,
"dir/file2.txt"
]),
# Mixed paths in content, Unix paths in get_addable_relative_files
(
"Check file1.txt, dir/file2.txt, and other
\\
file3.txt"
,
[
"file1.txt"
,
"dir/file2.txt"
,
"other/file3.txt"
],
),
# Mixed paths in content, Windows paths in get_addable_relative_files
(
"Check file1.txt, dir/file2.txt, and other
\\
file3.txt"
,
[
"file1.txt"
,
"dir
\\
file2.txt"
,
"other
\\
file3.txt"
],
),
]
for
content
,
addable_files
in
test_cases
:
coder
.
get_addable_relative_files
=
MagicMock
(
return_value
=
set
(
addable_files
))
mentioned_files
=
coder
.
get_file_mentions
(
content
)
expected_files
=
set
(
addable_files
)
assert
(
mentioned_files
==
expected_files
),
f"Failed for content:
{
content
}
, addable_files:
{
addable_files
}
"
async
def
test_run_with_file_deletion
(
self
):
# Create a few temporary files
tempdir
=
Path
(
tempfile
.
mkdtemp
())
file1
=
tempdir
/
"file1.txt"
file2
=
tempdir
/
"file2.txt"
file1
.
touch
()
file2
.
touch
()
files
=
[
file1
,
file2
]
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
=
InputOutput
(),
fnames
=
files
)
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
"ok"
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
coder
.
send
=
mock_send
# Call the run method with a message
await
coder
.
run
(
with_message
=
"hi"
)
assert
len
(
coder
.
abs_fnames
)
==
2
file1
.
unlink
()
# Call the run method again with a message
await
coder
.
run
(
with_message
=
"hi"
)
assert
len
(
coder
.
abs_fnames
)
==
1
async
def
test_run_with_file_unicode_error
(
self
):
# Create a few temporary files
_
,
file1
=
tempfile
.
mkstemp
()
_
,
file2
=
tempfile
.
mkstemp
()
files
=
[
file1
,
file2
]
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
=
InputOutput
(),
fnames
=
files
)
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
"ok"
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
coder
.
send
=
mock_send
# Call the run method with a message
await
coder
.
run
(
with_message
=
"hi"
)
assert
len
(
coder
.
abs_fnames
)
==
2
# Write some non-UTF8 text into the file
with
open
(
file1
,
"wb"
)
as
f
:
f
.
write
(
b"
\x80
abc"
)
# Call the run method again with a message
await
coder
.
run
(
with_message
=
"hi"
)
assert
len
(
coder
.
abs_fnames
)
==
2
async
def
test_choose_fence
(
self
):
# Create a few temporary files
_
,
file1
=
tempfile
.
mkstemp
()
with
open
(
file1
,
"wb"
)
as
f
:
f
.
write
(
b"this contains
\n
```
\n
backticks"
)
files
=
[
file1
]
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
=
InputOutput
(),
fnames
=
files
)
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
"ok"
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
coder
.
send
=
mock_send
# Call the run method with a message
await
coder
.
run
(
with_message
=
"hi"
)
assert
coder
.
fence
[
0
]
!=
"```"
async
def
test_run_with_file_utf_unicode_error
(
self
):
"make sure that we honor InputOutput(encoding) and don't just assume utf-8"
encoding
=
"utf-16"
_
,
file1
=
tempfile
.
mkstemp
()
_
,
file2
=
tempfile
.
mkstemp
()
files
=
[
file1
,
file2
]
# Initialize the Coder object with the mocked IO and mocked repo
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
=
InputOutput
(
encoding
=
encoding
),
fnames
=
files
,
)
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
"ok"
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
coder
.
send
=
mock_send
# Call the run method with a message
await
coder
.
run
(
with_message
=
"hi"
)
assert
len
(
coder
.
abs_fnames
)
==
2
some_content_which_will_error_if_read_with_encoding_utf8
=
"ÅÍÎÏ"
.
encode
(
encoding
)
with
open
(
file1
,
"wb"
)
as
f
:
f
.
write
(
some_content_which_will_error_if_read_with_encoding_utf8
)
await
coder
.
run
(
with_message
=
"hi"
)
# both files should still be here
assert
len
(
coder
.
abs_fnames
)
==
2
async
def
test_new_file_edit_one_commit
(
self
):
"""A new file should get pre-committed before the GPT edit commit"""
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname
=
Path
(
"file.txt"
)
io
=
InputOutput
(
yes
=
True
)
io
.
tool_warning
=
MagicMock
()
coder
=
await
Coder
.
create
(
self
.
GPT35
,
"diff"
,
io
=
io
,
fnames
=
[
str
(
fname
)])
assert
fname
.
exists
()
# make sure it was not committed
with
pytest
.
raises
(
git
.
exc
.
GitCommandError
):
list
(
repo
.
iter_commits
(
repo
.
active_branch
.
name
))
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
f"""
Do this:
{
str
(
fname
)
}
<<<<<<< SEARCH
=======
new
>>>>>>> REPLACE
"""
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
coder
.
send
=
mock_send
coder
.
repo
.
get_commit_message
=
AsyncMock
(
return_value
=
"commit message"
)
await
coder
.
run
(
with_message
=
"hi"
)
content
=
fname
.
read_text
()
assert
content
==
"new
\n
"
num_commits
=
len
(
list
(
repo
.
iter_commits
(
repo
.
active_branch
.
name
)))
assert
num_commits
==
2
async
def
test_only_commit_gpt_edited_file
(
self
):
"""
Only commit file that gpt edits, not other dirty files.
Also ensure commit msg only depends on diffs from the GPT edited file.
"""
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname1
=
Path
(
"file1.txt"
)
fname2
=
Path
(
"file2.txt"
)
fname1
.
write_text
(
"one
\n
"
)
fname2
.
write_text
(
"two
\n
"
)
repo
.
git
.
add
(
str
(
fname1
))
repo
.
git
.
add
(
str
(
fname2
))
repo
.
git
.
commit
(
"-m"
,
"new"
)
# DIRTY!
fname1
.
write_text
(
"ONE
\n
"
)
io
=
InputOutput
(
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
"diff"
,
io
=
io
,
fnames
=
[
str
(
fname1
),
str
(
fname2
)])
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
f"""
Do this:
{
str
(
fname2
)
}
<<<<<<< SEARCH
two
=======
TWO
>>>>>>> REPLACE
"""
coder
.
partial_response_function_call
=
dict
()
return
yield
def
mock_get_commit_message
(
diffs
,
context
,
user_language
=
None
):
assert
"one"
not
in
diffs
assert
"ONE"
not
in
diffs
return
"commit message"
coder
.
send
=
mock_send
coder
.
repo
.
get_commit_message
=
MagicMock
(
side_effect
=
mock_get_commit_message
)
await
coder
.
run
(
with_message
=
"hi"
)
content
=
fname2
.
read_text
()
assert
content
==
"TWO
\n
"
assert
repo
.
is_dirty
(
path
=
str
(
fname1
))
async
def
test_gpt_edit_to_dirty_file
(
self
):
"""A dirty file should be committed before the GPT edits are committed"""
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname
=
Path
(
"file.txt"
)
fname
.
write_text
(
"one
\n
"
)
repo
.
git
.
add
(
str
(
fname
))
fname2
=
Path
(
"other.txt"
)
fname2
.
write_text
(
"other
\n
"
)
repo
.
git
.
add
(
str
(
fname2
))
repo
.
git
.
commit
(
"-m"
,
"new"
)
# dirty
fname
.
write_text
(
"two
\n
"
)
fname2
.
write_text
(
"OTHER
\n
"
)
io
=
InputOutput
(
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
"diff"
,
io
=
io
,
fnames
=
[
str
(
fname
)])
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
f"""
Do this:
{
str
(
fname
)
}
<<<<<<< SEARCH
two
=======
three
>>>>>>> REPLACE
"""
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
saved_diffs
=
[]
async
def
mock_get_commit_message
(
diffs
,
context
,
user_language
=
None
,
coder
=
None
):
saved_diffs
.
append
(
diffs
)
return
"commit message"
coder
.
repo
.
get_commit_message
=
mock_get_commit_message
coder
.
send
=
mock_send
await
coder
.
run
(
with_message
=
"hi"
)
content
=
fname
.
read_text
()
assert
content
==
"three
\n
"
num_commits
=
len
(
list
(
repo
.
iter_commits
(
repo
.
active_branch
.
name
)))
assert
num_commits
==
3
diff
=
repo
.
git
.
diff
([
"HEAD~2"
,
"HEAD~1"
])
assert
"one"
in
diff
assert
"two"
in
diff
assert
"three"
not
in
diff
assert
"other"
not
in
diff
assert
"OTHER"
not
in
diff
diff
=
saved_diffs
[
0
]
assert
"one"
in
diff
assert
"two"
in
diff
assert
"three"
not
in
diff
assert
"other"
not
in
diff
assert
"OTHER"
not
in
diff
diff
=
repo
.
git
.
diff
([
"HEAD~1"
,
"HEAD"
])
assert
"one"
not
in
diff
assert
"two"
in
diff
assert
"three"
in
diff
assert
"other"
not
in
diff
assert
"OTHER"
not
in
diff
diff
=
saved_diffs
[
1
]
assert
"one"
not
in
diff
assert
"two"
in
diff
assert
"three"
in
diff
assert
"other"
not
in
diff
assert
"OTHER"
not
in
diff
assert
len
(
saved_diffs
)
==
2
async
def
test_gpt_edit_to_existing_file_not_in_repo
(
self
):
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname
=
Path
(
"file.txt"
)
fname
.
write_text
(
"one
\n
"
)
fname2
=
Path
(
"other.txt"
)
fname2
.
write_text
(
"other
\n
"
)
repo
.
git
.
add
(
str
(
fname2
))
repo
.
git
.
commit
(
"-m"
,
"initial"
)
io
=
InputOutput
(
yes
=
True
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
"diff"
,
io
=
io
,
fnames
=
[
str
(
fname
)])
async
def
mock_send
(
*
args
,
**
kwargs
):
coder
.
partial_response_content
=
f"""
Do this:
{
str
(
fname
)
}
<<<<<<< SEARCH
one
=======
two
>>>>>>> REPLACE
"""
coder
.
partial_response_function_call
=
dict
()
coder
.
partial_response_chunks
=
[]
return
yield
saved_diffs
=
[]
async
def
mock_get_commit_message
(
diffs
,
context
,
user_language
=
None
,
coder
=
None
):
saved_diffs
.
append
(
diffs
)
return
"commit message"
coder
.
repo
.
get_commit_message
=
mock_get_commit_message
coder
.
send
=
mock_send
await
coder
.
run
(
with_message
=
"hi"
)
content
=
fname
.
read_text
()
assert
content
==
"two
\n
"
diff
=
saved_diffs
[
0
]
assert
"file.txt"
in
diff
async
def
test_skip_cecli_ignored_files
(
self
):
with
GitTemporaryDirectory
():
repo
=
git
.
Repo
()
fname1
=
"ignoreme1.txt"
fname2
=
"ignoreme2.txt"
fname3
=
"dir/ignoreme3.txt"
Path
(
fname2
).
touch
()
repo
.
git
.
add
(
str
(
fname2
))
repo
.
git
.
commit
(
"-m"
,
"initial"
)
io
=
InputOutput
(
yes
=
True
)
fnames
=
[
fname1
,
fname2
,
fname3
]
aignore
=
Path
(
"cecli.ignore"
)
aignore
.
write_text
(
f"
{
fname1
}
\n
{
fname2
}
\n
dir
\n
"
)
repo
=
GitRepo
(
io
,
fnames
,
None
,
cecli_ignore_file
=
str
(
aignore
),
)
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
,
fnames
=
fnames
,
repo
=
repo
,
)
assert
fname1
not
in
str
(
coder
.
abs_fnames
)
assert
fname2
not
in
str
(
coder
.
abs_fnames
)
assert
fname3
not
in
str
(
coder
.
abs_fnames
)
async
def
test_skip_gitignored_files_on_init
(
self
):
with
GitTemporaryDirectory
()
as
_
:
repo_path
=
Path
(
"."
)
repo
=
git
.
Repo
.
init
(
repo_path
)
ignored_file
=
repo_path
/
"ignored_by_git.txt"
ignored_file
.
write_text
(
"This file should be ignored by git."
)
regular_file
=
repo_path
/
"regular_file.txt"
regular_file
.
write_text
(
"This is a regular file."
)
gitignore_content
=
"ignored_by_git.txt
\n
"
(
repo_path
/
".gitignore"
).
write_text
(
gitignore_content
)
repo
.
index
.
add
([
str
(
regular_file
),
".gitignore"
])
repo
.
index
.
commit
(
"Initial commit with gitignore and regular file"
)
mock_io
=
MagicMock
()
mock_io
.
tool_warning
=
MagicMock
()
fnames_to_add
=
[
str
(
ignored_file
),
str
(
regular_file
)]
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
mock_io
,
fnames
=
fnames_to_add
)
assert
str
(
ignored_file
.
resolve
())
not
in
coder
.
abs_fnames
assert
str
(
regular_file
.
resolve
())
in
coder
.
abs_fnames
_
=
any
(
call
.
kwargs
.
get
(
"message"
)
==
f"Skipping
{
ignored_file
.
name
}
that matches gitignore spec."
for
call
in
mock_io
.
tool_warning
.
call_args_list
)
async
def
test_check_for_urls
(
self
):
io
=
InputOutput
(
yes
=
True
)
mock_args
=
MagicMock
()
mock_args
.
yes_always_commands
=
True
mock_args
.
disable_scraping
=
False
coder
=
await
Coder
.
create
(
self
.
GPT35
,
None
,
io
=
io
,
args
=
mock_args
)
# Mock the execute command to return scraped content
async
def
mock_execute
(
cmd_name
,
url
,
**
kwargs
):
if
cmd_name
==
"web"
and
kwargs
.
get
(
"return_content"
):
return
f"Scraped content from
{
url
}
"
return
None
coder
.
commands
.
execute
=
mock_execute
# Test various URL formats
test_cases
=
[
(
"Check http://example.com, it's cool"
,
"http://example.com"
),
(
"Visit https://www.example.com/page and see stuff"
,
"https://www.example.com/page"
,
),
(
"Go to http://subdomain.example.com:8080/path?query=value, or not"
,
"http://subdomain.example.com:8080/path?query=value"
,
),
(
"See https://example.com/path#fragment for example"
,
"https://example.com/path#fragment"
,
),
(
"Look at http://localhost:3000"
,
"http://localhost:3000"
),
(
"View https://example.com/setup#whatever"
,
"https://example.com/setup#whatever"
,
),
(
"Open http://127.0.0.1:8000/api/v1/"
,
"http://127.0.0.1:8000/api/v1/"
),
(
"Try https://example.com/path/to/page.html?param1=value1¶m2=value2"
,
"https://example.com/path/to/page.html?param1=value1¶m2=value2"
,
),
(
"Access http://user:password@example.com"
,
"http://user:password@example.com"
,
),
(
"Use https://example.com/path_(with_parentheses)"
,
"https://example.com/path_(with_parentheses)"
,
),
]
for
input_text
,
expected_url
in
test_cases
:
result
=
await
coder
.
check_for_urls
(
input_text
)
assert
expected_url
in
result
# Test cases from the GitHub issue
issue_cases
=
[
(
"check http://localhost:3002, there is an error"
,
"http://localhost:3002"
),
(
"can you check out https://example.com/setup#whatever"
,
"https://example.com/setup#whatever"
,
),
]
for
input_text
,
expected_url
in
issue_cases
:
result
=
await
coder
.
check_for_urls
(
input_text
)
assert
expected_url
in
result
# Test case with multiple URLs
multi_url_input
=
"Check http://example1.com and https://example2.com/page"
result
=
await
coder
.
check_for_urls
(
multi_url_input
)
assert
"http://example1.com"
in
result
assert
"https://example2.com/page"
in
result
# Test case with no URL
no_url_input
=
"This text contains no URL"
result
=
await
coder
.
check_for_urls
(
no_url_input
)
assert
result
==
no_url_input
# Test case with the same URL appearing multiple times
repeated_url_input
=
(
"Check https://example.com, then https://example.com again, and https://example.com one"
" more time"
)
result
=
await
coder
.
check_for_urls
(
repeated_url_input
)
# the original 3 in the input text, plus 1 more for the scraped text
assert
result
.
count
(
"https://example.com"
)
==
4
assert
"https://example.com"
in
result
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL