forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ui_human_mode.py
More file actions
1832 lines (1625 loc) · 65.9 KB
/
test_ui_human_mode.py
File metadata and controls
1832 lines (1625 loc) · 65.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for human-mode output: ui.py functions, _human_* formatters, cli help."""
import io
import json
import sys
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from cypilot.utils.ui import (
set_json_mode,
is_json_mode,
header,
step,
substep,
success,
error,
warn,
info,
detail,
hint,
blank,
divider,
table,
file_action,
result,
_has_color,
_c,
ui,
)
class _HumanModeBase(unittest.TestCase):
"""Base that switches to human mode for each test."""
def setUp(self):
set_json_mode(False)
def tearDown(self):
set_json_mode(True)
# ---------------------------------------------------------------------------
# ui.py core functions
# ---------------------------------------------------------------------------
class TestUIFunctions(_HumanModeBase):
"""Test all ui.py output functions in human (non-JSON) mode."""
def test_header(self):
buf = io.StringIO()
with redirect_stderr(buf):
header("Test Header")
self.assertIn("Test Header", buf.getvalue())
def test_step(self):
buf = io.StringIO()
with redirect_stderr(buf):
step("Doing something")
self.assertIn("Doing something", buf.getvalue())
def test_substep(self):
buf = io.StringIO()
with redirect_stderr(buf):
substep("sub item")
self.assertIn("sub item", buf.getvalue())
def test_success(self):
buf = io.StringIO()
with redirect_stderr(buf):
success("All good")
self.assertIn("All good", buf.getvalue())
def test_error(self):
buf = io.StringIO()
with redirect_stderr(buf):
error("Something broke")
self.assertIn("Something broke", buf.getvalue())
def test_warn(self):
buf = io.StringIO()
with redirect_stderr(buf):
warn("Watch out")
self.assertIn("Watch out", buf.getvalue())
def test_info(self):
buf = io.StringIO()
with redirect_stderr(buf):
info("FYI")
self.assertIn("FYI", buf.getvalue())
def test_detail(self):
buf = io.StringIO()
with redirect_stderr(buf):
detail("Key", "Value")
out = buf.getvalue()
self.assertIn("Key", out)
self.assertIn("Value", out)
def test_hint(self):
buf = io.StringIO()
with redirect_stderr(buf):
hint("Try this")
self.assertIn("Try this", buf.getvalue())
def test_blank(self):
buf = io.StringIO()
with redirect_stderr(buf):
blank()
self.assertEqual(buf.getvalue(), "\n")
def test_divider(self):
buf = io.StringIO()
with redirect_stderr(buf):
divider()
self.assertIn("─", buf.getvalue())
def test_table_with_rows(self):
buf = io.StringIO()
with redirect_stderr(buf):
table(["Name", "Value"], [["foo", "bar"], ["baz", "qux"]])
out = buf.getvalue()
self.assertIn("Name", out)
self.assertIn("foo", out)
self.assertIn("baz", out)
def test_table_empty_rows(self):
buf = io.StringIO()
with redirect_stderr(buf):
table(["Name"], [])
self.assertEqual(buf.getvalue(), "")
def test_table_extra_columns_in_row(self):
buf = io.StringIO()
with redirect_stderr(buf):
table(["A"], [["x", "y"]])
out = buf.getvalue()
self.assertIn("x", out)
self.assertIn("y", out)
def test_file_action_created(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("foo.py", "created")
out = buf.getvalue()
self.assertIn("foo.py", out)
self.assertIn("created", out)
def test_file_action_updated(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("bar.py", "updated")
self.assertIn("updated", buf.getvalue())
def test_file_action_deleted(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("baz.py", "deleted")
self.assertIn("deleted", buf.getvalue())
def test_file_action_unknown(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("x.py", "unknown_action")
self.assertIn("x.py", buf.getvalue())
def test_file_action_skipped(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("s.py", "skipped")
self.assertIn("skipped", buf.getvalue())
def test_file_action_missing_in_cache(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("m.py", "missing_in_cache")
self.assertIn("missing_in_cache", buf.getvalue())
def test_file_action_preserved(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("p.py", "preserved")
self.assertIn("preserved", buf.getvalue())
def test_file_action_dry_run(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("d.py", "dry_run")
self.assertIn("dry_run", buf.getvalue())
def test_file_action_unchanged(self):
buf = io.StringIO()
with redirect_stderr(buf):
file_action("u.py", "unchanged")
self.assertIn("unchanged", buf.getvalue())
class TestUIColorHelpers(_HumanModeBase):
"""Test _has_color and _c helpers."""
def test_has_color_false_in_test(self):
self.assertFalse(_has_color())
def test_c_no_color(self):
self.assertEqual(_c("\033[1m", "text"), "text")
class TestUIResult(_HumanModeBase):
"""Test result() in human mode — generic fallback and custom fn."""
def test_result_pass_status(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "PASS"})
self.assertIn("Done", buf.getvalue())
def test_result_ok_status(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "OK", "message": "fine"})
self.assertIn("fine", buf.getvalue())
def test_result_dry_run_status(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "DRY_RUN"})
self.assertIn("DRY_RUN", buf.getvalue())
def test_result_fail_status(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "FAIL", "message": "broken"})
self.assertIn("broken", buf.getvalue())
def test_result_error_status_no_message(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "ERROR"})
self.assertIn("ERROR", buf.getvalue())
def test_result_aborted_status(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "ABORTED", "message": "user cancelled"})
self.assertIn("Aborted", buf.getvalue())
self.assertIn("user cancelled", buf.getvalue())
def test_result_aborted_no_message(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "ABORTED"})
self.assertIn("Aborted", buf.getvalue())
def test_result_unknown_status(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "WEIRD"})
self.assertIn("WEIRD", buf.getvalue())
def test_result_unknown_status_with_message(self):
buf = io.StringIO()
with redirect_stderr(buf):
result({"status": "CUSTOM", "message": "hello"})
self.assertIn("hello", buf.getvalue())
def test_result_custom_human_fn(self):
called = []
def my_fn(d):
called.append(d)
result({"status": "PASS"}, human_fn=my_fn)
self.assertEqual(len(called), 1)
self.assertEqual(called[0]["status"], "PASS")
def test_result_json_mode(self):
set_json_mode(True)
buf = io.StringIO()
with redirect_stdout(buf):
result({"status": "OK"})
out = json.loads(buf.getvalue())
self.assertEqual(out["status"], "OK")
set_json_mode(False)
class TestUISingleton(unittest.TestCase):
"""Test the ui singleton exposes all methods."""
def test_ui_has_all_methods(self):
for attr in ["header", "step", "substep", "success", "error", "warn",
"info", "detail", "hint", "blank", "divider", "table",
"file_action", "result", "is_json"]:
self.assertTrue(hasattr(ui, attr), f"ui missing {attr}")
# ---------------------------------------------------------------------------
# Human formatters from commands
# ---------------------------------------------------------------------------
class TestHumanValidate(_HumanModeBase):
"""Test _human_validate formatter."""
def test_validate_pass(self):
from cypilot.commands.validate import _human_validate
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate({
"status": "PASS",
"artifacts_validated": 5,
"error_count": 0,
"warning_count": 0,
"code_files_scanned": 10,
"coverage": "50/50",
"next_step": "Review content",
})
out = buf.getvalue()
self.assertIn("Validate", out)
self.assertIn("passed", out)
def test_validate_fail_with_errors(self):
from cypilot.commands.validate import _human_validate
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate({
"status": "FAIL",
"artifacts_validated": 3,
"error_count": 2,
"warning_count": 1,
"errors": [
{"message": "Missing section", "path": "DESIGN.md", "line": "10"},
{"message": "Bad ref"},
] + [{"message": f"err{i}"} for i in range(35)],
"warnings": [{"message": f"w{i}"} for i in range(20)],
})
out = buf.getvalue()
self.assertIn("failed", out)
self.assertIn("DESIGN.md:10", out)
self.assertIn("more error", out)
self.assertIn("more warning", out)
def test_validate_other_status(self):
from cypilot.commands.validate import _human_validate
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate({"status": "PARTIAL", "artifact_count": 1, "error_count": 0, "warning_count": 0})
self.assertIn("PARTIAL", buf.getvalue())
def test_validate_string_errors(self):
from cypilot.commands.validate import _human_validate
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate({
"status": "FAIL", "error_count": 1, "warning_count": 1,
"errors": ["simple error string"],
"warnings": ["simple warning"],
})
out = buf.getvalue()
self.assertIn("simple error string", out)
self.assertIn("simple warning", out)
def test_validate_rich_issue_fields(self):
"""Cover _format_issue with code, reasons, fixing_prompt, extra keys."""
from cypilot.commands.validate import _human_validate
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate({
"status": "FAIL", "error_count": 2, "warning_count": 1,
"errors": [
{
"message": "Missing ID", "code": "E001",
"path": "DESIGN.md", "line": 5,
"reasons": ["No template match", "Slug invalid"],
"fixing_prompt": "Add an ID block",
"id": "cpt-test-item",
},
{"message": "No location error"},
],
"warnings": [
{"message": "Warn with list field", "ids": ["a", "b"]},
],
})
out = buf.getvalue()
self.assertIn("E001", out)
self.assertIn("No template match", out)
self.assertIn("Fix:", out)
self.assertIn("cpt-test-item", out)
self.assertIn("No location error", out)
self.assertIn("a, b", out)
def test_validate_issue_location_without_line(self):
"""Cover _issue_location with path but no line."""
from cypilot.commands.validate import _human_validate
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate({
"status": "FAIL", "error_count": 1, "warning_count": 0,
"errors": [{"message": "err", "path": "README.md"}],
})
out = buf.getvalue()
self.assertIn("README.md", out)
class TestHumanValidateKits(_HumanModeBase):
"""Test _human_validate_kits formatter."""
def test_pass(self):
from cypilot.commands.validate_kits import _human_validate_kits
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate_kits({
"status": "PASS", "kits_validated": 1,
"error_count": 0,
"kits": [{"kit": "sdlc", "status": "PASS"}],
})
out = buf.getvalue()
self.assertIn("all passed", out)
self.assertIn("sdlc", out)
def test_fail_with_errors(self):
from cypilot.commands.validate_kits import _human_validate_kits
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate_kits({
"status": "FAIL", "kits_validated": 1,
"error_count": 3,
"kits": [{"kit": "sdlc", "status": "FAIL", "error_count": 3}],
"failed_kits": [{"kit": "sdlc", "error_count": 3}],
})
out = buf.getvalue()
self.assertIn("error(s)", out)
self.assertIn("sdlc", out)
class TestHumanValidateToc(_HumanModeBase):
"""Test _human_validate_toc formatter."""
def test_pass(self):
from cypilot.commands.validate_toc import _human_validate_toc
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate_toc({
"status": "PASS", "files_validated": 2,
"error_count": 0, "warning_count": 0,
"results": [{"file": "a.md", "status": "PASS"}],
})
out = buf.getvalue()
self.assertIn("all TOCs correct", out)
def test_fail_with_errors_and_warnings(self):
from cypilot.commands.validate_toc import _human_validate_toc
buf = io.StringIO()
with redirect_stderr(buf):
_human_validate_toc({
"status": "FAIL", "files_validated": 1,
"error_count": 1, "warning_count": 1,
"results": [
{"file": "README.md", "status": "FAIL", "error_count": 1, "warning_count": 1,
"errors": ["missing toc"], "warnings": ["stale toc"]},
],
})
out = buf.getvalue()
self.assertIn("error(s) found", out)
self.assertIn("README.md", out)
self.assertIn("stale toc", out)
class TestHumanToc(_HumanModeBase):
"""Test _human_toc formatter."""
def test_pass(self):
from cypilot.commands.toc import _human_toc
buf = io.StringIO()
with redirect_stderr(buf):
_human_toc({
"status": "PASS", "files_processed": 2,
"results": [
{"file": "a.md", "status": "UPDATED"},
{"file": "b.md", "status": "UNCHANGED"},
{"file": "c.md", "status": "CREATED"},
{"file": "d.md", "status": "ERROR", "message": "bad file"},
{"file": "e.md", "status": "CUSTOM"},
{"file": "f.md", "status": "OK", "validation": {"status": "FAIL", "details": ["stale entry"]}},
],
})
out = buf.getvalue()
self.assertIn("updated", out)
self.assertIn("unchanged", out)
self.assertIn("created", out)
self.assertIn("bad file", out)
self.assertIn("CUSTOM", out)
self.assertIn("stale entry", out)
def test_validation_fail_status(self):
from cypilot.commands.toc import _human_toc
buf = io.StringIO()
with redirect_stderr(buf):
_human_toc({
"status": "VALIDATION_FAIL", "files_processed": 1,
"results": [],
})
self.assertIn("validation errors", buf.getvalue())
def test_other_status(self):
from cypilot.commands.toc import _human_toc
buf = io.StringIO()
with redirect_stderr(buf):
_human_toc({
"status": "PARTIAL", "files_processed": 1,
"results": [],
})
self.assertIn("PARTIAL", buf.getvalue())
class TestHumanSpecCoverage(_HumanModeBase):
"""Test _human_spec_coverage formatter."""
def test_pass(self):
from cypilot.commands.spec_coverage import _human_spec_coverage
buf = io.StringIO()
with redirect_stderr(buf):
_human_spec_coverage({
"status": "PASS",
"summary": {
"covered_files": 8,
"total_files": 10,
"coverage_pct": 95.0,
"granularity_score": 0.88,
},
})
out = buf.getvalue()
self.assertIn("95.0%", out)
self.assertIn("thresholds met", out)
def test_fail(self):
from cypilot.commands.spec_coverage import _human_spec_coverage
buf = io.StringIO()
with redirect_stderr(buf):
_human_spec_coverage({
"status": "FAIL",
"summary": {
"covered_files": 2,
"total_files": 10,
"coverage_pct": 50.0,
"granularity_score": 0.3,
},
"threshold_failures": ["coverage below 80%"],
})
out = buf.getvalue()
self.assertIn("Threshold check failed", out)
self.assertIn("coverage below 80%", out)
def test_other_status(self):
from cypilot.commands.spec_coverage import _human_spec_coverage
buf = io.StringIO()
with redirect_stderr(buf):
_human_spec_coverage({"status": "PARTIAL", "summary": {}})
self.assertIn("PARTIAL", buf.getvalue())
class TestHumanInfo(_HumanModeBase):
"""Test _human_info formatter."""
def test_full_info(self):
from cypilot.commands.adapter_info import _human_info
buf = io.StringIO()
with redirect_stderr(buf):
_human_info({
"status": "FOUND",
"project_name": "MyProject",
"project_root": "/tmp/myproject",
"relative_path": ".bootstrap",
"config_version": "1.0",
"has_config": True,
"directories": {".core": True, ".gen": True, "config": True},
"kit_details": {
"sdlc": {
"name": "SDLC Kit", "slug": "sdlc",
"version": "4",
"artifact_kinds": ["PRD", "DESIGN"],
"workflows": ["pr-review"],
},
},
"autodetect_registry": {
"systems": [
{"name": "Main", "slug": "main", "kit": "sdlc",
"children": [{"name": "Sub", "slug": "sub"}]},
],
},
"artifacts_registry": {
"systems": [
{
"name": "Main", "slug": "main", "kit": "sdlc",
"artifacts": [
{"path": "arch/PRD.md", "kind": "PRD", "traceability": "FULL"},
{"path": "arch/DESIGN.md", "kind": "DESIGN", "traceability": "DOCS-ONLY"},
],
"codebase": [
{"path": "src/", "extensions": [".py", ".ts"]},
{"path": "lib/"},
],
"children": [
{
"name": "Sub", "slug": "sub",
"artifacts": [{"path": "sub/PRD.md", "kind": "PRD"}],
"codebase": [{"path": "sub/src", "extensions": [".py"]}],
},
],
},
],
},
"rules": ["conventions", "testing"],
"agent_integrations": ["windsurf", "cursor"],
"artifacts_registry_error": None,
})
out = buf.getvalue()
self.assertIn("MyProject", out)
self.assertIn(".bootstrap", out)
self.assertIn("1.0", out)
self.assertIn("SDLC Kit", out)
self.assertIn("v4", out)
self.assertIn("PRD, DESIGN", out)
self.assertIn("pr-review", out)
self.assertIn("Main (main)", out)
self.assertIn("arch/PRD.md", out)
self.assertIn("FULL", out)
self.assertIn("Sub (sub)", out)
self.assertIn("conventions", out)
self.assertIn("windsurf", out)
def test_minimal_info(self):
from cypilot.commands.adapter_info import _human_info
buf = io.StringIO()
with redirect_stderr(buf):
_human_info({
"status": "FOUND",
"project_root": "/tmp/x",
"cypilot_dir": "/tmp/x/cypilot",
"has_config": False,
"directories": {".core": True, ".gen": True, "config": True},
"rules": [],
"kit_details": {},
"agent_integrations": [],
})
out = buf.getvalue()
self.assertIn("Project root", out)
self.assertNotIn("Missing", out)
def test_info_with_registry_error(self):
from cypilot.commands.adapter_info import _human_info
buf = io.StringIO()
with redirect_stderr(buf):
_human_info({
"status": "FOUND",
"project_root": "/tmp/x",
"has_config": True,
"directories": {},
"rules": [],
"kit_details": {},
"agent_integrations": [],
"artifacts_registry_error": "MISSING",
})
self.assertIn("MISSING", buf.getvalue())
def test_info_kit_no_drift(self):
from cypilot.commands.adapter_info import _human_info
buf = io.StringIO()
with redirect_stderr(buf):
_human_info({
"status": "FOUND",
"project_root": "/tmp",
"has_config": True,
"directories": {},
"kit_details": {
"sdlc": {"name": "Kit", "version": "4"},
},
"rules": [],
"agent_integrations": [],
})
out = buf.getvalue()
self.assertIn("v4", out)
self.assertNotIn("migration", out)
def test_info_autodetect_systems_fallback(self):
"""When artifacts_registry has no systems, falls back to autodetect_registry."""
from cypilot.commands.adapter_info import _human_info
buf = io.StringIO()
with redirect_stderr(buf):
_human_info({
"status": "FOUND",
"project_root": "/tmp",
"has_config": True,
"directories": {},
"kit_details": {},
"rules": [],
"agent_integrations": [],
"autodetect_registry": {
"systems": [{"name": "Sys", "slug": "sys"}],
},
"artifacts_registry": {},
})
self.assertIn("Sys (sys)", buf.getvalue())
class TestReadKitConf(unittest.TestCase):
"""Test _read_kit_conf helper."""
def test_reads_valid_conf(self):
from cypilot.commands.adapter_info import _read_kit_conf
with TemporaryDirectory() as td:
conf = Path(td) / "conf.toml"
conf.write_text('version = 4\nslug = "sdlc"\nname = "SDLC Kit"\n')
result = _read_kit_conf(conf)
self.assertEqual(result["version"], 4)
self.assertEqual(result["slug"], "sdlc")
self.assertEqual(result["name"], "SDLC Kit")
def test_missing_file(self):
from cypilot.commands.adapter_info import _read_kit_conf
result = _read_kit_conf(Path("/nonexistent/conf.toml"))
self.assertEqual(result, {})
def test_invalid_toml(self):
from cypilot.commands.adapter_info import _read_kit_conf
with TemporaryDirectory() as td:
conf = Path(td) / "conf.toml"
conf.write_text("not valid toml {{{")
result = _read_kit_conf(conf)
self.assertEqual(result, {})
# ---------------------------------------------------------------------------
# CLI help branch
# ---------------------------------------------------------------------------
class TestCLIHelpHumanMode(_HumanModeBase):
"""Test CLI help in human mode."""
def test_help_human_mode(self):
from cypilot.cli import main
buf = io.StringIO()
with redirect_stderr(buf):
rc = main(["--help"])
self.assertEqual(rc, 0)
out = buf.getvalue()
self.assertIn("Cypilot CLI", out)
self.assertIn("validate", out)
self.assertIn("--json", out)
def test_empty_command_human_mode(self):
from cypilot.cli import main
buf = io.StringIO()
with redirect_stderr(buf):
rc = main([])
self.assertEqual(rc, 0)
self.assertIn("Cypilot CLI", buf.getvalue())
# ---------------------------------------------------------------------------
# init.py human formatters + _inject_root_claude
# ---------------------------------------------------------------------------
class TestHumanInitOk(_HumanModeBase):
"""Test _human_init_ok formatter."""
def test_normal_init(self):
from cypilot.commands.init import _human_init_ok
buf = io.StringIO()
with redirect_stderr(buf):
_human_init_ok(
{"status": "PASS", "dry_run": False, "actions": {}},
Path("/tmp/proj"),
Path("/tmp/proj/cypilot"),
"cypilot",
"MyProject",
{"sdlc": {"files_written": 10, "artifact_kinds": ["PRD", "DESIGN"]}},
)
out = buf.getvalue()
self.assertIn("Cypilot Init", out)
self.assertIn("MyProject", out)
self.assertIn("Core files", out)
self.assertIn("sdlc", out)
self.assertIn("initialized", out)
self.assertIn("Next steps", out)
def test_dry_run_init(self):
from cypilot.commands.init import _human_init_ok
buf = io.StringIO()
with redirect_stderr(buf):
_human_init_ok(
{"status": "PASS", "dry_run": True, "actions": {}},
Path("/tmp/proj"),
Path("/tmp/proj/cypilot"),
"cypilot",
"Proj",
{},
)
out = buf.getvalue()
self.assertIn("dry-run", out)
self.assertIn("Dry run complete", out)
def test_no_kits(self):
from cypilot.commands.init import _human_init_ok
buf = io.StringIO()
with redirect_stderr(buf):
_human_init_ok(
{"status": "PASS", "dry_run": False, "actions": {}},
Path("/tmp/proj"),
Path("/tmp/proj/cypilot"),
"cypilot",
"Proj",
{},
)
out = buf.getvalue()
self.assertNotIn("Kits installed", out)
class TestHumanInitError(_HumanModeBase):
"""Test _human_init_error formatter."""
def test_dict_errors(self):
from cypilot.commands.init import _human_init_error
buf = io.StringIO()
with redirect_stderr(buf):
_human_init_error({
"status": "ERROR",
"errors": [
{"path": "core.toml", "error": "permission denied"},
"simple error string",
],
})
out = buf.getvalue()
self.assertIn("failed", out)
self.assertIn("core.toml", out)
self.assertIn("simple error string", out)
def test_empty_errors(self):
from cypilot.commands.init import _human_init_error
buf = io.StringIO()
with redirect_stderr(buf):
_human_init_error({"status": "ERROR", "errors": []})
self.assertIn("failed", buf.getvalue())
class TestInjectRootClaude(unittest.TestCase):
"""Test _inject_root_claude update/insert paths."""
def test_update_existing_block(self):
from cypilot.commands.init import _inject_root_claude, MARKER_START, MARKER_END
with TemporaryDirectory() as td:
root = Path(td)
claude = root / "CLAUDE.md"
# Write a stale block
claude.write_text(f"Before\n{MARKER_START}\nOLD CONTENT\n{MARKER_END}\nAfter\n")
action = _inject_root_claude(root, "cypilot")
self.assertEqual(action, "updated")
content = claude.read_text()
self.assertIn(MARKER_START, content)
self.assertIn("After", content)
def test_insert_into_existing_no_markers(self):
from cypilot.commands.init import _inject_root_claude
with TemporaryDirectory() as td:
root = Path(td)
claude = root / "CLAUDE.md"
claude.write_text("Existing content\n")
action = _inject_root_claude(root, "cypilot")
self.assertEqual(action, "updated")
content = claude.read_text()
self.assertIn("Existing content", content)
def test_unchanged_block(self):
from cypilot.commands.init import _inject_root_claude, _compute_managed_block
with TemporaryDirectory() as td:
root = Path(td)
claude = root / "CLAUDE.md"
claude.write_text(_compute_managed_block("cypilot") + "\n")
action = _inject_root_claude(root, "cypilot")
self.assertEqual(action, "unchanged")
def test_create_new(self):
from cypilot.commands.init import _inject_root_claude
with TemporaryDirectory() as td:
root = Path(td)
action = _inject_root_claude(root, "cypilot")
self.assertEqual(action, "created")
self.assertTrue((root / "CLAUDE.md").exists())
class TestReadExistingInstall(unittest.TestCase):
"""Test _read_existing_install edge cases."""
def test_returns_none_when_dir_missing(self):
from cypilot.commands.init import _read_existing_install, MARKER_START
with TemporaryDirectory() as td:
root = Path(td)
agents = root / "AGENTS.md"
agents.write_text(f"{MARKER_START}\n```toml\ncypilot_path = \"missing\"\n```\n")
result = _read_existing_install(root)
self.assertIsNone(result)
def test_returns_none_for_invalid_toml(self):
from cypilot.commands.init import _read_existing_install, MARKER_START
with TemporaryDirectory() as td:
root = Path(td)
agents = root / "AGENTS.md"
agents.write_text(f"{MARKER_START}\n```toml\n{{{{invalid\n```\n")
result = _read_existing_install(root)
self.assertIsNone(result)
# ---------------------------------------------------------------------------
# agents.py human formatter
# ---------------------------------------------------------------------------
class TestHumanAgentsOk(_HumanModeBase):
"""Test _human_generate_agents_ok formatter."""
def test_pass_with_workflows_and_skills(self):
from cypilot.commands.agents import _human_generate_agents_ok
buf = io.StringIO()
with redirect_stderr(buf):
_human_generate_agents_ok(
{"status": "PASS"},
["windsurf", "cursor"],
{
"windsurf": {
"status": "PASS",
"workflows": {
"created": [".windsurf/workflows/gen.md"],
"updated": [".windsurf/workflows/analyze.md"],
"counts": {"created": 1, "updated": 1},
},
"skills": {
"created": [".windsurf/skills/cypilot/SKILL.md"],
"updated": [],
"counts": {"created": 1, "updated": 0},
},
},
"cursor": {
"status": "WARN",
"workflows": {"created": [], "updated": [], "counts": {}},
"skills": {"created": [], "updated": [], "counts": {}},
"errors": ["Missing config"],
},
},
dry_run=False,
)
out = buf.getvalue()
self.assertIn("windsurf", out)
self.assertIn("cursor", out)
self.assertIn("gen.md", out)
self.assertIn("created", out)
self.assertIn("updated", out)
self.assertIn("SKILL.md", out)
self.assertIn("workflow(s)", out)
self.assertIn("Missing config", out)
self.assertIn("Agent integration complete", out)
def test_dry_run(self):
from cypilot.commands.agents import _human_generate_agents_ok
buf = io.StringIO()
with redirect_stderr(buf):
_human_generate_agents_ok(
{"status": "PASS"},
["windsurf"],
{"windsurf": {
"status": "PASS",
"workflows": {"created": [], "updated": [], "counts": {}},
"skills": {"created": [], "updated": [], "counts": {}},
}},
dry_run=True,
)
self.assertIn("Dry run complete", buf.getvalue())
def test_errors_status(self):
from cypilot.commands.agents import _human_generate_agents_ok
buf = io.StringIO()
with redirect_stderr(buf):
_human_generate_agents_ok(
{"status": "PARTIAL"},
["windsurf"],
{"windsurf": {
"status": "ERROR",
"workflows": {"created": [], "updated": [], "counts": {}},
"skills": {"created": [], "updated": [], "counts": {}},
}},
dry_run=False,
)
self.assertIn("errors", buf.getvalue())
class TestEnsureCypilotLocal(unittest.TestCase):
"""Test _ensure_cypilot_local copy paths."""
def test_already_local(self):
from cypilot.commands.agents import _ensure_cypilot_local
with TemporaryDirectory() as td:
root = Path(td)