hyalo-cli 0.14.0

CLI for exploring and managing Markdown knowledge bases with YAML frontmatter
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
use super::common::hyalo_no_hints;
use std::fs;
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// .hyalo.toml creation
// ---------------------------------------------------------------------------

#[test]
fn init_creates_hyalo_toml() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let toml_path = tmp.path().join(".hyalo.toml");
    assert!(toml_path.exists(), ".hyalo.toml should have been created");

    let content = fs::read_to_string(&toml_path).unwrap();
    assert!(
        content.contains("dir ="),
        ".hyalo.toml should contain a dir setting; got: {content}"
    );
}

#[test]
fn init_does_not_overwrite_existing_toml() {
    // Without an explicit --dir flag, .hyalo.toml is skipped if it already exists.
    let tmp = TempDir::new().unwrap();
    let original = "dir = \"my-vault\"\n";
    fs::write(tmp.path().join(".hyalo.toml"), original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("skipped"),
        "expected 'skipped' in stdout; got: {stdout}"
    );

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert_eq!(
        content, original,
        ".hyalo.toml should not have been modified when no --dir given"
    );
}

#[test]
fn init_dir_flag_updates_existing_toml() {
    // When --dir is explicitly given, .hyalo.toml is updated even if it already exists.
    let tmp = TempDir::new().unwrap();
    fs::write(tmp.path().join(".hyalo.toml"), "dir = \"old\"\n").unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--dir", "new-dir"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' in stdout; got: {stdout}"
    );

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert_eq!(
        content, "dir = \"new-dir\"\n",
        ".hyalo.toml should have been updated to new-dir"
    );
}

#[test]
fn init_with_dir_flag() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--dir", "docs"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("dir = \"docs\""),
        ".hyalo.toml should contain dir = \"docs\"; got: {content}"
    );
}

#[test]
fn init_auto_detects_docs_dir() {
    let tmp = TempDir::new().unwrap();
    // Create docs/ with a .md file so auto-detection picks it up
    fs::create_dir_all(tmp.path().join("docs")).unwrap();
    fs::write(tmp.path().join("docs").join("note.md"), "# Hello").unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("dir = \"docs\""),
        "should have auto-detected docs/; got: {content}"
    );
}

#[test]
fn init_falls_back_to_dot_when_no_doc_dir() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("dir = \".\""),
        "should have defaulted to .; got: {content}"
    );
}

#[test]
fn init_smart_detection_picks_dir_with_most_md() {
    let tmp = TempDir::new().unwrap();

    // docs: 1 md file
    fs::create_dir_all(tmp.path().join("docs")).unwrap();
    fs::write(tmp.path().join("docs").join("a.md"), "# A").unwrap();

    // knowledgebase: 3 md files (including nested)
    fs::create_dir_all(tmp.path().join("knowledgebase").join("sub")).unwrap();
    fs::write(tmp.path().join("knowledgebase").join("b.md"), "# B").unwrap();
    fs::write(
        tmp.path().join("knowledgebase").join("sub").join("c.md"),
        "# C",
    )
    .unwrap();
    fs::write(
        tmp.path().join("knowledgebase").join("sub").join("d.md"),
        "# D",
    )
    .unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("dir = \"knowledgebase\""),
        "should have picked knowledgebase (most .md files); got: {content}"
    );
}

// ---------------------------------------------------------------------------
// --claude flag: skill creation
// ---------------------------------------------------------------------------

#[test]
fn init_claude_creates_skill() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let skill_path = tmp
        .path()
        .join(".claude")
        .join("skills")
        .join("hyalo")
        .join("SKILL.md");
    assert!(skill_path.exists(), "SKILL.md should have been created");

    let content = fs::read_to_string(&skill_path).unwrap();
    assert!(
        content.contains("hyalo"),
        "SKILL.md should contain hyalo content; got: {content}"
    );
    assert!(
        content.contains("name: hyalo"),
        "SKILL.md should have frontmatter name field; got: {content}"
    );
}

#[test]
fn init_claude_overwrites_existing_skill() {
    // Skills are always overwritten on re-run; summary says "updated".
    let tmp = TempDir::new().unwrap();
    let skill_dir = tmp.path().join(".claude").join("skills").join("hyalo");
    fs::create_dir_all(&skill_dir).unwrap();
    let skill_path = skill_dir.join("SKILL.md");
    let original = "---\nname: custom\n---\nstale content\n";
    fs::write(&skill_path, original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' in stdout for overwritten SKILL.md; got: {stdout}"
    );

    // Content should have been replaced with the canonical skill content
    let content = fs::read_to_string(&skill_path).unwrap();
    assert_ne!(
        content, original,
        "SKILL.md should have been overwritten, not preserved"
    );
    assert!(
        content.contains("name: hyalo"),
        "overwritten SKILL.md should contain canonical name field; got: {content}"
    );
}

// ---------------------------------------------------------------------------
// --claude flag: hyalo-tidy skill creation
// ---------------------------------------------------------------------------

#[test]
fn init_claude_creates_tidy_skill() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let tidy_skill_path = tmp
        .path()
        .join(".claude")
        .join("skills")
        .join("hyalo-tidy")
        .join("SKILL.md");
    assert!(
        tidy_skill_path.exists(),
        "hyalo-tidy SKILL.md should have been created"
    );

    let content = fs::read_to_string(&tidy_skill_path).unwrap();
    assert!(
        content.contains("name: hyalo-tidy"),
        "SKILL.md should have frontmatter name field; got: {content}"
    );
    assert!(
        content.contains("Knowledgebase Consolidation"),
        "SKILL.md should contain tidy skill content"
    );
}

#[test]
fn init_claude_overwrites_existing_tidy_skill() {
    // Tidy skill is always overwritten on re-run; summary says "updated".
    let tmp = TempDir::new().unwrap();
    let tidy_skill_dir = tmp.path().join(".claude").join("skills").join("hyalo-tidy");
    fs::create_dir_all(&tidy_skill_dir).unwrap();
    let tidy_skill_path = tidy_skill_dir.join("SKILL.md");
    let original = "---\nname: custom-tidy\n---\nstale content\n";
    fs::write(&tidy_skill_path, original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' for overwritten tidy SKILL.md; got: {stdout}"
    );

    let content = fs::read_to_string(&tidy_skill_path).unwrap();
    assert_ne!(
        content, original,
        "tidy SKILL.md should have been overwritten, not preserved"
    );
    assert!(
        content.contains("name: hyalo-tidy"),
        "overwritten tidy SKILL.md should contain canonical name field; got: {content}"
    );
}

// ---------------------------------------------------------------------------
// --claude flag: knowledgebase rule creation
// ---------------------------------------------------------------------------

#[test]
fn init_claude_creates_rule() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let rule_path = tmp
        .path()
        .join(".claude")
        .join("rules")
        .join("knowledgebase.md");
    assert!(
        rule_path.exists(),
        ".claude/rules/knowledgebase.md should have been created"
    );

    let content = fs::read_to_string(&rule_path).unwrap();
    assert!(
        content.contains("paths:"),
        "rule should contain a paths: key; got: {content}"
    );
    // Should not contain the template placeholder
    assert!(
        !content.contains("hyalo-knowledgebase/**"),
        "rule should have the placeholder replaced; got: {content}"
    );
}

#[test]
fn init_claude_overwrites_existing_rule() {
    // Rule is always overwritten on re-run; summary says "updated".
    let tmp = TempDir::new().unwrap();
    let rules_dir = tmp.path().join(".claude").join("rules");
    fs::create_dir_all(&rules_dir).unwrap();
    let rule_path = rules_dir.join("knowledgebase.md");
    let original = "---\npaths:\n  - \"old-vault/**\"\n---\nold content\n";
    fs::write(&rule_path, original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' for overwritten rule; got: {stdout}"
    );

    let content = fs::read_to_string(&rule_path).unwrap();
    assert_ne!(
        content, original,
        "rule should have been overwritten, not preserved"
    );
}

#[test]
fn init_claude_rule_uses_detected_dir() {
    // When a docs/ dir has .md files, the rule paths should reference docs/**.
    let tmp = TempDir::new().unwrap();
    fs::create_dir_all(tmp.path().join("docs")).unwrap();
    fs::write(tmp.path().join("docs").join("note.md"), "# Hello").unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let rule_path = tmp
        .path()
        .join(".claude")
        .join("rules")
        .join("knowledgebase.md");
    let content = fs::read_to_string(&rule_path).unwrap();
    assert!(
        content.contains("docs/**"),
        "rule paths should reference docs/**; got: {content}"
    );
    assert!(
        !content.contains("hyalo-knowledgebase/**"),
        "rule should not contain the placeholder; got: {content}"
    );
}

#[test]
fn init_claude_rule_uses_explicit_dir() {
    // --dir my-vault should be reflected in the rule paths.
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude", "--dir", "my-vault"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let rule_path = tmp
        .path()
        .join(".claude")
        .join("rules")
        .join("knowledgebase.md");
    let content = fs::read_to_string(&rule_path).unwrap();
    assert!(
        content.contains("my-vault/**"),
        "rule paths should reference my-vault/**; got: {content}"
    );
    assert!(
        !content.contains("hyalo-knowledgebase/**"),
        "rule should not contain the placeholder; got: {content}"
    );
}

// ---------------------------------------------------------------------------
// --claude flag: CLAUDE.md creation and update
// ---------------------------------------------------------------------------

#[test]
fn init_claude_creates_claude_md() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let claude_md_path = tmp.path().join(".claude").join("CLAUDE.md");
    assert!(
        claude_md_path.exists(),
        ".claude/CLAUDE.md should have been created"
    );

    let content = fs::read_to_string(&claude_md_path).unwrap();
    assert!(
        content.contains("hyalo"),
        ".claude/CLAUDE.md should contain hyalo hint; got: {content}"
    );
    assert!(
        content.contains("<!-- hyalo:start -->"),
        ".claude/CLAUDE.md should contain start marker; got: {content}"
    );
    assert!(
        content.contains("<!-- hyalo:end -->"),
        ".claude/CLAUDE.md should contain end marker; got: {content}"
    );
}

#[test]
fn init_claude_appends_to_existing_claude_md() {
    let tmp = TempDir::new().unwrap();
    let claude_dir = tmp.path().join(".claude");
    fs::create_dir_all(&claude_dir).unwrap();
    let claude_md_path = claude_dir.join("CLAUDE.md");
    let original = "# Project Instructions\n\nSome existing content.\n";
    fs::write(&claude_md_path, original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' in stdout; got: {stdout}"
    );

    let content = fs::read_to_string(&claude_md_path).unwrap();
    // Original content must still be present
    assert!(
        content.contains("Some existing content."),
        "original content should be preserved; got: {content}"
    );
    // Hint must have been appended with markers
    assert!(
        content.contains("hyalo"),
        "hyalo hint should have been appended; got: {content}"
    );
    assert!(
        content.contains("<!-- hyalo:start -->"),
        "start marker should be present; got: {content}"
    );
    assert!(
        content.contains("<!-- hyalo:end -->"),
        "end marker should be present; got: {content}"
    );
}

#[test]
fn init_claude_no_duplicate_in_claude_md() {
    let tmp = TempDir::new().unwrap();
    let claude_dir = tmp.path().join(".claude");
    fs::create_dir_all(&claude_dir).unwrap();
    let claude_md_path = claude_dir.join("CLAUDE.md");
    // Pre-populate with the marker-wrapped section (as a prior run would have written).
    let hint = "Use `hyalo` CLI (not Read/Grep/Glob) for all markdown knowledgebase operations (frontmatter, tags, tasks, search). Run `hyalo --help` for usage. Use `--format text` for compact LLM-friendly output.";
    let original = format!("<!-- hyalo:start -->\n{hint}\n<!-- hyalo:end -->\n");
    fs::write(&claude_md_path, &original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' (replaced managed section) in stdout; got: {stdout}"
    );

    // The file should contain exactly one copy of the hint and one pair of markers.
    let content = fs::read_to_string(&claude_md_path).unwrap();
    let occurrences = content.matches("hyalo --help").count();
    assert_eq!(
        occurrences, 1,
        "hint should appear exactly once; got {occurrences} times in: {content}"
    );
    assert_eq!(
        content.matches("<!-- hyalo:start -->").count(),
        1,
        "start marker should appear exactly once; got: {content}"
    );
    assert_eq!(
        content.matches("<!-- hyalo:end -->").count(),
        1,
        "end marker should appear exactly once; got: {content}"
    );
}

#[test]
fn init_claude_updates_managed_section_on_rerun() {
    // Run init twice; verify the section is replaced (not duplicated) and
    // surrounding content is preserved.
    let tmp = TempDir::new().unwrap();
    let claude_dir = tmp.path().join(".claude");
    fs::create_dir_all(&claude_dir).unwrap();
    let claude_md_path = claude_dir.join("CLAUDE.md");
    let surrounding = "# Project Rules\n\nKeep these instructions.\n";
    fs::write(&claude_md_path, surrounding).unwrap();

    // First run — appends section.
    let out1 = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();
    let stderr1 = String::from_utf8_lossy(&out1.stderr);
    assert!(out1.status.success(), "first run stderr: {stderr1}");

    // Second run — should replace, not duplicate.
    let out2 = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();
    let stdout2 = String::from_utf8_lossy(&out2.stdout);
    let stderr2 = String::from_utf8_lossy(&out2.stderr);
    assert!(out2.status.success(), "second run stderr: {stderr2}");
    assert!(
        stdout2.contains("updated"),
        "second run should report 'updated'; got: {stdout2}"
    );

    let content = fs::read_to_string(&claude_md_path).unwrap();
    // Surrounding content preserved.
    assert!(
        content.contains("Keep these instructions."),
        "surrounding content should be preserved; got: {content}"
    );
    // Exactly one copy of the managed section.
    assert_eq!(
        content.matches("<!-- hyalo:start -->").count(),
        1,
        "start marker should appear exactly once; got: {content}"
    );
    assert_eq!(
        content.matches("<!-- hyalo:end -->").count(),
        1,
        "end marker should appear exactly once; got: {content}"
    );
    assert_eq!(
        content.matches("hyalo --help").count(),
        1,
        "hint should appear exactly once; got: {content}"
    );
}

#[test]
fn init_claude_appends_section_when_no_markers_exist() {
    // Pre-populate CLAUDE.md with plain content (no markers). Init should append
    // the managed section rather than replacing anything.
    let tmp = TempDir::new().unwrap();
    let claude_dir = tmp.path().join(".claude");
    fs::create_dir_all(&claude_dir).unwrap();
    let claude_md_path = claude_dir.join("CLAUDE.md");
    let original = "# Header\n\nSome existing instructions.\n\n# Footer\n";
    fs::write(&claude_md_path, original).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("updated"),
        "expected 'updated' in stdout; got: {stdout}"
    );

    let content = fs::read_to_string(&claude_md_path).unwrap();
    assert!(
        content.contains("# Header"),
        "header preserved; got: {content}"
    );
    assert!(
        content.contains("# Footer"),
        "footer preserved; got: {content}"
    );
    assert!(
        content.contains("<!-- hyalo:start -->"),
        "start marker added; got: {content}"
    );
    assert!(
        content.contains("<!-- hyalo:end -->"),
        "end marker added; got: {content}"
    );
    assert!(
        content.contains("hyalo --help"),
        "hint present; got: {content}"
    );
}

// ---------------------------------------------------------------------------
// Summary output
// ---------------------------------------------------------------------------

#[test]
fn init_prints_summary_of_actions() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(!stdout.is_empty(), "stdout should contain a summary");
    // Should mention the toml and the skills at minimum
    assert!(
        stdout.contains(".hyalo.toml"),
        "summary should mention .hyalo.toml; got: {stdout}"
    );
    assert!(
        stdout.contains("skills/hyalo/SKILL.md"),
        "summary should mention hyalo SKILL.md; got: {stdout}"
    );
    assert!(
        stdout.contains("skills/hyalo-tidy/SKILL.md"),
        "summary should mention hyalo-tidy SKILL.md; got: {stdout}"
    );
    assert!(
        stdout.contains("CLAUDE.md"),
        "summary should mention CLAUDE.md; got: {stdout}"
    );
}

#[test]
fn init_summary_mentions_rule() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        stdout.contains("rules/knowledgebase.md"),
        "summary should mention .claude/rules/knowledgebase.md; got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// Skill parameterization
// ---------------------------------------------------------------------------

#[test]
fn init_claude_tidy_skill_parameterized_with_dir() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude", "--dir", "my-kb"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let tidy_skill_path = tmp
        .path()
        .join(".claude")
        .join("skills")
        .join("hyalo-tidy")
        .join("SKILL.md");
    let content = fs::read_to_string(&tidy_skill_path).unwrap();

    // Sentinel must have been replaced
    assert!(
        !content.contains("hyalo-knowledgebase"),
        "tidy SKILL.md should not contain sentinel after parameterization; got: {content}"
    );
    // Actual dir name must be present
    assert!(
        content.contains("my-kb"),
        "tidy SKILL.md should contain the configured dir 'my-kb'; got: {content}"
    );
}

#[test]
fn init_claude_hyalo_skill_has_no_sentinel() {
    // The hyalo SKILL.md template doesn't currently use the sentinel — verify
    // that parameterization is a safe no-op (no hyalo-knowledgebase leaks).
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude", "--dir", "custom-docs"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let skill_path = tmp
        .path()
        .join(".claude")
        .join("skills")
        .join("hyalo")
        .join("SKILL.md");
    let content = fs::read_to_string(&skill_path).unwrap();
    assert!(
        !content.contains("hyalo-knowledgebase"),
        "hyalo SKILL.md should not contain sentinel; got: {content}"
    );
}

// ---------------------------------------------------------------------------
// Fuzzy directory auto-detection
// ---------------------------------------------------------------------------

#[test]
fn init_auto_detects_fuzzy_knowledgebase_dir() {
    let tmp = TempDir::new().unwrap();

    // "my-knowledgebase" contains "knowledgebase" → should be auto-detected
    fs::create_dir_all(tmp.path().join("my-knowledgebase")).unwrap();
    fs::write(
        tmp.path().join("my-knowledgebase").join("index.md"),
        "# Index",
    )
    .unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("my-knowledgebase"),
        "should auto-detect 'my-knowledgebase'; stdout: {stdout}, toml: {content}"
    );
}

#[test]
fn init_auto_detects_fuzzy_wiki_dir() {
    let tmp = TempDir::new().unwrap();

    fs::create_dir_all(tmp.path().join("project-wiki")).unwrap();
    fs::write(tmp.path().join("project-wiki").join("home.md"), "# Home").unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init"])
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("project-wiki"),
        "should auto-detect 'project-wiki'; toml: {content}"
    );
}

// ---------------------------------------------------------------------------
// deinit
// ---------------------------------------------------------------------------

#[test]
fn deinit_removes_all_init_claude_artifacts() {
    let tmp = TempDir::new().unwrap();

    // Bootstrap with all Claude artifacts present.
    let init_out = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude", "--dir", "docs"])
        .output()
        .unwrap();
    let init_stderr = String::from_utf8_lossy(&init_out.stderr);
    assert!(init_out.status.success(), "init stderr: {init_stderr}");

    // Verify artifacts were actually created before we deinit.
    assert!(
        tmp.path().join(".hyalo.toml").exists(),
        ".hyalo.toml should exist after init"
    );
    assert!(
        tmp.path().join(".claude/skills/hyalo/SKILL.md").exists(),
        "hyalo SKILL.md should exist after init"
    );

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["deinit"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    assert!(
        !tmp.path().join(".claude/skills/hyalo/SKILL.md").exists(),
        "hyalo SKILL.md should have been removed; stdout: {stdout}"
    );
    assert!(
        !tmp.path()
            .join(".claude/skills/hyalo-tidy/SKILL.md")
            .exists(),
        "hyalo-tidy SKILL.md should have been removed; stdout: {stdout}"
    );
    assert!(
        !tmp.path().join(".claude/rules/knowledgebase.md").exists(),
        "rules/knowledgebase.md should have been removed; stdout: {stdout}"
    );
    assert!(
        !tmp.path().join(".claude/CLAUDE.md").exists(),
        ".claude/CLAUDE.md should have been removed (only managed section); stdout: {stdout}"
    );
    assert!(
        !tmp.path().join(".hyalo.toml").exists(),
        ".hyalo.toml should have been removed; stdout: {stdout}"
    );
    assert!(
        !tmp.path().join(".claude").exists(),
        ".claude/ dir should have been cleaned up; stdout: {stdout}"
    );
    assert!(
        stdout.contains("removed"),
        "stdout should mention 'removed' for each file; got: {stdout}"
    );
}

#[test]
fn deinit_preserves_non_managed_claude_md() {
    let tmp = TempDir::new().unwrap();

    let init_out = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude", "--dir", "docs"])
        .output()
        .unwrap();
    let init_stderr = String::from_utf8_lossy(&init_out.stderr);
    assert!(init_out.status.success(), "init stderr: {init_stderr}");

    // Prepend user content to .claude/CLAUDE.md so it is not purely managed.
    let claude_md_path = tmp.path().join(".claude/CLAUDE.md");
    let existing = fs::read_to_string(&claude_md_path).unwrap();
    let user_prefix = "# My project notes\n\nCustom content.\n\n";
    fs::write(&claude_md_path, format!("{user_prefix}{existing}")).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["deinit"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    assert!(
        claude_md_path.exists(),
        ".claude/CLAUDE.md should still exist when user content is present; stdout: {stdout}"
    );

    let content = fs::read_to_string(&claude_md_path).unwrap();
    assert!(
        content.contains("My project notes"),
        "user content 'My project notes' should be preserved; got: {content}"
    );
    assert!(
        content.contains("Custom content"),
        "user content 'Custom content' should be preserved; got: {content}"
    );
    assert!(
        !content.contains("<!-- hyalo:start -->"),
        "managed section start marker should have been stripped; got: {content}"
    );
    assert!(
        !content.contains("<!-- hyalo:end -->"),
        "managed section end marker should have been stripped; got: {content}"
    );
    assert!(
        stdout.contains("stripped managed section"),
        "stdout should mention 'stripped managed section'; got: {stdout}"
    );
}

#[test]
fn deinit_idempotent() {
    let tmp = TempDir::new().unwrap();

    let init_out = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--claude", "--dir", "docs"])
        .output()
        .unwrap();
    let init_stderr = String::from_utf8_lossy(&init_out.stderr);
    assert!(init_out.status.success(), "init stderr: {init_stderr}");

    // First deinit — should succeed.
    let first = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["deinit"])
        .output()
        .unwrap();
    let first_stderr = String::from_utf8_lossy(&first.stderr);
    assert!(
        first.status.success(),
        "first deinit stderr: {first_stderr}"
    );

    // Second deinit — should also succeed with "skipped" messages.
    let second = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["deinit"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&second.stdout);
    let stderr = String::from_utf8_lossy(&second.stderr);
    assert!(second.status.success(), "second deinit stderr: {stderr}");
    assert!(
        stdout.contains("skipped"),
        "second deinit should report 'skipped' for already-removed files; got: {stdout}"
    );
    assert!(
        stdout.contains("skipped") && stdout.contains(".hyalo.toml"),
        "second deinit should report '.hyalo.toml' as skipped (not found); got: {stdout}"
    );
}

#[test]
fn deinit_removes_hyalo_toml() {
    let tmp = TempDir::new().unwrap();

    // Init without --claude so only .hyalo.toml is created.
    let init_out = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--dir", "docs"])
        .output()
        .unwrap();
    let init_stderr = String::from_utf8_lossy(&init_out.stderr);
    assert!(init_out.status.success(), "init stderr: {init_stderr}");

    let toml_path = tmp.path().join(".hyalo.toml");
    assert!(toml_path.exists(), ".hyalo.toml should exist after init");

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["deinit"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");
    assert!(
        !toml_path.exists(),
        ".hyalo.toml should have been removed; stdout: {stdout}"
    );
    assert!(
        stdout.contains("removed") && stdout.contains(".hyalo.toml"),
        "stdout should confirm removal of .hyalo.toml; got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// init --dir missing directory creation
// ---------------------------------------------------------------------------

#[test]
fn init_dir_creates_missing_directory() {
    let tmp = TempDir::new().unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--dir", "my-new-docs"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    assert!(
        tmp.path().join("my-new-docs").is_dir(),
        "my-new-docs/ directory should have been created; stdout: {stdout}"
    );
    assert!(
        stdout.contains("created") && stdout.contains("my-new-docs/"),
        "stdout should mention 'created  my-new-docs/'; got: {stdout}"
    );

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("my-new-docs"),
        ".hyalo.toml should reference 'my-new-docs'; got: {content}"
    );
}

#[test]
fn init_dir_does_not_create_existing_directory() {
    let tmp = TempDir::new().unwrap();

    fs::create_dir_all(tmp.path().join("existing-docs")).unwrap();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["init", "--dir", "existing-docs"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "stderr: {stderr}");

    assert!(
        !stdout.contains("created  existing-docs/"),
        "stdout should NOT report creating an already-existing dir; got: {stdout}"
    );

    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        content.contains("existing-docs"),
        ".hyalo.toml should reference 'existing-docs'; got: {content}"
    );
}