gwm-cli 1.6.1

git worktree manager — TUI + CLI, native libgit2, per-repo bootstrap
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
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
//! `gwm doctor` checks. Each test exercises one diagnostic in isolation.

mod common;

use common::init_repo;
use gwm::config::Config;
use gwm::doctor::{self, CheckStatus, DoctorCtx, Severity};
use std::sync::{Mutex, OnceLock};

/// Serialise the env-mutating tests in this binary (only the forge-CLI
/// override probe today), since `set_var` is process-wide.
fn env_lock() -> &'static Mutex<()> {
  static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
  LOCK.get_or_init(|| Mutex::new(()))
}

fn ctx_for<'a>(repo: &'a git2::Repository, workdir: &'a std::path::Path, config: &'a Config) -> DoctorCtx<'a> {
  DoctorCtx {
    repo_workdir: workdir,
    repo,
    config,
    // Isolated by default: no global layer is read for injected test contexts.
    global_config_path: None,
  }
}

#[test]
fn fresh_repo_without_config_reports_defaults_assumed() {
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let cfg = report
    .checks
    .iter()
    .find(|c| c.name.contains(".gwm.toml"))
    .expect("expected a `.gwm.toml` check in the report");

  // Missing config is not an error — defaults are perfectly usable.
  assert_eq!(cfg.status, CheckStatus::Ok);
  assert!(
    cfg.detail.to_lowercase().contains("default"),
    "missing config should mention 'defaults assumed', got: {}",
    cfg.detail
  );
}

#[test]
fn invalid_toml_marks_config_check_failed_with_severity_failed() {
  let (dir, repo) = init_repo();
  std::fs::write(dir.path().join(".gwm.toml"), "this is = not valid [toml").unwrap();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let cfg = report
    .checks
    .iter()
    .find(|c| c.name.contains(".gwm.toml"))
    .expect("expected a `.gwm.toml` check");

  assert_eq!(cfg.status, CheckStatus::Failed);
  assert_eq!(report.severity(), Severity::Failed);
  assert_eq!(report.exit_code(), 2);
}

#[test]
fn valid_toml_marks_config_check_ok() {
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    r#"[worktree]
base = "{home}/wt/{repo}"
path_pattern = "{type}-{issue}-{desc}"
branch_pattern = "{type}/#{issue}-{desc}"
"#,
  )
  .unwrap();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let cfg = report
    .checks
    .iter()
    .find(|c| c.name.contains(".gwm.toml"))
    .expect("expected a `.gwm.toml` check");
  assert_eq!(cfg.status, CheckStatus::Ok);
}

#[test]
fn semantically_invalid_profile_marks_config_check_failed() {
  // #324 review (P2): a profile that parses but is semantically invalid
  // (`dirs = [".."]` escapes the worktree) must fail the `.gwm.toml` check,
  // not be reported green — doctor mirrors what the loader/commands reject.
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    "[clean.profiles.default]\ndirs = [\"..\"]\n",
  )
  .unwrap();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let cfg = report
    .checks
    .iter()
    .find(|c| c.name.contains(".gwm.toml"))
    .expect("expected a `.gwm.toml` check");
  assert_eq!(cfg.status, CheckStatus::Failed);
}

// Severity/exit-code arithmetic is asserted on hand-built reports so the
// test is independent of the environment (whether `lazygit` happens to be
// on PATH, whether `~/cc-worktree/` already exists, etc.). The end-to-end
// `doctor::run` is exercised by the per-check tests above.

#[test]
fn severity_ok_when_all_checks_ok() {
  let mut report = gwm::doctor::DoctorReport::new();
  report.checks.push(gwm::doctor::Check::ok("a", "fine"));
  report.checks.push(gwm::doctor::Check::ok("b", "fine"));
  assert_eq!(report.severity(), Severity::Ok);
  assert_eq!(report.exit_code(), 0);
}

#[test]
fn severity_warning_when_any_check_warns() {
  let mut report = gwm::doctor::DoctorReport::new();
  report.checks.push(gwm::doctor::Check::ok("a", "fine"));
  report.checks.push(gwm::doctor::Check::warning("b", "meh"));
  report.checks.push(gwm::doctor::Check::ok("c", "fine"));
  assert_eq!(report.severity(), Severity::Warning);
  assert_eq!(report.exit_code(), 1);
}

#[test]
fn severity_failed_dominates_warning() {
  let mut report = gwm::doctor::DoctorReport::new();
  report.checks.push(gwm::doctor::Check::warning("a", "meh"));
  report.checks.push(gwm::doctor::Check::failed("b", "broken"));
  report.checks.push(gwm::doctor::Check::warning("c", "meh"));
  // A single Failed must lift the report to Failed regardless of how many
  // Warnings sit alongside — that's the contract the exit-code 2 relies on.
  assert_eq!(report.severity(), Severity::Failed);
  assert_eq!(report.exit_code(), 2);
}

// --------------------------------------------------------------------------
// Check #2 — guard references resolve
// --------------------------------------------------------------------------

#[test]
fn dangling_guard_reference_is_failed() {
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.copy.push(gwm::config::CopyStep {
    from: ".env".into(),
    to: ".env".into(),
    required: false,
    guards: vec!["does-not-exist".into()],
    fallback: None,
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("guard"))
    .expect("expected a guard-references check");
  assert_eq!(c.status, CheckStatus::Failed);
  assert!(c.detail.contains("does-not-exist"));
  assert_eq!(report.severity(), Severity::Failed);
}

#[test]
fn matching_guard_reference_is_ok() {
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.guard.push(gwm::config::Guard {
    name: "no-aws-rds".into(),
    deny_patterns: vec!["amazonaws".into()],
    on_match: "abort".into(),
    example_file: None,
  });
  config.bootstrap.copy.push(gwm::config::CopyStep {
    from: ".env".into(),
    to: ".env".into(),
    required: false,
    guards: vec!["no-aws-rds".into()],
    fallback: None,
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("guard")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
}

// --------------------------------------------------------------------------
// Check #3 — `when` predicates use a supported keyword
// --------------------------------------------------------------------------

#[test]
fn unsupported_when_predicate_is_failed() {
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "noop".into(),
    run: "true".into(),
    when: Some("bogus_predicate:FOO".into()),
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("when"))
    .expect("expected a `when` predicate check");
  assert_eq!(c.status, CheckStatus::Failed);
  assert!(c.detail.contains("bogus_predicate"));
}

#[test]
fn negated_supported_keyword_is_ok() {
  // `!env_set:CI` should be accepted: the doctor must reach past the
  // leading `!` (and any other boolean operator) and validate each
  // atom against the supported-keyword list.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "skip-in-ci".into(),
    run: "./scripts/full-build.sh".into(),
    when: Some("!env_set:CI".into()),
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("when"))
    .expect("expected a `when` predicate check");
  assert_eq!(c.status, CheckStatus::Ok);
}

#[test]
fn unsupported_keyword_on_rhs_of_and_is_failed() {
  // Compound expressions need atom-level validation. A LHS that looks
  // legitimate (`file_exists:a`) must not paper over a bogus RHS.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "compound".into(),
    run: "true".into(),
    when: Some("file_exists:a && bogus_predicate:1".into()),
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("when"))
    .expect("expected a `when` predicate check");
  assert_eq!(c.status, CheckStatus::Failed);
  assert!(c.detail.contains("bogus_predicate"));
}

#[test]
fn file_exists_when_predicate_is_ok() {
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "direnv allow".into(),
    run: "direnv allow .".into(),
    when: Some("file_exists:.envrc".into()),
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("when")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
}

#[test]
fn no_when_predicates_is_ok() {
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("when")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
}

#[test]
fn when_predicates_detail_counts_checked_predicates_not_keywords() {
  // regression: doctor detail used SUPPORTED_WHEN_PREFIXES.len() and reported
  // "1 predicate" regardless of the number of `when:` clauses actually checked.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  // Three commands carrying a `when:` predicate. The detail message must
  // reflect the count we actually checked (3), not the count of supported
  // keywords (1, `file_exists:`). Pre-fix the impl wrote
  // `format!("{} predicate(s) recognised", SUPPORTED_WHEN_PREFIXES.len()…)`
  // which always reported 1 regardless of the number of predicates.
  for n in 0..3 {
    config.bootstrap.command.push(gwm::config::CommandStep {
      name: format!("step-{n}"),
      run: "true".into(),
      when: Some("file_exists:.envrc".into()),
      env: Default::default(),
    });
  }

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("when")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
  assert!(
    c.detail.contains("3 predicate"),
    "expected detail to mention 3 checked predicates, got: {}",
    c.detail
  );
}

#[test]
fn when_predicates_detail_says_none_when_no_predicates_configured() {
  // regression: same SUPPORTED_WHEN_PREFIXES.len() miscount as above, surfaced
  // even when zero `when:` predicates were configured.
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("when")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
  // Pre-fix the impl said "1 predicate(s) recognised" even with zero
  // configured. After the fix, no predicates → detail mentions 0 or "none".
  assert!(
    !c.detail.contains("1 predicate"),
    "no predicates were configured; detail must not claim 1, got: {}",
    c.detail
  );
}

// --------------------------------------------------------------------------
// Check #4 — binaries referenced by bootstrap commands resolve on PATH
// --------------------------------------------------------------------------

#[test]
fn missing_command_binary_is_warning() {
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "phantom".into(),
    run: "definitely-not-on-path-xyz123 --help".into(),
    when: None,
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("PATH"))
    .expect("expected a PATH check");
  // A missing optional binary should not be a hard failure — the user may
  // not need that step. But it must surface as a Warning so it's visible.
  assert_eq!(c.status, CheckStatus::Warning);
  assert!(c.detail.contains("definitely-not-on-path-xyz123"));
}

#[test]
fn resolvable_command_binary_is_ok() {
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  // `sh` is on every POSIX system; CI macOS + Linux both have it.
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "noop".into(),
    run: "sh -c 'true'".into(),
    when: None,
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  // We don't assert Ok strictly — `lazygit` may be missing on a CI runner.
  // The relevant assertion is: when the doctor reports missing binaries, `sh`
  // is not in that list. Distinguished from the previous loose `!contains("sh ")`
  // which would pass even on `[sh,other]` or `sh\n` formatting.
  if c.status == CheckStatus::Warning {
    let missing_section = c.detail.split("not on PATH:").nth(1).unwrap_or("");
    let missing: Vec<&str> = missing_section.split([',', '\n']).map(str::trim).collect();
    assert!(
      !missing.contains(&"sh"),
      "sh must not be reported missing, got: {}",
      c.detail
    );
  }
}

#[test]
fn missing_review_binary_is_warning_not_failure() {
  // Issue #75: [review] is opt-in. A missing review binary should
  // surface as Warning (exit code 1), never Failed (exit code 2),
  // so CI / pre-commit hooks that already gate on doctor still pass
  // when the user only set [review] for their own local convenience.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.review.command = Some("definitely-not-on-path-review-xyz {base}..{head}".into());

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("PATH"))
    .expect("expected a PATH check");
  assert_eq!(c.status, CheckStatus::Warning);
  assert!(
    c.detail.contains("definitely-not-on-path-review-xyz"),
    "missing review binary must appear in the detail: {}",
    c.detail
  );
}

#[test]
fn missing_review_tool_preset_is_warning() {
  // `tool = "lumen"` resolves to `lumen diff ...`. If the user names a
  // preset but `lumen` itself isn't installed, the doctor warns about
  // the binary name from the preset table, not about the literal
  // string `"lumen"` token in their config.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.review.tool = Some("lumen".into());

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  // Lumen is almost certainly not on the CI matrix. If it happens to
  // be installed locally we don't have anything to assert; the loose
  // contract is "if it's missing, the report names it".
  if c.status == CheckStatus::Warning && c.detail.contains("lumen") {
    assert!(
      c.detail.to_lowercase().contains("lumen"),
      "preset's resolved binary must be named in the warning: {}",
      c.detail
    );
  }
}

#[test]
fn launcher_wrapped_by_env_warns_on_real_binary_not_wrapper() {
  // PR #76 Copilot review: `extract_binary` returned the first token
  // that didn't contain '=', which for `env FOO=bar phantom-bin diff`
  // was `env` itself. The doctor then checked `env` (always on PATH)
  // and missed the real launcher binary. Confirm that the doctor now
  // names the actual tool when an `env` wrapper is present.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.review.command = Some("env FOO=bar definitely-not-on-path-wrapped-zz {base}..{head}".into());

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  assert_eq!(c.status, CheckStatus::Warning, "wrapped missing binary must warn");
  assert!(
    c.detail.contains("definitely-not-on-path-wrapped-zz"),
    "wrapper must be peeled: detail should name the real binary, got: {}",
    c.detail
  );
  assert!(
    !c.detail.split("not on PATH:").nth(1).unwrap_or("").contains("env"),
    "`env` must not appear in the missing-binaries section: {}",
    c.detail
  );
}

#[test]
fn launcher_wrapped_by_command_warns_on_real_binary_not_wrapper() {
  // Sibling case for the POSIX `command` builtin: `command tool ...`
  // (used e.g. inside shell aliases to bypass functions) should peel
  // the wrapper just like `env`. Same shape as the env test above.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.git_tui.command = Some("command definitely-not-on-path-cmd-yy -d {path}".into());

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  assert!(
    c.detail.contains("definitely-not-on-path-cmd-yy"),
    "command wrapper must be peeled: detail should name the real binary, got: {}",
    c.detail
  );
}

#[test]
fn missing_git_tui_binary_is_warning() {
  // A user overriding [git_tui] to a missing binary deserves the same
  // visibility treatment as a missing review tool — gwm's `l` keybinding
  // would surface a status-bar error at runtime, but doctor should
  // catch it upfront.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.git_tui.command = Some("definitely-not-on-path-tui-xyz -d {path}".into());

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  assert_eq!(c.status, CheckStatus::Warning);
  assert!(
    c.detail.contains("definitely-not-on-path-tui-xyz"),
    "missing git_tui binary must appear in the detail: {}",
    c.detail
  );
}

#[test]
fn review_unset_does_not_force_lazygit_warning_to_failure() {
  // Pre-issue-#75 the doctor flagged a missing `lazygit` as Warning.
  // Confirm that adding the new launcher checks doesn't tip the
  // severity over to Failed when only [review] is unconfigured.
  let (dir, repo) = init_repo();
  let config = Config::default(); // nothing review-related set
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  // The aggregate severity must stay at Warning or Ok — never Failed.
  assert!(
    matches!(report.severity(), CheckStatus::Ok | CheckStatus::Warning),
    "default config must not push doctor into Failed: severity = {:?}",
    report.severity()
  );
}

#[test]
fn extract_binary_handles_shell_quoted_run_strings() {
  // regression: `extract_binary` used `split_whitespace` and returned the
  // leading-quoted token `"my` for `"my tool" --flag` before the shell-words
  // migration.
  // Pre-fix, `extract_binary` used `split_whitespace` and returned `"my`
  // as the binary name for a quoted run-string like `"my tool" --flag`,
  // producing a "binary not on PATH" warning that doesn't match anything
  // the user actually wrote. After the shell-words migration, the
  // binary is correctly identified as the full quoted command name.
  let (dir, repo) = init_repo();
  let mut config = Config::default();
  config.bootstrap.command.push(gwm::config::CommandStep {
    name: "quoted".into(),
    run: r#""definitely-not-on-path-quoted-xyz" --help"#.into(),
    when: None,
    env: Default::default(),
  });

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  assert!(
    c.detail.contains("definitely-not-on-path-quoted-xyz"),
    "shell-quoted binary name must be unquoted in the report, got: {}",
    c.detail
  );
  assert!(
    !c.detail.contains("\"definitely"),
    "the leading quote must be stripped, got: {}",
    c.detail
  );
}

// --------------------------------------------------------------------------
// Check #7 — base directory exists and is writable
// --------------------------------------------------------------------------

#[test]
fn base_dir_existing_and_writable_is_ok() {
  let (dir, repo) = init_repo();
  // Override base to a guaranteed-writable tempdir-scoped path.
  let base_dir = dir.path().join("wt-base");
  std::fs::create_dir(&base_dir).unwrap();
  let mut config = Config::default();
  config.worktree.base = base_dir.to_string_lossy().into_owned();

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("base"))
    .expect("expected a base-dir check");
  assert_eq!(c.status, CheckStatus::Ok);
}

#[test]
fn base_dir_missing_but_parent_writable_is_ok() {
  let (dir, repo) = init_repo();
  // Point at a not-yet-existing subdir of the tempdir. gwm creates the
  // worktree base on first `create`, so absence is a routine state.
  let base_dir = dir.path().join("future-base");
  let mut config = Config::default();
  config.worktree.base = base_dir.to_string_lossy().into_owned();

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("base")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
}

// --------------------------------------------------------------------------
// Check #5 — no prunable worktrees
// --------------------------------------------------------------------------

#[test]
fn fresh_repo_has_no_prunable_worktrees() {
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("prunable"))
    .expect("expected a prunable check");
  assert_eq!(c.status, CheckStatus::Ok);
}

// --------------------------------------------------------------------------
// Check #6 — orphan branches matching <type>/#<issue>-<desc>
// --------------------------------------------------------------------------

#[test]
fn orphan_unmerged_gwm_branch_is_warning() {
  let (dir, repo) = init_repo();
  // Build a commit that is NOT reachable from main, then branch off it.
  // This is what an in-flight WIP branch looks like: still divergent from
  // the trunk, so leaving it around is genuine dead weight.
  let head = repo.head().unwrap().peel_to_commit().unwrap();
  let sig = git2::Signature::now("test", "test@test").unwrap();
  let tree = head.tree().unwrap();
  let oid = repo
    .commit(None, &sig, &sig, "off-main commit", &tree, &[&head])
    .unwrap();
  let commit = repo.find_commit(oid).unwrap();
  repo.branch("feat/#99-stale-thing", &commit, false).unwrap();

  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("orphan"))
    .expect("expected an orphan-branches check");
  assert_eq!(c.status, CheckStatus::Warning);
  assert!(
    c.detail.contains("feat/#99-stale-thing"),
    "orphan branch should be quoted in the detail, got: {}",
    c.detail
  );
}

#[test]
fn merged_gwm_branch_is_not_flagged_as_orphan() {
  // CONTRIBUTING.md mandates "never delete the source branch after merge".
  // So a branch fully merged into a trunk (`dev` or `main`) is preserved
  // on purpose — flagging it would be noise on every doctor run. The
  // doctor must filter it out.
  //
  // This test exercises the *equality* short-circuit: the branch tip is
  // the same commit as main's tip. See
  // `merged_via_merge_commit_gwm_branch_is_not_flagged_as_orphan` for the
  // descendant-of case, which is what every real "merge commit" flow
  // produces.
  let (dir, repo) = init_repo();
  let head = repo.head().unwrap().peel_to_commit().unwrap();
  repo.branch("feat/#99-already-merged", &head, false).unwrap();

  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("orphan")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
  assert!(
    !c.detail.contains("feat/#99-already-merged"),
    "merged branch must not appear in the orphan list, got: {}",
    c.detail
  );
}

#[test]
fn merged_via_merge_commit_gwm_branch_is_not_flagged_as_orphan() {
  // The realistic case: a feature branch had its own commit, then a
  // merge commit on `main` joined it back. After that, `main`'s tip is
  // a descendant of the feature tip, but they're NOT equal. The
  // equality short-circuit alone would miss this; the descendant check
  // (`graph_descendant_of`) is what catches it.
  let (dir, repo) = init_repo();
  let main_initial = repo.head().unwrap().peel_to_commit().unwrap();
  let sig = git2::Signature::now("test", "test@test").unwrap();
  let tree = main_initial.tree().unwrap();

  // Feature branch with its own commit, not on main yet.
  let feature_oid = repo
    .commit(None, &sig, &sig, "feature work", &tree, &[&main_initial])
    .unwrap();
  let feature_commit = repo.find_commit(feature_oid).unwrap();
  repo
    .branch("feat/#88-merged-via-merge", &feature_commit, false)
    .unwrap();

  // Merge commit on main combining the initial commit and the feature.
  // Main now points at a commit that has the feature tip as one of its
  // parents — `graph_descendant_of(main_tip, feature_tip) == true`,
  // but `main_tip != feature_tip`.
  repo
    .commit(
      Some("refs/heads/main"),
      &sig,
      &sig,
      "merge feat/#88",
      &tree,
      &[&main_initial, &feature_commit],
    )
    .unwrap();

  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("orphan")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
  assert!(
    !c.detail.contains("feat/#88-merged-via-merge"),
    "branch merged via a merge commit must not appear in the orphan list, got: {}",
    c.detail
  );
}

#[test]
fn non_gwm_branch_is_not_flagged_as_orphan() {
  let (dir, repo) = init_repo();
  // Branches that don't match the <type>/#<issue>-<desc> shape are user-
  // managed (release branches, dependabot bumps, etc.) and must be left alone.
  let head = repo.head().unwrap().peel_to_commit().unwrap();
  repo.branch("release-2.0", &head, false).unwrap();
  repo.branch("dependabot/cargo/serde-1.0.200", &head, false).unwrap();

  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("orphan")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
}

#[test]
fn orphan_check_honours_configured_trunks() {
  // Repos with non-standard trunk conventions (`master`, `release-3.x`,
  // …) must be able to opt in via `[doctor].trunks`. Pre-#59 the trunk
  // list was hardcoded to `["dev", "main"]` and `[doctor].trunks` was
  // silently ignored, so any repo with a different trunk saw every
  // merged gwm-style branch flagged as "unmerged orphan".
  let (dir, repo) = init_repo();
  let head = repo.head().unwrap().peel_to_commit().unwrap();
  let sig = git2::Signature::now("test", "test@test").unwrap();
  let tree = head.tree().unwrap();

  // Divergent commit off main's HEAD. This is what an in-flight feature
  // branch looks like before merge.
  let feature_oid = repo.commit(None, &sig, &sig, "feature work", &tree, &[&head]).unwrap();
  let feature_commit = repo.find_commit(feature_oid).unwrap();
  repo.branch("feat/#77-on-custom-trunk", &feature_commit, false).unwrap();

  // `custom-trunk` carries the feature work — i.e. the gwm branch is
  // fully merged into the configured trunk but NOT into `main`.
  repo.branch("custom-trunk", &feature_commit, false).unwrap();

  let mut config = Config::default();
  config.doctor.trunks = vec!["custom-trunk".into()];

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("orphan"))
    .expect("expected an orphan-branches check");
  assert_eq!(c.status, CheckStatus::Ok);
  assert!(
    !c.detail.contains("feat/#77-on-custom-trunk"),
    "merged branch must not appear in the orphan list when its trunk is configured, got: {}",
    c.detail
  );
}

#[test]
fn orphan_check_with_empty_trunks_disables_merge_filter() {
  // `trunks = []` is the documented escape hatch: report every unclaimed
  // gwm-style branch, regardless of whether it's merged. Pre-#59 the
  // empty config silently fell back to the hardcoded `["dev", "main"]`
  // because the value lived in a `const`. Confirms the config value is
  // actually wired through.
  let (dir, repo) = init_repo();
  let head = repo.head().unwrap().peel_to_commit().unwrap();

  // A gwm-style branch pointing at main's tip. With the default config
  // this would be filtered out (equality short-circuit, merged into main).
  repo.branch("feat/#88-merged-into-main", &head, false).unwrap();

  let mut config = Config::default();
  config.doctor.trunks = vec![];

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.contains("orphan"))
    .expect("expected an orphan-branches check");
  assert_eq!(c.status, CheckStatus::Warning);
  assert!(
    c.detail.contains("feat/#88-merged-into-main"),
    "with no configured trunks every gwm branch must surface as orphan, got: {}",
    c.detail
  );
}

#[test]
fn orphan_check_ignores_configured_trunks_that_do_not_exist() {
  // A trunk listed in config but absent from the repo must not crash
  // the check — doctor should silently skip the missing trunk and use
  // the rest of the list. Matches the existing tolerance for "no `dev`
  // branch" in a fresh `gwm init` repo.
  let (dir, repo) = init_repo();
  let head = repo.head().unwrap().peel_to_commit().unwrap();
  repo.branch("feat/#99-merged-into-main", &head, false).unwrap();

  let mut config = Config::default();
  // `phantom-trunk` doesn't exist; `main` does and reaches the gwm branch.
  config.doctor.trunks = vec!["phantom-trunk".into(), "main".into()];

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("orphan")).unwrap();
  assert_eq!(c.status, CheckStatus::Ok);
}

// --- TUI keymap check (issue #87) ---------------------------------------

#[test]
fn doctor_passes_with_default_keymap() {
  // No `[tui.keys]` overrides → the resolved keymap is the built-in
  // default. Every required action is bound, so the check is green.
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("expected a TUI keymap check in the report");
  assert_eq!(
    c.status,
    CheckStatus::Ok,
    "default keymap must pass cleanly, got: {} — {}",
    match c.status {
      CheckStatus::Ok => "ok",
      CheckStatus::Warning => "warning",
      CheckStatus::Failed => "failed",
    },
    c.detail
  );
}

#[test]
fn doctor_warns_when_user_unbinds_quit() {
  // `quit` is the only action with a hard-coded escape hatch
  // (`Ctrl+C` in `run_app`). Even so, leaving it without any other
  // binding is hostile UX: warn the user so they can either restore
  // a binding or acknowledge the choice.
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    r#"
[tui.keys]
quit = []
"#,
  )
  .unwrap();
  let config = Config::load_layered(dir.path(), None).unwrap();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("expected a TUI keymap check in the report");
  assert_eq!(c.status, CheckStatus::Warning);
  assert!(
    c.detail.to_lowercase().contains("quit"),
    "expected message to name the missing action, got: {}",
    c.detail
  );
  assert!(
    c.detail.to_lowercase().contains("ctrl"),
    "expected message to mention the Ctrl+C fallback, got: {}",
    c.detail
  );
}

#[test]
fn doctor_reports_modal_binding_count_on_default_keymap() {
  // Issue #219: the keymap check now also resolves the contextual modal
  // keymap and folds its bound count into the detail line.
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("expected a TUI keymap check");
  assert_eq!(c.status, CheckStatus::Ok);
  assert!(
    c.detail.contains("modal"),
    "detail must mention the modal binding count, got: {}",
    c.detail
  );
}

#[test]
fn doctor_fails_on_in_context_modal_conflict() {
  // A `[tui.keys.modal.confirm]` block binding two verbs to the same key is a
  // per-context conflict. Written to disk (load-time validation rejects it, so
  // the lenient doctor context defaults ctx.config away) to prove `gwm doctor`
  // re-reads the file and independently catches it.
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    "[tui.keys.modal.confirm]\nconfirm = [\"x\"]\ncancel = [\"x\"]\n",
  )
  .unwrap();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("expected a TUI keymap check");
  assert_eq!(c.status, CheckStatus::Failed);
  assert!(
    c.detail.contains("conflict"),
    "detail must explain the conflict, got: {}",
    c.detail
  );
}

#[test]
fn doctor_keymap_check_reads_only_the_threaded_global_layer() {
  // #219 review (P2): the keymap check must merge the global layer that was
  // *threaded into the context*, never an ambient `global_config_path()` read —
  // otherwise a developer's real `~/.config/gwm` would make an isolated temp
  // repo's check flap. A bad global is seen only when explicitly threaded.
  let (dir, repo) = init_repo();
  let global = dir.path().join("global.toml");
  std::fs::write(&global, "[tui.keys.modal.confirm]\nconfirm = [\"g g\"]\n").unwrap();
  let config = Config::default();

  // Threaded → the bad global keymap surfaces.
  let with = DoctorCtx {
    repo_workdir: dir.path(),
    repo: &repo,
    config: &config,
    global_config_path: Some(global.as_path()),
  };
  let c = doctor::run(&with)
    .unwrap()
    .checks
    .into_iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("keymap check");
  assert_eq!(
    c.status,
    CheckStatus::Failed,
    "a threaded bad global must fail the check: {}",
    c.detail
  );

  // Not threaded (None) → the very same file is ignored; the check is clean.
  let without = DoctorCtx {
    repo_workdir: dir.path(),
    repo: &repo,
    config: &config,
    global_config_path: None,
  };
  let c2 = doctor::run(&without)
    .unwrap()
    .checks
    .into_iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("keymap check");
  assert_eq!(
    c2.status,
    CheckStatus::Ok,
    "an un-threaded global config must be ignored: {}",
    c2.detail
  );
}

#[test]
fn doctor_modal_error_outranks_the_quit_warning() {
  // #219 review (P2): an invalid modal binding is a hard Failed. When the same
  // config also unbinds `quit` (a Warning), the modal error must still win —
  // otherwise the quit-warning early return hides the actionable modal error.
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    "[tui.keys]\nquit = []\n\n[tui.keys.modal.confirm]\nconfirm = [\"g g\"]\n",
  )
  .unwrap();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("expected a TUI keymap check");
  assert_eq!(
    c.status,
    CheckStatus::Failed,
    "the modal error must outrank the quit warning, got: {}",
    c.detail
  );
  assert!(
    c.detail.contains("single keystroke"),
    "detail must be the modal error, not the quit warning, got: {}",
    c.detail
  );
}

#[test]
fn doctor_fails_on_disk_modal_error_even_when_context_defaulted() {
  // #219 review (P2): `repo_context_lenient` returns `Config::default()` when
  // `load_for_repo` rejects the user's `.gwm.toml` (here a multi-stroke modal
  // chord, which load-time validation refuses). If doctor only re-validated
  // that defaulted ctx.config it would report OK for a config that actually
  // refuses to start the TUI — so the keymap check must re-read the on-disk
  // file. This reproduces the real lenient path (the existing conflict test
  // injects straight into ctx.config and never exercises the default-away).
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    "[tui.keys.modal.confirm]\nconfirm = [\"g g\"]\n",
  )
  .unwrap();
  // The file must be load-invalid (so the lenient loader would default it).
  assert!(
    Config::load_layered(dir.path(), None).is_err(),
    "the on-disk modal chord must be rejected at load time for this regression"
  );
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report
    .checks
    .iter()
    .find(|c| c.name.to_lowercase().contains("keymap"))
    .expect("expected a TUI keymap check");
  assert_eq!(
    c.status,
    CheckStatus::Failed,
    "doctor must flag the on-disk modal error, not the defaulted ctx.config: {}",
    c.detail
  );
  assert!(
    c.detail.contains("single keystroke"),
    "detail must explain the multi-stroke modal chord rejection, got: {}",
    c.detail
  );
}

// Check #4 — the forge CLI is probed only when `forge` is set explicitly
// (issue #419).

#[test]
fn forge_cli_is_not_probed_when_the_key_is_unset() {
  // The non-regression half, and the one that runs identically everywhere:
  // a config that never opts into a forge must not start warning about a
  // missing `gh` / `glab` for users who don't touch issue/PR linking.
  let (dir, repo) = init_repo();
  let config = Config::default();

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  let missing = c.detail.split("not on PATH:").nth(1).unwrap_or("");

  assert!(
    !missing.contains("glab") && !missing.contains("gh"),
    "no forge CLI should be probed without an explicit `forge` key, got: {}",
    c.detail
  );
}

#[test]
fn an_explicit_gitlab_forge_probes_glab() {
  let (dir, repo) = init_repo();
  let config = Config {
    forge: Some(gwm::forge::ForgeKind::GitLab),
    ..Default::default()
  };

  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();

  // `glab` may legitimately be installed on a contributor's machine, so
  // the positive assertion is scoped to the case where it is genuinely
  // absent — which is every CI runner. Asserting the warning
  // unconditionally would be exactly the ambient-`$PATH` flake the repo
  // rules call out.
  if which::which("glab").is_err() {
    assert_eq!(c.status, CheckStatus::Warning, "missing glab must warn: {}", c.detail);
    assert!(
      c.detail.contains("glab"),
      "the missing forge CLI should be named, got: {}",
      c.detail
    );
  }
  // Env-independent in both directions: selecting GitLab must never probe
  // for the GitHub CLI.
  let missing = c.detail.split("not on PATH:").nth(1).unwrap_or("");
  assert!(
    !missing.split(',').any(|b| b.trim() == "gh"),
    "selecting GitLab must not probe for `gh`, got: {}",
    c.detail
  );
}

#[test]
fn a_forge_hosts_entry_is_also_an_opt_in_for_the_cli_probe() {
  // `[forge_hosts]` is the second way to say "I talk to this forge", and it
  // says it about *this host* specifically — a stronger opt-in signal than
  // the bare `forge` key, not a weaker one. Probing only on `forge` left a
  // user who authorises purely through the global table with a clean
  // `gwm doctor` and no `glab` installed.
  let (dir, repo) = init_repo();
  repo
    .remote("origin", "https://gitlab.acme.internal/team/proj.git")
    .unwrap();
  let global = dir.path().join("global.toml");
  std::fs::write(&global, "[forge_hosts]\n\"gitlab.acme.internal\" = \"gitlab\"\n").unwrap();
  // No `forge` key anywhere: the host entry is the whole opt-in.
  let config = Config::default();

  let report = doctor::run(&DoctorCtx {
    repo_workdir: dir.path(),
    repo: &repo,
    config: &config,
    global_config_path: Some(&global),
  })
  .unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();

  // Same env-independence rule as `an_explicit_gitlab_forge_probes_glab`:
  // the positive assertion only holds where `glab` is genuinely absent.
  if which::which("glab").is_err() {
    assert_eq!(c.status, CheckStatus::Warning, "missing glab must warn: {}", c.detail);
    assert!(
      c.detail.contains("glab"),
      "the forge CLI should be named, got: {}",
      c.detail
    );
  }
  let missing = c.detail.split("not on PATH:").nth(1).unwrap_or("");
  assert!(
    !missing.split(',').any(|b| b.trim() == "gh"),
    "a GitLab host entry must not probe for `gh`, got: {}",
    c.detail
  );
}

#[test]
fn a_forge_hosts_entry_for_another_host_probes_nothing() {
  // The bound: the table authorises *named* hosts, so an entry that does not
  // match this repo's origin is not an opt-in for it. Without this, adding
  // one host to the global config would start warning in every unrelated
  // repo — the same over-probing the `forge` key was careful to avoid.
  let (dir, repo) = init_repo();
  repo.remote("origin", "https://github.com/team/proj.git").unwrap();
  let global = dir.path().join("global.toml");
  std::fs::write(&global, "[forge_hosts]\n\"gitlab.acme.internal\" = \"gitlab\"\n").unwrap();
  let config = Config::default();

  let report = doctor::run(&DoctorCtx {
    repo_workdir: dir.path(),
    repo: &repo,
    config: &config,
    global_config_path: Some(&global),
  })
  .unwrap();
  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  let missing = c.detail.split("not on PATH:").nth(1).unwrap_or("");

  assert!(
    !missing.contains("glab") && !missing.split(',').any(|b| b.trim() == "gh"),
    "an unrelated host entry must not probe any forge CLI, got: {}",
    c.detail
  );
}

#[test]
fn the_forge_cli_probe_honours_the_gwm_gh_override() {
  // `$GWM_GH` / `$GWM_GLAB` point at an alternative binary; probing the
  // bare name `gh` regardless warned about a setup that works, and pushed
  // the exit code to 1 (Codex review #458).
  let (dir, repo) = init_repo();
  // Windows resolves executability from the extension, not a mode bit, so
  // an extensionless file is genuinely not runnable there — the probe was
  // right to report it missing and the test was wrong to expect otherwise.
  // A real override on Windows names a `.exe`, so the fixture does too.
  let fake = dir.path().join(if cfg!(windows) { "my-gh.exe" } else { "my-gh" });
  std::fs::write(&fake, "#!/bin/sh\nexit 0\n").unwrap();
  #[cfg(unix)]
  {
    use std::os::unix::fs::PermissionsExt;
    let mut perms = std::fs::metadata(&fake).unwrap().permissions();
    perms.set_mode(0o755);
    std::fs::set_permissions(&fake, perms).unwrap();
  }
  let config = Config {
    forge: Some(gwm::forge::ForgeKind::GitHub),
    ..Default::default()
  };

  let _env = env_lock().lock().unwrap_or_else(|p| p.into_inner());
  let prior = std::env::var("GWM_GH").ok();
  // SAFETY: env mutation guarded by the lock above; restored below.
  unsafe {
    std::env::set_var("GWM_GH", &fake);
  }
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();
  unsafe {
    match prior {
      Some(v) => std::env::set_var("GWM_GH", v),
      None => std::env::remove_var("GWM_GH"),
    }
  }

  let c = report.checks.iter().find(|c| c.name.contains("PATH")).unwrap();
  let missing = c.detail.split("not on PATH:").nth(1).unwrap_or("");
  assert!(
    !missing.contains("gh"),
    "the overridden binary exists, so nothing should be reported missing: {}",
    c.detail
  );
}

/// Issue #415: a `worktree.branch_pattern` gwm cannot read back silently
/// disables every feature that re-parses a branch name, so `gwm doctor`
/// states the limitation instead of leaving it silent.
///
/// Issue #417 derived the parser from the pattern, which shrank the set this
/// applies to: `{type}-{issue}-{desc}` was the example here and round-trips
/// now. A `~`-leading pattern is what still reaches the "everything inactive"
/// verdict, because the writer ends with a tilde expansion the reader cannot
/// undo.
#[test]
fn a_branch_pattern_nothing_reads_back_warns_that_the_parser_is_blind() {
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    "[worktree]\nbranch_pattern = \"~/{type}/#{issue}-{desc}\"\n",
  )
  .unwrap();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let check = report
    .checks
    .iter()
    .find(|c| c.name.contains("branch_pattern"))
    .expect("expected a `branch_pattern` check in the report");

  assert_eq!(check.status, CheckStatus::Warning);
  // The message has to name the consequence, not just the divergence —
  // the whole point is connecting cause to effect.
  for expected in ["auto-linking", "gitmoji", "branch-convention"] {
    assert!(
      check.detail.contains(expected),
      "warning should name the '{}' consequence, got: {}",
      expected,
      check.detail
    );
  }
}

#[test]
fn default_branch_pattern_does_not_warn() {
  let (dir, repo) = init_repo();
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let check = report
    .checks
    .iter()
    .find(|c| c.name.contains("branch_pattern"))
    .expect("expected a `branch_pattern` check in the report");

  assert_eq!(check.status, CheckStatus::Ok);
}

/// Issue #415 (Codex review): `repo_context_lenient` hands `doctor` a
/// `Config::default()` when the on-disk config fails to load for an
/// unrelated semantic reason. Reading `ctx.config` would then report the
/// default pattern as fine while the file on disk carries a broken one —
/// a false `✓` from the very check that exists to stop silent failures.
/// Re-derive from disk, as `check_tui_keymap` already does.
#[test]
fn branch_pattern_check_reads_the_on_disk_config_not_the_lenient_fallback() {
  let (dir, repo) = init_repo();
  std::fs::write(
    dir.path().join(".gwm.toml"),
    "[worktree]\nbranch_pattern = \"~/{type}/#{issue}-{desc}\"\n",
  )
  .unwrap();
  // What `repo_context_lenient` would have handed us after a load failure.
  let config = Config::default();
  let report = doctor::run(&ctx_for(&repo, dir.path(), &config)).unwrap();

  let check = report
    .checks
    .iter()
    .find(|c| c.name.contains("branch_pattern"))
    .expect("expected a `branch_pattern` check in the report");

  assert_eq!(check.status, CheckStatus::Warning);
}