davit 0.4.1

Kubernetes management utilities
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
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
mod blueprint;
mod config;
mod dashboard;
mod git;
mod info;
mod prompt;
mod registry;

use anyhow::{Context, Result};
use blueprint::Blueprint;
use chrono::Utc;
use clap::{Parser, Subcommand};
use config::{Config, Environment, ServiceSource, YamlSource};
use crossterm::{
    cursor::MoveToColumn,
    event::{self, Event, KeyCode, KeyEventKind},
    terminal::{Clear, ClearType, disable_raw_mode, enable_raw_mode, size as terminal_size},
};
use dashboard::{Dashboard, DashboardExit};
use git::{Git, GitPullReport};
use inquire::{Confirm, Select, Text};
use prompt::{PromptPolicy, format_candidates};
use registry::{ImageMetadata, Registry};
use std::collections::HashSet;
use std::fs;
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};

const MAX_PARALLEL_PULLS: usize = 5;
const TAG_RETRY_INTERVAL: Duration = Duration::from_secs(60);
const TAG_WAIT_POLL_INTERVAL: Duration = Duration::from_millis(250);
const TAG_WAIT_CANCELLED_MESSAGE: &str = "__TAG_WAIT_CANCELLED__";
/// Stands for "declares no namespace of its own", in the NAMESPACE column and
/// in `--namespace` alike, so the listing round-trips. A lone `-` cannot
/// collide with a real namespace: those must start and end alphanumerically.
const NO_NAMESPACE: &str = "-";
/// Caps the SERVICE column. A name disambiguated all the way down to its
/// manifest path can run past 100 characters, and padding every other row to
/// match makes the listing unreadable.
const MAX_SERVICE_COLUMN: usize = 48;

#[derive(Debug, PartialEq, Eq)]
enum RemoteManifestDiffCheck {
    InSync,
    Drift(String),
    CheckFailed(String),
    SkippedDryRun,
}

#[derive(Parser)]
#[command(name = "davit")]
#[command(version)]
#[command(about = "A safe Kubernetes deployment wrapper & TUI", long_about = None)]
struct Cli {
    /// Never prompt; fail instead, naming the decision that would have been asked
    #[arg(long, global = true)]
    non_interactive: bool,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Deploy a service to an environment
    Deploy {
        /// Target environment (e.g., staging, production)
        #[arg(short, long)]
        env: Option<String>,

        /// Service name to deploy
        #[arg(short, long)]
        service: Option<String>,

        /// Kubernetes namespace, or `-` for manifests that declare none, to disambiguate a service name
        #[arg(short, long, allow_hyphen_values = true)]
        namespace: Option<String>,

        /// Image tag to deploy
        #[arg(short, long)]
        tag: Option<String>,

        /// Wait until the provided image tag appears in the registry instead of prompting for similar tags
        #[arg(long, conflicts_with = "tag", value_name = "TAG")]
        wait_for_tag: Option<String>,

        /// Dry run: show commands without executing them
        #[arg(long)]
        dry_run: bool,

        /// Read the YAML sources as they are on disk, without running git pull first
        #[arg(long)]
        no_fetch: bool,

        /// Apply the selected version immediately and continue automatically through rollout and Git steps
        #[arg(long)]
        auto_apply: bool,

        /// After `kubectl apply`, continue automatically through rollout completion and Git push unless errors occur
        #[arg(long)]
        auto_continue: bool,

        /// Confirm a protected environment without prompting; must match the target environment name
        #[arg(long, value_name = "NAME")]
        confirm_env: Option<String>,
    },
    /// Show deployment information for a service
    Info {
        /// Target environment (e.g., staging, production)
        #[arg(short, long)]
        env: Option<String>,

        /// Kubernetes namespace filter, or `-` for manifests that declare none
        #[arg(short, long, allow_hyphen_values = true)]
        namespace: Option<String>,

        /// Service name to inspect
        #[arg(short, long)]
        service: Option<String>,

        /// Read the YAML sources as they are on disk, without running git pull first
        #[arg(long)]
        no_fetch: bool,
    },
    /// List the identifiers accepted by --env and --service
    List {
        #[command(subcommand)]
        command: ListCommands,
    },
    /// Configuration management
    Config {
        #[command(subcommand)]
        command: ConfigCommands,
    },
}

#[derive(Subcommand)]
enum ListCommands {
    /// List the configured environments
    Envs,
    /// List the services discovered in an environment's YAML sources
    Services {
        /// Target environment (e.g., staging, production)
        #[arg(short, long)]
        env: Option<String>,

        /// Only list services in this namespace, or `-` for those that declare none
        #[arg(short, long, allow_hyphen_values = true)]
        namespace: Option<String>,

        /// Read the YAML sources as they are on disk, without running git pull first
        #[arg(long)]
        no_fetch: bool,
    },
}

#[derive(Subcommand)]
enum ConfigCommands {
    /// Show current configuration
    Show,
    /// Get path to configuration file
    Path,
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let config = Config::load().context("Failed to load configuration")?;
    let policy = PromptPolicy::resolve(cli.non_interactive);

    match cli.command {
        Commands::Deploy {
            env,
            service,
            namespace,
            tag,
            wait_for_tag,
            dry_run,
            no_fetch,
            auto_apply,
            auto_continue,
            confirm_env,
        } => {
            let auto_continue = auto_continue || auto_apply;
            let selected_env = resolve_environment(&config, env, policy)?;

            pull_yaml_sources(&selected_env, dry_run, no_fetch, "deployment", policy)?;

            let selected_service =
                resolve_service_with_ns_filter(&selected_env, service, namespace, policy)?;

            let remote_diff = check_remote_manifest_diff(
                &selected_env.kubectl_context,
                &selected_service.yaml_path,
                dry_run,
            )?;
            if !confirm_remote_manifest_diff(&selected_service.yaml_path, remote_diff, policy)? {
                println!("Deployment cancelled. No changes made.");
                return Ok(());
            }

            let selected_tag =
                match resolve_tag(&selected_env, &selected_service, tag, wait_for_tag, policy) {
                    Ok(tag) => tag,
                    Err(err) if err.to_string() == TAG_WAIT_CANCELLED_MESSAGE => {
                        println!("Tag wait cancelled. Deployment aborted.");
                        return Ok(());
                    }
                    Err(err) => return Err(err),
                };

            // 6.3 Production Protection
            if selected_env.protected.unwrap_or(false) {
                confirm_protected_environment(
                    &selected_env.name,
                    confirm_env.as_deref(),
                    dry_run,
                    policy,
                )?;
            }

            // Phase 4 - YAML modification & Visual Diff
            let yaml_path = selected_service.yaml_path.clone();

            let original_content = fs::read_to_string(&yaml_path)
                .with_context(|| format!("Failed to read YAML file at {}", yaml_path.display()))?;

            let base_image = selected_service
                .image_path
                .split([':', '@'])
                .next()
                .unwrap_or(&selected_service.image_path);

            let updated_content =
                Blueprint::update_image_tag(&original_content, base_image, &selected_tag)
                    .context("Failed to update image tag in YAML")?;

            let mut show_unified = true;
            let filename = yaml_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("deployment.yaml");

            loop {
                Blueprint::show_diff(&original_content, &updated_content, filename, show_unified);

                if dry_run {
                    println!(
                        "Dry-run: would write updated YAML to {}",
                        yaml_path.display()
                    );
                    break;
                }

                if auto_apply {
                    fs::write(&yaml_path, &updated_content).with_context(|| {
                        format!("Failed to write updated YAML to {}", yaml_path.display())
                    })?;
                    println!("Auto-apply enabled. Local YAML updated. Executing kubectl apply...");
                    break;
                }

                let choices = if show_unified {
                    vec!["Apply", "Show full diff", "Dismiss"]
                } else {
                    vec!["Apply", "Show unified diff", "Dismiss"]
                };

                let selection = policy.select(
                    "approval of the manifest change",
                    "Action:",
                    choices.into_iter().map(str::to_string).collect(),
                    "Re-run with --auto-apply to apply it without asking, or --dry-run to preview only.",
                )?;

                let selection = selection.as_str();

                match selection {
                    "Apply" => {
                        fs::write(&yaml_path, &updated_content).with_context(|| {
                            format!("Failed to write updated YAML to {}", yaml_path.display())
                        })?;
                        println!("Local YAML updated. Executing kubectl apply...");
                        break;
                    }
                    "Show full diff" => show_unified = false,
                    "Show unified diff" => show_unified = true,
                    _ => {
                        println!("Deployment cancelled. No changes made.");
                        return Ok(());
                    }
                }
            }

            if dry_run {
                println!(
                    "Dry-run: kubectl --context {} apply -f {}",
                    selected_env.kubectl_context,
                    yaml_path.display()
                );
            } else {
                let output = Command::new("kubectl")
                    .args([
                        "--context",
                        &selected_env.kubectl_context,
                        "apply",
                        "-f",
                        yaml_path.to_str().unwrap(),
                    ])
                    .output()
                    .context("Failed to execute kubectl apply")?;

                if !output.status.success() {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    println!("❌ kubectl apply failed: {}", stderr);
                    offer_revert(&yaml_path, &original_content, auto_continue, policy)?;
                    return Err(anyhow::anyhow!("kubectl apply failed"));
                }
            }

            if dry_run {
                println!(
                    "Dry-run: would monitor the rollout of {} in the dashboard.",
                    selected_service.name
                );
            } else {
                println!("Deployment applied. Starting dashboard...");

                let mut dashboard = Dashboard::new(
                    selected_service.name.clone(),
                    selected_service.kind.clone(),
                    selected_env.name.clone(),
                    selected_tag.clone(),
                    selected_env.kubectl_context.clone(),
                    selected_service.namespace.clone(),
                    selected_service.selector.clone(),
                    selected_service.container_name.clone(),
                    auto_continue,
                );
                let res = dashboard.run().await;

                match res {
                    Err(e) => {
                        println!("❌ Dashboard error or aborted: {}", e);
                        offer_revert(&yaml_path, &original_content, auto_continue, policy)?;
                        return Err(e);
                    }
                    Ok(DashboardExit::UserQuit) => {
                        if auto_continue {
                            return Err(anyhow::anyhow!(
                                "Dashboard closed before rollout completion in auto-continue mode"
                            ));
                        }
                        println!("Dashboard closed before rollout completion check.");
                    }
                    Ok(DashboardExit::RolloutCompleted) => {
                        println!("Rollout completed. Continuing to the Git step...");
                    }
                }
            }

            // 6.1 Git Automation
            println!("\n🚀 Deployment successful. Preparing to commit changes...");
            let commit_msg = format!(
                "deploy({}): update {} to {}",
                selected_env.name, selected_service.name, selected_tag
            );

            println!("\n--- Commit Recap ---");
            println!("File to commit:   {}", yaml_path.display());
            println!("Commit message:   {}", commit_msg);
            Blueprint::show_diff(&original_content, &updated_content, filename, true);
            println!("--------------------\n");

            if auto_continue || dry_run {
                Git::commit_and_push(
                    &selected_service.source_root,
                    &commit_msg,
                    &yaml_path,
                    dry_run,
                )?;
                if !dry_run {
                    println!("✅ Changes committed and pushed to Git.");
                }
            } else {
                if policy.confirm(
                    "approval to commit and push",
                    "Do you want to commit and push these changes?",
                    true,
                    "Re-run with --auto-continue to commit and push without asking.",
                )? {
                    if let Err(e) = Git::commit_and_push(
                        &selected_service.source_root,
                        &commit_msg,
                        &yaml_path,
                        dry_run,
                    ) {
                        println!("⚠️  Failed to commit/push changes: {}", e);
                    } else if !dry_run {
                        println!("✅ Changes committed and pushed to Git.");
                    }
                } else {
                    println!("Committing skipped by user.");
                }
            }
        }
        Commands::Info {
            env,
            namespace,
            service,
            no_fetch,
        } => {
            let selected_env = resolve_environment(&config, env, policy)?;

            pull_yaml_sources(&selected_env, false, no_fetch, "info", policy)?;

            let selected_service =
                resolve_service_with_ns_filter(&selected_env, service, namespace, policy)?;
            info::show_info(&selected_env, &selected_service).await?;
        }
        Commands::List { command } => match command {
            ListCommands::Envs => {
                for environment in &config.environments {
                    let protected = if environment.protected.unwrap_or(false) {
                        "  (protected)"
                    } else {
                        ""
                    };
                    println!("{}{}", environment.name, protected);
                }
            }
            ListCommands::Services {
                env,
                namespace,
                no_fetch,
            } => {
                let selected_env = resolve_environment(&config, env, policy)?;

                pull_yaml_sources(&selected_env, false, no_fetch, "listing", policy)?;

                let services = list_services_in_namespace(&selected_env, namespace.as_deref())?;
                print_service_list(&services);
            }
        },
        Commands::Config { command } => match command {
            ConfigCommands::Show => {
                println!("{:#?}", config);
            }
            ConfigCommands::Path => {
                let path = Config::get_config_path()?;
                println!("{}", path.display());
            }
        },
    }

    Ok(())
}

fn resolve_environment(
    config: &Config,
    input: Option<String>,
    policy: PromptPolicy,
) -> Result<Environment> {
    let env_names: Vec<String> = config.environments.iter().map(|e| e.name.clone()).collect();

    let name = match input {
        Some(val) => resolve_from_list("Environment", &env_names, val, policy)?,
        None => policy.select(
            "an environment",
            "Select Environment:",
            env_names.clone(),
            &format!("Pass --env with one of:\n{}", format_candidates(&env_names)),
        )?,
    };

    config
        .environments
        .iter()
        .find(|e| e.name == name)
        .cloned()
        .context("Environment not found in config")
}

fn get_service_display_name(s: &ServiceSource, all_services: &[ServiceSource]) -> String {
    let duplicates: Vec<&ServiceSource> = all_services
        .iter()
        .filter(|&other| other.name == s.name)
        .collect();

    if duplicates.len() <= 1 {
        return s.name.clone();
    }

    // Multiple services with same name, check namespace
    let same_namespace: Vec<&&ServiceSource> = duplicates
        .iter()
        .filter(|&other| other.namespace == s.namespace)
        .collect();

    if same_namespace.len() <= 1 {
        return format!(
            "{} ({})",
            s.name,
            s.namespace.as_deref().unwrap_or("no-namespace")
        );
    }

    // Multiple services with same name and same namespace, use relative path
    let relative_path = get_service_source_display_path(s);

    format!(
        "{} ({}) {}",
        s.name,
        s.namespace.as_deref().unwrap_or("no-namespace"),
        relative_path
    )
}

/// Lists services in a stable order, optionally narrowed to one namespace.
///
/// The order matters beyond presentation: it fixes the order of the candidate
/// lists reported when a selection cannot be made, and of the prompt itself.
fn list_services_in_namespace(
    env: &Environment,
    namespace: Option<&str>,
) -> Result<Vec<ServiceSource>> {
    let all_services = env.list_services().context("Failed to list services")?;

    let mut services = match namespace {
        Some(ns) => {
            let filtered: Vec<ServiceSource> = all_services
                .into_iter()
                .filter(|service| namespace_label(service) == ns)
                .collect();
            if filtered.is_empty() {
                return Err(if ns == NO_NAMESPACE {
                    anyhow::anyhow!("No services without a declared namespace found")
                } else {
                    anyhow::anyhow!("No services found in namespace '{}'", ns)
                });
            }
            filtered
        }
        None => all_services,
    };

    services.sort_by(|a, b| {
        a.name
            .cmp(&b.name)
            .then_with(|| a.namespace.cmp(&b.namespace))
            .then_with(|| a.yaml_path.cmp(&b.yaml_path))
    });

    Ok(services)
}

fn resolve_service_with_ns_filter(
    env: &Environment,
    input: Option<String>,
    namespace: Option<String>,
    policy: PromptPolicy,
) -> Result<ServiceSource> {
    let services = list_services_in_namespace(env, namespace.as_deref())?;
    let input = input.map(|value| strip_namespace_suffix(value, namespace.as_deref()));
    resolve_service_from_list(services, env, input, policy)
}

/// Drops the `(namespace)` suffix from a service name already narrowed by `--namespace`.
///
/// `list services` disambiguates names across the whole environment, so it
/// prints `svc-api (tenant-b)`. Passing that back together with the NAMESPACE
/// column would otherwise fail, because within the narrowed set the name no
/// longer collides and is rendered plain.
fn strip_namespace_suffix(value: String, namespace: Option<&str>) -> String {
    let Some(namespace) = namespace else {
        return value;
    };

    let rendered = if namespace == NO_NAMESPACE {
        "no-namespace"
    } else {
        namespace
    };

    match value.strip_suffix(&format!(" ({})", rendered)) {
        Some(stripped) => stripped.to_string(),
        None => value,
    }
}

fn resolve_service_from_list(
    services: Vec<ServiceSource>,
    env: &Environment,
    input: Option<String>,
    policy: PromptPolicy,
) -> Result<ServiceSource> {
    if services.is_empty() {
        return Err(anyhow::anyhow!(
            "No services found in configured YAML sources for {}",
            env.name
        ));
    }

    let service_map: Vec<(String, ServiceSource)> = services
        .iter()
        .cloned()
        .map(|s| (get_service_display_name(&s, &services), s))
        .collect();

    let display_names: Vec<String> = service_map.iter().map(|(n, _)| n.clone()).collect();

    let selected_name = match input {
        Some(val) => resolve_from_list("Service", &display_names, val, policy)?,
        None => policy.select(
            "a service",
            "Select Service:",
            display_names.clone(),
            &format!(
                "Pass --service with one of:\n{}",
                format_candidates(&display_names)
            ),
        )?,
    };

    service_map
        .into_iter()
        .find(|(n, _)| n == &selected_name)
        .map(|(_, s)| s)
        .context("Resolved service not found in list")
}

/// Prints the values `--service` accepts, alongside what each one resolves to.
fn print_service_list(services: &[ServiceSource]) {
    let rows: Vec<(String, &ServiceSource)> = services
        .iter()
        .map(|service| (get_service_display_name(service, services), service))
        .collect();

    let name_width = service_column_width(&rows);
    let namespace_width = rows
        .iter()
        .map(|(_, service)| namespace_label(service).chars().count())
        .chain(std::iter::once("NAMESPACE".len()))
        .max()
        .unwrap_or_default();

    println!(
        "{:<name_width$}  {:<namespace_width$}  {:<12}  MANIFEST",
        "SERVICE", "NAMESPACE", "KIND"
    );

    for (name, service) in &rows {
        println!(
            "{:<name_width$}  {:<namespace_width$}  {:<12}  {}",
            name,
            namespace_label(service),
            service.kind,
            get_service_source_display_path(service)
        );
    }
}

/// Indefinite article for a label, so an error reads "an image tag", not
/// "a image tag". A leading-vowel test is enough for the labels in use
/// (environment, service, image tag) and for anything similar.
fn article_for(noun: &str) -> &'static str {
    match noun.chars().next() {
        Some('a' | 'e' | 'i' | 'o' | 'u') => "an",
        _ => "a",
    }
}

/// Width of the SERVICE column: the widest name that still fits the cap.
///
/// Names past the cap overflow their own row instead of widening every other
/// one. They are never truncated: the value has to stay copy-pasteable into
/// `--service`, which is the whole point of the listing.
fn service_column_width<T>(rows: &[(String, T)]) -> usize {
    rows.iter()
        .map(|(name, _)| name.chars().count())
        .chain(std::iter::once("SERVICE".len()))
        .filter(|width| *width <= MAX_SERVICE_COLUMN)
        .max()
        .unwrap_or(MAX_SERVICE_COLUMN)
}

/// The namespace as the manifest declares it; `--namespace` matches this value.
fn namespace_label(service: &ServiceSource) -> &str {
    service.namespace.as_deref().unwrap_or(NO_NAMESPACE)
}

fn get_service_source_display_path(service: &ServiceSource) -> String {
    let relative_path = pathdiff::diff_paths(&service.yaml_path, &service.source_root)
        .unwrap_or_else(|| service.yaml_path.clone());
    format!("[{}]/{}", service.source_name, relative_path.display())
}

fn pull_yaml_sources(
    env: &Environment,
    dry_run: bool,
    no_fetch: bool,
    action: &str,
    policy: PromptPolicy,
) -> Result<()> {
    if no_fetch {
        println!("Skipping the YAML source refresh (--no-fetch); reading manifests from disk.");
        return Ok(());
    }

    let sources = unique_yaml_sources(env);

    if sources.is_empty() {
        return Ok(());
    }

    println!("🔄 Checking for updates in configured YAML sources...");
    if sources.len() > 1 {
        println!(
            "Running up to {} git pulls in parallel and showing each repository output sequentially.",
            MAX_PARALLEL_PULLS
        );
    }

    let mut failures = Vec::new();
    for (source, result) in
        collect_parallel_pull_results(&sources, MAX_PARALLEL_PULLS, move |source| {
            pull_source(source, dry_run)
        })
    {
        println!("  - [{}] {}", source.name, source.root.display());
        match result {
            Ok(report) => {
                print_git_pull_report(&report.stdout, false);
                print_git_pull_report(&report.stderr, true);

                if !report.success {
                    failures.push((source, "git pull failed".to_string()));
                }
            }
            Err(error) => failures.push((source, error.to_string())),
        }
    }

    if failures.is_empty() {
        return Ok(());
    }

    println!("⚠️  Some YAML sources could not be updated:");
    for (source, error) in &failures {
        println!("  - [{}] {}: {}", source.name, source.root.display(), error);
    }

    if !policy.confirm(
        &format!(
            "approval to continue with {} after a failed git pull",
            action
        ),
        &format!("Do you want to continue with {} anyway?", action),
        false,
        "Fix the YAML sources listed above before retrying.",
    )? {
        return Err(anyhow::anyhow!(
            "{} aborted by user after git pull failure.",
            capitalize_action(action)
        ));
    }

    Ok(())
}

/// Pulls one YAML source, unless that would merge into uncommitted local work.
///
/// Leaving the source stale is reported and treated as a success: it is not a
/// failure the caller has to confirm, and refusing to touch edited manifests is
/// the point.
fn pull_source(source: &YamlSource, dry_run: bool) -> Result<GitPullReport> {
    if !dry_run && Git::is_dirty(&source.root)? {
        return Ok(GitPullReport {
            stdout: "Skipped: the working copy has uncommitted changes, so it was not pulled.\nManifests read from it may be stale.\n".to_string(),
            stderr: String::new(),
            success: true,
        });
    }

    Git::pull(&source.root, dry_run)
}

fn collect_parallel_pull_results<T, F>(
    sources: &[YamlSource],
    max_parallel: usize,
    pull: F,
) -> Vec<(YamlSource, Result<T>)>
where
    T: Send + 'static,
    F: Fn(&YamlSource) -> Result<T> + Send + Sync + 'static,
{
    let max_parallel = max_parallel.max(1);
    let pull = Arc::new(pull);
    let mut results = Vec::with_capacity(sources.len());

    for batch in sources.chunks(max_parallel) {
        let mut handles = Vec::with_capacity(batch.len());

        for source in batch {
            let source = source.clone();
            let pull = Arc::clone(&pull);
            handles.push(thread::spawn(move || {
                let result = pull(&source);
                (source, result)
            }));
        }

        for handle in handles {
            results.push(handle.join().unwrap_or_else(|_| {
                (
                    YamlSource {
                        name: "unknown".to_string(),
                        root: std::path::PathBuf::new(),
                    },
                    Err(anyhow::anyhow!("git pull worker thread panicked")),
                )
            }));
        }
    }

    results
}

fn print_git_pull_report(output: &str, is_stderr: bool) {
    for line in output.lines() {
        if is_stderr {
            eprintln!("    {}", line);
        } else {
            println!("    {}", line);
        }
    }
}

fn unique_yaml_sources(env: &Environment) -> Vec<YamlSource> {
    let mut seen = HashSet::new();
    let mut unique = Vec::new();

    for source in env.yaml_sources() {
        let key = source
            .root
            .canonicalize()
            .unwrap_or_else(|_| source.root.clone());
        if seen.insert(key) {
            unique.push(source);
        }
    }

    unique
}

fn capitalize_action(action: &str) -> String {
    let mut chars = action.chars();
    match chars.next() {
        Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()),
        None => String::new(),
    }
}

fn check_remote_manifest_diff(
    kubectl_context: &str,
    yaml_path: &Path,
    dry_run: bool,
) -> Result<RemoteManifestDiffCheck> {
    if dry_run {
        println!(
            "Dry-run: kubectl --context {} diff -f {}",
            kubectl_context,
            yaml_path.display()
        );
        return Ok(RemoteManifestDiffCheck::SkippedDryRun);
    }

    let output = Command::new("kubectl")
        .args([
            "--context",
            kubectl_context,
            "diff",
            "-f",
            yaml_path.to_str().unwrap(),
        ])
        .output()
        .context("Failed to execute kubectl diff")?;

    Ok(classify_kubectl_diff_result(
        output.status.code(),
        &output.stdout,
        &output.stderr,
    ))
}

fn classify_kubectl_diff_result(
    status_code: Option<i32>,
    stdout: &[u8],
    stderr: &[u8],
) -> RemoteManifestDiffCheck {
    let output = combine_command_output(stdout, stderr);

    match status_code {
        Some(0) => RemoteManifestDiffCheck::InSync,
        Some(1) => RemoteManifestDiffCheck::Drift(output),
        Some(code) => RemoteManifestDiffCheck::CheckFailed(format!(
            "kubectl diff exited with status {}.\n{}",
            code,
            fallback_command_output(output)
        )),
        None => RemoteManifestDiffCheck::CheckFailed(format!(
            "kubectl diff terminated without an exit status.\n{}",
            fallback_command_output(output)
        )),
    }
}

fn combine_command_output(stdout: &[u8], stderr: &[u8]) -> String {
    let stdout = String::from_utf8_lossy(stdout).trim().to_string();
    let stderr = String::from_utf8_lossy(stderr).trim().to_string();

    match (stdout.is_empty(), stderr.is_empty()) {
        (false, false) => format!("{}\n{}", stdout, stderr),
        (false, true) => stdout,
        (true, false) => stderr,
        (true, true) => String::new(),
    }
}

fn fallback_command_output(output: String) -> String {
    if output.is_empty() {
        "No output returned by kubectl diff.".to_string()
    } else {
        output
    }
}

fn confirm_remote_manifest_diff(
    yaml_path: &Path,
    diff_check: RemoteManifestDiffCheck,
    policy: PromptPolicy,
) -> Result<bool> {
    match diff_check {
        RemoteManifestDiffCheck::InSync | RemoteManifestDiffCheck::SkippedDryRun => Ok(true),
        RemoteManifestDiffCheck::Drift(diff) => {
            println!(
                "\n⚠️  Remote cluster state is not aligned with {}.",
                yaml_path.display()
            );
            if diff.is_empty() {
                println!("kubectl diff detected changes but did not return a diff body.");
            } else {
                println!("{}", diff);
            }

            policy.confirm(
                "approval to deploy despite remote drift",
                "Do you want to continue with deployment despite this drift?",
                false,
                "Align the cluster with the manifest before deploying, or inspect the drift with --dry-run.",
            )
        }
        RemoteManifestDiffCheck::CheckFailed(message) => {
            println!("\n⚠️  Could not verify remote manifest alignment.");
            println!("{}", message);

            policy.confirm(
                "approval to deploy without the remote alignment check",
                "Do you want to continue without the remote alignment check?",
                false,
                "Make `kubectl diff` succeed against the target context before deploying.",
            )
        }
    }
}

/// Guards a deployment to a protected environment.
///
/// Interactively the guard is typing the environment name back. Unattended it
/// is `--confirm-env`, an explicit opt-in the caller has to spell out, rather
/// than a prompt that a non-TTY run would simply skip past.
fn confirm_protected_environment(
    env_name: &str,
    confirm_env: Option<&str>,
    dry_run: bool,
    policy: PromptPolicy,
) -> Result<()> {
    println!("⚠️  WARNING: Deployment to {} is PROTECTED!", env_name);

    if dry_run {
        println!("Dry-run: skipping the protected environment confirmation.");
        return Ok(());
    }

    let confirmation = match confirm_env {
        Some(value) => value.to_string(),
        None if policy.is_interactive() => Text::new(&format!(
            "Type the environment name '{}' to confirm:",
            env_name
        ))
        .prompt()
        .context("Production confirmation was cancelled")?,
        None => {
            return Err(policy.cannot_ask(
                "confirmation of a protected environment",
                &format!(
                    "Pass --confirm-env {} to confirm the deployment explicitly.",
                    env_name
                ),
            ));
        }
    };

    if confirmation != env_name {
        return Err(anyhow::anyhow!(
            "Confirmation '{}' does not match the target environment '{}'. Deployment aborted.",
            confirmation,
            env_name
        ));
    }

    Ok(())
}

/// Offers to undo the local manifest edit after a failed deploy.
///
/// This runs on an error path, so a missing terminal must not replace the
/// original failure with a prompt failure: the edit is reported and left in
/// place for the caller to deal with.
fn offer_revert(
    yaml_path: &Path,
    original_content: &str,
    auto_continue: bool,
    policy: PromptPolicy,
) -> Result<()> {
    if auto_continue {
        return Ok(());
    }

    if !policy.is_interactive() {
        println!(
            "Local YAML at {} still holds the updated tag; revert it manually if needed.",
            yaml_path.display()
        );
        return Ok(());
    }

    if Confirm::new("Revert local YAML changes?")
        .with_default(true)
        .prompt()?
    {
        fs::write(yaml_path, original_content)?;
        println!("YAML reverted.");
    }

    Ok(())
}

fn resolve_tag(
    env: &Environment,
    service: &ServiceSource,
    input: Option<String>,
    wait_for_tag: Option<String>,
    policy: PromptPolicy,
) -> Result<String> {
    if let Some(tag) = wait_for_tag {
        return wait_for_exact_tag(env, service, tag, policy);
    }

    if let Some(tag) = input {
        let images = fetch_service_images(env, service, true)?;
        let available_tags = collect_available_tags(&images);

        if available_tags.is_empty() {
            return Err(anyhow::anyhow!(
                "No images found for service {}",
                service.name
            ));
        }

        return resolve_from_list("Image tag", &available_tags, tag, policy);
    }

    let images = fetch_service_images(env, service, true)?;
    let available_tags = collect_available_tags(&images);

    if available_tags.is_empty() {
        return Err(anyhow::anyhow!(
            "No images found for service {}",
            service.name
        ));
    }

    let options: Vec<String> = images
        .iter()
        .map(|img| {
            format!(
                "{:<15} ({}) [{}]",
                img.display_tag(),
                img.age_string(),
                img.short_hash()
            )
        })
        .collect();

    let selection = policy.select(
        "an image tag",
        "Select Image Tag:",
        options,
        &format!(
            "Pass --tag with one of:\n{}",
            format_candidates(&available_tags)
        ),
    )?;

    let tag = selection
        .split_whitespace()
        .next()
        .unwrap_or("")
        .trim_end_matches(',')
        .to_string();
    Ok(tag)
}

fn fetch_service_images(
    env: &Environment,
    service: &ServiceSource,
    announce: bool,
) -> Result<Vec<ImageMetadata>> {
    let project = env.gcp_project.as_deref().unwrap_or("MOCK_PROJECT");

    if announce {
        println!(
            "Fetching images for {} using path {}...",
            service.name, service.image_path
        );
    }

    let images = match Registry::fetch_images(&service.image_path) {
        Ok(imgs) => imgs,
        Err(e) => {
            if project == "MOCK_PROJECT" {
                mock_images()
            } else {
                return Err(e).context("Failed to fetch images from Artifact Registry");
            }
        }
    };

    Ok(images)
}

fn collect_available_tags(images: &[ImageMetadata]) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut tags = Vec::new();

    for image in images {
        for tag in &image.tags {
            if seen.insert(tag.clone()) {
                tags.push(tag.clone());
            }
        }
    }

    tags
}

fn wait_for_exact_tag(
    env: &Environment,
    service: &ServiceSource,
    tag: String,
    policy: PromptPolicy,
) -> Result<String> {
    let mut attempt = 1;

    println!(
        "Waiting for image tag '{}' for service '{}'.",
        tag, service.name
    );
    println!("Registry image: {}", service.image_path);
    println!(
        "Polling every {} seconds.{}",
        TAG_RETRY_INTERVAL.as_secs(),
        if policy.is_interactive() {
            " Press 'q' to cancel."
        } else {
            ""
        }
    );

    loop {
        if policy.is_interactive() {
            render_tag_wait_status(&tag, &service.name, attempt, "Checking registry", None, '.')?;
        }

        let images = fetch_service_images(env, service, false)?;
        let available_tags = collect_available_tags(&images);

        if available_tags.iter().any(|available| available == &tag) {
            if policy.is_interactive() {
                clear_tag_wait_status_line()?;
            }
            if attempt == 1 {
                println!("Found image tag '{}'.", tag);
            } else {
                println!(
                    "Found image tag '{}' after {} checks. Continuing deployment.",
                    tag, attempt
                );
            }
            return Ok(tag);
        }

        wait_for_next_tag_check(service, &tag, attempt, policy)?;
        attempt += 1;
    }
}

fn wait_for_next_tag_check(
    service: &ServiceSource,
    tag: &str,
    attempt: usize,
    policy: PromptPolicy,
) -> Result<()> {
    // Raw mode and the 'q' shortcut both need a terminal; without one, just wait.
    if !policy.is_interactive() {
        println!(
            "Tag '{}' not available yet (check {}). Retrying in {} seconds.",
            tag,
            attempt,
            TAG_RETRY_INTERVAL.as_secs()
        );
        thread::sleep(TAG_RETRY_INTERVAL);
        return Ok(());
    }

    let _raw_mode = RawModeGuard::new()?;
    let spinner = ['|', '/', '-', '\\'];
    let start = Instant::now();
    let mut frame = 0usize;

    loop {
        let elapsed = start.elapsed();
        if elapsed >= TAG_RETRY_INTERVAL {
            break;
        }

        let remaining = TAG_RETRY_INTERVAL.saturating_sub(elapsed).as_secs();
        let minutes = remaining / 60;
        let seconds = remaining % 60;

        render_tag_wait_status(
            tag,
            &service.name,
            attempt,
            "Tag not available yet",
            Some((minutes, seconds)),
            spinner[frame % spinner.len()],
        )?;

        if event::poll(TAG_WAIT_POLL_INTERVAL)? {
            loop {
                if let Event::Key(key) = event::read()?
                    && matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
                {
                    match key.code {
                        KeyCode::Char('q') | KeyCode::Esc => {
                            clear_tag_wait_status_line()?;
                            return Err(anyhow::anyhow!(TAG_WAIT_CANCELLED_MESSAGE));
                        }
                        _ => {}
                    }
                }

                if !event::poll(Duration::from_millis(0))? {
                    break;
                }
            }
        }

        frame += 1;
    }

    clear_tag_wait_status_line()?;
    Ok(())
}

fn render_tag_wait_status(
    _tag: &str,
    _service_name: &str,
    attempt: usize,
    phase: &str,
    remaining: Option<(u64, u64)>,
    marker: char,
) -> Result<()> {
    clear_tag_wait_status_line()?;

    let status = match remaining {
        Some((minutes, seconds)) => {
            format!(
                "{} {} | check {} | next {:02}:{:02} | q cancel",
                marker, phase, attempt, minutes, seconds
            )
        }
        None => {
            format!(
                "{} {} | completed {} | q cancel",
                marker,
                phase,
                attempt.saturating_sub(1)
            )
        }
    };

    let terminal_width = terminal_size()
        .map(|(width, _)| width as usize)
        .unwrap_or(120);
    print!("{}", truncate_for_terminal_width(&status, terminal_width));

    io::stdout().flush()?;
    Ok(())
}

fn clear_tag_wait_status_line() -> Result<()> {
    crossterm::execute!(
        io::stdout(),
        MoveToColumn(0),
        Clear(ClearType::FromCursorDown)
    )
    .context("Failed to refresh tag wait status line")?;
    Ok(())
}

fn truncate_for_terminal_width(input: &str, width: usize) -> String {
    let safe_width = width.saturating_sub(1);
    let char_count = input.chars().count();
    if char_count <= safe_width {
        return input.to_string();
    }

    if safe_width == 0 {
        return String::new();
    }

    if safe_width <= 3 {
        return ".".repeat(safe_width);
    }

    let mut truncated = String::new();
    for ch in input.chars().take(safe_width - 3) {
        truncated.push(ch);
    }
    truncated.push_str("...");
    truncated
}

struct RawModeGuard;

impl RawModeGuard {
    fn new() -> Result<Self> {
        enable_raw_mode()
            .context("Failed to enable terminal raw mode while waiting for image tag")?;
        Ok(Self)
    }
}

impl Drop for RawModeGuard {
    fn drop(&mut self) {
        let _ = disable_raw_mode();
    }
}

fn mock_images() -> Vec<ImageMetadata> {
    use chrono::Duration;
    let now = Utc::now();
    vec![
        ImageMetadata {
            tags: vec!["v1.2.3".to_string(), "latest".to_string()],
            update_time: now - Duration::hours(2),
            name: "auth-service@sha256:abcdef123456789".to_string(),
        },
        ImageMetadata {
            tags: vec!["v1.2.2".to_string()],
            update_time: now - Duration::days(1),
            name: "auth-service@sha256:123456789abcdef".to_string(),
        },
        ImageMetadata {
            tags: vec!["v1.1.0".to_string()],
            update_time: now - Duration::days(5),
            name: "auth-service@sha256:987654321fedcba".to_string(),
        },
    ]
}

/// Generic disambiguation logic
fn resolve_from_list(
    label: &str,
    items: &[String],
    input: String,
    policy: PromptPolicy,
) -> Result<String> {
    // 1. Exact match
    if items.contains(&input) {
        return Ok(input);
    }

    // 2. Partial matches
    let matches: Vec<&String> = items.iter().filter(|&i| i.contains(&input)).collect();
    let noun = label.to_lowercase();
    let decision = format!("{} {}", article_for(&noun), noun);

    match matches.len() {
        0 => {
            if policy.is_interactive() {
                println!("No {} matches '{}'.", noun, input);
            }
            policy.select(
                &decision,
                &format!("Select {}:", label),
                items.to_vec(),
                &format!(
                    "'{}' matches no {}. Valid values:\n{}",
                    input,
                    noun,
                    format_candidates(items)
                ),
            )
        }
        // A single fuzzy match is a guess, so it is offered rather than applied.
        // Without a human to confirm it, resolving it silently could target the
        // wrong service after a rename, so the exact value is demanded instead.
        1 => {
            let suggest = matches[0].clone();
            if !policy.is_interactive() {
                return Err(policy.cannot_ask(
                    &decision,
                    &format!(
                        "'{}' is not an exact {}; the closest match is '{}'. Pass it verbatim.",
                        input, noun, suggest
                    ),
                ));
            }

            if Confirm::new(&format!("Did you mean '{}'?", suggest))
                .with_default(true)
                .prompt()?
            {
                Ok(suggest)
            } else {
                Select::new(&format!("Select {}:", label), items.to_vec())
                    .prompt()
                    .context(format!("{} selection was cancelled", label))
            }
        }
        _ => policy.select(
            &decision,
            &format!("Multiple matches for '{}'. Select {}:", input, noun),
            matches.iter().map(|m| (*m).clone()).collect(),
            &format!(
                "'{}' matches {} entries:\n{}\nPass one of them verbatim.",
                input,
                matches.len(),
                format_candidates(&matches)
            ),
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Duration;

    #[test]
    fn test_service_display_name_unique() {
        let s = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img1".to_string(),
            container_name: "c1".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir1/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let all = vec![s.clone()];
        assert_eq!(get_service_display_name(&s, &all), "service1");
    }

    #[test]
    fn test_service_display_name_duplicate_name() {
        let s1 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img1".to_string(),
            container_name: "c1".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir1/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let s2 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img2".to_string(),
            container_name: "c2".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir2/deploy.yaml"),
            namespace: Some("ns2".to_string()),
            selector: None,
        };
        let all = vec![s1.clone(), s2.clone()];
        assert_eq!(get_service_display_name(&s1, &all), "service1 (ns1)");
        assert_eq!(get_service_display_name(&s2, &all), "service1 (ns2)");
    }

    #[test]
    fn test_service_display_name_duplicate_name_and_ns() {
        let s1 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img1".to_string(),
            container_name: "c1".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir1/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let s2 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img2".to_string(),
            container_name: "c2".to_string(),
            source_name: "demo".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir2/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let all = vec![s1.clone(), s2.clone()];
        assert_eq!(
            get_service_display_name(&s1, &all),
            "service1 (ns1) [main]/dir1/deploy.yaml"
        );
        assert_eq!(
            get_service_display_name(&s2, &all),
            "service1 (ns1) [demo]/dir2/deploy.yaml"
        );
    }

    #[test]
    fn test_collect_available_tags_preserves_order_and_deduplicates() {
        let now = Utc::now();
        let images = vec![
            ImageMetadata {
                tags: vec!["v1.2.3".to_string(), "latest".to_string()],
                update_time: now,
                name: "service@sha256:abcdef1".to_string(),
            },
            ImageMetadata {
                tags: vec!["latest".to_string(), "v1.2.2".to_string()],
                update_time: now,
                name: "service@sha256:abcdef2".to_string(),
            },
        ];

        assert_eq!(
            collect_available_tags(&images),
            vec![
                "v1.2.3".to_string(),
                "latest".to_string(),
                "v1.2.2".to_string()
            ]
        );
    }

    #[test]
    fn test_deploy_wait_for_tag_conflicts_with_tag() {
        let parse = Cli::try_parse_from([
            "davit",
            "deploy",
            "--tag",
            "v1.2.3",
            "--wait-for-tag",
            "v1.2.4",
        ]);
        assert!(parse.is_err());
    }

    #[test]
    fn test_deploy_wait_for_tag_accepts_value() {
        let parse = Cli::try_parse_from(["davit", "deploy", "--wait-for-tag", "v1.2.3"]);
        assert!(parse.is_ok());
    }

    #[test]
    fn test_deploy_auto_apply_accepts_flag() {
        let parse = Cli::try_parse_from(["davit", "deploy", "--auto-apply"]);
        assert!(parse.is_ok());
    }

    /// Non-interactive resolution never falls through to a prompt, and says why.
    fn blocked() -> PromptPolicy {
        PromptPolicy::new(true, true)
    }

    #[test]
    fn test_decision_labels_read_grammatically() {
        assert_eq!(article_for("environment"), "an");
        assert_eq!(article_for("image tag"), "an");
        assert_eq!(article_for("service"), "a");

        // The label reaches the user inside the error, so check it end to end.
        let items = vec!["v1.2.3".to_string(), "v1.2.4".to_string()];
        let err = resolve_from_list("Image tag", &items, "v1.2".to_string(), blocked())
            .unwrap_err()
            .to_string();
        assert!(err.starts_with("Cannot ask for an image tag:"), "{err}");
    }

    #[test]
    fn test_resolve_from_list_accepts_an_exact_match_without_prompting() {
        let items = vec!["svc-api (no-namespace)".to_string(), "billing".to_string()];
        let resolved =
            resolve_from_list("Service", &items, "billing".to_string(), blocked()).unwrap();
        assert_eq!(resolved, "billing");
    }

    #[test]
    fn test_resolve_from_list_lists_candidates_when_ambiguous() {
        let items = vec![
            "svc-api (no-namespace)".to_string(),
            "svc-api (tenant-b)".to_string(),
            "billing".to_string(),
        ];
        let err = resolve_from_list("Service", &items, "svc-api".to_string(), blocked())
            .unwrap_err()
            .to_string();

        assert!(err.contains("Cannot ask for a service"), "{err}");
        assert!(err.contains("'svc-api' matches 2 entries"), "{err}");
        assert!(err.contains("  - svc-api (no-namespace)"), "{err}");
        assert!(err.contains("  - svc-api (tenant-b)"), "{err}");
        assert!(!err.contains("billing"), "{err}");
    }

    #[test]
    fn test_resolve_from_list_refuses_to_guess_a_single_fuzzy_match() {
        let items = vec!["billing".to_string()];
        let err = resolve_from_list("Service", &items, "billin".to_string(), blocked())
            .unwrap_err()
            .to_string();

        assert!(err.contains("the closest match is 'billing'"), "{err}");
    }

    #[test]
    fn test_resolve_from_list_lists_valid_values_when_nothing_matches() {
        let items = vec!["preprod".to_string(), "production".to_string()];
        let err = resolve_from_list("Environment", &items, "staging".to_string(), blocked())
            .unwrap_err()
            .to_string();

        assert!(err.contains("matches no environment"), "{err}");
        assert!(err.contains("  - preprod"), "{err}");
        assert!(err.contains("  - production"), "{err}");
    }

    #[test]
    fn test_protected_environment_is_not_confirmed_in_dry_run() {
        assert!(confirm_protected_environment("production", None, true, blocked()).is_ok());
    }

    #[test]
    fn test_protected_environment_accepts_a_matching_confirm_env() {
        assert!(
            confirm_protected_environment("production", Some("production"), false, blocked())
                .is_ok()
        );
    }

    #[test]
    fn test_protected_environment_rejects_a_mismatched_confirm_env() {
        let err = confirm_protected_environment("production", Some("preprod"), false, blocked())
            .unwrap_err()
            .to_string();

        assert!(err.contains("'preprod' does not match"), "{err}");
        assert!(err.contains("'production'"), "{err}");
    }

    #[test]
    fn test_protected_environment_demands_confirm_env_when_unattended() {
        let err = confirm_protected_environment("production", None, false, blocked())
            .unwrap_err()
            .to_string();

        assert!(err.contains("Pass --confirm-env production"), "{err}");
    }

    /// A manifest per entry, so the environment sees colliding service names.
    fn env_with_manifests(dir: &std::path::Path, manifests: &[(&str, &str)]) -> Environment {
        for (file, body) in manifests {
            std::fs::write(dir.join(file), body).unwrap();
        }

        Environment {
            name: "preprod".to_string(),
            env_yaml_dir: dir.to_path_buf(),
            env_yaml_dir_extra: Default::default(),
            kubectl_context: "ctx".to_string(),
            gcp_project: None,
            protected: None,
        }
    }

    fn manifest(name: &str, namespace: Option<&str>) -> String {
        let namespace = namespace
            .map(|ns| format!("\n  namespace: {ns}"))
            .unwrap_or_default();
        format!(
            "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: {name}{namespace}\nspec:\n  template:\n    spec:\n      containers:\n      - name: {name}\n        image: gcr.io/p/{name}:v1\n"
        )
    }

    #[test]
    fn test_list_services_in_namespace_is_ordered_by_name_then_namespace() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(
            dir.path(),
            &[
                ("c.yml", &manifest("svc-api", Some("tenant-b"))),
                ("a.yml", &manifest("billing", Some("core"))),
                ("b.yml", &manifest("svc-api", None)),
            ],
        );

        let services = list_services_in_namespace(&env, None).unwrap();
        let ordered: Vec<(&str, Option<&str>)> = services
            .iter()
            .map(|s| (s.name.as_str(), s.namespace.as_deref()))
            .collect();

        assert_eq!(
            ordered,
            vec![
                ("billing", Some("core")),
                ("svc-api", None),
                ("svc-api", Some("tenant-b")),
            ]
        );
    }

    #[test]
    fn test_list_services_in_namespace_filters_and_disambiguates() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(
            dir.path(),
            &[
                ("a.yml", &manifest("svc-api", Some("tenant-b"))),
                ("b.yml", &manifest("svc-api", None)),
            ],
        );

        let services = list_services_in_namespace(&env, Some("tenant-b")).unwrap();
        assert_eq!(services.len(), 1);
        // Once narrowed the name no longer collides, so --service takes it plain.
        assert_eq!(get_service_display_name(&services[0], &services), "svc-api");
    }

    #[test]
    fn test_list_services_in_namespace_selects_manifests_declaring_none() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(
            dir.path(),
            &[
                ("a.yml", &manifest("svc-api", Some("tenant-b"))),
                ("b.yml", &manifest("svc-api", None)),
                ("c.yml", &manifest("reports", None)),
            ],
        );

        let services = list_services_in_namespace(&env, Some(NO_NAMESPACE)).unwrap();
        let names: Vec<&str> = services.iter().map(|s| s.name.as_str()).collect();

        assert_eq!(names, vec!["reports", "svc-api"]);
        assert!(services.iter().all(|s| s.namespace.is_none()));
    }

    /// The NAMESPACE column and the --namespace filter must not drift apart.
    #[test]
    fn test_every_listed_namespace_is_accepted_by_the_filter() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(
            dir.path(),
            &[
                ("a.yml", &manifest("svc-api", Some("tenant-b"))),
                ("b.yml", &manifest("svc-api", None)),
                ("c.yml", &manifest("billing", Some("core"))),
            ],
        );

        for service in list_services_in_namespace(&env, None).unwrap() {
            let printed = namespace_label(&service).to_string();
            assert!(
                list_services_in_namespace(&env, Some(&printed)).is_ok(),
                "listing printed '{printed}', which --namespace rejects"
            );
        }
    }

    #[test]
    fn test_list_services_in_namespace_reports_no_undeclared_namespace_distinctly() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(dir.path(), &[("a.yml", &manifest("billing", Some("core")))]);

        let err = list_services_in_namespace(&env, Some(NO_NAMESPACE))
            .unwrap_err()
            .to_string();
        assert_eq!(err, "No services without a declared namespace found");
    }

    #[test]
    fn test_strip_namespace_suffix_accepts_both_spellings() {
        let cases = [
            // (printed by `list services`, --namespace, expected)
            ("svc-api (tenant-b)", Some("tenant-b"), "svc-api"),
            ("svc-api (no-namespace)", Some(NO_NAMESPACE), "svc-api"),
            // Already plain, or no --namespace to narrow by: left alone.
            ("svc-api", Some("tenant-b"), "svc-api"),
            ("svc-api (tenant-b)", None, "svc-api (tenant-b)"),
            // A suffix naming a different namespace is not the one we narrowed to.
            ("svc-api (tenant-b)", Some("core"), "svc-api (tenant-b)"),
            // The three-part form stays ambiguous inside the narrowed set too.
            (
                "svc-api (core) [main]/a.yml",
                Some("core"),
                "svc-api (core) [main]/a.yml",
            ),
        ];

        for (value, namespace, expected) in cases {
            assert_eq!(
                strip_namespace_suffix(value.to_string(), namespace),
                expected,
                "{value} with --namespace {namespace:?}"
            );
        }
    }

    /// Rows are (display name, anything); the payload is irrelevant to the width.
    fn named(names: &[&str]) -> Vec<(String, ())> {
        names.iter().map(|n| ((*n).to_string(), ())).collect()
    }

    #[test]
    fn test_service_column_fits_the_widest_name() {
        assert_eq!(
            service_column_width(&named(&["billing", "svc-api (tenant-b)"])),
            18
        );
    }

    #[test]
    fn test_service_column_never_shrinks_below_its_header() {
        assert_eq!(service_column_width(&named(&["a", "bc"])), "SERVICE".len());
        assert_eq!(service_column_width::<()>(&[]), "SERVICE".len());
    }

    #[test]
    fn test_service_column_ignores_names_past_the_cap() {
        let outlier = "x".repeat(MAX_SERVICE_COLUMN + 60);
        let width = service_column_width(&named(&["svc-api (tenant-b)", &outlier]));

        // The outlier overflows its own row rather than padding every other one.
        assert_eq!(width, 18);
    }

    #[test]
    fn test_service_column_falls_back_to_the_cap_when_all_names_overflow() {
        let long = "x".repeat(MAX_SERVICE_COLUMN + 1);
        assert_eq!(
            service_column_width(&named(&[&long, &long])),
            "SERVICE".len()
        );
    }

    #[test]
    fn test_version_flag_is_available() {
        // `Cli` is not Debug, so unwrap_err() is not available here.
        let Err(err) = Cli::try_parse_from(["davit", "--version"]) else {
            panic!("--version should short-circuit parsing");
        };
        assert_eq!(err.kind(), clap::error::ErrorKind::DisplayVersion);
        assert!(err.to_string().contains(env!("CARGO_PKG_VERSION")));
    }

    #[test]
    fn test_namespace_accepts_the_bare_sentinel() {
        let parsed =
            Cli::try_parse_from(["davit", "list", "services", "--namespace", NO_NAMESPACE])
                .unwrap();
        match parsed.command {
            Commands::List {
                command: ListCommands::Services { namespace, .. },
            } => assert_eq!(namespace.as_deref(), Some(NO_NAMESPACE)),
            _ => panic!("expected list services"),
        }
    }

    #[test]
    fn test_list_services_in_namespace_rejects_an_empty_namespace() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(dir.path(), &[("a.yml", &manifest("billing", Some("core")))]);

        let err = list_services_in_namespace(&env, Some("absent"))
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("No services found in namespace 'absent'"),
            "{err}"
        );
    }

    #[test]
    fn test_no_fetch_skips_the_refresh_entirely() {
        let dir = tempfile::tempdir().unwrap();
        let env = env_with_manifests(dir.path(), &[("a.yml", &manifest("billing", Some("core")))]);

        // The directory is not a git repository, so a refresh fails and, with
        // nobody to confirm the failure, aborts. --no-fetch never gets there.
        assert!(pull_yaml_sources(&env, false, false, "listing", blocked()).is_err());
        assert!(pull_yaml_sources(&env, false, true, "listing", blocked()).is_ok());
    }

    #[test]
    fn test_pull_source_skips_a_dirty_working_copy() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        assert!(
            Command::new("git")
                .arg("-C")
                .arg(root)
                .args(["init", "-q"])
                .status()
                .unwrap()
                .success()
        );
        // Untracked content is enough to make the working copy dirty.
        std::fs::write(root.join("manifest.yml"), "kind: Deployment\n").unwrap();

        let source = YamlSource {
            name: "main".to_string(),
            root: root.to_path_buf(),
        };

        let report = pull_source(&source, false).unwrap();
        assert!(report.success);
        assert!(
            report.stdout.contains("uncommitted changes"),
            "{}",
            report.stdout
        );
    }

    #[test]
    fn test_pull_source_does_not_inspect_the_working_copy_in_dry_run() {
        let dir = tempfile::tempdir().unwrap();
        let source = YamlSource {
            name: "main".to_string(),
            root: dir.path().to_path_buf(),
        };

        // Not a repository at all: reaching git status would fail.
        let report = pull_source(&source, true).unwrap();
        assert!(report.stdout.contains("Dry-run"), "{}", report.stdout);
    }

    #[test]
    fn test_no_fetch_is_accepted_by_every_reading_command() {
        assert!(Cli::try_parse_from(["davit", "deploy", "--no-fetch"]).is_ok());
        assert!(Cli::try_parse_from(["davit", "info", "--no-fetch"]).is_ok());
        assert!(Cli::try_parse_from(["davit", "list", "services", "--no-fetch"]).is_ok());
    }

    #[test]
    fn test_deploy_accepts_a_namespace() {
        let parse = Cli::try_parse_from([
            "davit",
            "deploy",
            "--service",
            "svc-api",
            "--namespace",
            "tenant-b",
        ]);
        assert!(parse.is_ok());
    }

    #[test]
    fn test_list_subcommands_parse() {
        assert!(Cli::try_parse_from(["davit", "list", "envs"]).is_ok());
        assert!(Cli::try_parse_from(["davit", "list", "services", "--env", "preprod"]).is_ok());
    }

    #[test]
    fn test_non_interactive_is_a_global_flag() {
        assert!(
            Cli::try_parse_from(["davit", "deploy", "--non-interactive"])
                .unwrap()
                .non_interactive
        );
        assert!(
            Cli::try_parse_from(["davit", "--non-interactive", "info"])
                .unwrap()
                .non_interactive
        );
    }

    #[test]
    fn test_classify_kubectl_diff_result_in_sync() {
        assert_eq!(
            classify_kubectl_diff_result(Some(0), b"", b""),
            RemoteManifestDiffCheck::InSync
        );
    }

    #[test]
    fn test_classify_kubectl_diff_result_detects_drift() {
        assert_eq!(
            classify_kubectl_diff_result(Some(1), b"diff body\n", b""),
            RemoteManifestDiffCheck::Drift("diff body".to_string())
        );
    }

    #[test]
    fn test_classify_kubectl_diff_result_combines_stdout_and_stderr_on_failure() {
        assert_eq!(
            classify_kubectl_diff_result(Some(2), b"stdout\n", b"stderr\n"),
            RemoteManifestDiffCheck::CheckFailed(
                "kubectl diff exited with status 2.\nstdout\nstderr".to_string()
            )
        );
    }

    #[test]
    fn test_classify_kubectl_diff_result_handles_missing_output() {
        assert_eq!(
            classify_kubectl_diff_result(Some(3), b"", b""),
            RemoteManifestDiffCheck::CheckFailed(
                "kubectl diff exited with status 3.\nNo output returned by kubectl diff."
                    .to_string()
            )
        );
    }

    #[test]
    fn test_truncate_for_terminal_width_truncates_with_ellipsis() {
        assert_eq!(
            truncate_for_terminal_width("1234567890", 8),
            "1234...".to_string()
        );
    }

    #[test]
    fn test_truncate_for_terminal_width_keeps_short_message() {
        assert_eq!(
            truncate_for_terminal_width("short", 10),
            "short".to_string()
        );
    }

    #[test]
    fn test_truncate_for_terminal_width_reserves_last_terminal_column() {
        let rendered = truncate_for_terminal_width("1234567890", 10);
        assert_eq!(rendered.chars().count(), 9);
        assert_eq!(rendered, "123456...".to_string());
    }

    #[test]
    fn test_collect_parallel_pull_results_preserves_source_order() {
        let sources = vec![
            YamlSource {
                name: "one".to_string(),
                root: PathBuf::from("/tmp/one"),
            },
            YamlSource {
                name: "two".to_string(),
                root: PathBuf::from("/tmp/two"),
            },
            YamlSource {
                name: "three".to_string(),
                root: PathBuf::from("/tmp/three"),
            },
        ];

        let results = collect_parallel_pull_results(&sources, 2, |source| Ok(source.name.clone()));
        let ordered_names: Vec<String> = results
            .into_iter()
            .map(|(source, result)| {
                assert_eq!(result.as_ref().unwrap(), &source.name);
                source.name
            })
            .collect();

        assert_eq!(ordered_names, vec!["one", "two", "three"]);
    }

    #[test]
    fn test_collect_parallel_pull_results_respects_parallel_limit() {
        let sources: Vec<YamlSource> = (0..12)
            .map(|idx| YamlSource {
                name: format!("repo-{idx}"),
                root: PathBuf::from(format!("/tmp/repo-{idx}")),
            })
            .collect();

        let active = Arc::new(AtomicUsize::new(0));
        let max_seen = Arc::new(AtomicUsize::new(0));

        let active_for_pull = Arc::clone(&active);
        let max_seen_for_pull = Arc::clone(&max_seen);

        let results = collect_parallel_pull_results(&sources, 5, move |_source| {
            let current = active_for_pull.fetch_add(1, Ordering::SeqCst) + 1;
            max_seen_for_pull.fetch_max(current, Ordering::SeqCst);
            thread::sleep(Duration::from_millis(20));
            active_for_pull.fetch_sub(1, Ordering::SeqCst);
            Ok(())
        });

        assert_eq!(results.len(), 12);
        assert_eq!(max_seen.load(Ordering::SeqCst), 5);
    }
}