selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
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
//! Test Dashboard
//!
//! Testing UI with:
//! - Test explorer tree view
//! - Watch mode with debouncing
//! - Coverage tracking
//! - Failing test isolation
//! - Progress visualization

#![allow(dead_code, unused_imports, unused_variables)]

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

/// Test status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum TestStatus {
    #[default]
    Pending,
    Running,
    Passed,
    Failed,
    Ignored,
    Skipped,
}

impl TestStatus {
    /// Icon for display
    pub fn icon(&self) -> &'static str {
        match self {
            TestStatus::Pending => "",
            TestStatus::Running => "",
            TestStatus::Passed => "",
            TestStatus::Failed => "",
            TestStatus::Ignored => "",
            TestStatus::Skipped => "",
        }
    }

    /// Color code
    pub fn color(&self) -> &'static str {
        match self {
            TestStatus::Pending => "\x1b[90m", // Gray
            TestStatus::Running => "\x1b[33m", // Yellow
            TestStatus::Passed => "\x1b[32m",  // Green
            TestStatus::Failed => "\x1b[31m",  // Red
            TestStatus::Ignored => "\x1b[35m", // Magenta
            TestStatus::Skipped => "\x1b[90m", // Gray
        }
    }

    /// Is this a final state?
    pub fn is_final(&self) -> bool {
        matches!(
            self,
            TestStatus::Passed | TestStatus::Failed | TestStatus::Ignored | TestStatus::Skipped
        )
    }
}

impl std::fmt::Display for TestStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            TestStatus::Pending => "Pending",
            TestStatus::Running => "Running",
            TestStatus::Passed => "Passed",
            TestStatus::Failed => "Failed",
            TestStatus::Ignored => "Ignored",
            TestStatus::Skipped => "Skipped",
        };
        write!(f, "{}", name)
    }
}

/// A single test
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Test {
    /// Full test name (module::submodule::test_name)
    pub name: String,
    /// Module path
    pub module: String,
    /// Test function name
    pub function: String,
    /// Source file
    pub file: Option<PathBuf>,
    /// Line number
    pub line: Option<usize>,
    /// Current status
    pub status: TestStatus,
    /// Duration of last run
    pub duration: Option<Duration>,
    /// Failure message if failed
    pub failure_message: Option<String>,
    /// Failure location
    pub failure_location: Option<String>,
    /// Is this test ignored?
    pub ignored: bool,
    /// Tags/attributes
    pub tags: Vec<String>,
}

impl Test {
    /// Create a new test
    pub fn new(name: String) -> Self {
        let parts: Vec<&str> = name.rsplitn(2, "::").collect();
        let (function, module) = if parts.len() == 2 {
            (parts[0].to_string(), parts[1].to_string())
        } else {
            (name.clone(), String::new())
        };

        Self {
            name,
            module,
            function,
            file: None,
            line: None,
            status: TestStatus::Pending,
            duration: None,
            failure_message: None,
            failure_location: None,
            ignored: false,
            tags: Vec::new(),
        }
    }

    /// Set file and line
    pub fn with_location(mut self, file: PathBuf, line: usize) -> Self {
        self.file = Some(file);
        self.line = Some(line);
        self
    }

    /// Mark as running
    pub fn start(&mut self) {
        self.status = TestStatus::Running;
    }

    /// Mark as passed
    pub fn pass(&mut self, duration: Duration) {
        self.status = TestStatus::Passed;
        self.duration = Some(duration);
        self.failure_message = None;
    }

    /// Mark as failed
    pub fn fail(&mut self, duration: Duration, message: String) {
        self.status = TestStatus::Failed;
        self.duration = Some(duration);
        self.failure_message = Some(message);
    }

    /// Mark as ignored
    pub fn ignore(&mut self) {
        self.status = TestStatus::Ignored;
        self.ignored = true;
    }

    /// Display formatted
    pub fn display(&self) -> String {
        let duration = self
            .duration
            .map(|d| format!(" ({:.2}s)", d.num_milliseconds() as f64 / 1000.0))
            .unwrap_or_default();

        format!(
            "{}{} {}{}",
            self.status.color(),
            self.status.icon(),
            self.name,
            duration
        )
    }

    /// Short display (just status and function name)
    pub fn short_display(&self) -> String {
        format!("{} {}", self.status.icon(), self.function)
    }
}

/// Test suite (collection of tests)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TestSuite {
    /// Suite name (crate/module name)
    pub name: String,
    /// All tests in this suite
    pub tests: Vec<Test>,
    /// Child suites (submodules)
    pub children: Vec<TestSuite>,
    /// Total test count (including children)
    pub total: usize,
    /// Passed count
    pub passed: usize,
    /// Failed count
    pub failed: usize,
    /// Ignored count
    pub ignored: usize,
}

impl TestSuite {
    /// Create new suite
    pub fn new(name: String) -> Self {
        Self {
            name,
            ..Default::default()
        }
    }

    /// Add a test to the suite
    pub fn add_test(&mut self, test: Test) {
        self.tests.push(test);
        self.update_counts();
    }

    /// Add a child suite
    pub fn add_child(&mut self, suite: TestSuite) {
        self.children.push(suite);
        self.update_counts();
    }

    /// Update counts from tests and children
    pub fn update_counts(&mut self) {
        let mut total = self.tests.len();
        let mut passed = 0;
        let mut failed = 0;
        let mut ignored = 0;

        for test in &self.tests {
            match test.status {
                TestStatus::Passed => passed += 1,
                TestStatus::Failed => failed += 1,
                TestStatus::Ignored | TestStatus::Skipped => ignored += 1,
                _ => {}
            }
        }

        for child in &self.children {
            total += child.total;
            passed += child.passed;
            failed += child.failed;
            ignored += child.ignored;
        }

        self.total = total;
        self.passed = passed;
        self.failed = failed;
        self.ignored = ignored;
    }

    /// Get all tests (flattened)
    pub fn all_tests(&self) -> Vec<&Test> {
        let mut all: Vec<&Test> = self.tests.iter().collect();
        for child in &self.children {
            all.extend(child.all_tests());
        }
        all
    }

    /// Get failed tests
    pub fn failed_tests(&self) -> Vec<&Test> {
        self.all_tests()
            .into_iter()
            .filter(|t| t.status == TestStatus::Failed)
            .collect()
    }

    /// Get passed tests
    pub fn passed_tests(&self) -> Vec<&Test> {
        self.all_tests()
            .into_iter()
            .filter(|t| t.status == TestStatus::Passed)
            .collect()
    }

    /// Is all passed?
    pub fn is_passing(&self) -> bool {
        self.failed == 0
    }

    /// Progress percentage
    pub fn progress(&self) -> f32 {
        if self.total == 0 {
            return 100.0;
        }
        ((self.passed + self.failed + self.ignored) as f32 / self.total as f32) * 100.0
    }

    /// Display summary
    pub fn summary(&self) -> String {
        format!(
            "{}: {} total, {} passed, {} failed, {} ignored",
            self.name, self.total, self.passed, self.failed, self.ignored
        )
    }
}

/// Test run result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestRun {
    /// Run ID
    pub id: String,
    /// Start time
    pub started_at: DateTime<Utc>,
    /// End time
    pub ended_at: Option<DateTime<Utc>>,
    /// Test results
    pub results: Vec<Test>,
    /// Overall status
    pub status: RunStatus,
    /// Total duration
    pub duration: Option<Duration>,
}

/// Run status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum RunStatus {
    #[default]
    Running,
    Passed,
    Failed,
    Cancelled,
}

impl TestRun {
    /// Create new run
    pub fn new() -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            started_at: Utc::now(),
            ended_at: None,
            results: Vec::new(),
            status: RunStatus::Running,
            duration: None,
        }
    }

    /// Add test result
    pub fn add_result(&mut self, test: Test) {
        self.results.push(test);
    }

    /// Complete the run
    pub fn complete(&mut self) {
        self.ended_at = Some(Utc::now());
        self.duration = Some(Utc::now() - self.started_at);

        // Determine overall status
        let has_failures = self.results.iter().any(|t| t.status == TestStatus::Failed);
        self.status = if has_failures {
            RunStatus::Failed
        } else {
            RunStatus::Passed
        };
    }

    /// Cancel the run
    pub fn cancel(&mut self) {
        self.ended_at = Some(Utc::now());
        self.duration = Some(Utc::now() - self.started_at);
        self.status = RunStatus::Cancelled;
    }

    /// Get summary
    pub fn summary(&self) -> TestRunSummary {
        TestRunSummary {
            total: self.results.len(),
            passed: self
                .results
                .iter()
                .filter(|t| t.status == TestStatus::Passed)
                .count(),
            failed: self
                .results
                .iter()
                .filter(|t| t.status == TestStatus::Failed)
                .count(),
            ignored: self
                .results
                .iter()
                .filter(|t| t.status == TestStatus::Ignored)
                .count(),
            duration: self.duration,
        }
    }
}

impl Default for TestRun {
    fn default() -> Self {
        Self::new()
    }
}

/// Test run summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestRunSummary {
    pub total: usize,
    pub passed: usize,
    pub failed: usize,
    pub ignored: usize,
    pub duration: Option<Duration>,
}

impl TestRunSummary {
    /// Success rate as percentage
    pub fn success_rate(&self) -> f32 {
        if self.total == 0 {
            return 100.0;
        }
        (self.passed as f32 / self.total as f32) * 100.0
    }

    /// Display
    pub fn display(&self) -> String {
        let duration = self
            .duration
            .map(|d| format!(" in {:.2}s", d.num_milliseconds() as f64 / 1000.0))
            .unwrap_or_default();

        format!(
            "{} passed, {} failed, {} ignored ({:.1}% pass rate){}",
            self.passed,
            self.failed,
            self.ignored,
            self.success_rate(),
            duration
        )
    }
}

/// Test explorer tree node
#[derive(Debug, Clone)]
pub struct TestTreeNode {
    /// Node name
    pub name: String,
    /// Full path
    pub path: String,
    /// Is this a test (leaf) or module (branch)?
    pub is_test: bool,
    /// Status if test
    pub status: Option<TestStatus>,
    /// Children
    pub children: Vec<TestTreeNode>,
    /// Is expanded in UI?
    pub expanded: bool,
}

impl Default for TestTreeNode {
    fn default() -> Self {
        Self {
            name: String::new(),
            path: String::new(),
            is_test: false,
            status: None,
            children: Vec::new(),
            expanded: true,
        }
    }
}

impl TestTreeNode {
    /// Create module node
    pub fn module(name: String, path: String) -> Self {
        Self {
            name,
            path,
            is_test: false,
            status: None,
            children: Vec::new(),
            expanded: true,
        }
    }

    /// Create test node
    pub fn test(name: String, path: String, status: TestStatus) -> Self {
        Self {
            name,
            path,
            is_test: true,
            status: Some(status),
            children: Vec::new(),
            expanded: false,
        }
    }

    /// Add child node
    pub fn add_child(&mut self, child: TestTreeNode) {
        self.children.push(child);
    }

    /// Find or create a path in the tree
    pub fn find_or_create(&mut self, parts: &[&str]) -> &mut TestTreeNode {
        if parts.is_empty() {
            return self;
        }

        let name = parts[0];
        let path = if self.path.is_empty() {
            name.to_string()
        } else {
            format!("{}::{}", self.path, name)
        };

        // Find existing child
        let idx = self.children.iter().position(|c| c.name == name);

        if let Some(idx) = idx {
            self.children[idx].find_or_create(&parts[1..])
        } else {
            // Create new child
            let child = TestTreeNode::module(name.to_string(), path);
            self.children.push(child);
            let last = self.children.len() - 1;
            self.children[last].find_or_create(&parts[1..])
        }
    }

    /// Toggle expansion
    pub fn toggle(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Count tests in this node and children
    pub fn test_count(&self) -> usize {
        let own = if self.is_test { 1 } else { 0 };
        own + self.children.iter().map(|c| c.test_count()).sum::<usize>()
    }

    /// Count failed tests
    pub fn failed_count(&self) -> usize {
        let own = if self.status == Some(TestStatus::Failed) {
            1
        } else {
            0
        };
        own + self
            .children
            .iter()
            .map(|c| c.failed_count())
            .sum::<usize>()
    }

    /// Aggregate status for modules
    pub fn aggregate_status(&self) -> TestStatus {
        if self.is_test {
            return self.status.unwrap_or(TestStatus::Pending);
        }

        let mut has_failed = false;
        let mut has_running = false;
        let mut all_passed = true;

        for child in &self.children {
            let status = child.aggregate_status();
            match status {
                TestStatus::Failed => has_failed = true,
                TestStatus::Running => has_running = true,
                TestStatus::Passed => {}
                _ => all_passed = false,
            }
        }

        if has_failed {
            TestStatus::Failed
        } else if has_running {
            TestStatus::Running
        } else if all_passed && !self.children.is_empty() {
            TestStatus::Passed
        } else {
            TestStatus::Pending
        }
    }
}

/// Test explorer for navigating tests
#[derive(Debug, Default)]
pub struct TestExplorer {
    /// Root of test tree
    root: TestTreeNode,
    /// Currently selected path
    selected: Option<String>,
    /// Filter pattern
    filter: Option<String>,
    /// Show only failed tests
    show_failed_only: bool,
}

impl TestExplorer {
    /// Create new explorer
    pub fn new() -> Self {
        Self {
            root: TestTreeNode::module(String::new(), String::new()),
            selected: None,
            filter: None,
            show_failed_only: false,
        }
    }

    /// Build tree from tests
    pub fn build_tree(&mut self, tests: &[Test]) {
        self.root = TestTreeNode::module(String::new(), String::new());

        for test in tests {
            let parts: Vec<&str> = test.name.split("::").collect();
            if parts.len() > 1 {
                // Navigate to parent module
                let parent = self.root.find_or_create(&parts[..parts.len() - 1]);
                // Add test as leaf
                parent.add_child(TestTreeNode::test(
                    test.function.clone(),
                    test.name.clone(),
                    test.status,
                ));
            } else {
                // Top-level test
                self.root.add_child(TestTreeNode::test(
                    test.function.clone(),
                    test.name.clone(),
                    test.status,
                ));
            }
        }
    }

    /// Get visible nodes (respecting expansion and filters)
    pub fn visible_nodes(&self) -> Vec<(usize, &TestTreeNode)> {
        let mut result = Vec::new();
        self.collect_visible(&self.root, 0, &mut result);
        result
    }

    fn collect_visible<'a>(
        &'a self,
        node: &'a TestTreeNode,
        depth: usize,
        result: &mut Vec<(usize, &'a TestTreeNode)>,
    ) {
        // Skip root
        if !node.path.is_empty() {
            // Apply filter
            if let Some(filter) = &self.filter {
                if !node.path.to_lowercase().contains(&filter.to_lowercase()) {
                    // Check if any child matches
                    if !node
                        .children
                        .iter()
                        .any(|c| c.path.to_lowercase().contains(&filter.to_lowercase()))
                    {
                        return;
                    }
                }
            }

            // Apply failed-only filter
            if self.show_failed_only && node.failed_count() == 0 {
                return;
            }

            result.push((depth, node));
        }

        if node.expanded || node.path.is_empty() {
            for child in &node.children {
                self.collect_visible(child, depth + 1, result);
            }
        }
    }

    /// Set filter
    pub fn set_filter(&mut self, filter: Option<String>) {
        self.filter = filter;
    }

    /// Toggle failed-only mode
    pub fn toggle_failed_only(&mut self) {
        self.show_failed_only = !self.show_failed_only;
    }

    /// Get selected test
    pub fn selected(&self) -> Option<&str> {
        self.selected.as_deref()
    }

    /// Select a test
    pub fn select(&mut self, path: String) {
        self.selected = Some(path);
    }
}

/// Watch mode for continuous testing
#[derive(Debug)]
pub struct WatchMode {
    /// Debounce duration
    debounce: Duration,
    /// Last trigger time
    last_trigger: Option<DateTime<Utc>>,
    /// Pending changes
    pending_changes: HashSet<PathBuf>,
    /// Test patterns to run
    test_patterns: Vec<String>,
    /// Is watch mode active?
    active: bool,
    /// Run on save
    run_on_save: bool,
}

impl Default for WatchMode {
    fn default() -> Self {
        Self::new()
    }
}

impl WatchMode {
    /// Create new watch mode
    pub fn new() -> Self {
        Self {
            debounce: Duration::milliseconds(500),
            last_trigger: None,
            pending_changes: HashSet::new(),
            test_patterns: Vec::new(),
            active: false,
            run_on_save: true,
        }
    }

    /// Set debounce duration
    pub fn with_debounce(mut self, ms: i64) -> Self {
        self.debounce = Duration::milliseconds(ms);
        self
    }

    /// Start watch mode
    pub fn start(&mut self) {
        self.active = true;
    }

    /// Stop watch mode
    pub fn stop(&mut self) {
        self.active = false;
    }

    /// Is active?
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Record a file change
    pub fn file_changed(&mut self, path: PathBuf) {
        if self.active {
            self.pending_changes.insert(path);
        }
    }

    /// Check if we should run tests (debounce check)
    pub fn should_run(&mut self) -> bool {
        if !self.active || self.pending_changes.is_empty() {
            return false;
        }

        let now = Utc::now();
        if let Some(last) = self.last_trigger {
            if now - last < self.debounce {
                return false;
            }
        }

        self.last_trigger = Some(now);
        true
    }

    /// Get and clear pending changes
    pub fn take_changes(&mut self) -> HashSet<PathBuf> {
        std::mem::take(&mut self.pending_changes)
    }

    /// Add test pattern to run
    pub fn add_pattern(&mut self, pattern: String) {
        self.test_patterns.push(pattern);
    }

    /// Get patterns
    pub fn patterns(&self) -> &[String] {
        &self.test_patterns
    }

    /// Clear patterns
    pub fn clear_patterns(&mut self) {
        self.test_patterns.clear();
    }

    /// Toggle run on save
    pub fn toggle_run_on_save(&mut self) {
        self.run_on_save = !self.run_on_save;
    }
}

/// Coverage data for a file
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FileCoverage {
    /// File path
    pub path: PathBuf,
    /// Lines covered
    pub covered_lines: HashSet<usize>,
    /// Lines not covered
    pub uncovered_lines: HashSet<usize>,
    /// Lines that are executable
    pub executable_lines: HashSet<usize>,
    /// Branch coverage data
    pub branches: Vec<BranchCoverage>,
}

impl FileCoverage {
    /// Create new coverage for a file
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            ..Default::default()
        }
    }

    /// Add covered line
    pub fn cover_line(&mut self, line: usize) {
        self.covered_lines.insert(line);
        self.executable_lines.insert(line);
        self.uncovered_lines.remove(&line);
    }

    /// Add uncovered line
    pub fn add_uncovered(&mut self, line: usize) {
        if !self.covered_lines.contains(&line) {
            self.uncovered_lines.insert(line);
        }
        self.executable_lines.insert(line);
    }

    /// Coverage percentage
    pub fn percentage(&self) -> f32 {
        if self.executable_lines.is_empty() {
            return 100.0;
        }
        (self.covered_lines.len() as f32 / self.executable_lines.len() as f32) * 100.0
    }

    /// Is line covered?
    pub fn is_covered(&self, line: usize) -> Option<bool> {
        if self.covered_lines.contains(&line) {
            Some(true)
        } else if self.uncovered_lines.contains(&line) {
            Some(false)
        } else {
            None // Not an executable line
        }
    }

    /// Get gutter symbol for line
    pub fn gutter(&self, line: usize) -> &'static str {
        match self.is_covered(line) {
            Some(true) => "",
            Some(false) => "",
            None => " ",
        }
    }

    /// Get gutter color for line
    pub fn gutter_color(&self, line: usize) -> &'static str {
        match self.is_covered(line) {
            Some(true) => "\x1b[32m",  // Green
            Some(false) => "\x1b[31m", // Red
            None => "",
        }
    }
}

/// Branch coverage info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchCoverage {
    /// Line number
    pub line: usize,
    /// Branch index
    pub branch: usize,
    /// Was this branch taken?
    pub taken: bool,
}

/// Coverage tracker for the whole project
#[derive(Debug, Default)]
pub struct CoverageTracker {
    /// Coverage by file
    files: HashMap<PathBuf, FileCoverage>,
    /// Last update
    last_update: Option<DateTime<Utc>>,
}

impl CoverageTracker {
    /// Create new tracker
    pub fn new() -> Self {
        Self::default()
    }

    /// Add file coverage
    pub fn add_file(&mut self, coverage: FileCoverage) {
        self.files.insert(coverage.path.clone(), coverage);
        self.last_update = Some(Utc::now());
    }

    /// Get coverage for a file
    pub fn get(&self, path: &Path) -> Option<&FileCoverage> {
        self.files.get(path)
    }

    /// Overall coverage percentage
    pub fn total_percentage(&self) -> f32 {
        if self.files.is_empty() {
            return 0.0;
        }

        let total_covered: usize = self.files.values().map(|f| f.covered_lines.len()).sum();
        let total_executable: usize = self.files.values().map(|f| f.executable_lines.len()).sum();

        if total_executable == 0 {
            return 100.0;
        }

        (total_covered as f32 / total_executable as f32) * 100.0
    }

    /// Files below threshold
    pub fn below_threshold(&self, threshold: f32) -> Vec<&FileCoverage> {
        self.files
            .values()
            .filter(|f| f.percentage() < threshold)
            .collect()
    }

    /// Files sorted by coverage (ascending)
    pub fn sorted_by_coverage(&self) -> Vec<&FileCoverage> {
        let mut files: Vec<_> = self.files.values().collect();
        files.sort_by(|a, b| {
            a.percentage()
                .partial_cmp(&b.percentage())
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        files
    }

    /// Clear all coverage data
    pub fn clear(&mut self) {
        self.files.clear();
        self.last_update = None;
    }

    /// File count
    pub fn file_count(&self) -> usize {
        self.files.len()
    }
}

/// Progress bar for test runs
#[derive(Debug, Clone)]
pub struct TestProgress {
    /// Total tests
    pub total: usize,
    /// Completed tests
    pub completed: usize,
    /// Passed tests
    pub passed: usize,
    /// Failed tests
    pub failed: usize,
    /// Bar width
    pub width: usize,
    /// Show percentage
    pub show_percentage: bool,
}

impl TestProgress {
    /// Create new progress
    pub fn new(total: usize) -> Self {
        Self {
            total,
            completed: 0,
            passed: 0,
            failed: 0,
            width: 40,
            show_percentage: true,
        }
    }

    /// Update progress
    pub fn update(&mut self, passed: bool) {
        self.completed += 1;
        if passed {
            self.passed += 1;
        } else {
            self.failed += 1;
        }
    }

    /// Completion percentage
    pub fn percentage(&self) -> f32 {
        if self.total == 0 {
            return 100.0;
        }
        (self.completed as f32 / self.total as f32) * 100.0
    }

    /// Render progress bar
    pub fn render(&self) -> String {
        let filled = if self.total > 0 {
            (self.completed * self.width) / self.total
        } else {
            self.width
        };
        let empty = self.width - filled;

        let bar: String = "".repeat(filled) + &"".repeat(empty);

        let status = if self.failed > 0 {
            format!("\x1b[31m{} failed\x1b[0m", self.failed)
        } else {
            format!("\x1b[32m{} passed\x1b[0m", self.passed)
        };

        if self.show_percentage {
            format!("[{}] {:.1}% {}", bar, self.percentage(), status)
        } else {
            format!("[{}] {}/{} {}", bar, self.completed, self.total, status)
        }
    }

    /// Is complete?
    pub fn is_complete(&self) -> bool {
        self.completed >= self.total
    }

    /// Is all passing?
    pub fn is_passing(&self) -> bool {
        self.failed == 0
    }
}

/// Sparkline for test history
#[derive(Debug, Clone)]
pub struct TestSparkline {
    /// Data points (success rates)
    points: Vec<f32>,
    /// Max points to keep
    max_points: usize,
}

impl TestSparkline {
    /// Create new sparkline
    pub fn new(max_points: usize) -> Self {
        Self {
            points: Vec::new(),
            max_points,
        }
    }

    /// Add a point
    pub fn add(&mut self, success_rate: f32) {
        self.points.push(success_rate);
        if self.points.len() > self.max_points {
            self.points.remove(0);
        }
    }

    /// Render sparkline
    pub fn render(&self) -> String {
        const BLOCKS: &[char] = &['', '', '', '', '', '', '', ''];

        self.points
            .iter()
            .map(|&p| {
                let idx = ((p / 100.0) * (BLOCKS.len() - 1) as f32) as usize;
                let idx = idx.min(BLOCKS.len() - 1);
                BLOCKS[idx]
            })
            .collect()
    }

    /// Trend direction
    pub fn trend(&self) -> Trend {
        if self.points.len() < 2 {
            return Trend::Stable;
        }

        let recent = self
            .points
            .iter()
            .rev()
            .take(3)
            .copied()
            .collect::<Vec<_>>();
        let avg_recent: f32 = recent.iter().sum::<f32>() / recent.len() as f32;

        let older = self
            .points
            .iter()
            .rev()
            .skip(3)
            .take(3)
            .copied()
            .collect::<Vec<_>>();
        if older.is_empty() {
            return Trend::Stable;
        }
        let avg_older: f32 = older.iter().sum::<f32>() / older.len() as f32;

        if avg_recent > avg_older + 5.0 {
            Trend::Improving
        } else if avg_recent < avg_older - 5.0 {
            Trend::Declining
        } else {
            Trend::Stable
        }
    }

    /// Point count
    pub fn len(&self) -> usize {
        self.points.len()
    }

    /// Is empty
    pub fn is_empty(&self) -> bool {
        self.points.is_empty()
    }
}

/// Trend direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Trend {
    Improving,
    Stable,
    Declining,
}

impl Trend {
    /// Icon
    pub fn icon(&self) -> &'static str {
        match self {
            Trend::Improving => "",
            Trend::Stable => "",
            Trend::Declining => "",
        }
    }

    /// Color
    pub fn color(&self) -> &'static str {
        match self {
            Trend::Improving => "\x1b[32m",
            Trend::Stable => "\x1b[33m",
            Trend::Declining => "\x1b[31m",
        }
    }
}

/// Test dashboard combining all components
#[derive(Debug)]
pub struct TestDashboard {
    /// Test explorer
    pub explorer: TestExplorer,
    /// Watch mode
    pub watch: WatchMode,
    /// Coverage tracker
    pub coverage: CoverageTracker,
    /// Current test run
    pub current_run: Option<TestRun>,
    /// Run history
    pub history: Vec<TestRun>,
    /// Sparkline
    pub sparkline: TestSparkline,
    /// Max history
    max_history: usize,
}

impl Default for TestDashboard {
    fn default() -> Self {
        Self::new()
    }
}

impl TestDashboard {
    /// Create new dashboard
    pub fn new() -> Self {
        Self {
            explorer: TestExplorer::new(),
            watch: WatchMode::new(),
            coverage: CoverageTracker::new(),
            current_run: None,
            history: Vec::new(),
            sparkline: TestSparkline::new(20),
            max_history: 50,
        }
    }

    /// Start a new test run
    pub fn start_run(&mut self) -> &mut TestRun {
        self.current_run = Some(TestRun::new());
        self.current_run.as_mut().unwrap()
    }

    /// Complete current run
    pub fn complete_run(&mut self) {
        if let Some(mut run) = self.current_run.take() {
            run.complete();

            // Update sparkline
            let summary = run.summary();
            self.sparkline.add(summary.success_rate());

            // Add to history
            self.history.push(run);
            if self.history.len() > self.max_history {
                self.history.remove(0);
            }
        }
    }

    /// Get last run
    pub fn last_run(&self) -> Option<&TestRun> {
        self.history.last()
    }

    /// Get failure streak (consecutive failing runs)
    pub fn failure_streak(&self) -> usize {
        self.history
            .iter()
            .rev()
            .take_while(|r| r.status == RunStatus::Failed)
            .count()
    }

    /// Get success streak
    pub fn success_streak(&self) -> usize {
        self.history
            .iter()
            .rev()
            .take_while(|r| r.status == RunStatus::Passed)
            .count()
    }

    /// Update explorer with test results
    pub fn update_explorer(&mut self, tests: &[Test]) {
        self.explorer.build_tree(tests);
    }
}

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

    #[test]
    fn test_test_status_icons() {
        assert_eq!(TestStatus::Pending.icon(), "");
        assert_eq!(TestStatus::Running.icon(), "");
        assert_eq!(TestStatus::Passed.icon(), "");
        assert_eq!(TestStatus::Failed.icon(), "");
        assert_eq!(TestStatus::Ignored.icon(), "");
        assert_eq!(TestStatus::Skipped.icon(), "");
    }

    #[test]
    fn test_test_status_colors() {
        assert!(TestStatus::Passed.color().contains("32"));
        assert!(TestStatus::Failed.color().contains("31"));
    }

    #[test]
    fn test_test_status_is_final() {
        assert!(!TestStatus::Pending.is_final());
        assert!(!TestStatus::Running.is_final());
        assert!(TestStatus::Passed.is_final());
        assert!(TestStatus::Failed.is_final());
        assert!(TestStatus::Ignored.is_final());
    }

    #[test]
    fn test_test_status_default() {
        let s: TestStatus = Default::default();
        assert_eq!(s, TestStatus::Pending);
    }

    #[test]
    fn test_test_status_display() {
        assert_eq!(format!("{}", TestStatus::Passed), "Passed");
        assert_eq!(format!("{}", TestStatus::Failed), "Failed");
    }

    #[test]
    fn test_test_creation() {
        let t = Test::new("module::submodule::test_name".to_string());
        assert_eq!(t.function, "test_name");
        assert_eq!(t.module, "module::submodule");
        assert_eq!(t.status, TestStatus::Pending);
    }

    #[test]
    fn test_test_with_location() {
        let t = Test::new("test".to_string()).with_location(PathBuf::from("test.rs"), 42);
        assert_eq!(t.file, Some(PathBuf::from("test.rs")));
        assert_eq!(t.line, Some(42));
    }

    #[test]
    fn test_test_lifecycle() {
        let mut t = Test::new("test".to_string());
        t.start();
        assert_eq!(t.status, TestStatus::Running);

        t.pass(Duration::milliseconds(100));
        assert_eq!(t.status, TestStatus::Passed);
        assert!(t.duration.is_some());
    }

    #[test]
    fn test_test_failure() {
        let mut t = Test::new("test".to_string());
        t.fail(Duration::milliseconds(50), "assertion failed".to_string());
        assert_eq!(t.status, TestStatus::Failed);
        assert!(t.failure_message.is_some());
    }

    #[test]
    fn test_test_ignore() {
        let mut t = Test::new("test".to_string());
        t.ignore();
        assert_eq!(t.status, TestStatus::Ignored);
        assert!(t.ignored);
    }

    #[test]
    fn test_test_display() {
        let mut t = Test::new("module::test_fn".to_string());
        t.pass(Duration::seconds(1));
        let display = t.display();
        assert!(display.contains(""));
        assert!(display.contains("module::test_fn"));
    }

    #[test]
    fn test_test_short_display() {
        let t = Test::new("module::test_fn".to_string());
        let display = t.short_display();
        assert!(display.contains("test_fn"));
    }

    #[test]
    fn test_test_suite_creation() {
        let suite = TestSuite::new("test_suite".to_string());
        assert_eq!(suite.name, "test_suite");
        assert_eq!(suite.total, 0);
    }

    #[test]
    fn test_test_suite_add_test() {
        let mut suite = TestSuite::new("suite".to_string());
        let mut t = Test::new("test".to_string());
        t.pass(Duration::milliseconds(10));
        suite.add_test(t);

        assert_eq!(suite.total, 1);
        assert_eq!(suite.passed, 1);
    }

    #[test]
    fn test_test_suite_add_child() {
        let mut parent = TestSuite::new("parent".to_string());
        let mut child = TestSuite::new("child".to_string());
        child.add_test(Test::new("test".to_string()));
        parent.add_child(child);

        assert_eq!(parent.total, 1);
    }

    #[test]
    fn test_test_suite_all_tests() {
        let mut suite = TestSuite::new("suite".to_string());
        suite.add_test(Test::new("t1".to_string()));
        suite.add_test(Test::new("t2".to_string()));

        let all = suite.all_tests();
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn test_test_suite_failed_tests() {
        let mut suite = TestSuite::new("suite".to_string());
        let mut t1 = Test::new("t1".to_string());
        t1.pass(Duration::milliseconds(10));
        suite.add_test(t1);

        let mut t2 = Test::new("t2".to_string());
        t2.fail(Duration::milliseconds(10), "failed".to_string());
        suite.add_test(t2);

        assert_eq!(suite.failed_tests().len(), 1);
        assert_eq!(suite.passed_tests().len(), 1);
    }

    #[test]
    fn test_test_suite_is_passing() {
        let mut suite = TestSuite::new("suite".to_string());
        let mut t = Test::new("t".to_string());
        t.pass(Duration::milliseconds(10));
        suite.add_test(t);

        assert!(suite.is_passing());
    }

    #[test]
    fn test_test_suite_progress() {
        let mut suite = TestSuite::new("suite".to_string());
        let mut t = Test::new("t".to_string());
        t.pass(Duration::milliseconds(10));
        suite.add_test(t);
        suite.add_test(Test::new("t2".to_string())); // Pending

        assert_eq!(suite.progress(), 50.0);
    }

    #[test]
    fn test_test_suite_summary() {
        let suite = TestSuite::new("suite".to_string());
        let summary = suite.summary();
        assert!(summary.contains("suite"));
        assert!(summary.contains("total"));
    }

    #[test]
    fn test_test_run_creation() {
        let run = TestRun::new();
        assert_eq!(run.status, RunStatus::Running);
        assert!(run.ended_at.is_none());
    }

    #[test]
    fn test_test_run_add_result() {
        let mut run = TestRun::new();
        run.add_result(Test::new("test".to_string()));
        assert_eq!(run.results.len(), 1);
    }

    #[test]
    fn test_test_run_complete() {
        let mut run = TestRun::new();
        let mut t = Test::new("test".to_string());
        t.pass(Duration::milliseconds(10));
        run.add_result(t);
        run.complete();

        assert_eq!(run.status, RunStatus::Passed);
        assert!(run.ended_at.is_some());
    }

    #[test]
    fn test_test_run_complete_with_failures() {
        let mut run = TestRun::new();
        let mut t = Test::new("test".to_string());
        t.fail(Duration::milliseconds(10), "failed".to_string());
        run.add_result(t);
        run.complete();

        assert_eq!(run.status, RunStatus::Failed);
    }

    #[test]
    fn test_test_run_cancel() {
        let mut run = TestRun::new();
        run.cancel();
        assert_eq!(run.status, RunStatus::Cancelled);
    }

    #[test]
    fn test_test_run_summary() {
        let mut run = TestRun::new();
        let mut t = Test::new("test".to_string());
        t.pass(Duration::milliseconds(10));
        run.add_result(t);
        run.complete();

        let summary = run.summary();
        assert_eq!(summary.total, 1);
        assert_eq!(summary.passed, 1);
    }

    #[test]
    fn test_test_run_summary_success_rate() {
        let summary = TestRunSummary {
            total: 10,
            passed: 8,
            failed: 2,
            ignored: 0,
            duration: None,
        };
        assert_eq!(summary.success_rate(), 80.0);
    }

    #[test]
    fn test_test_run_summary_display() {
        let summary = TestRunSummary {
            total: 10,
            passed: 8,
            failed: 2,
            ignored: 0,
            duration: Some(Duration::seconds(1)),
        };
        let display = summary.display();
        assert!(display.contains("8 passed"));
        assert!(display.contains("2 failed"));
    }

    #[test]
    fn test_test_tree_node_module() {
        let node = TestTreeNode::module("module".to_string(), "module".to_string());
        assert!(!node.is_test);
        assert!(node.status.is_none());
    }

    #[test]
    fn test_test_tree_node_test() {
        let node = TestTreeNode::test(
            "test".to_string(),
            "module::test".to_string(),
            TestStatus::Passed,
        );
        assert!(node.is_test);
        assert_eq!(node.status, Some(TestStatus::Passed));
    }

    #[test]
    fn test_test_tree_node_add_child() {
        let mut node = TestTreeNode::module("mod".to_string(), "mod".to_string());
        node.add_child(TestTreeNode::test(
            "test".to_string(),
            "mod::test".to_string(),
            TestStatus::Pending,
        ));
        assert_eq!(node.children.len(), 1);
    }

    #[test]
    fn test_test_tree_node_find_or_create() {
        let mut root = TestTreeNode::module(String::new(), String::new());
        let _ = root.find_or_create(&["a", "b", "c"]);
        assert!(!root.children.is_empty());
    }

    #[test]
    fn test_test_tree_node_toggle() {
        let mut node = TestTreeNode::module("mod".to_string(), "mod".to_string());
        assert!(node.expanded);
        node.toggle();
        assert!(!node.expanded);
    }

    #[test]
    fn test_test_tree_node_test_count() {
        let mut node = TestTreeNode::module("mod".to_string(), "mod".to_string());
        node.add_child(TestTreeNode::test(
            "t1".to_string(),
            "mod::t1".to_string(),
            TestStatus::Passed,
        ));
        node.add_child(TestTreeNode::test(
            "t2".to_string(),
            "mod::t2".to_string(),
            TestStatus::Failed,
        ));
        assert_eq!(node.test_count(), 2);
    }

    #[test]
    fn test_test_tree_node_failed_count() {
        let mut node = TestTreeNode::module("mod".to_string(), "mod".to_string());
        node.add_child(TestTreeNode::test(
            "t1".to_string(),
            "mod::t1".to_string(),
            TestStatus::Passed,
        ));
        node.add_child(TestTreeNode::test(
            "t2".to_string(),
            "mod::t2".to_string(),
            TestStatus::Failed,
        ));
        assert_eq!(node.failed_count(), 1);
    }

    #[test]
    fn test_test_tree_node_aggregate_status() {
        let mut node = TestTreeNode::module("mod".to_string(), "mod".to_string());
        node.add_child(TestTreeNode::test(
            "t1".to_string(),
            "mod::t1".to_string(),
            TestStatus::Passed,
        ));
        assert_eq!(node.aggregate_status(), TestStatus::Passed);
    }

    #[test]
    fn test_test_tree_node_aggregate_failed() {
        let mut node = TestTreeNode::module("mod".to_string(), "mod".to_string());
        node.add_child(TestTreeNode::test(
            "t1".to_string(),
            "mod::t1".to_string(),
            TestStatus::Failed,
        ));
        assert_eq!(node.aggregate_status(), TestStatus::Failed);
    }

    #[test]
    fn test_test_explorer_new() {
        let explorer = TestExplorer::new();
        assert!(explorer.selected().is_none());
    }

    #[test]
    fn test_test_explorer_build_tree() {
        let mut explorer = TestExplorer::new();
        explorer.build_tree(&[
            Test::new("mod::test1".to_string()),
            Test::new("mod::test2".to_string()),
        ]);
        let visible = explorer.visible_nodes();
        assert!(!visible.is_empty());
    }

    #[test]
    fn test_test_explorer_filter() {
        let mut explorer = TestExplorer::new();
        explorer.build_tree(&[
            Test::new("mod::test1".to_string()),
            Test::new("other::test2".to_string()),
        ]);
        explorer.set_filter(Some("mod".to_string()));
        // Filter is applied
    }

    #[test]
    fn test_test_explorer_toggle_failed_only() {
        let mut explorer = TestExplorer::new();
        assert!(!explorer.show_failed_only);
        explorer.toggle_failed_only();
        assert!(explorer.show_failed_only);
    }

    #[test]
    fn test_test_explorer_select() {
        let mut explorer = TestExplorer::new();
        explorer.select("mod::test".to_string());
        assert_eq!(explorer.selected(), Some("mod::test"));
    }

    #[test]
    fn test_watch_mode_new() {
        let watch = WatchMode::new();
        assert!(!watch.is_active());
    }

    #[test]
    fn test_watch_mode_start_stop() {
        let mut watch = WatchMode::new();
        watch.start();
        assert!(watch.is_active());
        watch.stop();
        assert!(!watch.is_active());
    }

    #[test]
    fn test_watch_mode_debounce() {
        let watch = WatchMode::new().with_debounce(1000);
        assert_eq!(watch.debounce, Duration::milliseconds(1000));
    }

    #[test]
    fn test_watch_mode_file_changed() {
        let mut watch = WatchMode::new();
        watch.start();
        watch.file_changed(PathBuf::from("test.rs"));
        assert!(!watch.pending_changes.is_empty());
    }

    #[test]
    fn test_watch_mode_should_run() {
        let mut watch = WatchMode::new();
        watch.start();
        watch.file_changed(PathBuf::from("test.rs"));
        assert!(watch.should_run());
    }

    #[test]
    fn test_watch_mode_take_changes() {
        let mut watch = WatchMode::new();
        watch.start();
        watch.file_changed(PathBuf::from("test.rs"));
        let changes = watch.take_changes();
        assert!(!changes.is_empty());
        assert!(watch.pending_changes.is_empty());
    }

    #[test]
    fn test_watch_mode_patterns() {
        let mut watch = WatchMode::new();
        watch.add_pattern("test_".to_string());
        assert_eq!(watch.patterns().len(), 1);
        watch.clear_patterns();
        assert!(watch.patterns().is_empty());
    }

    #[test]
    fn test_watch_mode_toggle_run_on_save() {
        let mut watch = WatchMode::new();
        assert!(watch.run_on_save);
        watch.toggle_run_on_save();
        assert!(!watch.run_on_save);
    }

    #[test]
    fn test_file_coverage_new() {
        let cov = FileCoverage::new(PathBuf::from("test.rs"));
        assert_eq!(cov.path, PathBuf::from("test.rs"));
        assert!(cov.covered_lines.is_empty());
    }

    #[test]
    fn test_file_coverage_cover_line() {
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(10);
        assert!(cov.covered_lines.contains(&10));
        assert!(cov.executable_lines.contains(&10));
    }

    #[test]
    fn test_file_coverage_add_uncovered() {
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.add_uncovered(5);
        assert!(cov.uncovered_lines.contains(&5));
    }

    #[test]
    fn test_file_coverage_percentage() {
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(1);
        cov.cover_line(2);
        cov.add_uncovered(3);
        cov.add_uncovered(4);

        assert_eq!(cov.percentage(), 50.0);
    }

    #[test]
    fn test_file_coverage_is_covered() {
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(1);
        cov.add_uncovered(2);

        assert_eq!(cov.is_covered(1), Some(true));
        assert_eq!(cov.is_covered(2), Some(false));
        assert_eq!(cov.is_covered(3), None);
    }

    #[test]
    fn test_file_coverage_gutter() {
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(1);
        cov.add_uncovered(2);

        assert_eq!(cov.gutter(1), "");
        assert_eq!(cov.gutter(2), "");
        assert_eq!(cov.gutter(3), " ");
    }

    #[test]
    fn test_coverage_tracker_new() {
        let tracker = CoverageTracker::new();
        assert_eq!(tracker.file_count(), 0);
    }

    #[test]
    fn test_coverage_tracker_add_file() {
        let mut tracker = CoverageTracker::new();
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(1);
        tracker.add_file(cov);

        assert_eq!(tracker.file_count(), 1);
        assert!(tracker.get(Path::new("test.rs")).is_some());
    }

    #[test]
    fn test_coverage_tracker_total_percentage() {
        let mut tracker = CoverageTracker::new();
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(1);
        cov.add_uncovered(2);
        tracker.add_file(cov);

        assert_eq!(tracker.total_percentage(), 50.0);
    }

    #[test]
    fn test_coverage_tracker_below_threshold() {
        let mut tracker = CoverageTracker::new();
        let mut cov = FileCoverage::new(PathBuf::from("test.rs"));
        cov.cover_line(1);
        cov.add_uncovered(2);
        cov.add_uncovered(3);
        cov.add_uncovered(4);
        tracker.add_file(cov);

        let below = tracker.below_threshold(50.0);
        assert_eq!(below.len(), 1);
    }

    #[test]
    fn test_coverage_tracker_sorted() {
        let mut tracker = CoverageTracker::new();

        let mut cov1 = FileCoverage::new(PathBuf::from("a.rs"));
        cov1.cover_line(1);
        cov1.add_uncovered(2);
        tracker.add_file(cov1);

        let mut cov2 = FileCoverage::new(PathBuf::from("b.rs"));
        cov2.cover_line(1);
        tracker.add_file(cov2);

        let sorted = tracker.sorted_by_coverage();
        assert_eq!(sorted.len(), 2);
        assert!(sorted[0].percentage() <= sorted[1].percentage());
    }

    #[test]
    fn test_coverage_tracker_clear() {
        let mut tracker = CoverageTracker::new();
        tracker.add_file(FileCoverage::new(PathBuf::from("test.rs")));
        tracker.clear();
        assert_eq!(tracker.file_count(), 0);
    }

    #[test]
    fn test_test_progress_new() {
        let progress = TestProgress::new(10);
        assert_eq!(progress.total, 10);
        assert_eq!(progress.completed, 0);
    }

    #[test]
    fn test_test_progress_update() {
        let mut progress = TestProgress::new(10);
        progress.update(true);
        progress.update(false);

        assert_eq!(progress.completed, 2);
        assert_eq!(progress.passed, 1);
        assert_eq!(progress.failed, 1);
    }

    #[test]
    fn test_test_progress_percentage() {
        let mut progress = TestProgress::new(10);
        progress.update(true);
        progress.update(true);

        assert_eq!(progress.percentage(), 20.0);
    }

    #[test]
    fn test_test_progress_render() {
        let mut progress = TestProgress::new(10);
        progress.update(true);
        let render = progress.render();
        assert!(render.contains(""));
        assert!(render.contains("passed"));
    }

    #[test]
    fn test_test_progress_is_complete() {
        let mut progress = TestProgress::new(2);
        assert!(!progress.is_complete());
        progress.update(true);
        progress.update(true);
        assert!(progress.is_complete());
    }

    #[test]
    fn test_test_progress_is_passing() {
        let mut progress = TestProgress::new(2);
        progress.update(true);
        assert!(progress.is_passing());
        progress.update(false);
        assert!(!progress.is_passing());
    }

    #[test]
    fn test_test_sparkline_new() {
        let sparkline = TestSparkline::new(10);
        assert!(sparkline.is_empty());
    }

    #[test]
    fn test_test_sparkline_add() {
        let mut sparkline = TestSparkline::new(5);
        sparkline.add(100.0);
        sparkline.add(50.0);
        assert_eq!(sparkline.len(), 2);
    }

    #[test]
    fn test_test_sparkline_max_points() {
        let mut sparkline = TestSparkline::new(3);
        sparkline.add(100.0);
        sparkline.add(90.0);
        sparkline.add(80.0);
        sparkline.add(70.0);
        assert_eq!(sparkline.len(), 3);
    }

    #[test]
    fn test_test_sparkline_render() {
        let mut sparkline = TestSparkline::new(5);
        sparkline.add(100.0);
        sparkline.add(50.0);
        sparkline.add(0.0);
        let render = sparkline.render();
        assert!(!render.is_empty());
    }

    #[test]
    fn test_test_sparkline_trend() {
        let mut sparkline = TestSparkline::new(10);
        // Stable
        assert_eq!(sparkline.trend(), Trend::Stable);

        // Improving
        for i in 0..6 {
            sparkline.add(50.0 + i as f32 * 10.0);
        }
        assert_eq!(sparkline.trend(), Trend::Improving);
    }

    #[test]
    fn test_test_sparkline_declining_trend() {
        let mut sparkline = TestSparkline::new(10);
        for i in 0..6 {
            sparkline.add(100.0 - i as f32 * 15.0);
        }
        assert_eq!(sparkline.trend(), Trend::Declining);
    }

    #[test]
    fn test_trend_icon() {
        assert_eq!(Trend::Improving.icon(), "");
        assert_eq!(Trend::Stable.icon(), "");
        assert_eq!(Trend::Declining.icon(), "");
    }

    #[test]
    fn test_trend_color() {
        assert!(Trend::Improving.color().contains("32"));
        assert!(Trend::Declining.color().contains("31"));
    }

    #[test]
    fn test_test_dashboard_new() {
        let dashboard = TestDashboard::new();
        assert!(dashboard.current_run.is_none());
        assert!(dashboard.history.is_empty());
    }

    #[test]
    fn test_test_dashboard_start_run() {
        let mut dashboard = TestDashboard::new();
        let run = dashboard.start_run();
        assert_eq!(run.status, RunStatus::Running);
    }

    #[test]
    fn test_test_dashboard_complete_run() {
        let mut dashboard = TestDashboard::new();
        {
            let run = dashboard.start_run();
            let mut t = Test::new("test".to_string());
            t.pass(Duration::milliseconds(10));
            run.add_result(t);
        }
        dashboard.complete_run();

        assert!(dashboard.current_run.is_none());
        assert_eq!(dashboard.history.len(), 1);
    }

    #[test]
    fn test_test_dashboard_last_run() {
        let mut dashboard = TestDashboard::new();
        dashboard.start_run();
        dashboard.complete_run();

        assert!(dashboard.last_run().is_some());
    }

    #[test]
    fn test_test_dashboard_streaks() {
        let mut dashboard = TestDashboard::new();

        // Add passing runs
        for _ in 0..3 {
            let run = dashboard.start_run();
            let mut t = Test::new("test".to_string());
            t.pass(Duration::milliseconds(10));
            run.add_result(t);
            dashboard.complete_run();
        }

        assert_eq!(dashboard.success_streak(), 3);
        assert_eq!(dashboard.failure_streak(), 0);
    }

    #[test]
    fn test_test_dashboard_update_explorer() {
        let mut dashboard = TestDashboard::new();
        dashboard.update_explorer(&[
            Test::new("mod::test1".to_string()),
            Test::new("mod::test2".to_string()),
        ]);
        // Explorer should have nodes
        let visible = dashboard.explorer.visible_nodes();
        assert!(!visible.is_empty());
    }
}