autom8-cli 0.3.0

CLI automation tool for orchestrating Claude-powered development
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
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
//! Shell completion infrastructure for autom8.
//!
//! This module provides:
//! - Shell detection from the `$SHELL` environment variable
//! - Completion script generation for bash, zsh, and fish
//! - Installation path resolution for each shell type
//!
//! # Usage
//!
//! ```ignore
//! use autom8::completion::{detect_shell, get_completion_path, generate_completion_script};
//!
//! // Detect user's shell
//! let shell = detect_shell()?;
//!
//! // Get the installation path
//! let path = get_completion_path(shell)?;
//!
//! // Generate the completion script
//! let script = generate_completion_script(shell);
//! ```

use crate::error::{Autom8Error, Result};
use clap::Command;
use clap_complete::{generate, Shell};
use std::io::Write;
use std::path::PathBuf;

/// List of supported shell names for error messages.
pub const SUPPORTED_SHELLS: &[&str] = &["bash", "zsh", "fish"];

/// Supported shell types for completion scripts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShellType {
    Bash,
    Zsh,
    Fish,
}

impl ShellType {
    /// Convert to the `clap_complete::Shell` type.
    pub fn to_clap_shell(self) -> Shell {
        match self {
            ShellType::Bash => Shell::Bash,
            ShellType::Zsh => Shell::Zsh,
            ShellType::Fish => Shell::Fish,
        }
    }

    /// Get the display name of the shell.
    pub fn name(&self) -> &'static str {
        match self {
            ShellType::Bash => "bash",
            ShellType::Zsh => "zsh",
            ShellType::Fish => "fish",
        }
    }

    /// Parse a shell type from a string name.
    ///
    /// # Arguments
    ///
    /// * `name` - The shell name (e.g., "bash", "zsh", "fish")
    ///
    /// # Returns
    ///
    /// * `Ok(ShellType)` - The parsed shell type
    /// * `Err(Autom8Error)` - If the shell name is not supported
    pub fn from_name(name: &str) -> Result<ShellType> {
        match name.to_lowercase().as_str() {
            "bash" => Ok(ShellType::Bash),
            "zsh" => Ok(ShellType::Zsh),
            "fish" => Ok(ShellType::Fish),
            _ => Err(Autom8Error::ShellCompletion(format!(
                "Unsupported shell: '{}'. Supported shells are: {}.",
                name,
                SUPPORTED_SHELLS.join(", ")
            ))),
        }
    }
}

impl std::fmt::Display for ShellType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// Detect the user's shell from the `$SHELL` environment variable.
///
/// Parses the shell path (e.g., `/bin/zsh`, `/usr/bin/bash`) and returns
/// the corresponding `ShellType`.
///
/// # Returns
///
/// * `Ok(ShellType)` - The detected shell type
/// * `Err(Autom8Error)` - If `$SHELL` is not set or the shell is unsupported
///
/// # Examples
///
/// ```ignore
/// // With $SHELL=/bin/zsh
/// let shell = detect_shell()?;
/// assert_eq!(shell, ShellType::Zsh);
/// ```
pub fn detect_shell() -> Result<ShellType> {
    let shell_path = std::env::var("SHELL").map_err(|_| {
        Autom8Error::ShellCompletion(
            "$SHELL environment variable is not set. \
             Please specify your shell manually or set the $SHELL variable."
                .to_string(),
        )
    })?;

    parse_shell_from_path(&shell_path)
}

/// Parse a shell type from a shell path.
///
/// Extracts the basename from the path and matches against supported shells.
///
/// # Arguments
///
/// * `shell_path` - Full path to the shell (e.g., `/bin/zsh`, `/usr/local/bin/fish`)
///
/// # Returns
///
/// * `Ok(ShellType)` - The detected shell type
/// * `Err(Autom8Error)` - If the shell is not supported
pub fn parse_shell_from_path(shell_path: &str) -> Result<ShellType> {
    // Extract the basename from the path
    let shell_name = std::path::Path::new(shell_path)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or(shell_path);

    match shell_name {
        "bash" => Ok(ShellType::Bash),
        "zsh" => Ok(ShellType::Zsh),
        "fish" => Ok(ShellType::Fish),
        _ => Err(Autom8Error::ShellCompletion(format!(
            "Unsupported shell: '{}'. \
             Supported shells are: bash, zsh, fish.",
            shell_name
        ))),
    }
}

/// Get the installation path for completion scripts.
///
/// Returns the appropriate path for each shell:
/// - **Bash**: `~/.local/share/bash-completion/completions/autom8` (XDG standard)
///   Falls back to `~/.bash_completion.d/autom8` if XDG path doesn't exist
/// - **Zsh**: `~/.zfunc/_autom8`
/// - **Fish**: `~/.config/fish/completions/autom8.fish`
///
/// # Arguments
///
/// * `shell` - The target shell type
///
/// # Returns
///
/// * `Ok(PathBuf)` - The path where the completion script should be installed
/// * `Err(Autom8Error)` - If the home directory cannot be determined
pub fn get_completion_path(shell: ShellType) -> Result<PathBuf> {
    let home = dirs::home_dir().ok_or_else(|| {
        Autom8Error::ShellCompletion("Could not determine home directory".to_string())
    })?;

    let path = match shell {
        ShellType::Bash => {
            // Prefer XDG path, check if the directory exists
            let xdg_path = home.join(".local/share/bash-completion/completions");
            if xdg_path.exists() {
                xdg_path.join("autom8")
            } else {
                // Fall back to traditional path
                home.join(".bash_completion.d/autom8")
            }
        }
        ShellType::Zsh => home.join(".zfunc/_autom8"),
        ShellType::Fish => home.join(".config/fish/completions/autom8.fish"),
    };

    Ok(path)
}

/// Ensure the parent directory for a completion script exists.
///
/// Creates the parent directory (and all ancestors) if it doesn't exist.
///
/// # Arguments
///
/// * `path` - The path to the completion script
///
/// # Returns
///
/// * `Ok(())` - Directory exists or was created successfully
/// * `Err(Autom8Error)` - If directory creation fails
pub fn ensure_completion_dir(path: &std::path::Path) -> Result<()> {
    if let Some(parent) = path.parent() {
        if !parent.exists() {
            std::fs::create_dir_all(parent).map_err(|e| {
                Autom8Error::ShellCompletion(format!(
                    "Failed to create completion directory '{}': {}",
                    parent.display(),
                    e
                ))
            })?;
        }
    }
    Ok(())
}

/// Build the clap Command structure for completion generation.
///
/// This creates a command hierarchy that mirrors the CLI defined in `main.rs`,
/// allowing clap_complete to generate accurate completion scripts.
fn build_cli() -> Command {
    Command::new("autom8")
        .version(env!("CARGO_PKG_VERSION"))
        .about("CLI automation tool for orchestrating Claude-powered development")
        .arg(
            clap::Arg::new("file")
                .help("Path to a spec.md or spec.json file (shorthand for `run --spec <file>`)")
                .value_hint(clap::ValueHint::FilePath),
        )
        .arg(
            clap::Arg::new("verbose")
                .short('v')
                .long("verbose")
                .help("Show full Claude output instead of spinner (useful for debugging)")
                .global(true)
                .action(clap::ArgAction::SetTrue),
        )
        .subcommand(
            Command::new("run")
                .about("Run the agent loop to implement spec stories")
                .arg(
                    clap::Arg::new("spec")
                        .long("spec")
                        .help("Path to the spec JSON or markdown file")
                        .default_value("./spec.json")
                        .value_hint(clap::ValueHint::FilePath),
                )
                .arg(
                    clap::Arg::new("skip-review")
                        .long("skip-review")
                        .help("Skip the review loop and go directly to committing")
                        .action(clap::ArgAction::SetTrue),
                ),
        )
        .subcommand(
            Command::new("status")
                .about("Check the current run status")
                .arg(
                    clap::Arg::new("all")
                        .short('a')
                        .long("all")
                        .help("Show status across all projects")
                        .action(clap::ArgAction::SetTrue),
                )
                .arg(
                    clap::Arg::new("global")
                        .short('g')
                        .long("global")
                        .help("Show status across all projects (alias for --all)")
                        .action(clap::ArgAction::SetTrue),
                ),
        )
        .subcommand(Command::new("resume").about("Resume a failed or interrupted run"))
        .subcommand(Command::new("clean").about("Clean up spec files from config directory"))
        .subcommand(
            Command::new("init")
                .about("Initialize autom8 config directory structure for current project"),
        )
        .subcommand(
            Command::new("projects").about("List all known projects in the config directory"),
        )
        .subcommand(Command::new("list").about("Show a tree view of all projects with status"))
        .subcommand(
            Command::new("describe")
                .about("Show detailed information about a specific project")
                .arg(
                    clap::Arg::new("project_name")
                        .help("The project name to describe (defaults to current directory)"),
                ),
        )
        .subcommand(
            Command::new("pr-review").about("Analyze PR review comments and fix real issues"),
        )
        .subcommand(
            Command::new("monitor")
                .about("Monitor autom8 activity across all projects (dashboard view)")
                .arg(
                    clap::Arg::new("project")
                        .short('p')
                        .long("project")
                        .help("Filter to a specific project"),
                )
                .arg(
                    clap::Arg::new("interval")
                        .short('i')
                        .long("interval")
                        .help("Polling interval in seconds (default: 1)")
                        .default_value("1"),
                ),
        )
        .subcommand(Command::new("gui").about("Launch the native GUI to monitor autom8 activity"))
        .subcommand(
            Command::new("improve").about(
                "Continue iterating on a feature with Claude using context from previous runs",
            ),
        )
        .subcommand(
            Command::new("config")
                .about("View, modify, or reset configuration")
                .arg(
                    clap::Arg::new("global")
                        .short('g')
                        .long("global")
                        .help("Show only the global configuration")
                        .action(clap::ArgAction::SetTrue)
                        .conflicts_with("project"),
                )
                .arg(
                    clap::Arg::new("project")
                        .short('p')
                        .long("project")
                        .help("Show only the project configuration")
                        .action(clap::ArgAction::SetTrue)
                        .conflicts_with("global"),
                )
                .subcommand(
                    Command::new("set")
                        .about("Set a configuration value")
                        .arg(
                            clap::Arg::new("global")
                                .short('g')
                                .long("global")
                                .help("Set in global config instead of project config")
                                .action(clap::ArgAction::SetTrue),
                        )
                        .arg(
                            clap::Arg::new("key")
                                .help("The configuration key to set")
                                .required(true)
                                .value_parser([
                                    "review",
                                    "commit",
                                    "pull_request",
                                    "worktree",
                                    "worktree_path_pattern",
                                    "worktree_cleanup",
                                ]),
                        )
                        .arg(
                            clap::Arg::new("value")
                                .help("The value to set (true/false for boolean keys)")
                                .required(true),
                        ),
                )
                .subcommand(
                    Command::new("reset")
                        .about("Reset configuration to default values")
                        .arg(
                            clap::Arg::new("global")
                                .short('g')
                                .long("global")
                                .help("Reset global config instead of project config")
                                .action(clap::ArgAction::SetTrue),
                        )
                        .arg(
                            clap::Arg::new("yes")
                                .short('y')
                                .long("yes")
                                .help("Skip confirmation prompt")
                                .action(clap::ArgAction::SetTrue),
                        ),
                ),
        )
}

/// Generate a completion script for the specified shell.
///
/// Creates a completion script that includes all autom8 commands, subcommands,
/// flags, and options. The script includes dynamic spec file completion that
/// queries `~/.config/autom8/*/spec/` at completion time.
///
/// # Arguments
///
/// * `shell` - The target shell type
///
/// # Returns
///
/// The completion script as a String.
pub fn generate_completion_script(shell: ShellType) -> String {
    let mut cmd = build_cli();
    let mut buf = Vec::new();
    generate(shell.to_clap_shell(), &mut cmd, "autom8", &mut buf);
    let base_script = String::from_utf8(buf).unwrap_or_default();

    // Append dynamic spec completion functions
    match shell {
        ShellType::Bash => format!("{}\n{}", base_script, generate_bash_spec_completion()),
        ShellType::Zsh => format!("{}\n{}", base_script, generate_zsh_spec_completion()),
        ShellType::Fish => format!("{}\n{}", base_script, generate_fish_spec_completion()),
    }
}

/// Generate bash-specific dynamic spec completion function.
fn generate_bash_spec_completion() -> &'static str {
    r#"
# Dynamic spec file completion for autom8
_autom8_spec_files() {
    local config_dir="$HOME/.config/autom8"
    local project_name=""
    local specs=()

    # Try to detect current project from git repo
    if git rev-parse --git-dir &>/dev/null; then
        project_name=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null)
    fi

    # If in a project and that project has specs, show only those
    if [[ -n "$project_name" && -d "$config_dir/$project_name/spec" ]]; then
        local spec_dir="$config_dir/$project_name/spec"
        if compgen -G "$spec_dir/*.json" &>/dev/null || compgen -G "$spec_dir/*.md" &>/dev/null; then
            for f in "$spec_dir"/*.json "$spec_dir"/*.md; do
                [[ -f "$f" ]] && specs+=("$(basename "$f")")
            done
        fi
    fi

    # If no project specs found, show specs from all projects
    if [[ ${#specs[@]} -eq 0 && -d "$config_dir" ]]; then
        for project_dir in "$config_dir"/*/spec; do
            if [[ -d "$project_dir" ]]; then
                for f in "$project_dir"/*.json "$project_dir"/*.md; do
                    [[ -f "$f" ]] && specs+=("$(basename "$f")")
                done
            fi
        done
    fi

    # Remove duplicates and sort
    printf '%s\n' "${specs[@]}" | sort -u
}

# Override completion for --spec flag and positional arguments
_autom8_complete() {
    local cur prev words cword
    _init_completion || return

    # Check if we're completing the --spec flag value
    if [[ "$prev" == "--spec" ]]; then
        COMPREPLY=($(compgen -W "$(_autom8_spec_files)" -- "$cur"))
        return
    fi

    # Check if completing first positional arg (not a subcommand)
    if [[ $cword -eq 1 && "$cur" != -* ]]; then
        # Get subcommands
        local subcommands="run status resume clean config init projects list describe pr-review monitor gui improve"
        # Get spec files
        local specs=$(_autom8_spec_files)
        COMPREPLY=($(compgen -W "$subcommands $specs" -- "$cur"))
        return
    fi

    # Config key completion
    if [[ "${words[1]}" == "config" && "${words[2]}" == "set" ]]; then
        if [[ $cword -eq 3 ]]; then
            # Complete config keys
            COMPREPLY=($(compgen -W "review commit pull_request worktree worktree_path_pattern worktree_cleanup" -- "$cur"))
            return
        elif [[ $cword -eq 4 && "${words[3]}" != "worktree_path_pattern" ]]; then
            # Complete boolean values for non-string keys
            COMPREPLY=($(compgen -W "true false" -- "$cur"))
            return
        fi
    fi

    # Fall back to default autom8 completion
    _autom8 "$@"
}

complete -F _autom8_complete autom8
"#
}

/// Generate zsh-specific dynamic spec completion function.
fn generate_zsh_spec_completion() -> &'static str {
    r#"
# Dynamic spec file completion for autom8
_autom8_spec_files() {
    local config_dir="$HOME/.config/autom8"
    local project_name=""
    local -a specs

    # Try to detect current project from git repo
    if git rev-parse --git-dir &>/dev/null; then
        project_name=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null)
    fi

    # If in a project and that project has specs, show only those
    if [[ -n "$project_name" && -d "$config_dir/$project_name/spec" ]]; then
        local spec_dir="$config_dir/$project_name/spec"
        specs=(${(f)"$(ls "$spec_dir"/*.json "$spec_dir"/*.md 2>/dev/null | xargs -n1 basename 2>/dev/null)"})
    fi

    # If no project specs found, show specs from all projects
    if [[ ${#specs[@]} -eq 0 && -d "$config_dir" ]]; then
        specs=(${(f)"$(ls "$config_dir"/*/spec/*.json "$config_dir"/*/spec/*.md 2>/dev/null | xargs -n1 basename 2>/dev/null)"})
    fi

    # Remove duplicates and print
    printf '%s\n' "${(u)specs[@]}"
}

# Override _autom8 to add spec file completion
if (( $+functions[_autom8_original] )); then
    : # Already patched
else
    # Save original function if it exists
    if (( $+functions[_autom8] )); then
        functions[_autom8_original]=$functions[_autom8]
    fi

    _autom8() {
        local curcontext="$curcontext" state line
        typeset -A opt_args

        # Check if completing --spec value
        if [[ "${words[$CURRENT-1]}" == "--spec" ]]; then
            local -a spec_files
            spec_files=(${(f)"$(_autom8_spec_files)"})
            _describe 'spec file' spec_files
            return
        fi

        # Check if completing first positional argument
        if [[ $CURRENT -eq 2 && "${words[2]}" != -* ]]; then
            local -a completions
            local -a spec_files
            spec_files=(${(f)"$(_autom8_spec_files)"})
            completions=(
                'run:Run the agent loop to implement spec stories'
                'status:Check the current run status'
                'resume:Resume a failed or interrupted run'
                'clean:Clean up spec files from config directory'
                'config:View, modify, or reset configuration'
                'init:Initialize autom8 config directory structure'
                'projects:List all known projects'
                'list:Show a tree view of all projects with status'
                'describe:Show detailed information about a specific project'
                'pr-review:Analyze PR review comments and fix real issues'
                'monitor:Monitor autom8 activity across all projects'
                'gui:Launch the native GUI to monitor autom8 activity'
                'improve:Continue iterating on a feature with Claude using context from previous runs'
            )
            for spec in "${spec_files[@]}"; do
                [[ -n "$spec" ]] && completions+=("$spec:Spec file")
            done
            _describe 'command or spec' completions
            return
        fi

        # Config set key/value completion
        if [[ "${words[2]}" == "config" && "${words[3]}" == "set" ]]; then
            if [[ $CURRENT -eq 4 ]]; then
                local -a config_keys
                config_keys=(
                    'review:Enable code review step'
                    'commit:Enable auto-commit'
                    'pull_request:Enable auto-PR creation'
                    'worktree:Enable worktree mode'
                    'worktree_path_pattern:Pattern for worktree names'
                    'worktree_cleanup:Auto-cleanup worktrees'
                )
                _describe 'config key' config_keys
                return
            elif [[ $CURRENT -eq 5 && "${words[4]}" != "worktree_path_pattern" ]]; then
                local -a bool_values
                bool_values=('true' 'false')
                _describe 'value' bool_values
                return
            fi
        fi

        # Fall back to original completion if it exists
        if (( $+functions[_autom8_original] )); then
            _autom8_original "$@"
        fi
    }

    compdef _autom8 autom8
fi
"#
}

/// Generate fish-specific dynamic spec completion function.
fn generate_fish_spec_completion() -> &'static str {
    r#"
# Dynamic spec file completion for autom8
function __autom8_spec_files
    set -l config_dir "$HOME/.config/autom8"
    set -l project_name ""

    # Try to detect current project from git repo
    if git rev-parse --git-dir &>/dev/null
        set project_name (basename (git rev-parse --show-toplevel 2>/dev/null) 2>/dev/null)
    end

    # If in a project and that project has specs, show only those
    if test -n "$project_name"; and test -d "$config_dir/$project_name/spec"
        set -l spec_dir "$config_dir/$project_name/spec"
        for f in $spec_dir/*.json $spec_dir/*.md
            if test -f "$f"
                basename "$f"
            end
        end
        return
    end

    # If no project specs found, show specs from all projects
    if test -d "$config_dir"
        for spec_dir in $config_dir/*/spec
            if test -d "$spec_dir"
                for f in $spec_dir/*.json $spec_dir/*.md
                    if test -f "$f"
                        basename "$f"
                    end
                end
            end
        end | sort -u
    end
end

# Add spec file completions for --spec flag
complete -c autom8 -l spec -xa '(__autom8_spec_files)'

# Add spec file completions for positional argument (first arg that's not a flag)
complete -c autom8 -n '__fish_is_first_arg; and not __fish_seen_subcommand_from run status resume clean config init projects list describe pr-review monitor gui improve' -xa '(__autom8_spec_files)'

# Config set key completion
complete -c autom8 -n '__fish_seen_subcommand_from config; and __fish_seen_subcommand_from set; and test (count (commandline -opc)) -eq 3' -xa 'review commit pull_request worktree worktree_path_pattern worktree_cleanup'

# Config set value completion (true/false for boolean keys)
complete -c autom8 -n '__fish_seen_subcommand_from config; and __fish_seen_subcommand_from set; and test (count (commandline -opc)) -eq 4; and not string match -q worktree_path_pattern (commandline -opc)[-1]' -xa 'true false'
"#
}

/// Output the completion script for a shell to stdout.
///
/// This is used by the hidden `completions` subcommand to let power users
/// manually manage their completion scripts.
///
/// # Arguments
///
/// * `shell` - The target shell type
pub fn print_completion_script(shell: ShellType) {
    print!("{}", generate_completion_script(shell));
}

/// Write a completion script to the specified path.
///
/// Creates parent directories if needed, then writes the completion script.
///
/// # Arguments
///
/// * `shell` - The target shell type
/// * `path` - The destination path for the script
///
/// # Returns
///
/// * `Ok(())` - Script written successfully
/// * `Err(Autom8Error)` - If directory creation or file write fails
pub fn write_completion_script(shell: ShellType, path: &std::path::Path) -> Result<()> {
    // Ensure parent directory exists
    ensure_completion_dir(path)?;

    // Generate and write the script
    let script = generate_completion_script(shell);
    let mut file = std::fs::File::create(path).map_err(|e| {
        Autom8Error::ShellCompletion(format!(
            "Failed to create completion file '{}': {}",
            path.display(),
            e
        ))
    })?;

    file.write_all(script.as_bytes()).map_err(|e| {
        Autom8Error::ShellCompletion(format!(
            "Failed to write completion script to '{}': {}",
            path.display(),
            e
        ))
    })?;

    Ok(())
}

/// Result of completion installation.
#[derive(Debug)]
pub struct CompletionInstallResult {
    /// The shell that completions were installed for.
    pub shell: ShellType,
    /// The path where the completion script was written.
    pub path: PathBuf,
    /// Additional setup instructions for the user, if any.
    pub setup_instructions: Option<String>,
}

/// Check if zsh fpath includes ~/.zfunc.
///
/// Returns true if ~/.zfunc is already configured in fpath.
fn is_zfunc_in_fpath() -> bool {
    // Check if FPATH environment variable includes .zfunc
    if let Ok(fpath) = std::env::var("FPATH") {
        let home = dirs::home_dir().unwrap_or_default();
        let zfunc = home.join(".zfunc");
        let zfunc_str = zfunc.to_string_lossy();

        for path in fpath.split(':') {
            if path == zfunc_str || path == "~/.zfunc" {
                return true;
            }
        }
    }
    false
}

/// Get setup instructions for zsh if ~/.zfunc is not in fpath.
fn get_zsh_setup_instructions() -> Option<String> {
    if is_zfunc_in_fpath() {
        None
    } else {
        Some(
            "To enable completions, add the following to your ~/.zshrc:\n\n\
             fpath=(~/.zfunc $fpath)\n\
             autoload -Uz compinit && compinit\n\n\
             Then restart your shell or run: source ~/.zshrc"
                .to_string(),
        )
    }
}

/// Get setup instructions for bash.
fn get_bash_setup_instructions(path: &std::path::Path) -> Option<String> {
    // Check if bash-completion is likely set up (XDG location)
    if path
        .to_string_lossy()
        .contains("bash-completion/completions")
    {
        // XDG location should be auto-loaded
        Some("Restart your shell to enable completions.".to_string())
    } else {
        // Non-XDG location needs manual sourcing
        Some(format!(
            "To enable completions, add to your ~/.bashrc:\n\n\
             source {}\n\n\
             Then restart your shell or run: source ~/.bashrc",
            path.display()
        ))
    }
}

/// Install shell completions for the current user.
///
/// Detects the user's shell from `$SHELL`, generates the appropriate
/// completion script, and writes it to the correct location.
///
/// # Returns
///
/// * `Ok(CompletionInstallResult)` - Installation succeeded
/// * `Err(Autom8Error)` - If shell detection or file writing fails
///
/// # Example
///
/// ```ignore
/// match install_completions() {
///     Ok(result) => {
///         println!("Installed {} completions to {}", result.shell, result.path.display());
///         if let Some(instructions) = result.setup_instructions {
///             println!("{}", instructions);
///         }
///     }
///     Err(e) => eprintln!("Failed: {}", e),
/// }
/// ```
pub fn install_completions() -> Result<CompletionInstallResult> {
    let shell = detect_shell()?;
    let path = get_completion_path(shell)?;

    write_completion_script(shell, &path)?;

    let setup_instructions = match shell {
        ShellType::Zsh => get_zsh_setup_instructions(),
        ShellType::Bash => get_bash_setup_instructions(&path),
        ShellType::Fish => {
            // Fish auto-loads from ~/.config/fish/completions/
            Some("Restart your shell to enable completions.".to_string())
        }
    };

    Ok(CompletionInstallResult {
        shell,
        path,
        setup_instructions,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    // ======================================================================
    // Tests for US-001: Shell detection
    // ======================================================================

    #[test]
    fn test_parse_shell_bash() {
        assert_eq!(parse_shell_from_path("/bin/bash").unwrap(), ShellType::Bash);
        assert_eq!(
            parse_shell_from_path("/usr/bin/bash").unwrap(),
            ShellType::Bash
        );
        assert_eq!(
            parse_shell_from_path("/usr/local/bin/bash").unwrap(),
            ShellType::Bash
        );
    }

    #[test]
    fn test_parse_shell_zsh() {
        assert_eq!(parse_shell_from_path("/bin/zsh").unwrap(), ShellType::Zsh);
        assert_eq!(
            parse_shell_from_path("/usr/bin/zsh").unwrap(),
            ShellType::Zsh
        );
        assert_eq!(
            parse_shell_from_path("/usr/local/bin/zsh").unwrap(),
            ShellType::Zsh
        );
    }

    #[test]
    fn test_parse_shell_fish() {
        assert_eq!(parse_shell_from_path("/bin/fish").unwrap(), ShellType::Fish);
        assert_eq!(
            parse_shell_from_path("/usr/bin/fish").unwrap(),
            ShellType::Fish
        );
        assert_eq!(
            parse_shell_from_path("/usr/local/bin/fish").unwrap(),
            ShellType::Fish
        );
        assert_eq!(
            parse_shell_from_path("/opt/homebrew/bin/fish").unwrap(),
            ShellType::Fish
        );
    }

    #[test]
    fn test_parse_shell_unsupported() {
        let result = parse_shell_from_path("/bin/sh");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Unsupported shell"));
        assert!(err.contains("sh"));

        let result = parse_shell_from_path("/bin/tcsh");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("tcsh"));
    }

    #[test]
    fn test_parse_shell_unsupported_contains_supported_list() {
        let result = parse_shell_from_path("/bin/ksh");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("bash"));
        assert!(err.contains("zsh"));
        assert!(err.contains("fish"));
    }

    // ======================================================================
    // Tests for US-001: Path resolution
    // ======================================================================

    #[test]
    fn test_completion_path_bash() {
        let path = get_completion_path(ShellType::Bash).unwrap();
        let path_str = path.to_string_lossy();

        // Should end with the expected filename
        assert!(path_str.ends_with("autom8"));

        // Should be in one of the two expected directories
        assert!(
            path_str.contains("bash-completion/completions")
                || path_str.contains(".bash_completion.d"),
            "Bash path should be in XDG or traditional location: {}",
            path_str
        );
    }

    #[test]
    fn test_completion_path_zsh() {
        let path = get_completion_path(ShellType::Zsh).unwrap();
        let path_str = path.to_string_lossy();

        assert!(
            path_str.ends_with(".zfunc/_autom8"),
            "Zsh path should end with .zfunc/_autom8: {}",
            path_str
        );
    }

    #[test]
    fn test_completion_path_fish() {
        let path = get_completion_path(ShellType::Fish).unwrap();
        let path_str = path.to_string_lossy();

        assert!(
            path_str.ends_with(".config/fish/completions/autom8.fish"),
            "Fish path should end with .config/fish/completions/autom8.fish: {}",
            path_str
        );
    }

    // ======================================================================
    // Tests for US-001: Script generation
    // ======================================================================

    #[test]
    fn test_generate_completion_script_bash() {
        let script = generate_completion_script(ShellType::Bash);

        // Should contain autom8 command
        assert!(script.contains("autom8"), "Script should reference autom8");

        // Should contain subcommands
        assert!(script.contains("run"), "Script should include run command");
        assert!(
            script.contains("status"),
            "Script should include status command"
        );
        assert!(
            script.contains("resume"),
            "Script should include resume command"
        );
        assert!(
            script.contains("clean"),
            "Script should include clean command"
        );
        assert!(
            script.contains("init"),
            "Script should include init command"
        );
        assert!(
            script.contains("projects"),
            "Script should include projects command"
        );
        assert!(
            script.contains("list"),
            "Script should include list command"
        );
        assert!(
            script.contains("describe"),
            "Script should include describe command"
        );
        assert!(
            script.contains("pr-review"),
            "Script should include pr-review command"
        );
        assert!(
            script.contains("monitor"),
            "Script should include monitor command"
        );
    }

    #[test]
    fn test_generate_completion_script_zsh() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should be a valid zsh completion script
        assert!(
            script.contains("#compdef autom8"),
            "Zsh script should start with #compdef"
        );

        // Should contain subcommands
        assert!(script.contains("run"));
        assert!(script.contains("status"));
        assert!(script.contains("init"));
    }

    #[test]
    fn test_generate_completion_script_fish() {
        let script = generate_completion_script(ShellType::Fish);

        // Should be a valid fish completion script
        assert!(
            script.contains("complete"),
            "Fish script should contain complete commands"
        );
        assert!(
            script.contains("autom8"),
            "Fish script should reference autom8"
        );
    }

    #[test]
    fn test_generate_completion_script_contains_flags() {
        let script = generate_completion_script(ShellType::Bash);

        // Should contain common flags
        assert!(
            script.contains("verbose") || script.contains("-v"),
            "Script should include verbose flag"
        );
        assert!(script.contains("spec"), "Script should include spec option");
        assert!(
            script.contains("skip-review"),
            "Script should include skip-review flag"
        );
        assert!(
            script.contains("all") || script.contains("-a"),
            "Script should include all flag"
        );
        assert!(
            script.contains("global") || script.contains("-g"),
            "Script should include global flag"
        );
        assert!(
            script.contains("project") || script.contains("-p"),
            "Script should include project flag"
        );
        assert!(
            script.contains("interval") || script.contains("-i"),
            "Script should include interval flag"
        );
    }

    // ======================================================================
    // Tests for US-001: ShellType utilities
    // ======================================================================

    #[test]
    fn test_shell_type_name() {
        assert_eq!(ShellType::Bash.name(), "bash");
        assert_eq!(ShellType::Zsh.name(), "zsh");
        assert_eq!(ShellType::Fish.name(), "fish");
    }

    #[test]
    fn test_shell_type_display() {
        assert_eq!(format!("{}", ShellType::Bash), "bash");
        assert_eq!(format!("{}", ShellType::Zsh), "zsh");
        assert_eq!(format!("{}", ShellType::Fish), "fish");
    }

    #[test]
    fn test_shell_type_to_clap_shell() {
        assert_eq!(ShellType::Bash.to_clap_shell(), Shell::Bash);
        assert_eq!(ShellType::Zsh.to_clap_shell(), Shell::Zsh);
        assert_eq!(ShellType::Fish.to_clap_shell(), Shell::Fish);
    }

    // ======================================================================
    // Tests for US-001: Directory creation
    // ======================================================================

    #[test]
    fn test_ensure_completion_dir_with_existing_parent() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("autom8");

        // Parent already exists
        let result = ensure_completion_dir(&path);
        assert!(result.is_ok());
    }

    #[test]
    fn test_ensure_completion_dir_creates_parent() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("new_dir").join("autom8");

        // Parent doesn't exist
        assert!(!path.parent().unwrap().exists());

        let result = ensure_completion_dir(&path);
        assert!(result.is_ok());
        assert!(path.parent().unwrap().exists());
    }

    #[test]
    fn test_ensure_completion_dir_creates_nested_parents() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("a").join("b").join("c").join("autom8");

        let result = ensure_completion_dir(&path);
        assert!(result.is_ok());
        assert!(path.parent().unwrap().exists());
    }

    // ======================================================================
    // Tests for US-001: Write completion script
    // ======================================================================

    #[test]
    fn test_write_completion_script_creates_file() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("autom8");

        let result = write_completion_script(ShellType::Bash, &path);
        assert!(result.is_ok());
        assert!(path.exists());

        // Verify content
        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("autom8"));
    }

    #[test]
    fn test_write_completion_script_creates_parent_dirs() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("nested").join("dir").join("autom8");

        let result = write_completion_script(ShellType::Zsh, &path);
        assert!(result.is_ok());
        assert!(path.exists());
    }

    // ======================================================================
    // Tests for US-002: Completion installation from init
    // ======================================================================

    #[test]
    fn test_completion_install_result_has_expected_fields() {
        // Verify CompletionInstallResult struct has all expected fields
        let result = CompletionInstallResult {
            shell: ShellType::Zsh,
            path: PathBuf::from("/tmp/test"),
            setup_instructions: Some("Test instructions".to_string()),
        };

        assert_eq!(result.shell, ShellType::Zsh);
        assert_eq!(result.path, PathBuf::from("/tmp/test"));
        assert_eq!(
            result.setup_instructions,
            Some("Test instructions".to_string())
        );
    }

    #[test]
    fn test_completion_install_result_without_setup_instructions() {
        // Verify setup_instructions can be None
        let result = CompletionInstallResult {
            shell: ShellType::Fish,
            path: PathBuf::from("/tmp/test"),
            setup_instructions: None,
        };

        assert!(result.setup_instructions.is_none());
    }

    #[test]
    fn test_zsh_setup_instructions_contain_fpath() {
        // When fpath check would fail (not in FPATH), instructions should mention fpath
        // We can't easily test the actual check without modifying FPATH,
        // but we can test the instruction content
        let instructions = "fpath=(~/.zfunc $fpath)\nautoload -Uz compinit && compinit";
        assert!(instructions.contains("fpath"));
        assert!(instructions.contains("compinit"));
        assert!(instructions.contains("autoload"));
    }

    #[test]
    fn test_bash_setup_instructions_for_xdg_path() {
        let path = PathBuf::from("/home/user/.local/share/bash-completion/completions/autom8");
        let instructions = get_bash_setup_instructions(&path);

        assert!(instructions.is_some());
        let instructions = instructions.unwrap();
        // XDG path should just say restart shell
        assert!(instructions.contains("Restart"));
    }

    #[test]
    fn test_bash_setup_instructions_for_non_xdg_path() {
        let path = PathBuf::from("/home/user/.bash_completion.d/autom8");
        let instructions = get_bash_setup_instructions(&path);

        assert!(instructions.is_some());
        let instructions = instructions.unwrap();
        // Non-XDG path should mention sourcing
        assert!(instructions.contains("source"));
        assert!(instructions.contains(&path.display().to_string()));
    }

    #[test]
    fn test_write_completion_script_overwrites_existing() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("autom8");

        // Write initial script
        let result = write_completion_script(ShellType::Bash, &path);
        assert!(result.is_ok());

        let content1 = std::fs::read_to_string(&path).unwrap();

        // Write again (should overwrite, not fail)
        let result = write_completion_script(ShellType::Bash, &path);
        assert!(result.is_ok());

        let content2 = std::fs::read_to_string(&path).unwrap();

        // Content should be the same (idempotent)
        assert_eq!(content1, content2);
    }

    #[test]
    fn test_write_completion_script_overwrites_different_shell() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("autom8");

        // Write bash script
        write_completion_script(ShellType::Bash, &path).unwrap();
        let bash_content = std::fs::read_to_string(&path).unwrap();

        // Overwrite with zsh script
        write_completion_script(ShellType::Zsh, &path).unwrap();
        let zsh_content = std::fs::read_to_string(&path).unwrap();

        // Content should be different
        assert_ne!(bash_content, zsh_content);
        assert!(zsh_content.contains("#compdef"));
    }

    #[test]
    fn test_install_completions_available_as_public_api() {
        // Verify install_completions is a public function
        // (This test verifies the API exists; actual installation depends on env)
        let _: fn() -> Result<CompletionInstallResult> = install_completions;
    }

    #[test]
    fn test_completion_install_result_shell_display() {
        // Verify shell type displays correctly for messages
        let result = CompletionInstallResult {
            shell: ShellType::Zsh,
            path: PathBuf::from("/home/user/.zfunc/_autom8"),
            setup_instructions: None,
        };

        let message = format!(
            "Installed {} completions to {}",
            result.shell,
            result.path.display()
        );
        assert!(message.contains("zsh"));
        assert!(message.contains("_autom8"));
    }

    #[test]
    fn test_get_zsh_setup_instructions_content() {
        // Test the content of zsh setup instructions (assuming fpath not set)
        // Since we can't easily manipulate FPATH in tests, we test the instruction format
        let expected_content = "fpath=(~/.zfunc $fpath)";

        // The instructions should include this if zfunc is not in fpath
        // We can verify the helper function produces valid instructions
        let home = dirs::home_dir().unwrap();
        let zfunc_path = home.join(".zfunc/_autom8");
        assert!(zfunc_path.to_string_lossy().contains(".zfunc"));

        // Verify expected content format
        assert!(expected_content.contains("fpath"));
        assert!(expected_content.contains("$fpath"));
    }

    // ======================================================================
    // Tests for US-003: Dynamic spec file completion and utility subcommand
    // ======================================================================

    #[test]
    fn test_shell_type_from_name_bash() {
        let result = ShellType::from_name("bash");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), ShellType::Bash);
    }

    #[test]
    fn test_shell_type_from_name_zsh() {
        let result = ShellType::from_name("zsh");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), ShellType::Zsh);
    }

    #[test]
    fn test_shell_type_from_name_fish() {
        let result = ShellType::from_name("fish");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), ShellType::Fish);
    }

    #[test]
    fn test_shell_type_from_name_case_insensitive() {
        // Should handle uppercase
        assert_eq!(ShellType::from_name("BASH").unwrap(), ShellType::Bash);
        assert_eq!(ShellType::from_name("ZSH").unwrap(), ShellType::Zsh);
        assert_eq!(ShellType::from_name("FISH").unwrap(), ShellType::Fish);

        // Should handle mixed case
        assert_eq!(ShellType::from_name("Bash").unwrap(), ShellType::Bash);
        assert_eq!(ShellType::from_name("Zsh").unwrap(), ShellType::Zsh);
        assert_eq!(ShellType::from_name("Fish").unwrap(), ShellType::Fish);
    }

    #[test]
    fn test_shell_type_from_name_invalid() {
        let result = ShellType::from_name("powershell");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Unsupported shell"));
        assert!(err.contains("powershell"));
    }

    #[test]
    fn test_shell_type_from_name_error_lists_supported_shells() {
        let result = ShellType::from_name("invalid");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("bash"));
        assert!(err.contains("zsh"));
        assert!(err.contains("fish"));
    }

    #[test]
    fn test_supported_shells_constant() {
        assert!(SUPPORTED_SHELLS.contains(&"bash"));
        assert!(SUPPORTED_SHELLS.contains(&"zsh"));
        assert!(SUPPORTED_SHELLS.contains(&"fish"));
        assert_eq!(SUPPORTED_SHELLS.len(), 3);
    }

    #[test]
    fn test_bash_completion_includes_dynamic_spec_function() {
        let script = generate_completion_script(ShellType::Bash);

        // Should include the dynamic spec completion function
        assert!(
            script.contains("_autom8_spec_files"),
            "Bash script should include _autom8_spec_files function"
        );

        // Should reference the config directory
        assert!(
            script.contains(".config/autom8"),
            "Bash script should reference the config directory"
        );

        // Should check for .json and .md files
        assert!(
            script.contains(".json") && script.contains(".md"),
            "Bash script should check for both .json and .md files"
        );

        // Should include git project detection
        assert!(
            script.contains("git rev-parse"),
            "Bash script should include git project detection"
        );
    }

    #[test]
    fn test_zsh_completion_includes_dynamic_spec_function() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should include the dynamic spec completion function
        assert!(
            script.contains("_autom8_spec_files"),
            "Zsh script should include _autom8_spec_files function"
        );

        // Should reference the config directory
        assert!(
            script.contains(".config/autom8"),
            "Zsh script should reference the config directory"
        );

        // Should check for .json and .md files
        assert!(
            script.contains(".json") && script.contains(".md"),
            "Zsh script should check for both .json and .md files"
        );

        // Should include git project detection
        assert!(
            script.contains("git rev-parse"),
            "Zsh script should include git project detection"
        );
    }

    #[test]
    fn test_fish_completion_includes_dynamic_spec_function() {
        let script = generate_completion_script(ShellType::Fish);

        // Should include the dynamic spec completion function
        assert!(
            script.contains("__autom8_spec_files"),
            "Fish script should include __autom8_spec_files function"
        );

        // Should reference the config directory
        assert!(
            script.contains(".config/autom8"),
            "Fish script should reference the config directory"
        );

        // Should check for .json and .md files
        assert!(
            script.contains(".json") && script.contains(".md"),
            "Fish script should check for both .json and .md files"
        );

        // Should include git project detection
        assert!(
            script.contains("git rev-parse"),
            "Fish script should include git project detection"
        );
    }

    #[test]
    fn test_bash_completion_includes_spec_flag_completion() {
        let script = generate_completion_script(ShellType::Bash);

        // Should have completion for --spec flag
        assert!(
            script.contains("--spec"),
            "Bash script should include --spec flag completion"
        );
    }

    #[test]
    fn test_zsh_completion_includes_spec_flag_completion() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should have completion for --spec flag
        assert!(
            script.contains("--spec"),
            "Zsh script should include --spec flag completion"
        );
    }

    #[test]
    fn test_fish_completion_includes_spec_flag_completion() {
        let script = generate_completion_script(ShellType::Fish);

        // Should have completion for --spec flag
        assert!(
            script.contains("--spec") || script.contains("-l spec"),
            "Fish script should include --spec flag completion"
        );
    }

    #[test]
    fn test_bash_completion_includes_subcommands_in_first_arg() {
        let script = generate_completion_script(ShellType::Bash);

        // Should list subcommands for first argument completion
        assert!(
            script.contains("run") && script.contains("status") && script.contains("resume"),
            "Bash script should include subcommands for first arg completion"
        );
    }

    #[test]
    fn test_print_completion_script_exists() {
        // Verify the function exists and is callable
        let _: fn(ShellType) = print_completion_script;
    }

    // ======================================================================
    // Tests for config subcommand completion
    // ======================================================================

    #[test]
    fn test_bash_completion_includes_config_subcommand() {
        let script = generate_completion_script(ShellType::Bash);

        // Should include config subcommand
        assert!(
            script.contains("autom8__config"),
            "Bash script should include config subcommand"
        );

        // Should include config in subcommands list
        assert!(
            script.contains("run status resume clean config init projects list describe pr-review monitor gui improve"),
            "Bash script should include all commands in dynamic subcommands list"
        );
    }

    #[test]
    fn test_zsh_completion_includes_config_subcommand() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should include config subcommand with description
        assert!(
            script.contains("'config:View, modify, or reset configuration'"),
            "Zsh script should include config subcommand with description"
        );
    }

    #[test]
    fn test_fish_completion_includes_config_subcommand() {
        let script = generate_completion_script(ShellType::Fish);

        // Should include config subcommand
        assert!(
            script.contains("-a \"config\"") || script.contains("config"),
            "Fish script should include config subcommand"
        );

        // Should include config in the exclusion list for spec file completion
        assert!(
            script.contains("run status resume clean config init projects list describe pr-review monitor gui improve"),
            "Fish script should include all commands in dynamic subcommands list"
        );
    }

    #[test]
    fn test_bash_completion_includes_config_set_subcommand() {
        let script = generate_completion_script(ShellType::Bash);

        // Should include config set subcommand
        assert!(
            script.contains("autom8__config__set"),
            "Bash script should include config set subcommand"
        );

        // Should include config reset subcommand
        assert!(
            script.contains("autom8__config__reset"),
            "Bash script should include config reset subcommand"
        );
    }

    #[test]
    fn test_zsh_completion_includes_config_set_subcommand() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should include config set subcommand
        assert!(
            script.contains("'set:Set a configuration value'"),
            "Zsh script should include config set subcommand"
        );

        // Should include config reset subcommand
        assert!(
            script.contains("'reset:Reset configuration to default values'"),
            "Zsh script should include config reset subcommand"
        );
    }

    #[test]
    fn test_fish_completion_includes_config_set_subcommand() {
        let script = generate_completion_script(ShellType::Fish);

        // Should include config set subcommand
        assert!(
            script.contains("\"set\"") && script.contains("Set a configuration value"),
            "Fish script should include config set subcommand"
        );

        // Should include config reset subcommand
        assert!(
            script.contains("\"reset\"")
                && script.contains("Reset configuration to default values"),
            "Fish script should include config reset subcommand"
        );
    }

    #[test]
    fn test_bash_completion_includes_config_keys() {
        let script = generate_completion_script(ShellType::Bash);

        // Should include all config keys
        let config_keys = [
            "review",
            "commit",
            "pull_request",
            "worktree",
            "worktree_path_pattern",
            "worktree_cleanup",
        ];

        for key in config_keys {
            assert!(
                script.contains(key),
                "Bash script should include config key: {}",
                key
            );
        }
    }

    #[test]
    fn test_zsh_completion_includes_config_keys() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should include all config keys in the dynamic completion
        let config_keys = [
            "review",
            "commit",
            "pull_request",
            "worktree",
            "worktree_path_pattern",
            "worktree_cleanup",
        ];

        for key in config_keys {
            assert!(
                script.contains(key),
                "Zsh script should include config key: {}",
                key
            );
        }
    }

    #[test]
    fn test_fish_completion_includes_config_keys() {
        let script = generate_completion_script(ShellType::Fish);

        // Should include all config keys
        let config_keys = [
            "review",
            "commit",
            "pull_request",
            "worktree",
            "worktree_path_pattern",
            "worktree_cleanup",
        ];

        for key in config_keys {
            assert!(
                script.contains(key),
                "Fish script should include config key: {}",
                key
            );
        }
    }

    #[test]
    fn test_bash_completion_includes_config_boolean_values() {
        let script = generate_completion_script(ShellType::Bash);

        // Should include true/false completion for boolean config values
        assert!(
            script.contains("\"true false\""),
            "Bash script should include true/false for boolean config values"
        );

        // Should exclude worktree_path_pattern from boolean completion
        assert!(
            script.contains("worktree_path_pattern"),
            "Bash script should handle worktree_path_pattern specially"
        );
    }

    #[test]
    fn test_zsh_completion_includes_config_boolean_values() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should include true/false completion for boolean config values
        assert!(
            script.contains("'true' 'false'"),
            "Zsh script should include true/false for boolean config values"
        );

        // Should exclude worktree_path_pattern from boolean completion
        assert!(
            script.contains("worktree_path_pattern"),
            "Zsh script should handle worktree_path_pattern specially"
        );
    }

    #[test]
    fn test_fish_completion_includes_config_boolean_values() {
        let script = generate_completion_script(ShellType::Fish);

        // Should include true/false completion for boolean config values
        assert!(
            script.contains("'true false'"),
            "Fish script should include true/false for boolean config values"
        );

        // Should exclude worktree_path_pattern from boolean completion
        assert!(
            script.contains("worktree_path_pattern"),
            "Fish script should handle worktree_path_pattern specially"
        );
    }

    #[test]
    fn test_bash_completion_config_set_dynamic_completion() {
        let script = generate_completion_script(ShellType::Bash);

        // Should have dynamic completion for config set
        assert!(
            script.contains(r#"[[ "${words[1]}" == "config" && "${words[2]}" == "set" ]]"#),
            "Bash script should have dynamic completion for config set"
        );
    }

    #[test]
    fn test_zsh_completion_config_set_dynamic_completion() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should have dynamic completion for config set
        assert!(
            script.contains(r#"[[ "${words[2]}" == "config" && "${words[3]}" == "set" ]]"#),
            "Zsh script should have dynamic completion for config set"
        );
    }

    #[test]
    fn test_fish_completion_config_set_dynamic_completion() {
        let script = generate_completion_script(ShellType::Fish);

        // Should have dynamic completion for config set
        assert!(
            script.contains("__fish_seen_subcommand_from config")
                && script.contains("__fish_seen_subcommand_from set"),
            "Fish script should have dynamic completion for config set"
        );
    }

    #[test]
    fn test_bash_completion_config_flags() {
        let script = generate_completion_script(ShellType::Bash);

        // Config command should have --global and --project flags
        assert!(
            script.contains("--global") && script.contains("--project"),
            "Bash script should include config --global and --project flags"
        );

        // Config reset should have --yes flag
        assert!(
            script.contains("--yes"),
            "Bash script should include config reset --yes flag"
        );
    }

    #[test]
    fn test_zsh_completion_config_flags() {
        let script = generate_completion_script(ShellType::Zsh);

        // Config command should have --global and --project flags
        assert!(
            script.contains("--global[Show only the global configuration]"),
            "Zsh script should include config --global flag"
        );
        assert!(
            script.contains("--project[Show only the project configuration]"),
            "Zsh script should include config --project flag"
        );

        // Config reset should have --yes flag
        assert!(
            script.contains("--yes[Skip confirmation prompt]"),
            "Zsh script should include config reset --yes flag"
        );
    }

    #[test]
    fn test_fish_completion_config_flags() {
        let script = generate_completion_script(ShellType::Fish);

        // Config command should have --global and --project flags
        assert!(
            script.contains("-l global") && script.contains("-l project"),
            "Fish script should include config --global and --project flags"
        );

        // Config reset should have --yes flag
        assert!(
            script.contains("-l yes"),
            "Fish script should include config reset --yes flag"
        );
    }

    #[test]
    fn test_all_shells_include_gui_and_improve() {
        // Bash: gui and improve in subcommands list
        let bash_script = generate_completion_script(ShellType::Bash);
        assert!(
            bash_script.contains("gui"),
            "Bash script should include gui command"
        );
        assert!(
            bash_script.contains("improve"),
            "Bash script should include improve command"
        );

        // Zsh: gui and improve with descriptions
        let zsh_script = generate_completion_script(ShellType::Zsh);
        assert!(
            zsh_script.contains("'gui:Launch the native GUI to monitor autom8 activity'"),
            "Zsh script should include gui command with description"
        );
        assert!(
            zsh_script.contains("'improve:Continue iterating on a feature with Claude using context from previous runs'"),
            "Zsh script should include improve command with description"
        );

        // Fish: gui and improve in subcommand exclusion list
        let fish_script = generate_completion_script(ShellType::Fish);
        assert!(
            fish_script.contains("__fish_seen_subcommand_from run status resume clean config init projects list describe pr-review monitor gui improve"),
            "Fish script should include gui and improve in subcommand list"
        );
    }

    #[test]
    fn test_zsh_completion_config_key_descriptions() {
        let script = generate_completion_script(ShellType::Zsh);

        // Should include descriptive completions for config keys
        assert!(
            script.contains("'review:Enable code review step'"),
            "Zsh script should include review key with description"
        );
        assert!(
            script.contains("'commit:Enable auto-commit'"),
            "Zsh script should include commit key with description"
        );
        assert!(
            script.contains("'pull_request:Enable auto-PR creation'"),
            "Zsh script should include pull_request key with description"
        );
        assert!(
            script.contains("'worktree:Enable worktree mode'"),
            "Zsh script should include worktree key with description"
        );
        assert!(
            script.contains("'worktree_path_pattern:Pattern for worktree names'"),
            "Zsh script should include worktree_path_pattern key with description"
        );
        assert!(
            script.contains("'worktree_cleanup:Auto-cleanup worktrees'"),
            "Zsh script should include worktree_cleanup key with description"
        );
    }
}