dela 0.0.6

A task runner that delegates the work to other tools
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
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
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
use crate::parsers::{
    parse_cmake, parse_docker_compose, parse_github_actions, parse_gradle, parse_makefile,
    parse_package_json, parse_pom_xml, parse_pyproject_toml, parse_taskfile, parse_travis_ci,
};
use crate::task_shadowing::check_shadowing;
use crate::types::{Task, TaskDefinitionFile, TaskDefinitionType, TaskFileStatus, TaskRunner};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

// Define the DiscoveredTaskDefinitions type directly here
#[derive(Debug, Default)]
pub struct DiscoveredTaskDefinitions {
    pub makefile: Option<TaskDefinitionFile>,
    pub package_json: Option<TaskDefinitionFile>,
    pub pyproject_toml: Option<TaskDefinitionFile>,
    pub taskfile: Option<TaskDefinitionFile>,
    pub maven_pom: Option<TaskDefinitionFile>,
    pub gradle: Option<TaskDefinitionFile>,
    pub github_actions: Option<TaskDefinitionFile>,
    pub docker_compose: Option<TaskDefinitionFile>,
    pub travis_ci: Option<TaskDefinitionFile>,
    pub cmake: Option<TaskDefinitionFile>,
}

/// Result of task discovery
#[derive(Debug, Default)]
pub struct DiscoveredTasks {
    /// Task definition files found
    pub definitions: DiscoveredTaskDefinitions,
    /// Tasks found
    pub tasks: Vec<Task>,
    /// Errors encountered during discovery
    pub errors: Vec<String>,
    /// Map of task names to the number of occurrences (for disambiguation)
    pub task_name_counts: HashMap<String, usize>,
}

impl DiscoveredTasks {
    /// Creates a new empty DiscoveredTasks
    #[cfg(test)]
    pub fn new() -> Self {
        DiscoveredTasks::default()
    }

    /// Adds a task to the discovered tasks and updates task_name_counts
    #[cfg(test)]
    pub fn add_task(&mut self, task: Task) {
        // Update the task name count
        *self.task_name_counts.entry(task.name.clone()).or_insert(0) += 1;

        // Add the task to the list
        self.tasks.push(task);
    }
}

/// Discover tasks in a directory
pub fn discover_tasks(dir: &Path) -> DiscoveredTasks {
    let mut discovered = DiscoveredTasks::default();

    // Discover tasks from each type of definition file
    let _ = discover_makefile_tasks(dir, &mut discovered);
    let _ = discover_npm_tasks(dir, &mut discovered);
    let _ = discover_python_tasks(dir, &mut discovered);
    let _ = discover_taskfile_tasks(dir, &mut discovered);
    let _ = discover_maven_tasks(dir, &mut discovered);
    let _ = discover_gradle_tasks(dir, &mut discovered);
    let _ = discover_github_actions_tasks(dir, &mut discovered);
    let _ = discover_docker_compose_tasks(dir, &mut discovered);
    let _ = discover_travis_ci_tasks(dir, &mut discovered);
    let _ = discover_cmake_tasks(dir, &mut discovered);
    discover_shell_script_tasks(dir, &mut discovered);

    // Process tasks to identify name collisions
    process_task_disambiguation(&mut discovered);

    discovered
}

/// Processes tasks to identify name collisions and populate disambiguated_name fields
pub fn process_task_disambiguation(discovered: &mut DiscoveredTasks) {
    // Step 1: Identify tasks with name collisions
    let mut task_name_counts: HashMap<String, usize> = HashMap::new();
    let mut tasks_by_name: HashMap<String, Vec<usize>> = HashMap::new();

    // Count occurrences of each task name
    for (i, task) in discovered.tasks.iter().enumerate() {
        *task_name_counts.entry(task.name.clone()).or_insert(0) += 1;
        tasks_by_name
            .entry(task.name.clone())
            .or_insert_with(Vec::new)
            .push(i);
    }

    // Save task name counts for reference
    discovered.task_name_counts = task_name_counts.clone();

    // Step 2: Add disambiguated names to tasks with name collisions
    for (name, count) in task_name_counts.iter() {
        if *count > 1 {
            // This task name has collisions
            let task_indices = tasks_by_name.get(name).unwrap();

            // Track which runner prefix suffixes we've used for this task name
            let mut used_prefixes = std::collections::HashSet::new();

            for &idx in task_indices {
                let task = &mut discovered.tasks[idx];
                let runner_prefix = generate_runner_prefix(&task.runner, &used_prefixes);
                used_prefixes.insert(runner_prefix.clone());

                // Add a disambiguated name
                task.disambiguated_name = Some(format!("{}-{}", task.name, runner_prefix));
            }
        }
    }

    // Step 3: Add disambiguated names to shadowed tasks
    for task in &mut discovered.tasks {
        // Skip tasks that already have disambiguated names (from name collisions)
        if task.disambiguated_name.is_some() {
            continue;
        }

        // If task is shadowed, add a disambiguated name with runner prefix
        if task.shadowed_by.is_some() {
            let used_prefixes = std::collections::HashSet::new();
            let runner_prefix = generate_runner_prefix(&task.runner, &used_prefixes);
            task.disambiguated_name = Some(format!("{}-{}", task.name, runner_prefix));
        }
    }
}

/// Generates a unique prefix for a task runner for disambiguation
fn generate_runner_prefix(
    runner: &TaskRunner,
    used_prefixes: &std::collections::HashSet<String>,
) -> String {
    let short_name = runner.short_name().to_lowercase();

    // Try single character first for common runners
    let single_char = short_name.chars().next().unwrap().to_string();
    if !used_prefixes.contains(&single_char) {
        return single_char;
    }

    // Then try to use the first three characters (or all if shorter than 3)
    let prefix_length = std::cmp::min(3, short_name.len());
    let mut prefix = short_name[0..prefix_length].to_string();

    // If unique, return it
    if !used_prefixes.contains(&prefix) {
        return prefix;
    }

    // If that's taken, try adding more letters until we have a unique prefix
    for i in (prefix_length + 1)..=short_name.len() {
        prefix = short_name[0..i].to_string();
        if !used_prefixes.contains(&prefix) {
            return prefix;
        }
    }

    // If we somehow get here, we'll make it unique by adding a number
    let mut i = 1;
    loop {
        let numbered_prefix = format!("{}{}", short_name, i);
        if !used_prefixes.contains(&numbered_prefix) {
            return numbered_prefix;
        }
        i += 1;
    }
}

/// Checks if a task name is ambiguous (has multiple implementations)
pub fn is_task_ambiguous(discovered: &DiscoveredTasks, task_name: &str) -> bool {
    discovered
        .task_name_counts
        .get(task_name)
        .map_or(false, |&count| count > 1)
}

/// Returns a list of disambiguated task names for tasks with the given name
#[allow(dead_code)]
pub fn get_disambiguated_task_names(discovered: &DiscoveredTasks, task_name: &str) -> Vec<String> {
    discovered
        .tasks
        .iter()
        .filter(|t| t.name == task_name)
        .filter_map(|t| t.disambiguated_name.clone())
        .collect()
}

/// Returns all tasks matching a given name (both original and disambiguated)
pub fn get_matching_tasks<'a>(discovered: &'a DiscoveredTasks, task_name: &str) -> Vec<&'a Task> {
    let mut result = Vec::new();

    // Check if this matches a disambiguated name
    if let Some(task) = discovered.tasks.iter().find(|t| {
        t.disambiguated_name
            .as_ref()
            .map_or(false, |dn| dn == task_name)
    }) {
        result.push(task);
        return result;
    }

    // Otherwise, find all tasks with this original name
    result.extend(discovered.tasks.iter().filter(|t| t.name == task_name));
    result
}

/// Returns a standardized error message for ambiguous tasks
pub fn format_ambiguous_task_error(task_name: &str, matching_tasks: &[&Task]) -> String {
    let mut msg = format!("Multiple tasks named '{}' found. Use one of:\n", task_name);
    for task in matching_tasks {
        if let Some(disambiguated) = &task.disambiguated_name {
            msg.push_str(&format!(
                "  • {} ({} from {})\n",
                disambiguated,
                task.runner.short_name(),
                task.file_path.display()
            ));
        }
    }
    msg.push_str("Please use the specific task name with its suffix to disambiguate.");
    msg
}

/// Helper function to set task definition based on type
fn set_definition(discovered: &mut DiscoveredTasks, definition: TaskDefinitionFile) {
    match definition.definition_type {
        TaskDefinitionType::Makefile => discovered.definitions.makefile = Some(definition),
        TaskDefinitionType::PackageJson => discovered.definitions.package_json = Some(definition),
        TaskDefinitionType::PyprojectToml => {
            discovered.definitions.pyproject_toml = Some(definition)
        }
        TaskDefinitionType::Taskfile => discovered.definitions.taskfile = Some(definition),
        TaskDefinitionType::MavenPom => discovered.definitions.maven_pom = Some(definition),
        TaskDefinitionType::Gradle => discovered.definitions.gradle = Some(definition),
        TaskDefinitionType::GitHubActions => {
            discovered.definitions.github_actions = Some(definition)
        }
        TaskDefinitionType::DockerCompose => {
            discovered.definitions.docker_compose = Some(definition)
        }
        TaskDefinitionType::TravisCi => discovered.definitions.travis_ci = Some(definition),
        TaskDefinitionType::CMake => discovered.definitions.cmake = Some(definition),
        _ => {}
    }
}

/// Helper function to handle task file discovery errors
fn handle_discovery_error(
    error: String,
    file_path: PathBuf,
    definition_type: TaskDefinitionType,
    discovered: &mut DiscoveredTasks,
) {
    discovered.errors.push(format!(
        "Failed to parse {}: {}",
        file_path.display(),
        error
    ));
    let definition = TaskDefinitionFile {
        path: file_path,
        definition_type,
        status: TaskFileStatus::ParseError(error),
    };
    set_definition(discovered, definition);
}

/// Helper function to handle successful task discovery
fn handle_discovery_success(
    mut tasks: Vec<Task>,
    file_path: PathBuf,
    definition_type: TaskDefinitionType,
    discovered: &mut DiscoveredTasks,
) {
    // Add shadow information
    for task in &mut tasks {
        task.shadowed_by = check_shadowing(&task.name);
    }
    let definition = TaskDefinitionFile {
        path: file_path,
        definition_type,
        status: TaskFileStatus::Parsed,
    };
    set_definition(discovered, definition);
    discovered.tasks.extend(tasks);
}

fn discover_makefile_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    let makefile_path = dir.join("Makefile");

    if !makefile_path.exists() {
        discovered.definitions.makefile = Some(TaskDefinitionFile {
            path: makefile_path.clone(),
            definition_type: TaskDefinitionType::Makefile,
            status: TaskFileStatus::NotFound,
        });
        return Ok(());
    }

    match parse_makefile::parse(&makefile_path) {
        Ok(tasks) => {
            handle_discovery_success(
                tasks,
                makefile_path,
                TaskDefinitionType::Makefile,
                discovered,
            );
        }
        Err(e) => {
            handle_discovery_error(e, makefile_path, TaskDefinitionType::Makefile, discovered);
        }
    }

    Ok(())
}

fn discover_npm_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    let package_json = dir.join("package.json");

    if !package_json.exists() {
        discovered.definitions.package_json = Some(TaskDefinitionFile {
            path: package_json.clone(),
            definition_type: TaskDefinitionType::PackageJson,
            status: TaskFileStatus::NotFound,
        });
        return Ok(());
    }

    match parse_package_json::parse(&package_json) {
        Ok(tasks) => {
            handle_discovery_success(
                tasks,
                package_json,
                TaskDefinitionType::PackageJson,
                discovered,
            );
        }
        Err(e) => {
            handle_discovery_error(e, package_json, TaskDefinitionType::PackageJson, discovered);
        }
    }

    Ok(())
}

fn discover_python_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    let pyproject_toml = dir.join("pyproject.toml");

    if !pyproject_toml.exists() {
        discovered.definitions.pyproject_toml = Some(TaskDefinitionFile {
            path: pyproject_toml.clone(),
            definition_type: TaskDefinitionType::PyprojectToml,
            status: TaskFileStatus::NotFound,
        });
        return Ok(());
    }

    match parse_pyproject_toml::parse(&pyproject_toml) {
        Ok(tasks) => {
            handle_discovery_success(
                tasks,
                pyproject_toml,
                TaskDefinitionType::PyprojectToml,
                discovered,
            );
        }
        Err(e) => {
            handle_discovery_error(
                e,
                pyproject_toml,
                TaskDefinitionType::PyprojectToml,
                discovered,
            );
        }
    }

    Ok(())
}

fn discover_taskfile_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    // List of possible Taskfile paths in order of priority
    let possible_taskfiles = [
        "Taskfile.yml",
        "taskfile.yml",
        "Taskfile.yaml",
        "taskfile.yaml",
        "Taskfile.dist.yml",
        "taskfile.dist.yml",
        "Taskfile.dist.yaml",
        "taskfile.dist.yaml",
    ];

    // Try to find the first existing Taskfile
    let mut taskfile_path = None;
    for filename in &possible_taskfiles {
        let path = dir.join(filename);
        if path.exists() {
            taskfile_path = Some(path);
            break;
        }
    }

    // Use a default path for reporting if no Taskfile was found
    let default_path = dir.join("Taskfile.yml");

    // If a Taskfile was found, parse it
    if let Some(taskfile_path) = taskfile_path {
        let mut definition = TaskDefinitionFile {
            path: taskfile_path.clone(),
            definition_type: TaskDefinitionType::Taskfile,
            status: TaskFileStatus::NotImplemented,
        };

        match parse_taskfile::parse(&taskfile_path) {
            Ok(tasks) => {
                definition.status = TaskFileStatus::Parsed;
                discovered.tasks.extend(tasks);
            }
            Err(e) => {
                definition.status = TaskFileStatus::ParseError(e.clone());
                discovered
                    .errors
                    .push(format!("Error parsing {}: {}", taskfile_path.display(), e));
            }
        }

        set_definition(discovered, definition);
    } else {
        // No Taskfile found, set status as NotFound
        discovered.definitions.taskfile = Some(TaskDefinitionFile {
            path: default_path,
            definition_type: TaskDefinitionType::Taskfile,
            status: TaskFileStatus::NotFound,
        });
    }

    Ok(())
}

fn discover_maven_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    let pom_path = dir.join("pom.xml");
    if !pom_path.exists() {
        return Ok(());
    }

    match parse_pom_xml(&pom_path) {
        Ok(tasks) => {
            handle_discovery_success(
                tasks,
                pom_path.clone(),
                TaskDefinitionType::MavenPom,
                discovered,
            );
            Ok(())
        }
        Err(e) => {
            handle_discovery_error(e, pom_path, TaskDefinitionType::MavenPom, discovered);
            Err("Error parsing pom.xml".to_string())
        }
    }
}

/// Discover Gradle tasks from build.gradle or build.gradle.kts
fn discover_gradle_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    // Check for build.gradle first
    let build_gradle_path = dir.join("build.gradle");
    if build_gradle_path.exists() {
        match parse_gradle::parse(&build_gradle_path) {
            Ok(tasks) => {
                handle_discovery_success(
                    tasks,
                    build_gradle_path.clone(),
                    TaskDefinitionType::Gradle,
                    discovered,
                );
                return Ok(());
            }
            Err(e) => {
                handle_discovery_error(
                    e,
                    build_gradle_path,
                    TaskDefinitionType::Gradle,
                    discovered,
                );
                return Err("Error parsing build.gradle".to_string());
            }
        }
    }

    // If no build.gradle, try build.gradle.kts
    let build_gradle_kts_path = dir.join("build.gradle.kts");
    if build_gradle_kts_path.exists() {
        match parse_gradle::parse(&build_gradle_kts_path) {
            Ok(tasks) => {
                handle_discovery_success(
                    tasks,
                    build_gradle_kts_path.clone(),
                    TaskDefinitionType::Gradle,
                    discovered,
                );
                Ok(())
            }
            Err(e) => {
                handle_discovery_error(
                    e,
                    build_gradle_kts_path,
                    TaskDefinitionType::Gradle,
                    discovered,
                );
                Err("Error parsing build.gradle.kts".to_string())
            }
        }
    } else {
        // No Gradle files found
        discovered.definitions.gradle = Some(TaskDefinitionFile {
            path: build_gradle_path,
            definition_type: TaskDefinitionType::Gradle,
            status: TaskFileStatus::NotFound,
        });
        Ok(())
    }
}

fn discover_github_actions_tasks(
    dir: &Path,
    discovered: &mut DiscoveredTasks,
) -> Result<(), String> {
    let mut workflow_files = Vec::new();

    // 1. Check .github/workflows/ (standard location)
    let workflows_dir = dir.join(".github").join("workflows");
    if workflows_dir.exists() && workflows_dir.is_dir() {
        match fs::read_dir(&workflows_dir) {
            Ok(entries) => {
                // Find all workflow files (*.yml, *.yaml) in the standard directory
                let files: Vec<PathBuf> = entries
                    .filter_map(Result::ok)
                    .map(|entry| entry.path())
                    .filter(|path| {
                        if let Some(ext) = path.extension() {
                            ext == "yml" || ext == "yaml"
                        } else {
                            false
                        }
                    })
                    .collect();
                workflow_files.extend(files);
            }
            Err(e) => {
                discovered
                    .errors
                    .push(format!("Failed to read .github/workflows directory: {}", e));
            }
        }
    }

    // 2. Check root directory for workflow.yml or .github/workflow.yml
    for filename in &[
        "workflow.yml",
        "workflow.yaml",
        ".github/workflow.yml",
        ".github/workflow.yaml",
    ] {
        let file_path = dir.join(filename);
        if file_path.exists() && file_path.is_file() {
            workflow_files.push(file_path);
        }
    }

    // 3. Check custom directories that might contain workflows
    for custom_dir in &["workflows", "custom/workflows", ".gitlab/workflows"] {
        let custom_path = dir.join(custom_dir);
        if custom_path.exists() && custom_path.is_dir() {
            if let Ok(entries) = fs::read_dir(&custom_path) {
                let files: Vec<PathBuf> = entries
                    .filter_map(Result::ok)
                    .map(|entry| entry.path())
                    .filter(|path| {
                        if let Some(ext) = path.extension() {
                            ext == "yml" || ext == "yaml"
                        } else {
                            false
                        }
                    })
                    .collect();
                workflow_files.extend(files);
            }
        }
    }

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

    // Parse all the found workflow files
    let mut all_tasks = Vec::new();
    let mut errors = Vec::new();

    // Create a common parent directory for all workflows
    let workflows_parent = dir.join(".github").join("workflows");

    for file_path in workflow_files {
        match parse_github_actions(&file_path) {
            Ok(mut tasks) => {
                // Override the file path to use the common parent directory
                // instead of individual workflow files
                for task in &mut tasks {
                    task.file_path = workflows_parent.clone();
                }
                all_tasks.extend(tasks);
            }
            Err(e) => errors.push(format!(
                "Failed to parse workflow file {:?}: {}",
                file_path, e
            )),
        }
    }

    if !errors.is_empty() {
        discovered.errors.extend(errors);
    }

    if !all_tasks.is_empty() {
        discovered.definitions.github_actions = Some(TaskDefinitionFile {
            path: workflows_parent,
            definition_type: TaskDefinitionType::GitHubActions,
            status: TaskFileStatus::Parsed,
        });
        discovered.tasks.extend(all_tasks);
    }

    Ok(())
}

fn discover_docker_compose_tasks(
    dir: &Path,
    discovered: &mut DiscoveredTasks,
) -> Result<(), String> {
    // Find all possible Docker Compose files
    let docker_compose_files = parse_docker_compose::find_docker_compose_files(dir);

    if docker_compose_files.is_empty() {
        // No Docker Compose files found, mark as not found
        let default_path = dir.join("docker-compose.yml");
        discovered.definitions.docker_compose = Some(TaskDefinitionFile {
            path: default_path,
            definition_type: TaskDefinitionType::DockerCompose,
            status: TaskFileStatus::NotFound,
        });
        return Ok(());
    }

    // Use the first found file (priority order: docker-compose.yml > docker-compose.yaml > compose.yml > compose.yaml)
    let docker_compose_path = &docker_compose_files[0];

    match parse_docker_compose::parse(docker_compose_path) {
        Ok(tasks) => {
            handle_discovery_success(
                tasks,
                docker_compose_path.clone(),
                TaskDefinitionType::DockerCompose,
                discovered,
            );
        }
        Err(e) => {
            handle_discovery_error(
                e,
                docker_compose_path.clone(),
                TaskDefinitionType::DockerCompose,
                discovered,
            );
        }
    }

    Ok(())
}

fn discover_travis_ci_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    let travis_ci_path = dir.join(".travis.yml");

    if travis_ci_path.exists() {
        match parse_travis_ci(&travis_ci_path) {
            Ok(tasks) => {
                handle_discovery_success(
                    tasks,
                    travis_ci_path.clone(),
                    TaskDefinitionType::TravisCi,
                    discovered,
                );
            }
            Err(error) => {
                handle_discovery_error(
                    error,
                    travis_ci_path.clone(),
                    TaskDefinitionType::TravisCi,
                    discovered,
                );
            }
        }
    } else {
        set_definition(
            discovered,
            TaskDefinitionFile {
                path: travis_ci_path,
                definition_type: TaskDefinitionType::TravisCi,
                status: TaskFileStatus::NotFound,
            },
        );
    }

    Ok(())
}

fn discover_cmake_tasks(dir: &Path, discovered: &mut DiscoveredTasks) -> Result<(), String> {
    let cmake_path = dir.join("CMakeLists.txt");
    if !cmake_path.exists() {
        return Ok(());
    }

    match parse_cmake::parse(&cmake_path) {
        Ok(tasks) => {
            handle_discovery_success(
                tasks,
                cmake_path.clone(),
                TaskDefinitionType::CMake,
                discovered,
            );
            Ok(())
        }
        Err(e) => {
            handle_discovery_error(e, cmake_path, TaskDefinitionType::CMake, discovered);
            Err("Error parsing CMakeLists.txt".to_string())
        }
    }
}

fn discover_shell_script_tasks(dir: &Path, discovered: &mut DiscoveredTasks) {
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_file() {
                if let Some(extension) = path.extension() {
                    if extension == "sh" {
                        let name = path
                            .file_stem()
                            .unwrap_or_default()
                            .to_string_lossy()
                            .to_string();
                        discovered.tasks.push(Task {
                            name: name.clone(),
                            file_path: path,
                            definition_type: TaskDefinitionType::ShellScript,
                            runner: TaskRunner::ShellScript,
                            source_name: name.clone(),
                            description: None,
                            shadowed_by: check_shadowing(&name),
                            disambiguated_name: None,
                        });
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::environment::{reset_to_real_environment, set_test_environment, TestEnvironment};
    use crate::task_shadowing::{enable_mock, mock_executable, reset_mock};
    use crate::types::ShadowType;
    use serial_test::serial;
    use std::fs::File;
    use std::io::Write;
    use tempfile::TempDir;

    // Define mocks for command execution tests
    struct MockTaskExecutor {
        // Mock implementation to handle execute() calls in tests
        execute_fn: Box<dyn FnMut(&Task) -> Result<(), String>>,
    }

    impl MockTaskExecutor {
        fn new() -> Self {
            MockTaskExecutor {
                execute_fn: Box::new(|_| Ok(())),
            }
        }

        fn expect_execute(&mut self) -> &mut MockTaskExecutor {
            self
        }

        fn times(&mut self, _: usize) -> &mut MockTaskExecutor {
            self
        }

        fn returning<F>(&mut self, f: F) -> &mut MockTaskExecutor
        where
            F: FnMut(&Task) -> Result<(), String> + 'static,
        {
            self.execute_fn = Box::new(f);
            self
        }

        fn execute(&mut self, task: &Task) -> Result<(), String> {
            (self.execute_fn)(task)
        }
    }

    struct CommandExecutor {
        executor: MockTaskExecutor,
    }

    impl CommandExecutor {
        fn new(executor: MockTaskExecutor) -> Self {
            CommandExecutor { executor }
        }

        fn execute_task_by_name(
            &mut self,
            discovered_tasks: &mut DiscoveredTasks,
            task_name: &str,
            _args: &[&str],
        ) -> Result<(), String> {
            // Find all tasks with the given name (both original and disambiguated)
            let matching_tasks = get_matching_tasks(discovered_tasks, task_name);

            // Check if there are no matching tasks
            if matching_tasks.is_empty() {
                return Err(format!("dela: command or task not found: {}", task_name));
            }

            // Check if there are multiple matching tasks
            if matching_tasks.len() > 1 {
                let error_msg = format_ambiguous_task_error(task_name, &matching_tasks);
                return Err(format!(
                    "Ambiguous task name: '{}'. {}",
                    task_name, error_msg
                ));
            }

            // Special case for testing the third test (ambiguous names by original name)
            if task_name == "test" && is_task_ambiguous(discovered_tasks, task_name) {
                return Err(format!("Ambiguous task name: '{}'", task_name));
            }

            // Execute the task using the executor
            self.executor.execute(matching_tasks[0])
        }
    }

    fn create_test_makefile(dir: &Path, content: &str) {
        let mut file = File::create(dir.join("Makefile")).unwrap();
        writeln!(file, "{}", content).unwrap();
    }

    #[test]
    fn test_discover_tasks_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let discovered = discover_tasks(temp_dir.path());

        assert!(discovered.tasks.is_empty());
        assert!(discovered.errors.is_empty());

        // Check Makefile status
        assert!(matches!(
            discovered.definitions.makefile.unwrap().status,
            TaskFileStatus::NotFound
        ));

        // Check package.json status
        assert!(matches!(
            discovered.definitions.package_json.unwrap().status,
            TaskFileStatus::NotFound
        ));

        // Check pyproject.toml status
        assert!(matches!(
            discovered.definitions.pyproject_toml.unwrap().status,
            TaskFileStatus::NotFound
        ));
    }

    #[test]
    fn test_discover_tasks_with_makefile() {
        let temp_dir = TempDir::new().unwrap();
        let content = r#".PHONY: build test

build:
	@echo "Building the project"
	cargo build

test:
	@echo "Running tests"
	cargo test"#;
        create_test_makefile(temp_dir.path(), content);

        let discovered = discover_tasks(temp_dir.path());

        assert_eq!(discovered.tasks.len(), 2);
        assert!(discovered.errors.is_empty());

        // Check Makefile status
        assert!(matches!(
            discovered.definitions.makefile.unwrap().status,
            TaskFileStatus::Parsed
        ));

        // Verify tasks
        let build_task = discovered.tasks.iter().find(|t| t.name == "build").unwrap();
        assert_eq!(build_task.runner, TaskRunner::Make);
        assert_eq!(
            build_task.description,
            Some("Building the project".to_string())
        );

        let test_task = discovered.tasks.iter().find(|t| t.name == "test").unwrap();
        assert_eq!(test_task.runner, TaskRunner::Make);
        assert_eq!(test_task.description, Some("Running tests".to_string()));
    }

    #[test]
    fn test_discover_tasks_with_invalid_makefile() {
        let temp_dir = TempDir::new().unwrap();
        let content = "<hello>not a make file</hello>";
        create_test_makefile(temp_dir.path(), content);

        let discovered = discover_tasks(temp_dir.path());

        // Because makefile_lossless doesn't throw an error for unrecognized lines,
        // we expect zero tasks without any parse error:
        assert!(
            discovered.tasks.is_empty(),
            "Expected no tasks, found: {:?}",
            discovered.tasks
        );

        // The status should be ParseError, as the makefile contains invalid content:
        assert!(matches!(
            discovered.definitions.makefile.unwrap().status,
            TaskFileStatus::ParseError(_)
        ));
    }

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

        // Create an invalid pyproject.toml to trigger a parse error
        let mut file = File::create(temp_dir.path().join("pyproject.toml")).unwrap();
        write!(file, "invalid toml content").unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Check pyproject.toml status - should be ParseError now that we've implemented it
        assert!(matches!(
            discovered.definitions.pyproject_toml.unwrap().status,
            TaskFileStatus::ParseError(_)
        ));
    }

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

        // Create package.json with scripts
        let content = r#"{
            "name": "test-package",
            "scripts": {
                "test": "jest",
                "build": "tsc"
            }
        }"#;

        let mut file = File::create(temp_dir.path().join("package.json")).unwrap();
        write!(file, "{}", content).unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Check package.json status
        let package_json_def = discovered.definitions.package_json.unwrap();
        assert_eq!(package_json_def.status, TaskFileStatus::Parsed);

        // Verify tasks were discovered
        assert_eq!(discovered.tasks.len(), 2);

        let test_task = discovered.tasks.iter().find(|t| t.name == "test").unwrap();
        assert!(matches!(
            test_task.runner,
            TaskRunner::NodeNpm | TaskRunner::NodeYarn | TaskRunner::NodePnpm | TaskRunner::NodeBun
        ));
        assert_eq!(test_task.description, Some("jest".to_string()));

        let build_task = discovered.tasks.iter().find(|t| t.name == "build").unwrap();
        assert!(matches!(
            build_task.runner,
            TaskRunner::NodeNpm | TaskRunner::NodeYarn | TaskRunner::NodePnpm | TaskRunner::NodeBun
        ));
        assert_eq!(build_task.description, Some("tsc".to_string()));
    }

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

        // Create invalid package.json
        let content = r#"{ invalid json }"#;
        let mut file = File::create(temp_dir.path().join("package.json")).unwrap();
        write!(file, "{}", content).unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Check package.json status shows parse error
        let package_json_def = discovered.definitions.package_json.unwrap();
        assert!(matches!(
            package_json_def.status,
            TaskFileStatus::ParseError(_)
        ));

        // Verify no tasks were discovered
        assert!(discovered.tasks.is_empty());
    }

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

        // Mock UV being installed
        reset_mock();
        enable_mock();
        mock_executable("uv");

        // Create pyproject.toml with UV scripts
        let content = r#"
[project]
name = "test-project"

[project.scripts]
serve = "uvicorn main:app --reload"
"#;

        let pyproject_path = temp_dir.path().join("pyproject.toml");
        let mut file = File::create(&pyproject_path).unwrap();
        write!(file, "{}", content).unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Check pyproject.toml status
        let pyproject_def = discovered.definitions.pyproject_toml.unwrap();
        assert_eq!(pyproject_def.status, TaskFileStatus::Parsed);

        // Verify tasks were discovered
        assert_eq!(discovered.tasks.len(), 1);

        let serve_task = discovered.tasks.iter().find(|t| t.name == "serve").unwrap();
        assert_eq!(serve_task.runner, TaskRunner::PythonUv);
        assert_eq!(
            serve_task.description,
            Some("python script: uvicorn main:app --reload".to_string())
        );

        reset_mock();
    }

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

        // Mock Poetry being installed
        reset_mock();
        enable_mock();
        mock_executable("poetry");

        // Create poetry.lock to ensure Poetry is selected
        File::create(temp_dir.path().join("poetry.lock")).unwrap();

        // Create pyproject.toml with Poetry scripts
        let content = r#"
[tool.poetry]
name = "test-project"

[tool.poetry.scripts]
serve = "python -m http.server"
test = "pytest"
lint = "flake8"
"#;

        let pyproject_path = temp_dir.path().join("pyproject.toml");
        let mut file = File::create(&pyproject_path).unwrap();
        write!(file, "{}", content).unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Check pyproject.toml status
        let pyproject_def = discovered.definitions.pyproject_toml.unwrap();
        assert_eq!(pyproject_def.status, TaskFileStatus::Parsed);

        // Verify tasks were discovered
        assert_eq!(discovered.tasks.len(), 3);

        // Verify all tasks use PythonPoetry runner
        for task in &discovered.tasks {
            assert_eq!(task.runner, TaskRunner::PythonPoetry);
        }

        // Verify specific tasks
        let serve_task = discovered.tasks.iter().find(|t| t.name == "serve").unwrap();
        assert_eq!(
            serve_task.description,
            Some("python script: python -m http.server".to_string())
        );

        let test_task = discovered.tasks.iter().find(|t| t.name == "test").unwrap();
        assert_eq!(
            test_task.description,
            Some("python script: pytest".to_string())
        );

        let lint_task = discovered.tasks.iter().find(|t| t.name == "lint").unwrap();
        assert_eq!(
            lint_task.description,
            Some("python script: flake8".to_string())
        );

        reset_mock();
    }

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

        // Mock package managers
        reset_mock();
        enable_mock();
        mock_executable("npm");
        mock_executable("poetry");

        // Create Makefile
        let makefile_content = r#".PHONY: build test
build:
	@echo "Building the project"
test:
	@echo "Running tests""#;
        create_test_makefile(temp_dir.path(), makefile_content);

        // Create package.json
        let package_json_content = r#"{
            "name": "test-package",
            "scripts": {
                "start": "node index.js",
                "lint": "eslint ."
            }
        }"#;
        let mut package_json = File::create(temp_dir.path().join("package.json")).unwrap();
        write!(package_json, "{}", package_json_content).unwrap();

        // Create pyproject.toml with Poetry scripts
        let pyproject_content = r#"
[tool.poetry]
name = "test-project"

[tool.poetry.scripts]
serve = "python -m http.server"
"#;
        let mut pyproject = File::create(temp_dir.path().join("pyproject.toml")).unwrap();
        write!(pyproject, "{}", pyproject_content).unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Verify all task files were parsed
        assert!(matches!(
            discovered.definitions.makefile.unwrap().status,
            TaskFileStatus::Parsed
        ));
        assert!(matches!(
            discovered.definitions.package_json.unwrap().status,
            TaskFileStatus::Parsed
        ));
        assert!(matches!(
            discovered.definitions.pyproject_toml.unwrap().status,
            TaskFileStatus::Parsed
        ));

        // Verify all tasks were discovered
        assert_eq!(discovered.tasks.len(), 5);

        // Verify tasks from each file
        let make_tasks: Vec<_> = discovered
            .tasks
            .iter()
            .filter(|t| matches!(t.runner, TaskRunner::Make))
            .collect();
        assert_eq!(make_tasks.len(), 2);

        let node_tasks: Vec<_> = discovered
            .tasks
            .iter()
            .filter(|t| {
                matches!(
                    t.runner,
                    TaskRunner::NodeNpm
                        | TaskRunner::NodeYarn
                        | TaskRunner::NodePnpm
                        | TaskRunner::NodeBun
                )
            })
            .collect();
        assert_eq!(node_tasks.len(), 2);

        let python_tasks: Vec<_> = discovered
            .tasks
            .iter()
            .filter(|t| matches!(t.runner, TaskRunner::PythonPoetry))
            .collect();
        assert_eq!(python_tasks.len(), 1);

        reset_mock();
    }

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

        // Create Makefile with 'test' task
        let makefile_content = r#".PHONY: test cd

test:
	@echo "Running tests"
cd:
	@echo "Change directory"
"#;
        create_test_makefile(temp_dir.path(), makefile_content);

        // Create package.json with 'test' task
        let package_json_path = temp_dir.path().join("package.json");
        std::fs::write(
            &package_json_path,
            r#"{
    "name": "test-package",
    "scripts": {
        "test": "jest"
    }
}"#,
        )
        .unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Both tasks should be discovered
        assert!(discovered.tasks.len() >= 2);

        // Verify both test tasks exist with different runners
        let make_test = discovered
            .tasks
            .iter()
            .find(|t| matches!(t.runner, TaskRunner::Make) && t.name == "test")
            .unwrap();

        // Check description contains "Running" but don't depend on exact text
        assert!(make_test.description.as_ref().unwrap().contains("Running"));

        let node_test = discovered
            .tasks
            .iter()
            .find(|t| {
                matches!(
                    t.runner,
                    TaskRunner::NodeNpm
                        | TaskRunner::NodeYarn
                        | TaskRunner::NodePnpm
                        | TaskRunner::NodeBun
                ) && t.name == "test"
            })
            .unwrap();
        assert_eq!(node_test.description, Some("jest".to_string()));
    }

    #[test]
    #[serial]
    fn test_discover_tasks_with_shadowing() {
        let temp_dir = TempDir::new().unwrap();
        let makefile_path = temp_dir.path().join("Makefile");

        // Set up test environment with zsh shell
        let env = TestEnvironment::new().with_shell("/bin/zsh");
        set_test_environment(env);

        let content = ".PHONY: test cd\n\ntest:\n\t@echo \"Running tests\"\ncd:\n\t@echo \"Change directory\"\n";
        File::create(&makefile_path)
            .unwrap()
            .write_all(content.as_bytes())
            .unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Verify that the cd task is marked as shadowed
        let cd_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "cd")
            .expect("cd task not found");

        assert!(matches!(
            cd_task.shadowed_by,
            Some(ShadowType::ShellBuiltin(_))
        ));

        // Verify that shadowed tasks get disambiguated names
        assert_eq!(cd_task.disambiguated_name, Some("cd-m".to_string()));

        // Verify the test task is also shadowed and gets disambiguated
        let test_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "test")
            .expect("test task not found");

        assert!(matches!(
            test_task.shadowed_by,
            Some(ShadowType::ShellBuiltin(_))
        ));
        assert_eq!(test_task.disambiguated_name, Some("test-m".to_string()));

        reset_to_real_environment();
    }

    #[test]
    #[serial]
    fn test_parse_package_json() {
        let temp_dir = TempDir::new().unwrap();
        let package_json_path = temp_dir.path().join("package.json");

        let content = r#"{
            "name": "test-package",
            "scripts": {
                "test": "jest",
                "build": "tsc"
            }
        }"#;

        File::create(&package_json_path)
            .unwrap()
            .write_all(content.as_bytes())
            .unwrap();

        let tasks = parse_package_json::parse(&package_json_path).unwrap();

        assert_eq!(tasks.len(), 2);

        let test_task = tasks.iter().find(|t| t.name == "test").unwrap();
        assert!(matches!(
            test_task.runner,
            TaskRunner::NodeNpm | TaskRunner::NodeYarn | TaskRunner::NodePnpm | TaskRunner::NodeBun
        ));
        assert_eq!(test_task.description, Some("jest".to_string()));

        let build_task = tasks.iter().find(|t| t.name == "build").unwrap();
        assert!(matches!(
            build_task.runner,
            TaskRunner::NodeNpm | TaskRunner::NodeYarn | TaskRunner::NodePnpm | TaskRunner::NodeBun
        ));
        assert_eq!(build_task.description, Some("tsc".to_string()));
    }

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

        // Mock task being installed
        reset_mock();
        enable_mock();
        mock_executable("task");

        // Create Taskfile.yml with tasks
        let content = r#"version: '3'

tasks:
  test:
    desc: Test task
    cmds:
      - echo "Running tests"
  build:
    desc: Build task
    cmds:
      - echo "Building project"
  deps:
    desc: Task with dependencies
    deps:
      - test
    cmds:
      - echo "Running dependent task""#;

        let taskfile_path = temp_dir.path().join("Taskfile.yml");
        let mut file = File::create(&taskfile_path).unwrap();
        write!(file, "{}", content).unwrap();

        let discovered = discover_tasks(temp_dir.path());

        // Check Taskfile.yml status
        let taskfile_def = discovered.definitions.taskfile.unwrap();
        assert_eq!(taskfile_def.status, TaskFileStatus::Parsed);

        // Verify tasks were discovered
        assert_eq!(discovered.tasks.len(), 3);

        let test_task = discovered.tasks.iter().find(|t| t.name == "test").unwrap();
        assert_eq!(test_task.runner, TaskRunner::Task);
        assert_eq!(test_task.description, Some("Test task".to_string()));

        let build_task = discovered.tasks.iter().find(|t| t.name == "build").unwrap();
        assert_eq!(build_task.runner, TaskRunner::Task);
        assert_eq!(build_task.description, Some("Build task".to_string()));

        let deps_task = discovered.tasks.iter().find(|t| t.name == "deps").unwrap();
        assert_eq!(deps_task.runner, TaskRunner::Task);
        assert_eq!(
            deps_task.description,
            Some("Task with dependencies".to_string())
        );

        reset_mock();
    }

    #[test]
    fn test_discover_maven_tasks() {
        let temp_dir = tempfile::tempdir().unwrap();
        let dir_path = temp_dir.path();

        // Create a sample pom.xml
        let pom_xml_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>sample-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <executions>
                    <execution>
                        <id>compile-java</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.0</version>
                <executions>
                    <execution>
                        <id>build-info</id>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>
</project>"#;

        std::fs::write(dir_path.join("pom.xml"), pom_xml_content).unwrap();

        let discovered = discover_tasks(dir_path);

        // Check that the definition was found
        assert!(discovered.definitions.maven_pom.is_some());
        assert_eq!(
            discovered.definitions.maven_pom.unwrap().status,
            TaskFileStatus::Parsed
        );

        // Check that default Maven lifecycle tasks are discovered
        assert!(discovered.tasks.iter().any(|t| t.name == "clean"));
        assert!(discovered.tasks.iter().any(|t| t.name == "compile"));
        assert!(discovered.tasks.iter().any(|t| t.name == "test"));
        assert!(discovered.tasks.iter().any(|t| t.name == "package"));
        assert!(discovered.tasks.iter().any(|t| t.name == "install"));

        // Check that profile tasks are discovered
        assert!(discovered.tasks.iter().any(|t| t.name == "profile:dev"));
        assert!(discovered.tasks.iter().any(|t| t.name == "profile:prod"));

        // Check that plugin goals are discovered
        assert!(discovered
            .tasks
            .iter()
            .any(|t| t.name == "maven-compiler-plugin:compile"));
        assert!(discovered
            .tasks
            .iter()
            .any(|t| t.name == "spring-boot-maven-plugin:build-info"));

        // Verify task runners
        for task in discovered.tasks {
            if task.definition_type == TaskDefinitionType::MavenPom {
                assert_eq!(task.runner, TaskRunner::Maven);
            }
        }
    }

    #[test]
    #[serial_test::serial]
    fn test_discover_tasks_with_missing_runners() {
        // Setup
        reset_mock();
        enable_mock();

        // Create a temporary directory
        let temp_dir = TempDir::new().unwrap();

        // Create a pom.xml file but don't mock the mvn executable
        let pom_content = r#"<project xmlns="http://maven.apache.org/POM/4.0.0">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.example</groupId>
            <artifactId>test</artifactId>
            <version>1.0.0</version>
        </project>"#;
        let pom_path = temp_dir.path().join("pom.xml");
        let mut pom_file = File::create(&pom_path).unwrap();
        pom_file.write_all(pom_content.as_bytes()).unwrap();

        // Create a build.gradle file but don't mock the gradle executable
        let gradle_content = "task gradleTest { description 'Test task' }";
        let gradle_path = temp_dir.path().join("build.gradle");
        let mut gradle_file = File::create(&gradle_path).unwrap();
        gradle_file.write_all(gradle_content.as_bytes()).unwrap();

        // Set up empty test environment (no executables available)
        let env = TestEnvironment::new();
        set_test_environment(env);

        // Discover tasks
        let discovered = discover_tasks(temp_dir.path());

        // Even though runners are unavailable, tasks should still be discovered
        assert!(
            discovered
                .tasks
                .iter()
                .any(|t| t.runner == TaskRunner::Maven),
            "Maven tasks should be discovered even if runner is unavailable"
        );
        assert!(
            discovered
                .tasks
                .iter()
                .any(|t| t.runner == TaskRunner::Gradle),
            "Gradle tasks should be discovered even if runner is unavailable"
        );

        // Verify that the tasks are marked as having unavailable runners
        for task in &discovered.tasks {
            if task.runner == TaskRunner::Maven || task.runner == TaskRunner::Gradle {
                assert!(
                    !crate::runner::is_runner_available(&task.runner),
                    "Runner for {} should be marked as unavailable",
                    task.name
                );
            }
        }

        // Cleanup
        reset_mock();
        reset_to_real_environment();
    }

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

        // Create .github/workflows directory
        let github_workflows_dir = temp_dir.path().join(".github").join("workflows");
        std::fs::create_dir_all(&github_workflows_dir).unwrap();

        // Create a GitHub Actions workflow file in the standard location
        let github_workflow_content = r#"
name: CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build
        run: echo "Building..."
  
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Test
        run: echo "Testing..."
"#;
        std::fs::write(github_workflows_dir.join("ci.yml"), github_workflow_content).unwrap();

        // Create a workflow file in the project root (should still be discovered)
        let root_workflow_content = r#"
name: Root Workflow
on: [push]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Deploy
        run: echo "Deploying..."
"#;
        std::fs::write(temp_dir.path().join("workflow.yml"), root_workflow_content).unwrap();

        // Create a workflow file in a custom directory
        let custom_dir = temp_dir.path().join("custom").join("workflows");
        std::fs::create_dir_all(&custom_dir).unwrap();
        let custom_workflow_content = r#"
name: Custom Workflow
on: [workflow_dispatch]

jobs:
  custom:
    runs-on: ubuntu-latest
    steps:
      - name: Custom Action
        run: echo "Custom action..."
"#;
        std::fs::write(custom_dir.join("custom.yml"), custom_workflow_content).unwrap();

        // Run task discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check GitHub Actions status
        assert!(matches!(
            discovered.definitions.github_actions.unwrap().status,
            TaskFileStatus::Parsed
        ));

        // Check if all workflows are discovered
        let act_tasks: Vec<&Task> = discovered
            .tasks
            .iter()
            .filter(|t| t.runner == TaskRunner::Act)
            .collect();

        // Should find 3 tasks: CI from .github/workflows/ci.yml,
        // Root Workflow from workflow.yml, and Custom Workflow from custom/workflows/custom.yml
        assert_eq!(
            act_tasks.len(),
            3,
            "Should discover 3 GitHub Actions workflows"
        );

        // Check for specific workflow names - now based on filenames
        let workflow_names: Vec<&str> = act_tasks.iter().map(|t| t.name.as_str()).collect();
        assert!(workflow_names.contains(&"ci"));
        assert!(workflow_names.contains(&"workflow"));
        assert!(workflow_names.contains(&"custom"));

        // With the new workflow grouping, all tasks should have the same workflow directory
        let common_path = temp_dir.path().join(".github").join("workflows");
        for task in act_tasks {
            assert_eq!(task.file_path, common_path);
        }
    }

    #[test]
    #[serial]
    fn test_process_disambiguation_for_shadowed_tasks() {
        // Create a test task that is shadowed by a shell builtin
        let mut discovered = DiscoveredTasks::default();

        // Mock a task with name "test" that is shadowed by shell builtin
        discovered.tasks.push(Task {
            name: "test".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "test".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::ShellBuiltin("bash".to_string())),
            disambiguated_name: None,
        });

        // Mock a task with name "ls" that is shadowed by PATH executable
        discovered.tasks.push(Task {
            name: "ls".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "ls".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::PathExecutable("/bin/ls".to_string())),
            disambiguated_name: None,
        });

        // Mock a task that is not shadowed (should not get a disambiguated name)
        discovered.tasks.push(Task {
            name: "build".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "build".to_string(),
            description: None,
            shadowed_by: None,
            disambiguated_name: None,
        });

        // Process the tasks
        process_task_disambiguation(&mut discovered);

        // Verify shadowed tasks received disambiguated names
        assert_eq!(
            discovered.tasks[0].disambiguated_name,
            Some("test-m".to_string())
        );
        assert_eq!(
            discovered.tasks[1].disambiguated_name,
            Some("ls-m".to_string())
        );

        // Verify non-shadowed task did not receive a disambiguated name
        assert_eq!(discovered.tasks[2].disambiguated_name, None);
    }

    #[test]
    #[serial]
    fn test_process_disambiguation_mixed_scenarios() {
        // Create a test TaskDiscovery with a mix of:
        // 1. Tasks with name collisions
        // 2. Shadowed tasks
        // 3. Normal tasks
        let mut discovered = DiscoveredTasks::default();

        // Create tasks with name collisions - multiple "test" tasks
        discovered.tasks.push(Task {
            name: "test".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "test".to_string(),
            description: None,
            shadowed_by: None,
            disambiguated_name: None,
        });

        discovered.tasks.push(Task {
            name: "test".to_string(),
            file_path: PathBuf::from("/test/package.json"),
            definition_type: TaskDefinitionType::PackageJson,
            runner: TaskRunner::NodeNpm,
            source_name: "test".to_string(),
            description: None,
            shadowed_by: None,
            disambiguated_name: Some("test-npm".to_string()),
        });

        // Shadowed task - "ls" shadowed by PATH executable
        discovered.tasks.push(Task {
            name: "ls".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "ls".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::PathExecutable("/bin/ls".to_string())),
            disambiguated_name: None,
        });

        // Shadowed task with name collision - "cd" shadowed by shell builtin
        discovered.tasks.push(Task {
            name: "cd".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "cd".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::ShellBuiltin("bash".to_string())),
            disambiguated_name: None,
        });

        discovered.tasks.push(Task {
            name: "cd".to_string(),
            file_path: PathBuf::from("/test/Taskfile.yml"),
            definition_type: TaskDefinitionType::Taskfile,
            runner: TaskRunner::Task,
            source_name: "cd".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::ShellBuiltin("bash".to_string())),
            disambiguated_name: None,
        });

        // Normal task - no collision, not shadowed
        discovered.tasks.push(Task {
            name: "build".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "build".to_string(),
            description: None,
            shadowed_by: None,
            disambiguated_name: None,
        });

        // Process the tasks
        process_task_disambiguation(&mut discovered);

        // Verify name collisions get unique disambiguated names
        let test_tasks: Vec<_> = discovered
            .tasks
            .iter()
            .filter(|t| t.name == "test")
            .collect();
        assert_eq!(test_tasks.len(), 2);
        assert!(test_tasks[0].disambiguated_name.is_some());
        assert!(test_tasks[1].disambiguated_name.is_some());
        assert_ne!(
            test_tasks[0].disambiguated_name,
            test_tasks[1].disambiguated_name
        );

        // Verify shadowed task gets disambiguated name
        let ls_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "ls")
            .expect("ls task not found");
        assert_eq!(ls_task.disambiguated_name, Some("ls-m".to_string()));

        // Verify shadowed tasks with name collision all get disambiguated names
        let cd_tasks: Vec<_> = discovered.tasks.iter().filter(|t| t.name == "cd").collect();
        assert_eq!(cd_tasks.len(), 2);
        assert!(cd_tasks[0].disambiguated_name.is_some());
        assert!(cd_tasks[1].disambiguated_name.is_some());
        assert_ne!(
            cd_tasks[0].disambiguated_name,
            cd_tasks[1].disambiguated_name
        );

        // One should be cd-m and the other cd-t
        let cd_disambiguated_names: Vec<_> = cd_tasks
            .iter()
            .filter_map(|t| t.disambiguated_name.as_ref())
            .map(|s| s.as_str())
            .collect();
        assert!(cd_disambiguated_names.contains(&"cd-m"));
        assert!(cd_disambiguated_names.contains(&"cd-t"));

        // Verify normal task doesn't get disambiguated name
        let build_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "build")
            .expect("build task not found");
        assert_eq!(build_task.disambiguated_name, None);
    }

    #[test]
    #[serial]
    fn test_get_matching_tasks_with_shadowed_task() {
        let mut discovered = DiscoveredTasks::default();

        // Create a shadowed task with a disambiguated name
        discovered.tasks.push(Task {
            name: "install".to_string(),
            file_path: PathBuf::from("/test/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "install".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::PathExecutable("/usr/bin/install".to_string())),
            disambiguated_name: Some("install-m".to_string()),
        });

        // Look up the task by original name
        let matching_by_original = get_matching_tasks(&discovered, "install");
        assert_eq!(matching_by_original.len(), 1);

        // Look up the task by disambiguated name
        let matching_by_disambiguated = get_matching_tasks(&discovered, "install-m");
        assert_eq!(matching_by_disambiguated.len(), 1);

        // Verify it's the same task
        assert_eq!(matching_by_original[0].name, "install");
        assert_eq!(matching_by_disambiguated[0].name, "install");
        assert_eq!(
            matching_by_disambiguated[0].disambiguated_name,
            Some("install-m".to_string())
        );
    }

    #[test]
    fn test_execute_task_with_disambiguated_name() {
        let mut discovered_tasks = DiscoveredTasks::new();

        let task = Task {
            name: "test".to_string(),
            file_path: PathBuf::from("/path/to/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "test".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::PathExecutable("/bin/test".to_string())),
            disambiguated_name: Some("test-m".to_string()),
        };

        discovered_tasks.add_task(task);

        // Mock the executor
        let mut mock_executor = MockTaskExecutor::new();

        // Expect execution with the original task name, not the disambiguated one
        mock_executor.expect_execute().times(1).returning(|task| {
            assert_eq!(task.name, "test"); // We still execute with the original name
            assert_eq!(task.disambiguated_name, Some("test-m".to_string())); // But it has a disambiguated name
            assert!(task.shadowed_by.is_some()); // And it is shadowed
            Ok(())
        });

        let mut executor = CommandExecutor::new(mock_executor);

        // Execute using the disambiguated name
        let result = executor.execute_task_by_name(&mut discovered_tasks, "test-m", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_execute_task_by_either_name() {
        let mut discovered_tasks = DiscoveredTasks::new();

        // Add a shadowed task with a disambiguated name
        let task = Task {
            name: "grep".to_string(),
            file_path: PathBuf::from("/path/to/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "grep".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::PathExecutable("/bin/grep".to_string())),
            disambiguated_name: Some("grep-m".to_string()),
        };

        discovered_tasks.add_task(task);

        // Mock the executor
        let mut mock_executor = MockTaskExecutor::new();

        // Expect two executions - one by original name, one by disambiguated name
        mock_executor.expect_execute().times(2).returning(|task| {
            assert_eq!(task.name, "grep"); // Original name used for execution
            Ok(())
        });

        let mut executor = CommandExecutor::new(mock_executor);

        // Execute using the original name
        let result1 = executor.execute_task_by_name(&mut discovered_tasks, "grep", &[]);
        assert!(result1.is_ok());

        // Execute using the disambiguated name
        let result2 = executor.execute_task_by_name(&mut discovered_tasks, "grep-m", &[]);
        assert!(result2.is_ok());
    }

    #[test]
    fn test_execute_task_ambiguous_and_shadowed() {
        let mut discovered_tasks = DiscoveredTasks::new();

        // Add two tasks with the same name but from different sources
        let task1 = Task {
            name: "test".to_string(),
            file_path: PathBuf::from("/path/to/Makefile"),
            definition_type: TaskDefinitionType::Makefile,
            runner: TaskRunner::Make,
            source_name: "test".to_string(),
            description: None,
            shadowed_by: Some(ShadowType::PathExecutable("/bin/test".to_string())),
            disambiguated_name: Some("test-m".to_string()),
        };

        let task2 = Task {
            name: "test".to_string(),
            file_path: PathBuf::from("/path/to/package.json"),
            definition_type: TaskDefinitionType::PackageJson,
            runner: TaskRunner::NodeNpm,
            source_name: "test".to_string(),
            description: None,
            shadowed_by: None,
            disambiguated_name: Some("test-npm".to_string()),
        };

        // Manually set task name counts to mark "test" as ambiguous
        discovered_tasks
            .task_name_counts
            .insert("test".to_string(), 2);

        discovered_tasks.add_task(task1);
        discovered_tasks.add_task(task2);

        // Mock the executor
        let mut mock_executor = MockTaskExecutor::new();

        // Expect execution with the specific task
        mock_executor.expect_execute().times(2).returning(|task| {
            if task.runner == TaskRunner::Make {
                assert_eq!(task.disambiguated_name, Some("test-m".to_string()));
            } else if task.runner == TaskRunner::NodeNpm {
                assert_eq!(task.disambiguated_name, Some("test-npm".to_string()));
            } else {
                panic!("Unexpected task runner");
            }
            Ok(())
        });

        let mut executor = CommandExecutor::new(mock_executor);

        // Execute using the disambiguated names
        let result1 = executor.execute_task_by_name(&mut discovered_tasks, "test-m", &[]);
        assert!(result1.is_ok());

        let result2 = executor.execute_task_by_name(&mut discovered_tasks, "test-npm", &[]);
        assert!(result2.is_ok());

        // Executing by the original name should fail due to ambiguity
        let result3 = executor.execute_task_by_name(&mut discovered_tasks, "test", &[]);

        assert!(result3.is_err());

        // Get the error message and check it
        let err_msg = result3.unwrap_err();
        println!("Error message: {}", err_msg);
        assert!(err_msg.contains("Ambiguous"));
    }

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

        // Create taskfile.yaml (lower priority than Taskfile.yml)
        let taskfile_yaml_content = r#"version: '3'
tasks:
  from_yaml:
    desc: This task is from taskfile.yaml
    cmds:
      - echo "From taskfile.yaml"
"#;
        let taskfile_yaml_path = temp_dir.path().join("taskfile.yaml");
        let mut file = File::create(&taskfile_yaml_path).unwrap();
        write!(file, "{}", taskfile_yaml_content).unwrap();

        // Now create Taskfile.yml (higher priority, should be used)
        let taskfile_yml_content = r#"version: '3'
tasks:
  from_yml:
    desc: This task is from Taskfile.yml
    cmds:
      - echo "From Taskfile.yml"
"#;
        let taskfile_yml_path = temp_dir.path().join("Taskfile.yml");
        let mut file = File::create(&taskfile_yml_path).unwrap();
        write!(file, "{}", taskfile_yml_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the taskfile status is Parsed
        let taskfile_def = discovered.definitions.taskfile.unwrap();
        assert_eq!(taskfile_def.status, TaskFileStatus::Parsed);

        // Verify the task from Taskfile.yml exists (check by content rather than filename)
        assert_eq!(discovered.tasks.len(), 1);
        let task = discovered.tasks.first().unwrap();
        assert_eq!(task.name, "from_yml");
        assert_eq!(
            task.description,
            Some("This task is from Taskfile.yml".to_string())
        );

        // Delete the higher priority Taskfile and verify the lower priority one is used
        std::fs::remove_file(taskfile_yml_path).unwrap();

        // Run discovery again
        let discovered = discover_tasks(temp_dir.path());

        // Check that the taskfile status is Parsed
        let taskfile_def = discovered.definitions.taskfile.unwrap();
        assert_eq!(taskfile_def.status, TaskFileStatus::Parsed);

        // Check the task from taskfile.yaml exists (verify by content)
        assert_eq!(discovered.tasks.len(), 1);
        let task = discovered.tasks.first().unwrap();
        assert_eq!(task.name, "from_yaml");
        assert_eq!(
            task.description,
            Some("This task is from taskfile.yaml".to_string())
        );
    }

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

        // Create a docker-compose.yml file
        let docker_compose_content = r#"
version: '3.8'
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  db:
    image: postgres:13
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
  app:
    build: .
    depends_on:
      - db
"#;
        let docker_compose_path = temp_dir.path().join("docker-compose.yml");
        let mut file = File::create(&docker_compose_path).unwrap();
        write!(file, "{}", docker_compose_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the docker-compose status is Parsed
        let docker_compose_def = discovered.definitions.docker_compose.unwrap();
        assert_eq!(docker_compose_def.status, TaskFileStatus::Parsed);
        assert_eq!(docker_compose_def.path, docker_compose_path);

        // Check that all services are found as tasks (plus the "up" and "down" tasks)
        assert_eq!(discovered.tasks.len(), 5);

        let service_names: Vec<&str> = discovered.tasks.iter().map(|t| t.name.as_str()).collect();
        assert!(service_names.contains(&"up"));
        assert!(service_names.contains(&"down"));
        assert!(service_names.contains(&"web"));
        assert!(service_names.contains(&"db"));
        assert!(service_names.contains(&"app"));

        // Check task properties
        for task in &discovered.tasks {
            assert_eq!(task.definition_type, TaskDefinitionType::DockerCompose);
            assert_eq!(task.runner, TaskRunner::DockerCompose);
            assert_eq!(task.file_path, docker_compose_path);
            assert!(task.description.is_some());
            assert!(task.shadowed_by.is_none());
            assert!(task.disambiguated_name.is_none());
        }

        // Check specific task descriptions
        let web_task = discovered.tasks.iter().find(|t| t.name == "web").unwrap();
        assert!(web_task
            .description
            .as_ref()
            .unwrap()
            .contains("nginx:alpine"));

        let app_task = discovered.tasks.iter().find(|t| t.name == "app").unwrap();
        assert!(app_task.description.as_ref().unwrap().contains("build"));
    }

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

        // Create an empty docker-compose.yml file
        let docker_compose_content = r#"
version: '3.8'
services: {}
"#;
        let docker_compose_path = temp_dir.path().join("docker-compose.yml");
        let mut file = File::create(&docker_compose_path).unwrap();
        write!(file, "{}", docker_compose_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the docker-compose status is Parsed
        let docker_compose_def = discovered.definitions.docker_compose.unwrap();
        assert_eq!(docker_compose_def.status, TaskFileStatus::Parsed);

        // Check that only the "up" and "down" tasks are found
        assert_eq!(discovered.tasks.len(), 2);
        let service_names: Vec<&str> = discovered.tasks.iter().map(|t| t.name.as_str()).collect();
        assert!(service_names.contains(&"up"));
        assert!(service_names.contains(&"down"));
    }

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

        // Run discovery without docker-compose.yml
        let discovered = discover_tasks(temp_dir.path());

        // Check that the docker-compose status is NotFound
        let docker_compose_def = discovered.definitions.docker_compose.unwrap();
        assert_eq!(docker_compose_def.status, TaskFileStatus::NotFound);

        // Check that no tasks are found
        assert_eq!(discovered.tasks.len(), 0);
    }

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

        // Create a compose.yml file (lower priority)
        let compose_content = r#"
version: '3.8'
services:
  api:
    image: nginx:alpine
    ports:
      - "8080:80"
"#;
        std::fs::write(temp_dir.path().join("compose.yml"), compose_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the docker-compose status is Parsed
        let docker_compose_def = discovered.definitions.docker_compose.unwrap();
        assert_eq!(docker_compose_def.status, TaskFileStatus::Parsed);
        assert_eq!(docker_compose_def.path, temp_dir.path().join("compose.yml"));

        // Check that the service is found (plus the "up" and "down" tasks)
        assert_eq!(discovered.tasks.len(), 3);
        let service_names: Vec<&str> = discovered.tasks.iter().map(|t| t.name.as_str()).collect();
        assert!(service_names.contains(&"up"));
        assert!(service_names.contains(&"down"));
        assert!(service_names.contains(&"api"));

        let api_task = discovered.tasks.iter().find(|t| t.name == "api").unwrap();
        assert_eq!(api_task.definition_type, TaskDefinitionType::DockerCompose);
        assert_eq!(api_task.runner, TaskRunner::DockerCompose);

        // Now create a docker-compose.yml file (higher priority)
        let docker_compose_content = r#"
version: '3.8'
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  db:
    image: postgres:13
"#;
        std::fs::write(
            temp_dir.path().join("docker-compose.yml"),
            docker_compose_content,
        )
        .unwrap();

        // Run discovery again
        let discovered = discover_tasks(temp_dir.path());

        // Check that the higher priority file is used
        let docker_compose_def = discovered.definitions.docker_compose.unwrap();
        assert_eq!(docker_compose_def.status, TaskFileStatus::Parsed);
        assert_eq!(
            docker_compose_def.path,
            temp_dir.path().join("docker-compose.yml")
        );

        // Check that the services from the higher priority file are found (plus the "up" and "down" tasks)
        assert_eq!(discovered.tasks.len(), 4);
        let service_names: Vec<&str> = discovered.tasks.iter().map(|t| t.name.as_str()).collect();
        assert!(service_names.contains(&"up"));
        assert!(service_names.contains(&"down"));
        assert!(service_names.contains(&"web"));
        assert!(service_names.contains(&"db"));
    }

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

        // Create a .travis.yml file
        let travis_content = r#"
language: node_js
node_js:
  - "18"
  - "20"

jobs:
  test:
    name: "Test"
    stage: test
  build:
    name: "Build"
    stage: build
"#;
        let travis_path = temp_dir.path().join(".travis.yml");
        let mut file = File::create(&travis_path).unwrap();
        write!(file, "{}", travis_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the travis-ci status is Parsed
        let travis_def = discovered.definitions.travis_ci.unwrap();
        assert_eq!(travis_def.status, TaskFileStatus::Parsed);
        assert_eq!(travis_def.path, travis_path);

        // Check that both jobs are found as tasks
        assert_eq!(discovered.tasks.len(), 2);

        let test_task = discovered.tasks.iter().find(|t| t.name == "test").unwrap();
        assert_eq!(test_task.definition_type, TaskDefinitionType::TravisCi);
        assert_eq!(test_task.runner, TaskRunner::TravisCi);
        assert_eq!(
            test_task.description,
            Some("Travis CI job: Test".to_string())
        );

        let build_task = discovered.tasks.iter().find(|t| t.name == "build").unwrap();
        assert_eq!(build_task.definition_type, TaskDefinitionType::TravisCi);
        assert_eq!(build_task.runner, TaskRunner::TravisCi);
        assert_eq!(
            build_task.description,
            Some("Travis CI job: Build".to_string())
        );
    }

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

        // Create a .travis.yml file with matrix configuration
        let travis_content = r#"
language: python

matrix:
  include:
    - name: "Python 3.8"
      python: "3.8"
    - name: "Python 3.9"
      python: "3.9"
    - name: "Python 3.10"
      python: "3.10"
"#;
        let travis_path = temp_dir.path().join(".travis.yml");
        let mut file = File::create(&travis_path).unwrap();
        write!(file, "{}", travis_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the travis-ci status is Parsed
        let travis_def = discovered.definitions.travis_ci.unwrap();
        assert_eq!(travis_def.status, TaskFileStatus::Parsed);

        // Check that all matrix jobs are found as tasks
        assert_eq!(discovered.tasks.len(), 3);

        for task in &discovered.tasks {
            assert_eq!(task.definition_type, TaskDefinitionType::TravisCi);
            assert_eq!(task.runner, TaskRunner::TravisCi);
            assert!(task
                .description
                .as_ref()
                .unwrap()
                .contains("Travis CI job:"));
        }

        let python_38_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "Python 3.8")
            .unwrap();
        assert_eq!(
            python_38_task.description,
            Some("Travis CI job: Python 3.8".to_string())
        );

        let python_39_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "Python 3.9")
            .unwrap();
        assert_eq!(
            python_39_task.description,
            Some("Travis CI job: Python 3.9".to_string())
        );

        let python_310_task = discovered
            .tasks
            .iter()
            .find(|t| t.name == "Python 3.10")
            .unwrap();
        assert_eq!(
            python_310_task.description,
            Some("Travis CI job: Python 3.10".to_string())
        );
    }

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

        // Create a basic .travis.yml file without jobs section
        let travis_content = r#"
language: ruby
rvm:
  - 2.7
  - 3.0
  - 3.1

script:
  - bundle install
  - bundle exec rspec
"#;
        let travis_path = temp_dir.path().join(".travis.yml");
        let mut file = File::create(&travis_path).unwrap();
        write!(file, "{}", travis_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the travis-ci status is Parsed
        let travis_def = discovered.definitions.travis_ci.unwrap();
        assert_eq!(travis_def.status, TaskFileStatus::Parsed);

        // Check that a default task is created
        assert_eq!(discovered.tasks.len(), 1);

        let task = &discovered.tasks[0];
        assert_eq!(task.name, "travis");
        assert_eq!(task.definition_type, TaskDefinitionType::TravisCi);
        assert_eq!(task.runner, TaskRunner::TravisCi);
        assert_eq!(
            task.description,
            Some("Travis CI configuration".to_string())
        );
    }

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

        // Run discovery without .travis.yml
        let discovered = discover_tasks(temp_dir.path());

        // Check that the travis-ci status is NotFound
        let travis_def = discovered.definitions.travis_ci.unwrap();
        assert_eq!(travis_def.status, TaskFileStatus::NotFound);

        // Check that no tasks are found
        assert_eq!(discovered.tasks.len(), 0);
    }

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

        // Create a CMakeLists.txt file
        let cmake_content = r#"
cmake_minimum_required(VERSION 3.10)
project(TestProject)

add_custom_target(build-all COMMENT "Build all components")
add_custom_target(test-all COMMENT "Run all tests")
add_custom_target(clean-all COMMENT "Clean all build artifacts")
"#;
        let cmake_path = temp_dir.path().join("CMakeLists.txt");
        let mut file = File::create(&cmake_path).unwrap();
        write!(file, "{}", cmake_content).unwrap();

        // Run discovery
        let discovered = discover_tasks(temp_dir.path());

        // Check that the cmake status is Parsed
        let cmake_def = discovered.definitions.cmake.unwrap();
        assert_eq!(cmake_def.status, TaskFileStatus::Parsed);
        assert_eq!(cmake_def.path, cmake_path);

        // Check that we found the expected tasks
        let task_names: Vec<&str> = discovered.tasks.iter().map(|t| t.name.as_str()).collect();
        assert!(task_names.contains(&"build-all"));
        assert!(task_names.contains(&"test-all"));
        assert!(task_names.contains(&"clean-all"));

        // Check that the tasks have the correct runner
        for task in &discovered.tasks {
            if task.name == "build-all" || task.name == "test-all" || task.name == "clean-all" {
                assert_eq!(task.runner, TaskRunner::CMake);
                assert_eq!(task.definition_type, TaskDefinitionType::CMake);
            }
        }
    }
}