-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
1116 lines (849 loc) · 45.5 KB
/
test_cli.py
File metadata and controls
1116 lines (849 loc) · 45.5 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
#!/usr/bin/env python3
"""
Comprehensive test suite for Devin CLI
Tests all functionality including auth management without requiring a real API key
"""
import subprocess
import sys
import os
import json
import tempfile
from unittest.mock import patch, MagicMock
import requests
from pathlib import Path
def run_cli_command(args, env_vars=None):
"""Run the CLI command and return result"""
cmd = [sys.executable, 'devin_cli.py'] + args
# Set up environment
env = os.environ.copy()
if env_vars:
env.update(env_vars)
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
env=env,
cwd=os.path.dirname(__file__)
)
return {
'returncode': result.returncode,
'stdout': result.stdout,
'stderr': result.stderr
}
except Exception as e:
return {
'returncode': -1,
'stdout': '',
'stderr': str(e)
}
def test_help_command():
"""Test --help command"""
print("🧪 Testing --help command...")
result = run_cli_command(['--help'])
assert result['returncode'] == 0, f"Help command failed: {result['stderr']}"
assert 'Devin CLI - Create and manage Devin sessions' in result['stdout'], "Help text missing"
assert 'auth' in result['stdout'], "Auth subcommand missing from help"
assert 'create' in result['stdout'], "Create subcommand missing from help"
assert 'get' in result['stdout'], "Get subcommand missing from help"
assert 'message' in result['stdout'], "Message subcommand missing from help"
print("✅ Help command works correctly")
def test_version_command():
"""Test --version command"""
print("🧪 Testing --version command...")
result = run_cli_command(['--version'])
assert result['returncode'] == 0, f"Version command failed: {result['stderr']}"
assert '1.1.0' in result['stdout'], "Version number missing"
print("✅ Version command works correctly")
def test_auth_help():
"""Test auth subcommand help"""
print("🧪 Testing auth --help command...")
result = run_cli_command(['auth', '--help'])
assert result['returncode'] == 0, f"Auth help command failed: {result['stderr']}"
assert 'Set or test your Devin API token' in result['stdout'], "Auth help text missing"
assert '--test' in result['stdout'], "Test option missing from auth help"
print("✅ Auth help command works correctly")
def test_create_help():
"""Test create subcommand help"""
print("🧪 Testing create --help command...")
result = run_cli_command(['create', '--help'])
assert result['returncode'] == 0, f"Create help command failed: {result['stderr']}"
assert 'Create a new Devin session' in result['stdout'], "Create help text missing"
assert '--prompt' in result['stdout'], "Prompt option missing from create help"
assert '--snapshot-id' in result['stdout'], "Snapshot ID option missing from create help"
print("✅ Create help command works correctly")
def test_missing_api_key():
"""Test behavior when API key is missing"""
print("🧪 Testing missing API key handling...")
# Test the get_api_key function directly to ensure it fails when no key is available
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import get_api_key, DevinAPIError
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Clear environment variable
with patch.dict(os.environ, {}, clear=True):
# Test that get_api_key raises an error when no key is available
try:
get_api_key()
assert False, "Expected exception to be raised when no API key is available"
except Exception as e:
# Should contain error message about missing API key
error_msg = str(e).lower()
assert any(keyword in error_msg for keyword in ['api key', 'token', 'auth', 'not found', 'missing']), f"Should mention missing API key. Error: {e}"
print("✅ Missing API key handling works correctly")
def test_argument_parsing():
"""Test that all arguments are parsed correctly"""
print("🧪 Testing argument parsing...")
# Test with fake API key to avoid actual API call, but check parsing
env = {'DEVIN_API_KEY': 'fake_key_for_testing'}
# This will fail at API call stage, but we can check that parsing worked
result = run_cli_command([
'create',
'--prompt', 'Test prompt',
'--snapshot-id', 'snap-123',
'--unlisted',
'--idempotent',
'--max-acu-limit', '100',
'--secret-ids', 'secret1,secret2',
'--knowledge-ids', 'kb1,kb2',
'--tags', 'test,cli',
'--title', 'Test Session',
'--output', 'json'
], env_vars=env)
# Should fail at API call (403 or network error), not at argument parsing
assert result['returncode'] == 1, "Expected API failure, not parsing failure"
assert 'Creating Devin session...' in result['stdout'], "Should reach API call stage"
print("✅ Argument parsing works correctly")
def test_json_output_format():
"""Test JSON output format"""
print("🧪 Testing JSON output format...")
env = {'DEVIN_API_KEY': 'fake_key_for_testing'}
result = run_cli_command([
'create',
'--prompt', 'Test JSON output',
'--output', 'json'
], env_vars=env)
# Should fail at API call but attempt JSON output
assert result['returncode'] == 1, "Expected API failure"
assert 'Creating Devin session...' in result['stdout'], "Should reach API call stage"
print("✅ JSON output format option works")
def test_list_parsing():
"""Test comma-separated list parsing"""
print("🧪 Testing list parsing functionality...")
# Import the parsing function directly
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import parse_list_input
# Test various list inputs
assert parse_list_input("") == []
assert parse_list_input("item1") == ["item1"]
assert parse_list_input("item1,item2") == ["item1", "item2"]
assert parse_list_input("item1, item2, item3") == ["item1", "item2", "item3"]
assert parse_list_input(" item1 , item2 ") == ["item1", "item2"]
print("✅ List parsing works correctly")
def test_token_file_operations():
"""Test token save/load operations"""
print("🧪 Testing token file operations...")
# Import functions directly
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import save_token, load_token, get_token_file
# Create a temporary directory for testing
with tempfile.TemporaryDirectory() as temp_dir:
# Mock the config directory to use temp directory for all operations
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Clear environment variable to ensure we're testing file operations
with patch.dict(os.environ, {}, clear=True):
# Test saving token
test_token = "test_token_12345"
save_token(test_token)
# Check file exists and has correct permissions
token_file = Path(temp_dir) / 'token'
assert token_file.exists(), "Token file should exist"
# Check file permissions (should be 0o600)
file_mode = oct(token_file.stat().st_mode)[-3:]
assert file_mode == '600', f"Token file should have 600 permissions, got {file_mode}"
# Test loading token
loaded_token = load_token()
assert loaded_token == test_token, f"Loaded token should match saved token. Expected: '{test_token}', Got: '{loaded_token}'"
print("✅ Token file operations work correctly")
def test_auth_token_validation():
"""Test auth token validation"""
print("🧪 Testing auth token validation...")
# Import test_token function
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import test_token
# Mock requests.post to simulate different responses
with patch('devin_cli.requests.post') as mock_post:
# Test valid token (200 response)
mock_response = MagicMock()
mock_response.status_code = 200
mock_post.return_value = mock_response
assert test_token("valid_token") == True, "Valid token should return True"
# Test invalid token (401 response)
mock_response.status_code = 401
assert test_token("invalid_token") == False, "Invalid token should return False"
# Test invalid token (403 response)
mock_response.status_code = 403
assert test_token("forbidden_token") == False, "Forbidden token should return False"
# Test network error (exception)
mock_post.side_effect = requests.exceptions.RequestException("Network error")
assert test_token("any_token") == True, "Network error should assume token is valid"
print("✅ Auth token validation works correctly")
def test_api_request_structure():
"""Test that API requests are structured correctly"""
print("🧪 Testing API request structure...")
# Import the API function
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import make_api_request
# Mock requests.post
with patch('devin_cli.requests.post') as mock_post:
# Mock successful response
mock_response = MagicMock()
mock_response.json.return_value = {
"session_id": "test-123",
"url": "https://app.devin.ai/sessions/test-123",
"is_new_session": True
}
mock_response.raise_for_status.return_value = None
mock_post.return_value = mock_response
# Set API key via environment
with patch.dict(os.environ, {'DEVIN_API_KEY': 'test_api_key'}):
# Test API request
payload = {
'prompt': 'Test prompt',
'snapshot_id': 'snap-123',
'idempotent': True,
'tags': ['test', 'api']
}
result = make_api_request(payload)
# Verify the request was made correctly
mock_post.assert_called_once()
call_args, call_kwargs = mock_post.call_args
assert call_args[0] == 'https://api.devin.ai/v1/sessions'
assert call_kwargs['headers']['Authorization'] == 'Bearer test_api_key'
assert call_kwargs['headers']['Content-Type'] == 'application/json'
assert call_kwargs['json'] == payload
# Verify response parsing
assert result['session_id'] == 'test-123'
assert result['is_new_session'] == True
print("✅ API request structure is correct")
def test_no_subcommand_shows_help():
"""Test that running CLI without subcommand shows help"""
print("🧪 Testing no subcommand behavior...")
result = run_cli_command([])
assert result['returncode'] == 0, f"Should show help, got returncode {result['returncode']}"
assert 'Devin CLI - Create and manage Devin sessions' in result['stdout'], "Should show help text"
assert 'Commands:' in result['stdout'], "Should show available commands"
print("✅ No subcommand correctly shows help")
def test_auth_command_interactive():
"""Test auth command for setting token interactively"""
print("🧪 Testing auth command (interactive token setting)...")
# Test the underlying save_token function instead of full CLI
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import save_token, test_token
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Test saving a token directly
test_token_value = "test_interactive_token_123"
save_token(test_token_value)
# Check if token was saved
token_file = Path(temp_dir) / 'token'
assert token_file.exists(), "Token file should be created"
with open(token_file, 'r') as f:
saved_token = f.read().strip()
assert saved_token == test_token_value, f"Wrong token saved: {saved_token}"
print("✅ Auth command interactive token setting works")
def test_auth_test_command():
"""Test auth --test command"""
print("🧪 Testing auth --test command...")
# Test with valid token
with patch('devin_cli.test_token') as mock_test_token:
mock_test_token.return_value = True
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Create a test token file
token_file = Path(temp_dir) / 'token'
with open(token_file, 'w') as f:
f.write("valid_test_token")
os.chmod(token_file, 0o600)
result = run_cli_command(['auth', '--test'])
assert result['returncode'] == 0, f"Auth test should succeed with valid token. stderr: {result['stderr']}"
assert '✅' in result['stdout'], "Should show success indicator"
print("✅ Auth --test command works correctly")
def test_environment_variable_priority():
"""Test that DEVIN_API_KEY environment variable takes precedence over saved token"""
print("🧪 Testing environment variable priority...")
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import load_token
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Create a saved token file
token_file = Path(temp_dir) / 'token'
with open(token_file, 'w') as f:
f.write("saved_file_token")
# Test with environment variable set
with patch.dict(os.environ, {'DEVIN_API_KEY': 'env_var_token'}):
loaded_token = load_token()
assert loaded_token == 'env_var_token', f"Environment variable should take precedence. Got: {loaded_token}"
# Test without environment variable (should use file)
with patch.dict(os.environ, {}, clear=True):
loaded_token = load_token()
assert loaded_token == 'saved_file_token', f"Should fall back to file token. Got: {loaded_token}"
print("✅ Environment variable priority works correctly")
def test_corrupted_token_file_handling():
"""Test handling of corrupted or unreadable token files"""
print("🧪 Testing corrupted token file handling...")
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import load_token
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Create a token file with no read permissions
token_file = Path(temp_dir) / 'token'
with open(token_file, 'w') as f:
f.write("test_token")
os.chmod(token_file, 0o000) # No permissions
# Clear environment variable
with patch.dict(os.environ, {}, clear=True):
# Should handle the permission error gracefully
loaded_token = load_token()
assert loaded_token is None, "Should return None for unreadable token file"
# Restore permissions for cleanup
os.chmod(token_file, 0o600)
print("✅ Corrupted token file handling works correctly")
def test_api_error_handling():
"""Test various API error scenarios"""
print("🧪 Testing API error handling...")
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import make_api_request, DevinAPIError
# Test network timeout
with patch('devin_cli.requests.post') as mock_post:
mock_post.side_effect = requests.exceptions.Timeout("Request timed out")
with patch.dict(os.environ, {'DEVIN_API_KEY': 'test_token'}):
try:
make_api_request({'prompt': 'test'})
assert False, "Should have raised DevinAPIError"
except DevinAPIError as e:
assert "Request timed out" in str(e), f"Should contain timeout message. Got: {e}"
# Test HTTP error
with patch('devin_cli.requests.post') as mock_post:
mock_response = MagicMock()
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("HTTP 500 Error")
mock_post.return_value = mock_response
with patch.dict(os.environ, {'DEVIN_API_KEY': 'test_token'}):
try:
make_api_request({'prompt': 'test'})
assert False, "Should have raised DevinAPIError"
except DevinAPIError as e:
assert "HTTP 500 Error" in str(e), f"Should contain HTTP error message. Got: {e}"
print("✅ API error handling works correctly")
def test_edge_case_list_parsing():
"""Test edge cases in list parsing"""
print("🧪 Testing edge case list parsing...")
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import parse_list_input
# Test edge cases
assert parse_list_input(None) == [], "None should return empty list"
assert parse_list_input(" ") == [], "Whitespace-only should return empty list"
assert parse_list_input(",,,") == [], "Only commas should return empty list"
assert parse_list_input("item1,,item2") == ["item1", "item2"], "Should skip empty items"
assert parse_list_input(" item1 , , item2 ") == ["item1", "item2"], "Should handle mixed whitespace and empty items"
print("✅ Edge case list parsing works correctly")
def test_config_directory_creation():
"""Test that config directory is created if it doesn't exist"""
print("🧪 Testing config directory creation...")
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import get_config_dir
with tempfile.TemporaryDirectory() as temp_dir:
# Point to a non-existent subdirectory
test_config_dir = Path(temp_dir) / 'test_config'
with patch('pathlib.Path.home') as mock_home:
mock_home.return_value = Path(temp_dir)
# Should create the directory
config_dir = get_config_dir()
assert config_dir.exists(), "Config directory should be created"
assert config_dir.is_dir(), "Config path should be a directory"
assert config_dir.name == '.devin-cli', "Should create .devin-cli directory"
print("✅ Config directory creation works correctly")
def test_create_api_request():
"""Test the underlying API request function for session creation"""
print("🧪 Testing session creation API request...")
# Test the underlying make_api_request function instead of full interactive CLI
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import make_api_request
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Create a test token file
token_file = Path(temp_dir) / 'token'
with open(token_file, 'w') as f:
f.write("interactive_test_token")
# Mock the API call to test session creation logic
with patch('devin_cli.requests.post') as mock_post:
mock_response = MagicMock()
mock_response.json.return_value = {
'id': 'test-session-123',
'status': 'created',
'url': 'https://preview.devin.ai/sessions/test-session-123'
}
mock_response.raise_for_status.return_value = None
mock_post.return_value = mock_response
# Test API request with session data (correct signature)
session_data = {
'prompt': 'Test interactive prompt',
'unlisted': False,
'idempotent': False
}
result = make_api_request(session_data)
# Verify the API was called correctly
assert mock_post.call_count == 1, "API should have been called once"
assert result['id'] == 'test-session-123', "Should return session ID"
print("✅ Session creation API request works correctly")
def test_setup_command_help():
"""Test setup command help"""
print("🧪 Testing setup --help command...")
result = run_cli_command(['setup', '--help'])
assert result['returncode'] == 0, f"Setup help command failed: {result['stderr']}"
assert 'Download latest Devin workflow and session guide' in result['stdout'], "Setup help text missing"
assert '--target-dir' in result['stdout'], "Target dir option missing from setup help"
assert '--force' in result['stdout'], "Force option missing from setup help"
print("✅ Setup help command works correctly")
def test_setup_command_success():
"""Test successful setup command execution"""
print("🧪 Testing setup command with mocked downloads...")
# Import CLI and use Click's testing utilities
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import cli
from click.testing import CliRunner
with tempfile.TemporaryDirectory() as temp_dir:
# Mock successful HTTP responses
with patch('devin_cli.requests.get') as mock_get:
mock_response = MagicMock()
mock_response.text = "# Mock file content\nThis is test content"
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
# Use Click's test runner
runner = CliRunner()
result = runner.invoke(cli, ['setup', '--target-dir', temp_dir, '--force'])
# Check command succeeded
assert result.exit_code == 0, f"Setup command failed: {result.output}"
assert 'Downloaded Devin Session Guide' in result.output, "Should show guide download message"
assert 'Downloaded Create Session Workflow' in result.output, "Should show workflow download message"
# Check files were created
guide_file = Path(temp_dir) / 'devin-session-guide.md'
workflow_file = Path(temp_dir) / '.windsurf' / 'workflows' / 'create-session.md'
assert guide_file.exists(), "Guide file should be created"
assert workflow_file.exists(), "Workflow file should be created"
# Check file contents
with open(guide_file, 'r') as f:
content = f.read()
assert "Mock file content" in content, "Guide file should have correct content"
with open(workflow_file, 'r') as f:
content = f.read()
assert "Mock file content" in content, "Workflow file should have correct content"
# Verify correct URLs were called
assert mock_get.call_count == 2, "Should make 2 HTTP requests"
urls_called = [call[0][0] for call in mock_get.call_args_list]
assert any('devin-session-guide.md' in url for url in urls_called), "Should request guide file"
assert any('create-session.md' in url for url in urls_called), "Should request workflow file"
print("✅ Setup command success test passed")
def test_setup_command_network_error():
"""Test setup command with network errors"""
print("🧪 Testing setup command with network errors...")
# Import CLI and use Click's testing utilities
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import cli
from click.testing import CliRunner
with tempfile.TemporaryDirectory() as temp_dir:
# Mock network error
with patch('devin_cli.requests.get') as mock_get:
mock_get.side_effect = requests.exceptions.ConnectionError("Network error")
# Use Click's test runner
runner = CliRunner()
result = runner.invoke(cli, ['setup', '--target-dir', temp_dir, '--force'])
# Command should complete but show errors
assert result.exit_code == 0, f"Setup command should not crash: {result.output}"
assert 'Failed to download' in result.output, "Should show download failure"
assert 'No files were downloaded' in result.output, "Should show no files downloaded"
# Files should not exist
guide_file = Path(temp_dir) / 'devin-session-guide.md'
workflow_file = Path(temp_dir) / '.windsurf' / 'workflows' / 'create-session.md'
assert not guide_file.exists(), "Guide file should not be created on error"
assert not workflow_file.exists(), "Workflow file should not be created on error"
print("✅ Setup command network error test passed")
def test_setup_command_file_exists_prompt():
"""Test setup command behavior when files already exist"""
print("🧪 Testing setup command with existing files...")
# Import CLI and use Click's testing utilities
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import cli
from click.testing import CliRunner
with tempfile.TemporaryDirectory() as temp_dir:
# Create existing files
guide_file = Path(temp_dir) / 'devin-session-guide.md'
workflows_dir = Path(temp_dir) / '.windsurf' / 'workflows'
workflows_dir.mkdir(parents=True)
workflow_file = workflows_dir / 'create-session.md'
guide_file.write_text("Existing guide content")
workflow_file.write_text("Existing workflow content")
# Test with --force flag (should overwrite without prompting)
with patch('devin_cli.requests.get') as mock_get:
mock_response = MagicMock()
mock_response.text = "New content from GitHub"
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
# Use Click's test runner
runner = CliRunner()
result = runner.invoke(cli, ['setup', '--target-dir', temp_dir, '--force'])
assert result.exit_code == 0, f"Setup with force should succeed: {result.output}"
assert 'Downloaded Devin Session Guide' in result.output, "Should download guide"
assert 'Downloaded Create Session Workflow' in result.output, "Should download workflow"
# Check files were overwritten
assert guide_file.read_text() == "New content from GitHub", "Guide should be overwritten"
assert workflow_file.read_text() == "New content from GitHub", "Workflow should be overwritten"
print("✅ Setup command existing files test passed")
def test_setup_command_directory_creation():
"""Test that setup command creates necessary directories"""
print("🧪 Testing setup command directory creation...")
with tempfile.TemporaryDirectory() as temp_dir:
target_dir = Path(temp_dir) / 'nonexistent' / 'nested' / 'path'
with patch('devin_cli.requests.get') as mock_get:
mock_response = MagicMock()
mock_response.text = "Test content"
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
result = run_cli_command(['setup', '--target-dir', str(target_dir), '--force'])
assert result['returncode'] == 0, f"Setup should create directories: {result['stderr']}"
# Check that directories were created
assert target_dir.exists(), "Target directory should be created"
workflows_dir = target_dir / '.windsurf' / 'workflows'
assert workflows_dir.exists(), "Workflows directory should be created"
# Check files exist
guide_file = target_dir / 'devin-session-guide.md'
workflow_file = workflows_dir / 'create-session.md'
assert guide_file.exists(), "Guide file should be created"
assert workflow_file.exists(), "Workflow file should be created"
print("✅ Setup command directory creation test passed")
def test_setup_command_http_error():
"""Test setup command with HTTP errors (404, 500, etc.)"""
print("🧪 Testing setup command with HTTP errors...")
# Import CLI and use Click's testing utilities
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import cli
from click.testing import CliRunner
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.requests.get') as mock_get:
# Mock HTTP error
mock_response = MagicMock()
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Not Found")
mock_get.return_value = mock_response
# Use Click's test runner
runner = CliRunner()
result = runner.invoke(cli, ['setup', '--target-dir', temp_dir, '--force'])
# Should complete but show errors
assert result.exit_code == 0, f"Setup should handle HTTP errors gracefully: {result.output}"
assert 'Failed to download' in result.output, "Should show HTTP error"
assert 'No files were downloaded' in result.output, "Should indicate no downloads"
print("✅ Setup command HTTP error test passed")
def test_setup_command_partial_success():
"""Test setup command when some files succeed and others fail"""
print("🧪 Testing setup command with partial success...")
# Import CLI and use Click's testing utilities
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import cli
from click.testing import CliRunner
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.requests.get') as mock_get:
# First call succeeds, second fails
responses = [
MagicMock(text="Guide content", raise_for_status=MagicMock()),
MagicMock(raise_for_status=MagicMock(side_effect=requests.exceptions.HTTPError("404")))
]
mock_get.side_effect = responses
# Use Click's test runner
runner = CliRunner()
result = runner.invoke(cli, ['setup', '--target-dir', temp_dir, '--force'])
assert result.exit_code == 0, f"Setup should handle partial success: {result.output}"
assert 'Downloaded Devin Session Guide' in result.output, "Should show successful download"
assert 'Failed to download Create Session Workflow' in result.output, "Should show failed download"
# Check that successful file exists
guide_file = Path(temp_dir) / 'devin-session-guide.md'
workflow_file = Path(temp_dir) / '.windsurf' / 'workflows' / 'create-session.md'
assert guide_file.exists(), "Guide file should be created"
assert not workflow_file.exists(), "Workflow file should not be created on error"
print("✅ Setup command partial success test passed")
def test_end_to_end_workflow():
"""Test complete workflow: save token -> create session"""
print("🧪 Testing end-to-end workflow...")
# Test the workflow by testing components separately since subprocess doesn't inherit mocks
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import save_token, load_token, make_api_request
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Step 1: Save a token
test_token = "workflow_test_token_456"
save_token(test_token)
# Step 2: Verify token can be loaded
with patch.dict(os.environ, {}, clear=True): # Clear env to use file
loaded_token = load_token()
assert loaded_token == test_token, f"Token should be loaded from file. Got: {loaded_token}"
# Step 3: Test API request with loaded token
with patch('devin_cli.requests.post') as mock_post:
mock_response = MagicMock()
mock_response.json.return_value = {
"session_id": "workflow-test-123",
"url": "https://app.devin.ai/sessions/workflow-test-123",
"is_new_session": True
}
mock_response.raise_for_status.return_value = None
mock_post.return_value = mock_response
# Clear environment to ensure we use saved token
with patch.dict(os.environ, {}, clear=True):
result = make_api_request({'prompt': 'End-to-end test'})
# Verify the result
assert result['session_id'] == 'workflow-test-123', "Should return correct session ID"
assert result['is_new_session'] == True, "Should indicate new session"
# Verify the API was called with correct token
mock_post.assert_called_once()
call_kwargs = mock_post.call_args[1]
assert call_kwargs['headers']['Authorization'] == f'Bearer {test_token}', "Should use saved token"
print("✅ End-to-end workflow works correctly")
def test_get_command_help():
"""Test get command help"""
print("🧪 Testing get --help command...")
result = run_cli_command(['get', '--help'])
assert result['returncode'] == 0, f"Get help command failed: {result['stderr']}"
assert 'Get details of an existing Devin session' in result['stdout'], "Get help text missing"
assert '--output' in result['stdout'], "Output option missing from get help"
print("✅ Get help command works correctly")
def test_get_command_success():
"""Test successful get command execution"""
print("🧪 Testing get command with mocked API response...")
# Import the get_session_details function
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import get_session_details
# Mock successful API response
with patch('devin_cli.requests.get') as mock_get:
mock_response = MagicMock()
mock_response.json.return_value = {
"session_id": "test-session-123",
"status": "active",
"title": "Test Session",
"created_at": "2024-01-01T12:00:00Z",
"updated_at": "2024-01-01T13:00:00Z",
"tags": ["test", "api"],
"messages": [
{"role": "user", "content": "Test message 1"},
{"role": "assistant", "content": "Test response 1"}
]
}
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
# Set API key via environment
with patch.dict(os.environ, {'DEVIN_API_KEY': 'test_api_key'}):
result = get_session_details("test-session-123")
# Verify the request was made correctly
mock_get.assert_called_once()
call_args = mock_get.call_args[0]
call_kwargs = mock_get.call_args[1]
assert call_args[0] == 'https://api.devin.ai/v1/sessions/test-session-123'
assert call_kwargs['headers']['Authorization'] == 'Bearer test_api_key'
assert call_kwargs['headers']['Content-Type'] == 'application/json'
# Verify response parsing
assert result['session_id'] == 'test-session-123'
assert result['status'] == 'active'
assert result['title'] == 'Test Session'
print("✅ Get command API structure is correct")
def test_get_command_cli_execution():
"""Test get command CLI execution"""
print("🧪 Testing get command CLI execution...")
env = {'DEVIN_API_KEY': 'fake_key_for_testing'}
result = run_cli_command([
'get', 'test-session-123',
'--output', 'json'
], env_vars=env)
# Should fail at API call but reach that stage
assert result['returncode'] == 1, "Expected API failure"
assert 'Retrieving session details for test-session-123...' in result['stdout'], "Should reach API call stage"
print("✅ Get command CLI execution works")
def test_message_command_help():
"""Test message command help"""
print("🧪 Testing message --help command...")
result = run_cli_command(['message', '--help'])
assert result['returncode'] == 0, f"Message help command failed: {result['stderr']}"
assert 'Send a message to an existing Devin session' in result['stdout'], "Message help text missing"
assert '--message' in result['stdout'], "Message option missing from message help"
assert '--output' in result['stdout'], "Output option missing from message help"
print("✅ Message help command works correctly")
def test_message_command_success():
"""Test successful message command execution"""
print("🧪 Testing message command with mocked API response...")
# Import the send_message_to_session function
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import send_message_to_session
# Mock successful API response
with patch('devin_cli.requests.post') as mock_post:
mock_response = MagicMock()
mock_response.json.return_value = {
"message_id": "msg-123",
"status": "sent"
}
mock_response.raise_for_status.return_value = None
mock_post.return_value = mock_response
# Set API key via environment
with patch.dict(os.environ, {'DEVIN_API_KEY': 'test_api_key'}):
payload = {'message': 'Test message'}
result = send_message_to_session("test-session-123", payload)
# Verify the request was made correctly
mock_post.assert_called_once()
call_args = mock_post.call_args[0]
call_kwargs = mock_post.call_args[1]
assert call_args[0] == 'https://api.devin.ai/v1/sessions/test-session-123/message'
assert call_kwargs['headers']['Authorization'] == 'Bearer test_api_key'
assert call_kwargs['headers']['Content-Type'] == 'application/json'
assert call_kwargs['json'] == payload
# Verify response parsing
assert result['message_id'] == 'msg-123'
assert result['status'] == 'sent'
print("✅ Message command API structure is correct")
def test_message_command_cli_execution():
"""Test message command CLI execution"""
print("🧪 Testing message command CLI execution...")
env = {'DEVIN_API_KEY': 'fake_key_for_testing'}
result = run_cli_command([
'message', 'test-session-123',
'--message', 'Test message',
'--output', 'json'
], env_vars=env)
# Should fail at API call but reach that stage
assert result['returncode'] == 1, "Expected API failure"
assert 'Sending message to session test-session-123...' in result['stdout'], "Should reach API call stage"
print("✅ Message command CLI execution works")
def test_message_api_function():
"""Test the underlying send_message_to_session function"""
print("🧪 Testing message sending API function...")
# Test the underlying send_message_to_session function instead of full interactive CLI
sys.path.insert(0, os.path.dirname(__file__))
from devin_cli import send_message_to_session
with tempfile.TemporaryDirectory() as temp_dir:
with patch('devin_cli.get_config_dir') as mock_config_dir:
mock_config_dir.return_value = Path(temp_dir)
# Create a test token file
token_file = Path(temp_dir) / 'token'
with open(token_file, 'w') as f:
f.write("interactive_test_token")
# Mock the API call to test message sending logic
with patch('devin_cli.requests.post') as mock_post:
mock_response = MagicMock()
mock_response.json.return_value = {
'id': 'msg-123',
'content': 'Test interactive message',
'timestamp': '2024-01-01T00:00:00Z'
}
mock_response.raise_for_status.return_value = None
mock_post.return_value = mock_response
# Test sending a message directly
result = send_message_to_session('test-session-123', 'Test interactive message')
# Verify the API was called correctly
assert mock_post.call_count == 1, "API should have been called once"
assert result['content'] == 'Test interactive message', "Should return message content"
print("✅ Message sending API function works correctly")
def test_get_and_message_api_error_handling():