tldr-cli 0.1.5

CLI binary for TLDR code analysis tool
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
//! Comprehensive tests for TLDR Daemon and Cache commands
//!
//! These tests define expected behavior from spec.md and should FAIL initially
//! since no implementation exists yet. They drive the implementation.
//!
//! Test categories:
//! 1. Unit Tests - Types & Serialization
//! 2. Daemon Lifecycle Tests
//! 3. IPC Protocol Tests
//! 4. Cache Tests
//! 5. Warm Command Tests
//! 6. Stats Command Tests
//! 7. Edge Case Tests

use assert_cmd::prelude::*;
use assert_cmd::Command as AssertCommand;
use predicates::prelude::*;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::time::Duration;
use tempfile::TempDir;

/// Get the path to the test binary (std::process::Command version)
fn tldr_cmd() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("tldr"))
}

/// Get the path to the test binary (assert_cmd::Command version for timeout support)
fn tldr_assert_cmd() -> AssertCommand {
    assert_cmd::cargo::cargo_bin_cmd!("tldr")
}

fn cleanup_daemon(project_path: &str) {
    let mut stop_cmd = tldr_cmd();
    let _ = stop_cmd
        .args(["daemon", "stop", "--project", project_path])
        .assert();
}

/// Get home directory (cross-platform)
fn home_dir() -> PathBuf {
    env::var("HOME")
        .or_else(|_| env::var("USERPROFILE"))
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from("/tmp"))
}

// =============================================================================
// Module: Types (to be implemented in daemon/types.rs)
// =============================================================================

/// These types mirror the spec and will be imported once implemented.
/// For now, we define them inline to make tests compilable.
mod daemon_types {
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;
    use std::path::PathBuf;

    /// Idle timeout before daemon auto-shutdown (30 minutes)
    pub const IDLE_TIMEOUT_SECS: u64 = 30 * 60;

    /// Default threshold for triggering semantic re-index
    pub const DEFAULT_REINDEX_THRESHOLD: usize = 20;

    /// Daemon configuration
    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    pub struct DaemonConfig {
        pub semantic_enabled: bool,
        pub auto_reindex_threshold: usize,
        pub semantic_model: String,
        pub idle_timeout_secs: u64,
    }

    impl Default for DaemonConfig {
        fn default() -> Self {
            Self {
                semantic_enabled: true,
                auto_reindex_threshold: DEFAULT_REINDEX_THRESHOLD,
                semantic_model: "bge-large-en-v1.5".to_string(),
                idle_timeout_secs: IDLE_TIMEOUT_SECS,
            }
        }
    }

    /// Daemon runtime status
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(rename_all = "snake_case")]
    pub enum DaemonStatus {
        Initializing,
        Indexing,
        Ready,
        ShuttingDown,
        Stopped,
    }

    /// Statistics for Salsa-style query cache
    #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
    pub struct SalsaCacheStats {
        pub hits: u64,
        pub misses: u64,
        pub invalidations: u64,
        pub recomputations: u64,
    }

    impl SalsaCacheStats {
        pub fn hit_rate(&self) -> f64 {
            let total = self.hits + self.misses;
            if total == 0 {
                return 0.0;
            }
            (self.hits as f64 / total as f64) * 100.0
        }
    }

    /// Per-session statistics for token tracking
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct SessionStats {
        pub session_id: String,
        pub raw_tokens: u64,
        pub tldr_tokens: u64,
        pub requests: u64,
    }

    impl SessionStats {
        pub fn savings_tokens(&self) -> i64 {
            self.raw_tokens as i64 - self.tldr_tokens as i64
        }

        pub fn savings_percent(&self) -> f64 {
            if self.raw_tokens == 0 {
                return 0.0;
            }
            (self.savings_tokens() as f64 / self.raw_tokens as f64) * 100.0
        }
    }

    /// Command sent to daemon via socket
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[serde(tag = "cmd", rename_all = "snake_case")]
    pub enum DaemonCommand {
        Ping,
        Status {
            #[serde(skip_serializing_if = "Option::is_none")]
            session: Option<String>,
        },
        Shutdown,
        Notify {
            file: PathBuf,
        },
        Track {
            hook: String,
            #[serde(default = "default_true")]
            success: bool,
            #[serde(default)]
            metrics: HashMap<String, f64>,
        },
        Warm {
            #[serde(default)]
            language: Option<String>,
        },
        Semantic {
            query: String,
            #[serde(default = "default_top_k")]
            top_k: usize,
        },
        Search {
            pattern: String,
            max_results: Option<usize>,
        },
        Extract {
            file: PathBuf,
            session: Option<String>,
        },
    }

    fn default_true() -> bool {
        true
    }
    fn default_top_k() -> usize {
        10
    }

    /// Response from daemon
    ///
    /// IMPORTANT: Variant order matters for serde(untagged)!
    /// Variants are tried in declaration order, so more specific variants
    /// (with more required fields) must come BEFORE less specific ones.
    ///
    /// Key design: Error uses "error" field, Status uses "message" field.
    /// This makes them structurally distinguishable for serde untagged.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[serde(untagged)]
    pub enum DaemonResponse {
        // FullStatus has 5 required fields including a typed enum status
        FullStatus {
            status: DaemonStatus,
            uptime: f64,
            files: usize,
            project: PathBuf,
            salsa_stats: SalsaCacheStats,
        },
        // NotifyResponse has 4 required fields
        NotifyResponse {
            status: String,
            dirty_count: usize,
            threshold: usize,
            reindex_triggered: bool,
        },
        // Error uses "error" field (not "message") to be distinguishable from Status
        Error {
            status: String,
            error: String,
        },
        // Status is the catch-all with only 1 required field (message is optional)
        Status {
            status: String,
            #[serde(skip_serializing_if = "Option::is_none")]
            message: Option<String>,
        },
    }

    /// Aggregated global stats
    #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
    pub struct GlobalStats {
        pub total_invocations: u64,
        pub estimated_tokens_saved: i64,
        pub raw_tokens_total: u64,
        pub tldr_tokens_total: u64,
        pub savings_percent: f64,
    }

    /// Cache file info for cache stats
    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    pub struct CacheFileInfo {
        pub file_count: usize,
        pub total_bytes: u64,
        pub total_size_human: String,
    }
}

use daemon_types::*;

// =============================================================================
// 1. Unit Tests - Types & Serialization
// =============================================================================

mod unit_types {
    use super::*;

    #[test]
    fn test_daemon_config_default() {
        let config = DaemonConfig::default();

        assert!(config.semantic_enabled);
        assert_eq!(config.auto_reindex_threshold, DEFAULT_REINDEX_THRESHOLD);
        assert_eq!(config.semantic_model, "bge-large-en-v1.5");
        assert_eq!(config.idle_timeout_secs, IDLE_TIMEOUT_SECS);
    }

    #[test]
    fn test_daemon_config_serialization() {
        let config = DaemonConfig::default();
        let json = serde_json::to_string(&config).unwrap();

        assert!(json.contains("semantic_enabled"));
        assert!(json.contains("auto_reindex_threshold"));
        assert!(json.contains("20")); // DEFAULT_REINDEX_THRESHOLD
    }

    #[test]
    fn test_daemon_config_deserialization() {
        let json = r#"{
            "semantic_enabled": false,
            "auto_reindex_threshold": 50,
            "semantic_model": "custom-model",
            "idle_timeout_secs": 3600
        }"#;

        let config: DaemonConfig = serde_json::from_str(json).unwrap();

        assert!(!config.semantic_enabled);
        assert_eq!(config.auto_reindex_threshold, 50);
        assert_eq!(config.semantic_model, "custom-model");
        assert_eq!(config.idle_timeout_secs, 3600);
    }

    #[test]
    fn test_daemon_command_ping_serialization() {
        let cmd = DaemonCommand::Ping;
        let json = serde_json::to_string(&cmd).unwrap();

        assert_eq!(json, r#"{"cmd":"ping"}"#);
    }

    #[test]
    fn test_daemon_command_status_serialization() {
        let cmd = DaemonCommand::Status { session: None };
        let json = serde_json::to_string(&cmd).unwrap();

        assert_eq!(json, r#"{"cmd":"status"}"#);
    }

    #[test]
    fn test_daemon_command_status_with_session() {
        let cmd = DaemonCommand::Status {
            session: Some("abc123".to_string()),
        };
        let json = serde_json::to_string(&cmd).unwrap();

        assert!(json.contains("abc123"));
    }

    #[test]
    fn test_daemon_command_notify_serialization() {
        let cmd = DaemonCommand::Notify {
            file: PathBuf::from("/path/to/file.rs"),
        };
        let json = serde_json::to_string(&cmd).unwrap();

        assert!(json.contains("notify"));
        assert!(json.contains("/path/to/file.rs"));
    }

    #[test]
    fn test_daemon_command_track_serialization() {
        let mut metrics = HashMap::new();
        metrics.insert("errors_found".to_string(), 3.0);

        let cmd = DaemonCommand::Track {
            hook: "pre-commit".to_string(),
            success: true,
            metrics,
        };
        let json = serde_json::to_string(&cmd).unwrap();

        assert!(json.contains("track"));
        assert!(json.contains("pre-commit"));
        assert!(json.contains("errors_found"));
    }

    #[test]
    fn test_daemon_response_status_deserialization() {
        let json = r#"{"status": "ok", "message": "Daemon started"}"#;
        let response: DaemonResponse = serde_json::from_str(json).unwrap();

        match response {
            DaemonResponse::Status { status, message } => {
                assert_eq!(status, "ok");
                assert_eq!(message, Some("Daemon started".to_string()));
            }
            _ => panic!("Expected Status response"),
        }
    }

    #[test]
    fn test_daemon_response_notify_deserialization() {
        let json = r#"{
            "status": "ok",
            "dirty_count": 5,
            "threshold": 20,
            "reindex_triggered": false
        }"#;
        let response: DaemonResponse = serde_json::from_str(json).unwrap();

        match response {
            DaemonResponse::NotifyResponse {
                dirty_count,
                threshold,
                reindex_triggered,
                ..
            } => {
                assert_eq!(dirty_count, 5);
                assert_eq!(threshold, 20);
                assert!(!reindex_triggered);
            }
            _ => panic!("Expected NotifyResponse"),
        }
    }

    #[test]
    fn test_daemon_response_error_deserialization() {
        // Error variant uses "error" field (not "message") to be distinguishable
        let json = r#"{"status": "error", "error": "Something went wrong"}"#;
        let response: DaemonResponse = serde_json::from_str(json).unwrap();

        match response {
            DaemonResponse::Error { status, error } => {
                assert_eq!(status, "error");
                assert_eq!(error, "Something went wrong");
            }
            _ => panic!("Expected Error response, got {:?}", response),
        }
    }

    #[test]
    fn test_daemon_response_status_only_deserialization() {
        // Status-only JSON should match Status variant (catch-all)
        let json = r#"{"status": "ok"}"#;
        let response: DaemonResponse = serde_json::from_str(json).unwrap();

        match response {
            DaemonResponse::Status { status, message } => {
                assert_eq!(status, "ok");
                assert_eq!(message, None);
            }
            _ => panic!("Expected Status response"),
        }
    }

    #[test]
    fn test_salsa_cache_stats_hit_rate_empty() {
        let stats = SalsaCacheStats::default();
        assert_eq!(stats.hit_rate(), 0.0);
    }

    #[test]
    fn test_salsa_cache_stats_hit_rate_calculation() {
        let stats = SalsaCacheStats {
            hits: 90,
            misses: 10,
            invalidations: 5,
            recomputations: 3,
        };
        assert!((stats.hit_rate() - 90.0).abs() < 0.01);
    }

    #[test]
    fn test_session_stats_savings_calculation() {
        let stats = SessionStats {
            session_id: "test123".to_string(),
            raw_tokens: 1000,
            tldr_tokens: 100,
            requests: 10,
        };

        assert_eq!(stats.savings_tokens(), 900);
        assert!((stats.savings_percent() - 90.0).abs() < 0.01);
    }

    #[test]
    fn test_session_stats_zero_tokens() {
        let stats = SessionStats {
            session_id: "empty".to_string(),
            raw_tokens: 0,
            tldr_tokens: 0,
            requests: 0,
        };

        assert_eq!(stats.savings_tokens(), 0);
        assert_eq!(stats.savings_percent(), 0.0);
    }

    #[test]
    fn test_daemon_status_serialization() {
        let status = DaemonStatus::Ready;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""ready""#);

        let status = DaemonStatus::Initializing;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""initializing""#);
    }

    #[test]
    fn test_global_stats_serialization() {
        let stats = GlobalStats {
            total_invocations: 12,
            estimated_tokens_saved: 345,
            raw_tokens_total: 1_000,
            tldr_tokens_total: 655,
            savings_percent: 34.5,
        };
        let json = serde_json::to_string(&stats).unwrap();
        let roundtrip: GlobalStats = serde_json::from_str(&json).unwrap();

        assert_eq!(roundtrip.total_invocations, 12);
        assert_eq!(roundtrip.estimated_tokens_saved, 345);
        assert_eq!(roundtrip.raw_tokens_total, 1_000);
        assert_eq!(roundtrip.tldr_tokens_total, 655);
        assert_eq!(roundtrip.savings_percent, 34.5);
    }

    #[test]
    fn test_cache_file_info_serialization() {
        let cache_info = CacheFileInfo {
            file_count: 3,
            total_bytes: 4_096,
            total_size_human: "4.0 KiB".to_string(),
        };
        let json = serde_json::to_string(&cache_info).unwrap();
        let roundtrip: CacheFileInfo = serde_json::from_str(&json).unwrap();

        assert_eq!(roundtrip.file_count, 3);
        assert_eq!(roundtrip.total_bytes, 4_096);
        assert_eq!(roundtrip.total_size_human, "4.0 KiB");
    }
}

// =============================================================================
// 2. Daemon Lifecycle Tests (CLI integration)
// =============================================================================

mod daemon_lifecycle {
    use super::*;

    #[test]
    #[ignore = "daemon start command not yet implemented"]
    fn test_daemon_start_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "start", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--project"))
            .stdout(predicate::str::contains("--foreground"));
    }

    #[test]
    #[ignore = "daemon start command not yet implemented"]
    fn test_daemon_start_creates_socket() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon in foreground mode with timeout
        let mut cmd = tldr_assert_cmd();
        cmd.args(["daemon", "start", "--project", project_path, "--foreground"])
            .timeout(Duration::from_secs(2));

        let output = cmd.output();

        // Verify socket path is mentioned in output
        if let Ok(output) = output {
            let stdout = String::from_utf8_lossy(&output.stdout);
            assert!(
                stdout.contains(".sock") || stdout.contains("socket"),
                "Expected socket path in output"
            );
        }
    }

    #[test]
    #[ignore = "daemon start command not yet implemented"]
    fn test_daemon_start_creates_pid_file() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "start", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("pid").or(predicate::str::contains("PID")));

        // Stop daemon (cleanup)
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon start command not yet implemented"]
    fn test_daemon_start_already_running_error() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start first daemon
        let mut cmd1 = tldr_cmd();
        cmd1.args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Try to start second daemon - should fail
        let mut cmd2 = tldr_cmd();
        cmd2.args(["daemon", "start", "--project", project_path])
            .assert()
            .failure()
            .stderr(predicate::str::contains("already running"));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon stop command not yet implemented"]
    fn test_daemon_stop_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "stop", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--project"));
    }

    #[test]
    #[ignore = "daemon stop command not yet implemented"]
    fn test_daemon_stop_removes_socket() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Stop daemon
        let mut stop_cmd = tldr_cmd();
        stop_cmd
            .args(["daemon", "stop", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("stopped"));
    }

    #[test]
    #[ignore = "daemon stop command not yet implemented"]
    fn test_daemon_stop_not_running() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Stop daemon when not running - should succeed with message
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "stop", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("not running"));
    }

    #[test]
    #[ignore = "daemon status command not yet implemented"]
    fn test_daemon_status_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "status", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--project"))
            .stdout(predicate::str::contains("--session"));
    }

    #[test]
    #[ignore = "daemon status command not yet implemented"]
    fn test_daemon_status_returns_uptime() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Wait a bit
        std::thread::sleep(Duration::from_millis(500));

        // Check status
        let mut status_cmd = tldr_cmd();
        status_cmd
            .args(["daemon", "status", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("uptime"));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon status command not yet implemented"]
    fn test_daemon_status_not_running() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "status", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("not running"));
    }

    #[test]
    #[ignore = "daemon status command not yet implemented"]
    fn test_daemon_status_json_output() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Get status in JSON format (default format is json)
        let mut status_cmd = tldr_cmd();
        let output = status_cmd
            .args(["daemon", "status", "--project", project_path])
            .output()
            .unwrap();

        let stdout = String::from_utf8_lossy(&output.stdout);

        // Verify JSON structure
        let status: serde_json::Value = serde_json::from_str(&stdout).expect("Valid JSON output");
        assert!(status.get("status").is_some());
        assert!(status.get("uptime").is_some());
        assert!(status.get("files").is_some());

        // Cleanup
        cleanup_daemon(project_path);
    }
}

// =============================================================================
// 3. IPC Protocol Tests
// =============================================================================

mod ipc_protocol {
    use super::*;

    #[test]
    #[ignore = "daemon query command not yet implemented"]
    fn test_daemon_query_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "query", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--project"))
            .stdout(predicate::str::contains("--json"));
    }

    #[test]
    #[ignore = "daemon query command not yet implemented"]
    fn test_daemon_query_ping() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Send ping query
        let mut query_cmd = tldr_cmd();
        query_cmd
            .args(["daemon", "query", "ping", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("pong").or(predicate::str::contains("ok")));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon query command not yet implemented"]
    fn test_daemon_query_roundtrip() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create test file
        fs::write(temp.path().join("test.py"), "def foo(): pass").unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Query structure
        let mut query_cmd = tldr_cmd();
        let output = query_cmd
            .args([
                "daemon",
                "query",
                "structure",
                "--project",
                project_path,
                "--json",
                &format!(r#"{{"path": "{}"}}"#, temp.path().join("test.py").display()),
            ])
            .output()
            .unwrap();

        let stdout = String::from_utf8_lossy(&output.stdout);
        let result: serde_json::Value = serde_json::from_str(&stdout).expect("Valid JSON response");
        assert!(result.get("status").is_some());

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon notify command not yet implemented"]
    fn test_daemon_notify_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "notify", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("FILE"));
    }

    #[test]
    #[ignore = "daemon notify command not yet implemented"]
    fn test_daemon_notify_tracks_dirty_files() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create test file
        let test_file = temp.path().join("test.py");
        fs::write(&test_file, "def foo(): pass").unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Notify about file change
        let mut notify_cmd = tldr_cmd();
        notify_cmd
            .args([
                "daemon",
                "notify",
                test_file.to_str().unwrap(),
                "--project",
                project_path,
            ])
            .assert()
            .success()
            .stdout(predicate::str::contains("dirty_count").or(predicate::str::contains("1/20")));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon notify command not yet implemented"]
    fn test_daemon_notify_triggers_reindex_at_threshold() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Notify about multiple file changes (threshold is 20)
        for i in 0..21 {
            let test_file = temp.path().join(format!("test{}.py", i));
            fs::write(&test_file, format!("def foo{}(): pass", i)).unwrap();

            let mut notify_cmd = tldr_cmd();
            let output = notify_cmd
                .args([
                    "daemon",
                    "notify",
                    test_file.to_str().unwrap(),
                    "--project",
                    project_path,
                ])
                .output()
                .unwrap();

            // Check if reindex was triggered on the 20th notification
            if i == 20 {
                let stdout = String::from_utf8_lossy(&output.stdout);
                assert!(
                    stdout.contains("reindex_triggered")
                        || stdout.contains("Reindex")
                        || stdout.contains("20/20"),
                    "Expected reindex to be triggered"
                );
            }
        }

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "daemon notify command not yet implemented"]
    fn test_daemon_notify_silent_when_not_running() {
        let temp = TempDir::new().unwrap();
        let test_file = temp.path().join("test.py");
        fs::write(&test_file, "def foo(): pass").unwrap();

        // Notify without daemon running - should exit 0 silently
        let mut cmd = tldr_cmd();
        cmd.args([
            "daemon",
            "notify",
            test_file.to_str().unwrap(),
            "--project",
            temp.path().to_str().unwrap(),
        ])
        .assert()
        .success();
    }
}

// =============================================================================
// 4. Cache Tests
// =============================================================================

mod cache_tests {
    use super::*;

    #[test]
    fn test_cache_stats_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["cache", "stats", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--project"));
    }

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

        let mut cmd = tldr_cmd();
        cmd.args(["cache", "stats", "--project", temp.path().to_str().unwrap()])
            .assert()
            .success()
            .stdout(
                predicate::str::contains("No cache")
                    .or(predicate::str::contains("file_count"))
                    .or(predicate::str::contains("0")),
            );
    }

    #[test]
    #[ignore = "cache stats command not yet implemented"]
    fn test_cache_stats_after_queries() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create test files
        fs::write(temp.path().join("test.py"), "def foo(): pass").unwrap();

        // Start daemon and make some queries
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Make a few queries to populate cache
        for _ in 0..5 {
            let mut query_cmd = tldr_cmd();
            query_cmd
                .args([
                    "daemon",
                    "query",
                    "structure",
                    "--project",
                    project_path,
                    "--json",
                    &format!(r#"{{"path": "{}"}}"#, temp.path().join("test.py").display()),
                ])
                .output()
                .ok();
        }

        // Check cache stats
        let mut stats_cmd = tldr_cmd();
        stats_cmd
            .args(["cache", "stats", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("hits").or(predicate::str::contains("misses")));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    fn test_cache_clear_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["cache", "clear", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--project"));
    }

    #[test]
    fn test_cache_clear_removes_files() {
        let temp = TempDir::new().unwrap();
        let cache_dir = temp.path().join(".tldr/cache");
        fs::create_dir_all(&cache_dir).unwrap();

        // Create some cache files
        fs::write(cache_dir.join("salsa_stats.json"), "{}").unwrap();
        fs::write(cache_dir.join("call_graph.json"), "{}").unwrap();
        fs::write(cache_dir.join("test.pkl"), "").unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["cache", "clear", "--project", temp.path().to_str().unwrap()])
            .assert()
            .success()
            .stdout(predicate::str::contains("cleared").or(predicate::str::contains("removed")));

        // Verify files are gone
        assert!(
            !cache_dir.join("salsa_stats.json").exists(),
            "salsa_stats.json should be removed"
        );
        assert!(
            !cache_dir.join("call_graph.json").exists(),
            "call_graph.json should be removed"
        );
        assert!(
            !cache_dir.join("test.pkl").exists(),
            "test.pkl should be removed"
        );
    }

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

        let mut cmd = tldr_cmd();
        cmd.args(["cache", "clear", "--project", temp.path().to_str().unwrap()])
            .assert()
            .success()
            .stdout(predicate::str::contains("No cache").or(predicate::str::contains("0")));
    }

    #[test]
    #[ignore = "cache invalidation not yet implemented"]
    fn test_cache_invalidation_on_file_change() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();
        let test_file = temp.path().join("test.py");

        // Create initial file
        fs::write(&test_file, "def foo(): pass").unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Query to populate cache
        let mut query1 = tldr_cmd();
        query1
            .args([
                "daemon",
                "query",
                "structure",
                "--project",
                project_path,
                "--json",
                &format!(r#"{{"path": "{}"}}"#, test_file.display()),
            ])
            .output()
            .ok();

        // Modify file
        fs::write(&test_file, "def foo(): return 1\ndef bar(): pass").unwrap();

        // Notify daemon
        let mut notify_cmd = tldr_cmd();
        notify_cmd
            .args([
                "daemon",
                "notify",
                test_file.to_str().unwrap(),
                "--project",
                project_path,
            ])
            .assert()
            .success();

        // Check stats for invalidation
        let mut stats_cmd = tldr_cmd();
        stats_cmd
            .args(["cache", "stats", "--project", project_path])
            .assert()
            .success()
            .stdout(predicate::str::contains("invalidations"));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    fn test_cache_stats_json_output() {
        let temp = TempDir::new().unwrap();
        let cache_dir = temp.path().join(".tldr/cache");
        fs::create_dir_all(&cache_dir).unwrap();

        // Create some cache files (not necessarily valid salsa cache, just for file stats)
        fs::write(cache_dir.join("test_cache.bin"), "test data").unwrap();
        fs::write(cache_dir.join("call_graph.json"), "{}").unwrap();

        let mut cmd = tldr_cmd();
        let output = cmd
            .args(["cache", "stats", "--project", temp.path().to_str().unwrap()])
            .output()
            .unwrap();

        let stdout = String::from_utf8_lossy(&output.stdout);
        let json: serde_json::Value = serde_json::from_str(&stdout).expect("Valid JSON output");

        // Should have cache_files info even without salsa_stats
        assert!(json.get("cache_files").is_some() || json.get("message").is_some());
    }
}

// =============================================================================
// 5. Warm Command Tests
// =============================================================================

mod warm_tests {
    use super::*;

    #[test]
    #[ignore = "warm command not yet implemented"]
    fn test_warm_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["warm", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--background"))
            .stdout(predicate::str::contains("--lang"));
    }

    #[test]
    #[ignore = "warm command not yet implemented"]
    fn test_warm_foreground_builds_cache() {
        let temp = TempDir::new().unwrap();

        // Create some Python files
        fs::write(temp.path().join("main.py"), "def main(): pass").unwrap();
        fs::write(
            temp.path().join("utils.py"),
            "def helper(): pass\ndef util(): pass",
        )
        .unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["warm", temp.path().to_str().unwrap(), "--lang", "python"])
            .assert()
            .success()
            .stdout(predicate::str::contains("Indexed").or(predicate::str::contains("files")))
            .stdout(predicate::str::contains("edges").or(predicate::str::contains("call")));

        // Verify cache file was created
        let cache_file = temp.path().join(".tldr/cache/call_graph.json");
        assert!(cache_file.exists(), "call_graph.json should be created");
    }

    #[test]
    #[ignore = "warm command not yet implemented"]
    fn test_warm_background_spawns_task() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join("main.py"), "def main(): pass").unwrap();

        let mut cmd = tldr_cmd();
        cmd.args([
            "warm",
            temp.path().to_str().unwrap(),
            "--background",
            "--lang",
            "python",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("background"));

        // Wait a bit for background process
        std::thread::sleep(Duration::from_secs(2));

        // Check if cache was eventually created
        let cache_file = temp.path().join(".tldr/cache/call_graph.json");
        // Note: This may be flaky; in real implementation we might check differently
        assert!(
            cache_file.exists(),
            "Background warm should eventually create cache"
        );
    }

    #[test]
    #[ignore = "warm command not yet implemented"]
    fn test_warm_json_output() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join("main.py"), "def main(): pass").unwrap();

        let mut cmd = tldr_cmd();
        let output = cmd
            .args([
                "warm",
                temp.path().to_str().unwrap(),
                "--lang",
                "python",
                "-q",
            ])
            .output()
            .unwrap();

        let stdout = String::from_utf8_lossy(&output.stdout);
        let json: serde_json::Value = serde_json::from_str(&stdout).expect("Valid JSON output");

        assert!(json.get("status").is_some());
        assert!(json.get("files").is_some());
        assert!(json.get("edges").is_some());
    }

    #[test]
    #[ignore = "warm command not yet implemented"]
    fn test_warm_auto_detect_languages() {
        let temp = TempDir::new().unwrap();

        // Create files in multiple languages
        fs::write(temp.path().join("main.py"), "def main(): pass").unwrap();
        fs::write(temp.path().join("lib.rs"), "fn main() {}").unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["warm", temp.path().to_str().unwrap()]) // No --lang, auto-detect
            .assert()
            .success()
            .stdout(predicate::str::contains("python").or(predicate::str::contains("rust")));
    }

    #[test]
    #[ignore = "warm command not yet implemented"]
    fn test_warm_creates_tldrignore() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join("main.py"), "def main(): pass").unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["warm", temp.path().to_str().unwrap()])
            .assert()
            .success();

        // Verify .tldrignore was created
        let ignore_file = temp.path().join(".tldrignore");
        assert!(
            ignore_file.exists(),
            ".tldrignore should be created with defaults"
        );
    }
}

// =============================================================================
// 6. Stats Command Tests
// =============================================================================

mod stats_tests {
    use super::*;

    #[test]
    #[ignore = "stats command not yet implemented"]
    fn test_stats_help() {
        let mut cmd = tldr_cmd();
        cmd.args(["stats", "--help"])
            .assert()
            .success()
            .stdout(predicate::str::contains("--format"));
    }

    #[test]
    #[ignore = "stats command not yet implemented"]
    fn test_stats_empty() {
        // Use a temporary directory to avoid affecting real stats
        let temp = TempDir::new().unwrap();
        let tldr_dir = temp.path().join(".tldr");
        fs::create_dir_all(&tldr_dir).ok();

        // Note: This test may need environment variable override
        // to point stats path to temp directory

        let mut cmd = tldr_cmd();
        cmd.args(["stats"])
            .assert()
            .success()
            .stdout(predicate::str::contains("No usage").or(predicate::str::contains("0")));
    }

    #[test]
    #[ignore = "stats command not yet implemented"]
    fn test_stats_formats_token_savings() {
        // Create a test stats file
        let tldr_dir = home_dir().join(".tldr");
        fs::create_dir_all(&tldr_dir).ok();

        let stats_path = tldr_dir.join("stats.jsonl");
        let backup_path = stats_path.with_extension("jsonl.bak");

        // Backup existing file
        if stats_path.exists() {
            fs::rename(&stats_path, &backup_path).ok();
        }

        // Write test data
        let test_data = r#"{"session_id":"test1","raw_tokens":1000,"tldr_tokens":100,"requests":10}
{"session_id":"test2","raw_tokens":2000,"tldr_tokens":200,"requests":20}"#;
        fs::write(&stats_path, test_data).unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["stats"])
            .assert()
            .success()
            .stdout(predicate::str::contains("2,700").or(predicate::str::contains("2700"))) // tokens saved
            .stdout(predicate::str::contains("90")); // percentage

        // Restore backup
        if backup_path.exists() {
            fs::rename(&backup_path, &stats_path).ok();
        } else {
            fs::remove_file(&stats_path).ok();
        }
    }

    #[test]
    #[ignore = "stats command not yet implemented"]
    fn test_stats_json_output() {
        let tldr_dir = home_dir().join(".tldr");
        fs::create_dir_all(&tldr_dir).ok();

        let stats_path = tldr_dir.join("stats.jsonl");
        let backup_path = stats_path.with_extension("jsonl.bak");

        // Backup existing file
        if stats_path.exists() {
            fs::rename(&stats_path, &backup_path).ok();
        }

        // Write test data
        let test_data =
            r#"{"session_id":"test1","raw_tokens":1000,"tldr_tokens":100,"requests":10}"#;
        fs::write(&stats_path, test_data).unwrap();

        let mut cmd = tldr_cmd();
        let output = cmd.args(["stats", "--format", "json"]).output().unwrap();

        let stdout = String::from_utf8_lossy(&output.stdout);
        let json: serde_json::Value = serde_json::from_str(&stdout).expect("Valid JSON output");

        assert!(json.get("total_invocations").is_some());
        assert!(json.get("estimated_tokens_saved").is_some());
        assert!(json.get("raw_tokens_total").is_some());
        assert!(json.get("tldr_tokens_total").is_some());
        assert!(json.get("savings_percent").is_some());

        // Restore backup
        if backup_path.exists() {
            fs::rename(&backup_path, &stats_path).ok();
        } else {
            fs::remove_file(&stats_path).ok();
        }
    }

    #[test]
    #[ignore = "stats command not yet implemented"]
    fn test_stats_text_output() {
        let tldr_dir = home_dir().join(".tldr");
        fs::create_dir_all(&tldr_dir).ok();

        let stats_path = tldr_dir.join("stats.jsonl");
        let backup_path = stats_path.with_extension("jsonl.bak");

        // Backup existing file
        if stats_path.exists() {
            fs::rename(&stats_path, &backup_path).ok();
        }

        // Write test data
        let test_data =
            r#"{"session_id":"test1","raw_tokens":5000,"tldr_tokens":500,"requests":50}"#;
        fs::write(&stats_path, test_data).unwrap();

        let mut cmd = tldr_cmd();
        cmd.args(["stats", "--format", "text"])
            .assert()
            .success()
            .stdout(predicate::str::contains("TLDR Usage Statistics"))
            .stdout(predicate::str::contains("Total Invocations"))
            .stdout(predicate::str::contains("Tokens Saved"));

        // Restore backup
        if backup_path.exists() {
            fs::rename(&backup_path, &stats_path).ok();
        } else {
            fs::remove_file(&stats_path).ok();
        }
    }
}

// =============================================================================
// 7. Edge Case Tests
// =============================================================================

mod edge_cases {
    use super::*;

    #[test]
    #[ignore = "stale PID recovery not yet implemented"]
    fn test_stale_pid_file_recovery() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create a stale PID file (process doesn't exist)
        let _tmp_dir = std::env::temp_dir();

        // Compute the expected PID file path (simplified - actual impl uses MD5)
        // For test purposes, we'll just create a file that looks stale
        let _pid_content = "99999999"; // Very unlikely to be a real PID

        // This test verifies the daemon can recover from stale PID files
        // The actual implementation should:
        // 1. Try to acquire lock
        // 2. Check if PID in file is a running process
        // 3. If not, clean up and proceed

        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "stale socket cleanup not yet implemented"]
    fn test_stale_socket_cleanup() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create a stale socket file (no process listening)
        let tmp_dir = std::env::temp_dir();
        let stale_socket = tmp_dir.join("tldr-stale-test.sock");

        // Create an empty file as a "stale socket"
        fs::write(&stale_socket, "").ok();

        // Daemon should detect stale socket and clean up
        let mut cmd = tldr_cmd();
        cmd.args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Cleanup
        cleanup_daemon(project_path);

        fs::remove_file(&stale_socket).ok();
    }

    #[test]
    #[ignore = "concurrent daemon start not yet implemented"]
    fn test_concurrent_daemon_start_fails() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start first daemon
        let mut cmd1 = tldr_cmd();
        cmd1.args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Immediately try to start another (race condition test)
        let mut cmd2 = tldr_cmd();
        cmd2.args(["daemon", "start", "--project", project_path])
            .assert()
            .failure()
            .stderr(predicate::str::contains("already running"));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "permission denied handling not yet implemented"]
    fn test_permission_denied_socket() {
        // This test is platform-specific and may need adjustment
        // It verifies proper error handling when socket creation fails

        #[cfg(unix)]
        {
            // Try to create socket in a directory we don't have write access to
            let mut cmd = tldr_cmd();
            cmd.args(["daemon", "start", "--project", "/root/nonexistent"])
                .assert()
                .failure()
                .stderr(
                    predicate::str::contains("Permission denied")
                        .or(predicate::str::contains("permission")),
                );
        }
    }

    #[test]
    #[ignore = "connection timeout not yet implemented"]
    fn test_daemon_connection_timeout() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Query without daemon running should fail gracefully
        // Using assert_cmd::Command for timeout support
        let mut cmd = tldr_assert_cmd();
        cmd.args(["daemon", "query", "ping", "--project", project_path])
            .timeout(Duration::from_secs(10))
            .assert()
            .failure()
            .stderr(
                predicate::str::contains("not running")
                    .or(predicate::str::contains("Connection"))
                    .or(predicate::str::contains("timeout")),
            );
    }

    #[test]
    #[ignore = "invalid command handling not yet implemented"]
    fn test_daemon_unknown_command() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Send unknown command
        let mut query_cmd = tldr_cmd();
        query_cmd
            .args([
                "daemon",
                "query",
                "nonexistent_command",
                "--project",
                project_path,
            ])
            .assert()
            .failure()
            .stderr(predicate::str::contains("unknown").or(predicate::str::contains("Unknown")));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "graceful shutdown not yet implemented"]
    fn test_daemon_graceful_shutdown_persists_stats() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create test file
        fs::write(temp.path().join("test.py"), "def foo(): pass").unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Make some queries to generate stats
        for _ in 0..3 {
            let mut query_cmd = tldr_cmd();
            query_cmd
                .args(["daemon", "query", "ping", "--project", project_path])
                .output()
                .ok();
        }

        // Stop daemon gracefully
        let mut stop_cmd = tldr_cmd();
        stop_cmd
            .args(["daemon", "stop", "--project", project_path])
            .assert()
            .success();

        // Verify stats were persisted
        let cache_dir = temp.path().join(".tldr/cache");
        let _stats_file = cache_dir.join("salsa_stats.json");

        // Stats should be written on shutdown
        // (actual path may vary based on implementation)
        // This assertion may need adjustment based on actual implementation
    }

    #[test]
    #[ignore = "idle timeout not yet implemented"]
    fn test_daemon_idle_timeout() {
        // This is a long-running test that verifies idle timeout behavior
        // In practice, we'd use a short timeout for testing

        let temp = TempDir::new().unwrap();
        let _project_path = temp.path().to_str().unwrap();

        // Start daemon with a very short idle timeout (would need config support)
        // For now, this test documents the expected behavior

        // Expected behavior:
        // 1. Daemon starts
        // 2. No queries for idle_timeout_secs
        // 3. Daemon auto-shuts down
        // 4. Status shows "not running"
    }
}

// =============================================================================
// 8. Socket Path Computation Tests
// =============================================================================

mod socket_path_tests {
    use super::*;
    use tldr_cli::commands::daemon::{compute_hash, compute_pid_path, compute_socket_path};

    #[test]
    fn test_socket_path_deterministic() {
        // Same project path should always produce same socket path
        let project = PathBuf::from("/test/project");

        // This test verifies the socket path computation is deterministic
        // Actual implementation uses MD5 hash of canonicalized path
        let path1 = compute_socket_path(&project);
        let path2 = compute_socket_path(&project);
        assert_eq!(path1, path2);

        // Also verify hash is deterministic
        let hash1 = compute_hash(&project);
        let hash2 = compute_hash(&project);
        assert_eq!(hash1, hash2);
        assert_eq!(hash1.len(), 8); // 8 hex chars
    }

    #[test]
    fn test_socket_path_different_projects() {
        // Different projects should have different socket paths
        let project1 = PathBuf::from("/test/project1");
        let project2 = PathBuf::from("/test/project2");

        let path1 = compute_socket_path(&project1);
        let path2 = compute_socket_path(&project2);
        assert_ne!(path1, path2);

        // Also verify hashes are different
        let hash1 = compute_hash(&project1);
        let hash2 = compute_hash(&project2);
        assert_ne!(hash1, hash2);
    }

    #[test]
    fn test_pid_path_matches_socket_hash() {
        // PID path should use same hash as socket path
        let project = PathBuf::from("/test/project");

        let socket_path = compute_socket_path(&project);
        let pid_path = compute_pid_path(&project);

        // Both should have same hash prefix
        // e.g., /tmp/tldr-a1b2c3d4.sock and /tmp/tldr-a1b2c3d4.pid
        let socket_name = socket_path.file_name().unwrap().to_str().unwrap();
        let pid_name = pid_path.file_name().unwrap().to_str().unwrap();

        // Extract hash portion: tldr-XXXXXXXX.ext -> XXXXXXXX
        let socket_hash = &socket_name[5..13];
        let pid_hash = &pid_name[5..13];

        assert_eq!(socket_hash, pid_hash);

        // Verify extensions are correct
        assert!(socket_name.ends_with(".sock"));
        assert!(pid_name.ends_with(".pid"));
    }
}

// =============================================================================
// 9. Hook Stats Tracking Tests
// =============================================================================

mod hook_stats_tests {
    use super::*;

    #[test]
    #[ignore = "track command not yet implemented"]
    fn test_daemon_track_hook_activity() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Track a hook invocation
        let mut query_cmd = tldr_cmd();
        query_cmd
            .args([
                "daemon",
                "query",
                "track",
                "--project",
                project_path,
                "--json",
                r#"{"hook": "pre-commit", "success": true, "metrics": {"files_checked": 5}}"#,
            ])
            .assert()
            .success()
            .stdout(predicate::str::contains("total_invocations"));

        // Cleanup
        cleanup_daemon(project_path);
    }

    #[test]
    #[ignore = "track flush not yet implemented"]
    fn test_track_flush_at_threshold() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Track multiple hook invocations (flush threshold is 5)
        for i in 0..6 {
            let mut query_cmd = tldr_cmd();
            let output = query_cmd
                .args([
                    "daemon",
                    "query",
                    "track",
                    "--project",
                    project_path,
                    "--json",
                    r#"{"hook": "test-hook", "success": true}"#,
                ])
                .output()
                .unwrap();

            // Check if flush occurred on 5th invocation
            if i == 5 {
                let stdout = String::from_utf8_lossy(&output.stdout);
                assert!(
                    stdout.contains("flushed") || stdout.contains("true"),
                    "Expected stats to be flushed"
                );
            }
        }

        // Cleanup
        cleanup_daemon(project_path);
    }
}

// =============================================================================
// 10. Semantic Search Tests (requires model)
// =============================================================================

mod semantic_tests {
    use super::*;

    #[test]
    #[ignore = "semantic search not yet implemented"]
    fn test_daemon_semantic_query() {
        let temp = TempDir::new().unwrap();
        let project_path = temp.path().to_str().unwrap();

        // Create some Python files with meaningful content
        fs::write(
            temp.path().join("auth.py"),
            "def authenticate(user, password):\n    '''Verify user credentials'''\n    pass",
        )
        .unwrap();
        fs::write(
            temp.path().join("db.py"),
            "def connect_database(host, port):\n    '''Connect to database'''\n    pass",
        )
        .unwrap();

        // Start daemon
        let mut start_cmd = tldr_cmd();
        start_cmd
            .args(["daemon", "start", "--project", project_path])
            .assert()
            .success();

        // Wait for indexing
        std::thread::sleep(Duration::from_secs(2));

        // Semantic search for authentication-related code
        let mut query_cmd = tldr_cmd();
        query_cmd
            .args([
                "daemon",
                "query",
                "semantic",
                "--project",
                project_path,
                "--json",
                r#"{"query": "user login verification", "top_k": 5}"#,
            ])
            .assert()
            .success()
            .stdout(predicate::str::contains("auth")); // Should find auth.py

        // Cleanup
        cleanup_daemon(project_path);
    }
}