leindex 1.7.1

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
// Integration Tests for LePasserelle
//
// These tests cover end-to-end workflows including:
// - CLI command workflows
// - Cache spilling and restoration
// - Storage persistence
// - Error handling

#![cfg(feature = "cli")]

use std::path::PathBuf;
use tempfile::TempDir;

// ============================================================================
// CLI WORKFLOW INTEGRATION TESTS
// ============================================================================

mod cli_workflow_tests {
    use super::*;
    use clap::Parser;
    use std::process::Command;

    #[test]
    fn test_cli_index_command_parsing() {
        use leindex::cli::Cli;

        let cli = Cli::parse_from(["leindex", "index", "/test/project", "--force", "--progress"]);

        use leindex::cli::Commands;
        match cli.command {
            Some(Commands::Index {
                path,
                force,
                progress,
                ..
            }) => {
                assert_eq!(path, PathBuf::from("/test/project"));
                assert!(force);
                assert!(progress);
            }
            _ => panic!("Expected Index command"),
        }
    }

    #[test]
    fn test_cli_search_command_parsing() {
        use leindex::cli::Cli;

        let cli = Cli::parse_from(["leindex", "search", "authentication", "--top-k", "20"]);

        use leindex::cli::Commands;
        match cli.command {
            Some(Commands::Search { query, top_k }) => {
                assert_eq!(query, "authentication");
                assert_eq!(top_k, 20);
            }
            _ => panic!("Expected Search command"),
        }
    }

    #[test]
    fn test_cli_analyze_command_parsing() {
        use leindex::cli::Cli;

        let cli = Cli::parse_from([
            "leindex",
            "analyze",
            "How does auth work?",
            "--tokens",
            "5000",
        ]);

        use leindex::cli::Commands;
        match cli.command {
            Some(Commands::Analyze {
                query,
                token_budget,
            }) => {
                assert_eq!(query, "How does auth work?");
                assert_eq!(token_budget, 5000);
            }
            _ => panic!("Expected Analyze command"),
        }
    }

    #[test]
    fn test_cli_diagnostics_command_parsing() {
        use leindex::cli::Cli;

        let cli = Cli::parse_from(["leindex", "diagnostics"]);

        use leindex::cli::Commands;
        match cli.command {
            Some(Commands::Diagnostics) => {
                // Successfully parsed
            }
            _ => panic!("Expected Diagnostics command"),
        }
    }

    #[test]
    fn test_cli_verbose_flag() {
        use leindex::cli::Cli;

        let cli = Cli::parse_from(["leindex", "-v", "diagnostics"]);

        assert!(cli.verbose);
    }

    #[test]
    fn test_cli_index_handles_deeply_nested_rust_without_aborting() {
        let temp_dir = TempDir::new().unwrap();
        let project_dir = temp_dir.path();
        let deep_file = project_dir.join("deep.rs");

        let mut source = String::from("fn main() {\n");
        let mut indent = String::from("    ");
        for _ in 0..6000 {
            source.push_str(&format!("{indent}if true {{\n"));
            indent.push_str("    ");
        }
        source.push_str(&format!("{indent}let _x = 1;\n"));
        for depth in (0..6000).rev() {
            indent.truncate(depth * 4 + 4);
            source.push_str(&format!("{indent}}}\n"));
        }
        source.push_str("}\n");
        std::fs::write(&deep_file, source).unwrap();

        let output = Command::new(env!("CARGO_BIN_EXE_leindex"))
            .arg("index")
            .arg(project_dir)
            .output()
            .unwrap();

        assert!(
            output.status.success(),
            "expected leindex index to succeed for deep nesting.\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}",
            output.status,
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

// ============================================================================
// CACHE MANAGEMENT INTEGRATION TESTS
// ============================================================================

mod cache_management_tests {
    use super::*;
    use leindex::cli::memory::{
        create_pdg_entry, create_search_entry, CacheEntry, CacheSpiller, MemoryConfig, WarmStrategy,
    };
    use std::collections::HashMap;

    #[test]
    fn test_cache_entry_creation() {
        let pdg_data = vec![1u8, 2, 3, 4];
        let pdg_entry = create_pdg_entry("test_project".to_string(), 100, 200, &pdg_data);

        match pdg_entry {
            CacheEntry::PDG {
                project_id,
                node_count,
                edge_count,
                ..
            } => {
                assert_eq!(project_id, "test_project");
                assert_eq!(node_count, 100);
                assert_eq!(edge_count, 200);
            }
            _ => panic!("Expected PDG entry"),
        }

        let search_data = vec![5u8, 6, 7, 8];
        let search_entry = create_search_entry("test_project".to_string(), 50, &search_data);

        match search_entry {
            CacheEntry::SearchIndex {
                project_id,
                entry_count,
                ..
            } => {
                assert_eq!(project_id, "test_project");
                assert_eq!(entry_count, 50);
            }
            _ => panic!("Expected SearchIndex entry"),
        }
    }

    #[test]
    fn test_cache_spiller_creation() {
        let temp_dir = TempDir::new().unwrap();
        let cache_dir = temp_dir.path().join("cache");

        let config = MemoryConfig {
            cache_dir,
            ..Default::default()
        };

        let spiller = CacheSpiller::new(config);
        assert!(spiller.is_ok());
    }

    #[test]
    fn test_cache_insert_and_retrieve() {
        let temp_dir = TempDir::new().unwrap();
        let cache_dir = temp_dir.path().join("cache");

        let config = MemoryConfig {
            cache_dir,
            max_cache_bytes: 10_000,
            ..Default::default()
        };

        let mut spiller = CacheSpiller::new(config).unwrap();
        let store = spiller.store_mut();

        let entry = CacheEntry::Binary {
            metadata: {
                let mut map = HashMap::new();
                map.insert("type".to_string(), "test".to_string());
                map
            },
            serialized_data: vec![0u8; 100],
        };

        store.insert("test_key".to_string(), entry.clone()).unwrap();
        let retrieved = store.get("test_key");

        assert!(retrieved.is_some());
    }

    #[test]
    fn test_cache_eviction_on_size_limit() {
        let temp_dir = TempDir::new().unwrap();
        let cache_dir = temp_dir.path().join("cache");

        let config = MemoryConfig {
            cache_dir,
            max_cache_bytes: 500, // Small limit
            ..Default::default()
        };

        let mut spiller = CacheSpiller::new(config).unwrap();
        let store = spiller.store_mut();

        // Insert entries that exceed the cache size
        for i in 0..10 {
            let entry = CacheEntry::Binary {
                metadata: HashMap::new(),
                serialized_data: vec![0u8; 100], // 100 bytes each
            };
            store.insert(format!("key_{}", i), entry).unwrap();
        }

        // Cache should have evicted some entries
        assert!(store.len() < 10);
        assert!(store.total_bytes() <= 500);
    }

    #[test]
    fn test_cache_key_generation() {
        use leindex::cli::memory::{analysis_cache_key, pdg_cache_key, search_cache_key};

        assert_eq!(pdg_cache_key("myproject"), "pdg:myproject");
        assert_eq!(search_cache_key("myproject"), "search:myproject");
        assert!(analysis_cache_key("how does auth work").starts_with("analysis:"));
    }

    #[test]
    fn test_warm_strategy_variants() {
        // Test that all warm strategy variants can be created and compared
        assert_eq!(WarmStrategy::All, WarmStrategy::All);
        assert_eq!(WarmStrategy::PDGOnly, WarmStrategy::PDGOnly);
        assert_eq!(WarmStrategy::SearchIndexOnly, WarmStrategy::SearchIndexOnly);
        assert_eq!(WarmStrategy::RecentFirst, WarmStrategy::RecentFirst);

        assert_ne!(WarmStrategy::All, WarmStrategy::PDGOnly);
        assert_ne!(WarmStrategy::PDGOnly, WarmStrategy::SearchIndexOnly);
    }
}

// ============================================================================
// STORAGE PERSISTENCE INTEGRATION TESTS
// ============================================================================

mod storage_persistence_tests {
    use super::*;
    use leindex::cli::memory::MemoryConfig;

    #[test]
    fn test_memory_config_default() {
        let config = MemoryConfig::default();
        assert_eq!(config.spill_threshold, 0.75); // A+ Section 5.6: lowered from 0.85
        assert_eq!(config.check_interval_secs, 30);
        assert!(config.auto_spill);
        assert_eq!(config.max_cache_bytes, 96_000_000); // A+ Section 5.6: lowered from 500 MB
    }

    #[test]
    fn test_memory_config_serialization() {
        let config = MemoryConfig {
            spill_threshold: 0.9,
            check_interval_secs: 60,
            auto_spill: false,
            max_cache_bytes: 1_000_000_000,
            cache_dir: PathBuf::from("/tmp/cache"),
        };

        let serialized = serde_json::to_string(&config).unwrap();
        let deserialized: MemoryConfig = serde_json::from_str(&serialized).unwrap();

        assert_eq!(deserialized.spill_threshold, 0.9);
        assert_eq!(deserialized.check_interval_secs, 60);
        assert!(!deserialized.auto_spill);
        assert_eq!(deserialized.max_cache_bytes, 1_000_000_000);
        assert_eq!(deserialized.cache_dir, PathBuf::from("/tmp/cache"));
    }
}

// ============================================================================
// ERROR HANDLING INTEGRATION TESTS
// ============================================================================

mod error_handling_tests {
    use leindex::cli::errors::LeIndexError;

    #[test]
    fn test_error_display() {
        let error = LeIndexError::Parse {
            message: "Test parse error".to_string(),
            file_path: Some("/test/file.rs".into()),
            suggestion: None,
        };

        let display_str = format!("{}", error);
        // Error display formats as "Parse error: <message> at <file>"
        assert!(display_str.contains("Test parse error"));
    }

    #[test]
    fn test_error_conversion() {
        use std::io;

        let io_error = io::Error::new(io::ErrorKind::NotFound, "File not found");
        let le_error: LeIndexError = io_error.into();

        match le_error {
            LeIndexError::Io { .. } => {
                // Successfully converted
            }
            _ => panic!("Expected Io error variant"),
        }
    }
}

// ============================================================================
// END-TO-END WORKFLOW TESTS
// ============================================================================

mod e2e_workflow_tests {
    use super::*;

    #[test]
    fn test_leindex_creation_workflow() {
        use leindex::cli::LeIndex;

        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create a simple test project
        let src_dir = project_path.join("src");
        std::fs::create_dir_all(&src_dir).unwrap();

        let main_file = src_dir.join("main.rs");
        std::fs::write(
            &main_file,
            "fn main() {\n    println!(\"Hello, world!\");\n}",
        )
        .unwrap();

        // Create LeIndex instance
        let leindex = LeIndex::new(project_path);
        assert!(leindex.is_ok());

        let leindex = leindex.unwrap();
        // Temp directory name varies, just check it's not empty
        assert!(!leindex.project_id().is_empty());
        assert!(leindex.project_path().starts_with(temp_dir.path()));
    }

    #[test]
    fn test_diagnostics_workflow() {
        use leindex::cli::LeIndex;

        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        let leindex = LeIndex::new(project_path).unwrap();
        let diagnostics = leindex.get_diagnostics();

        assert!(diagnostics.is_ok());
        let diag = diagnostics.unwrap();
        // Just check that diagnostics are available
        assert!(!diag.project_id.is_empty());
        assert!(diag.stats.files_parsed == 0); // Not indexed yet
    }

    #[test]
    fn test_cache_integration_with_leindex() {
        use leindex::cli::memory::MemoryConfig;
        use leindex::cli::LeIndex;

        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path().to_path_buf();
        let cache_dir = temp_dir.path().join("cache");

        let _config = MemoryConfig {
            cache_dir,
            max_cache_bytes: 10_000_000,
            ..Default::default()
        };

        // Create LeIndex with custom cache config
        let storage_path = project_path.join(".leindex");
        std::fs::create_dir_all(&storage_path).unwrap();

        let leindex = LeIndex::new(&project_path).unwrap();
        let diagnostics = leindex.get_diagnostics().unwrap();

        // Cache should be initialized
        assert_eq!(diagnostics.cache_entries, 0);
        assert_eq!(diagnostics.cache_bytes, 0);
        assert_eq!(diagnostics.spilled_entries, 0);
        assert_eq!(diagnostics.spilled_bytes, 0);
    }
}

// ============================================================================
// CACHE SPILLING AND RELOADING TESTS (Phase 5.2 & 5.3)
// ============================================================================

mod cache_spill_reload_tests {
    use super::*;
    use leindex::cli::memory::WarmStrategy;
    use leindex::cli::LeIndex;

    /// Helper function to create a test project with some code
    fn create_test_project(temp_dir: &TempDir) -> PathBuf {
        let project_path = temp_dir.path().to_path_buf();
        let src_dir = project_path.join("src");
        std::fs::create_dir_all(&src_dir).unwrap();

        // Create a simple Rust file
        let main_file = src_dir.join("main.rs");
        std::fs::write(
            &main_file,
            r#"
fn main() {
    println!("Hello, world!");
    greet();
}

fn greet() {
    println!("Greetings!");
}
"#,
        )
        .unwrap();

        project_path
    }

    #[test]
    fn test_spill_pdg_cache_when_no_pdg() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Spilling PDG when none is in memory should return an error
        let result = leindex.spill_pdg_cache();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No PDG in memory"));
    }

    #[test]
    fn test_spill_vector_cache() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Spilling vector cache should always work (just creates a marker)
        let result = leindex.spill_vector_cache();
        assert!(result.is_ok());

        // Verify the cache marker was created
        let stats = leindex.get_cache_stats().unwrap();
        assert!(stats.spilled_entries > 0 || stats.cache_entries > 0);
    }

    #[test]
    fn test_spill_all_caches() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Spill all caches should succeed
        let result = leindex.spill_all_caches();
        assert!(result.is_ok());

        let (pdg_bytes, vector_bytes) = result.unwrap();
        // Vector cache should have been spilled
        assert_eq!(vector_bytes, vector_bytes); // usize is non-negative
                                                // PDG bytes should be 0 since PDG wasn't loaded
        assert_eq!(pdg_bytes, 0);
    }

    #[test]
    fn test_reload_pdg_when_already_loaded() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // First, index the project to load PDG
        let _ = leindex.index_project(false);

        // Reloading when PDG is already in memory should return Ok immediately
        let result = leindex.reload_pdg_from_cache();
        assert!(result.is_ok());
    }

    #[test]
    fn test_reload_vector_without_pdg() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Reloading vector without PDG should error
        let result = leindex.reload_vector_from_pdg();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No PDG available"));
    }

    #[test]
    fn test_warm_caches_all_strategy() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Warm all caches
        let result = leindex.warm_caches(WarmStrategy::All);
        assert!(result.is_ok());

        let warm_result = result.unwrap();
        // Should have result (entries_warmed is usize, non-negative)
        assert_eq!(warm_result.entries_warmed, warm_result.entries_warmed);
    }

    #[test]
    fn test_warm_caches_pdg_only_strategy() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Warm PDG only
        let result = leindex.warm_caches(WarmStrategy::PDGOnly);
        assert!(result.is_ok());
    }

    #[test]
    fn test_warm_caches_search_only_strategy() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Warm search index only
        let result = leindex.warm_caches(WarmStrategy::SearchIndexOnly);
        assert!(result.is_ok());
    }

    #[test]
    fn test_get_cache_stats() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let leindex = LeIndex::new(&project_path).unwrap();

        // Get cache stats
        let result = leindex.get_cache_stats();
        assert!(result.is_ok());

        let stats = result.unwrap();
        // All stats should be valid (usize is always non-negative)
        assert_eq!(stats.cache_entries, stats.cache_entries);
        assert_eq!(stats.cache_bytes, stats.cache_bytes);
        assert_eq!(stats.spilled_entries, stats.spilled_entries);
        assert_eq!(stats.spilled_bytes, stats.spilled_bytes);
    }

    #[test]
    fn test_check_memory_and_spill_below_threshold() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Check memory - should return Ok(false) since below threshold
        let result = leindex.check_memory_and_spill();
        assert!(result.is_ok());

        let spilled = result.unwrap();
        // Should not have spilled since below threshold
        assert!(!spilled);
    }

    #[test]
    fn test_cache_spill_and_reload_workflow() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // First index the project
        let index_result = leindex.index_project(false);
        assert!(index_result.is_ok());

        // Get initial stats
        let initial_stats = leindex.get_cache_stats().unwrap();

        // Spill all caches
        let spill_result = leindex.spill_all_caches();
        assert!(spill_result.is_ok());

        // Verify caches were spilled
        let spilled_stats = leindex.get_cache_stats().unwrap();
        assert!(spilled_stats.spilled_entries >= initial_stats.spilled_entries);

        // Note: Full reload test would require storage persistence
        // which is beyond the scope of this unit test
    }

    #[test]
    fn test_warm_caches_recent_first_strategy() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Warm caches with RecentFirst strategy
        let result = leindex.warm_caches(WarmStrategy::RecentFirst);
        assert!(result.is_ok());

        let warm_result = result.unwrap();
        assert_eq!(warm_result.entries_warmed, warm_result.entries_warmed);
    }

    #[test]
    fn test_cache_stats_after_spill() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // Get initial stats
        let initial = leindex.get_cache_stats().unwrap();

        // Spill vector cache
        leindex.spill_vector_cache().unwrap();

        // Get stats after spill
        let after = leindex.get_cache_stats().unwrap();

        // Spilled entries should have increased or stayed the same
        assert!(after.spilled_entries >= initial.spilled_entries);
    }

    #[test]
    fn test_multiple_spill_operations() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();

        // First spill
        leindex.spill_vector_cache().unwrap();
        let first_stats = leindex.get_cache_stats().unwrap();

        // Second spill (should handle gracefully)
        leindex.spill_vector_cache().unwrap();
        let second_stats = leindex.get_cache_stats().unwrap();

        // Cache should handle multiple spills
        assert!(second_stats.spilled_entries >= first_stats.spilled_entries);
    }

    #[test]
    fn test_search_cache_hit_rate_increases_on_repeat_query() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();
        let _ = leindex.index_project(false).unwrap();

        let _ = leindex.search("greet", 5, None).unwrap();
        let first = leindex.get_cache_stats().unwrap();
        let _ = leindex.search("greet", 5, None).unwrap();
        let second = leindex.get_cache_stats().unwrap();

        assert!(second.cache_hits >= first.cache_hits);
        assert!(second.cache_hit_rate >= first.cache_hit_rate);
    }

    #[test]
    fn test_analysis_cache_persists_across_sessions() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        {
            let mut first = LeIndex::new(&project_path).unwrap();
            let _ = first.index_project(false).unwrap();
            let _ = first.analyze("How does greet work?", 800).unwrap();
            let stats = first.get_cache_stats().unwrap();
            assert!(stats.cache_writes > 0);
        }

        let mut second = LeIndex::new(&project_path).unwrap();
        let _ = second.load_from_storage();
        let _ = second.analyze("How does greet work?", 800).unwrap();
        let stats = second.get_cache_stats().unwrap();

        assert!(
            stats.cache_disk_hits > 0 || stats.cache_memory_hits > 0,
            "Expected cache hit from persisted session data"
        );
    }

    #[test]
    fn test_diagnostics_reports_cache_temperature() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = create_test_project(&temp_dir);

        let mut leindex = LeIndex::new(&project_path).unwrap();
        let cold = leindex.get_diagnostics().unwrap();
        assert_eq!(cold.cache_temperature, "cold");

        let stats = leindex.index_project(false).unwrap();
        assert!(
            stats.indexed_nodes > 0,
            "Expected indexed nodes > 0, got {:?}",
            stats
        );
        let _ = leindex.search("greet", 5, None).unwrap();
        let _ = leindex.search("greet", 5, None).unwrap();
        let cache_stats = leindex.get_cache_stats().unwrap();
        assert!(
            cache_stats.cache_hits > 0,
            "Expected cache hits > 0 after repeated query, got {:?}",
            cache_stats
        );
        let warm_or_hot = leindex.get_diagnostics().unwrap();
        assert!(
            warm_or_hot.cache_temperature == "warm" || warm_or_hot.cache_temperature == "hot",
            "Expected warm/hot cache temperature, got {:?}",
            warm_or_hot
        );
    }
}