pytest-language-server 0.22.3

A blazingly fast Language Server Protocol implementation for pytest
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
//! Performance and real-world LSP scenario tests.
//!
//! Tests for typical editor use cases like file changes, file moves, and
//! rapid consecutive operations that might occur in a real editor.
//!
//! All tests have a 10-second timeout to prevent hangs from blocking CI.

use ntest::timeout;
use pytest_language_server::FixtureDatabase;
use std::path::PathBuf;
use tempfile::TempDir;

/// Helper to create a temporary test file with given content
fn create_temp_test_file(dir: &TempDir, name: &str, content: &str) -> PathBuf {
    let file_path = dir.path().join(name);
    std::fs::write(&file_path, content).unwrap();
    file_path
}

#[test]
#[timeout(10000)]
fn test_rapid_file_changes() {
    // Simulate a user rapidly editing a file in an editor
    let temp_dir = TempDir::new().unwrap();
    let file_path = create_temp_test_file(
        &temp_dir,
        "test_file.py",
        r#"
import pytest

@pytest.fixture
def my_fixture():
    return 42

def test_something(my_fixture):
    assert my_fixture == 42
"#,
    );

    let db = FixtureDatabase::new();

    // Initial file analysis
    let content1 = std::fs::read_to_string(&file_path).unwrap();
    db.analyze_file(file_path.clone(), &content1);

    // Verify initial state
    assert_eq!(db.definitions.len(), 1);
    assert!(db.definitions.contains_key("my_fixture"));

    // Simulate rapid edits - user types more content
    let content2 = r#"
import pytest

@pytest.fixture
def my_fixture():
    return 42

@pytest.fixture
def another_fixture():
    return "hello"

def test_something(my_fixture, another_fixture):
    assert my_fixture == 42
    assert another_fixture == "hello"
"#;
    db.analyze_file(file_path.clone(), content2);

    // Should now have 2 fixtures
    assert_eq!(db.definitions.len(), 2);
    assert!(db.definitions.contains_key("my_fixture"));
    assert!(db.definitions.contains_key("another_fixture"));

    // Simulate user deleting a fixture
    let content3 = r#"
import pytest

@pytest.fixture
def another_fixture():
    return "hello"

def test_something(another_fixture):
    assert another_fixture == "hello"
"#;
    db.analyze_file(file_path.clone(), content3);

    // Should only have 1 fixture now
    assert_eq!(db.definitions.len(), 1);
    assert!(!db.definitions.contains_key("my_fixture"));
    assert!(db.definitions.contains_key("another_fixture"));
}

#[test]
#[timeout(10000)]
fn test_file_rename_scenario() {
    // Simulate renaming a test file (editor removes from one path, adds to another)
    let temp_dir = TempDir::new().unwrap();

    let original_content = r#"
import pytest

@pytest.fixture
def shared_fixture():
    return "data"

def test_one(shared_fixture):
    assert shared_fixture == "data"
"#;

    // Create original file
    let old_path = create_temp_test_file(&temp_dir, "test_old.py", original_content);

    let db = FixtureDatabase::new();
    db.analyze_file(old_path.clone(), original_content);

    // Verify fixture is tracked
    assert_eq!(db.definitions.len(), 1);
    assert!(db.definitions.contains_key("shared_fixture"));

    // Simulate file rename by analyzing new path with same content
    let new_path = temp_dir.path().join("test_new.py");
    std::fs::write(&new_path, original_content).unwrap();
    db.analyze_file(new_path.clone(), original_content);

    // Fixture should still be in the database (from both paths until old is cleaned up)
    assert!(db.definitions.contains_key("shared_fixture"));

    // In a real LSP scenario, the server would clean up the old file's data
    // when notified of file deletion. For this test, we verify both are accessible.
}

#[test]
#[timeout(10000)]
fn test_multiple_files_simultaneous_changes() {
    // Simulate multiple files being edited at the same time (e.g., multi-cursor edit)
    let temp_dir = TempDir::new().unwrap();

    let file1_content = r#"
import pytest

@pytest.fixture
def fixture_a():
    return 1
"#;

    let file2_content = r#"
import pytest

@pytest.fixture
def fixture_b():
    return 2
"#;

    let file1 = create_temp_test_file(&temp_dir, "test_1.py", file1_content);
    let file2 = create_temp_test_file(&temp_dir, "test_2.py", file2_content);

    let db = FixtureDatabase::new();

    // Analyze both files
    db.analyze_file(file1.clone(), file1_content);
    db.analyze_file(file2.clone(), file2_content);

    assert_eq!(db.definitions.len(), 2);

    // Simulate both files being edited to add the same fixture name (conflict scenario)
    let new_content = r#"
import pytest

@pytest.fixture
def shared_name():
    return 42
"#;

    db.analyze_file(file1.clone(), new_content);
    db.analyze_file(file2.clone(), new_content);

    // Should have 1 fixture name with 2 definitions
    assert_eq!(db.definitions.len(), 1);
    let defs = db.definitions.get("shared_name").unwrap();
    assert_eq!(defs.len(), 2);
}

#[test]
#[timeout(10000)]
fn test_large_file_incremental_changes() {
    // Test performance with a large file that gets incrementally edited
    let temp_dir = TempDir::new().unwrap();

    // Generate a large file with many fixtures
    let mut content = String::from("import pytest\n\n");
    for i in 0..50 {
        content.push_str(&format!(
            "@pytest.fixture\ndef fixture_{}():\n    return {}\n\n",
            i, i
        ));
    }

    let file_path = create_temp_test_file(&temp_dir, "test_large.py", &content);

    let db = FixtureDatabase::new();

    // Initial analysis
    let start = std::time::Instant::now();
    db.analyze_file(file_path.clone(), &content);
    let initial_duration = start.elapsed();

    assert_eq!(db.definitions.len(), 50);

    // Add one more fixture (simulating user typing)
    content.push_str("@pytest.fixture\ndef new_fixture():\n    return 999\n");

    // Re-analyze
    let start = std::time::Instant::now();
    db.analyze_file(file_path.clone(), &content);
    let update_duration = start.elapsed();

    assert_eq!(db.definitions.len(), 51);

    // Performance check: incremental update should be reasonably fast
    // This is a regression test - if changes make it much slower, this will fail
    // Using 1 second to be generous for CI/slower systems
    const MAX_UPDATE_TIME_MS: u64 = 1000;
    assert!(
        update_duration < std::time::Duration::from_millis(MAX_UPDATE_TIME_MS),
        "File re-analysis took too long: {:?} (max: {}ms)",
        update_duration,
        MAX_UPDATE_TIME_MS
    );

    println!(
        "Large file analysis: initial={:?}, update={:?}",
        initial_duration, update_duration
    );
}

#[test]
#[timeout(10000)]
fn test_conftest_hierarchy_with_changes() {
    // Test that fixture resolution remains correct when conftest files change
    let temp_dir = TempDir::new().unwrap();

    // Create directory structure
    let root = temp_dir.path();
    let subdir = root.join("subdir");
    std::fs::create_dir(&subdir).unwrap();

    // Root conftest
    let root_conftest = r#"
import pytest

@pytest.fixture
def root_fixture():
    return "root"
"#;

    // Subdir test file using the fixture
    let test_content = r#"
def test_something(root_fixture):
    assert root_fixture == "root"
"#;

    let root_conftest_path = root.join("conftest.py");
    std::fs::write(&root_conftest_path, root_conftest).unwrap();

    let test_path = subdir.join("test_sub.py");
    std::fs::write(&test_path, test_content).unwrap();
    // Canonicalize path since database stores canonical paths
    let test_path = test_path.canonicalize().unwrap();

    let db = FixtureDatabase::new();

    // Analyze both files
    db.analyze_file(root_conftest_path.clone(), root_conftest);
    db.analyze_file(test_path.clone(), test_content);

    // Verify fixture is found
    assert!(db.definitions.contains_key("root_fixture"));
    {
        // Scope the DashMap reference to avoid holding read lock across analyze_file
        let usages = db.usages.get(&test_path).unwrap();
        assert_eq!(usages.len(), 1);
        assert_eq!(usages[0].name, "root_fixture");
    }

    // Simulate conftest.py being edited to add another fixture
    let updated_conftest = r#"
import pytest

@pytest.fixture
def root_fixture():
    return "root"

@pytest.fixture
def new_root_fixture():
    return "new"
"#;

    db.analyze_file(root_conftest_path.clone(), updated_conftest);

    // Both fixtures should be available now
    assert_eq!(db.definitions.len(), 2);
    assert!(db.definitions.contains_key("root_fixture"));
    assert!(db.definitions.contains_key("new_root_fixture"));

    // Test file usages shouldn't change (only uses root_fixture)
    let usages = db.usages.get(&test_path).unwrap();
    assert_eq!(usages.len(), 1);
}

#[test]
#[timeout(10000)]
fn test_cache_effectiveness_on_repeated_access() {
    // Verify that line index caching improves performance on repeated access
    let temp_dir = TempDir::new().unwrap();

    let content = r#"
import pytest

@pytest.fixture
def my_fixture():
    return 42

def test_one(my_fixture):
    assert my_fixture == 42

def test_two(my_fixture):
    assert my_fixture == 42

def test_three(my_fixture):
    assert my_fixture == 42
"#;

    let file_path = create_temp_test_file(&temp_dir, "test_cache.py", content);
    // Canonicalize path since database stores canonical paths
    let file_path = file_path.canonicalize().unwrap();
    let db = FixtureDatabase::new();

    // First analysis - should populate cache
    db.analyze_file(file_path.clone(), content);

    // Check that line index cache is populated
    assert!(db.line_index_cache.contains_key(&file_path));

    // Get the cached hash to verify it's content-based
    let cached_hash = db.line_index_cache.get(&file_path).map(|e| e.value().0);
    assert!(cached_hash.is_some());

    // Perform multiple fixture lookups (simulating hover/goto operations)
    const LOOKUP_COUNT: usize = 10;
    const TEST_LINE: u32 = 7; // Line with "def test_one(my_fixture):"
    const FIXTURE_CHAR_POS: u32 = 15; // Character position of "my_fixture" parameter

    for _ in 0..LOOKUP_COUNT {
        let result = db.find_fixture_definition(&file_path, TEST_LINE, FIXTURE_CHAR_POS);
        assert!(result.is_some());
    }

    // Cache should still be there with the same hash (content unchanged)
    assert!(db.line_index_cache.contains_key(&file_path));
    let new_hash = db.line_index_cache.get(&file_path).map(|e| e.value().0);
    assert_eq!(cached_hash, new_hash, "Cache hash should remain the same");

    // Re-analyze with same content - cache should be reused (same hash)
    db.analyze_file(file_path.clone(), content);
    let reanalyzed_hash = db.line_index_cache.get(&file_path).map(|e| e.value().0);
    assert_eq!(
        cached_hash, reanalyzed_hash,
        "Cache should be reused for same content"
    );

    // Analyze with different content - cache should be updated (different hash)
    let new_content = r#"
import pytest

@pytest.fixture
def my_fixture():
    return 99  # Changed value
"#;
    db.analyze_file(file_path.clone(), new_content);
    let updated_hash = db.line_index_cache.get(&file_path).map(|e| e.value().0);
    assert_ne!(
        cached_hash, updated_hash,
        "Cache should be invalidated for different content"
    );
}

#[test]
#[timeout(10000)]
fn test_concurrent_file_modifications() {
    // Test that the database handles concurrent updates correctly
    // (simulates multiple threads/async tasks updating different files)
    use std::sync::Arc;
    use std::thread;

    let temp_dir = TempDir::new().unwrap();
    let db = Arc::new(FixtureDatabase::new());

    let mut handles = vec![];

    // Spawn multiple threads, each analyzing different files
    for i in 0..5 {
        let db_clone = Arc::clone(&db);
        let dir_path = temp_dir.path().to_path_buf();

        let handle = thread::spawn(move || {
            let content = format!(
                r#"
import pytest

@pytest.fixture
def fixture_{}():
    return {}
"#,
                i, i
            );

            let file_path = dir_path.join(format!("test_{}.py", i));
            std::fs::write(&file_path, &content).unwrap();

            // Each thread analyzes its file multiple times
            for _ in 0..3 {
                db_clone.analyze_file(file_path.clone(), &content);
            }
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    // Verify all fixtures were recorded
    assert_eq!(db.definitions.len(), 5);
    for i in 0..5 {
        assert!(db.definitions.contains_key(&format!("fixture_{}", i)));
    }
}

#[test]
#[timeout(10000)]
fn test_cleanup_file_cache_removes_cached_data() {
    // Test that cleanup_file_cache properly removes line_index_cache and file_cache entries
    let temp_dir = TempDir::new().unwrap();

    let content = r#"
import pytest

@pytest.fixture
def my_fixture():
    return 42

def test_one(my_fixture):
    assert my_fixture == 42
"#;

    let file_path = create_temp_test_file(&temp_dir, "test_cleanup.py", content);
    let canonical_path = file_path.canonicalize().unwrap();

    let db = FixtureDatabase::new();

    // Analyze file to populate caches
    db.analyze_file(file_path.clone(), content);

    // Verify caches are populated
    assert!(
        db.line_index_cache.contains_key(&canonical_path),
        "line_index_cache should contain the file"
    );
    assert!(
        db.file_cache.contains_key(&canonical_path),
        "file_cache should contain the file"
    );

    // Clean up caches for the file
    db.cleanup_file_cache(&file_path);

    // Verify caches are cleared
    assert!(
        !db.line_index_cache.contains_key(&canonical_path),
        "line_index_cache should be empty after cleanup"
    );
    assert!(
        !db.file_cache.contains_key(&canonical_path),
        "file_cache should be empty after cleanup"
    );

    // Definitions and usages should still be present (they're cleaned on next analyze)
    assert!(db.definitions.contains_key("my_fixture"));
    assert!(db.usages.contains_key(&canonical_path));
}

#[test]
#[timeout(10000)]
fn test_cleanup_file_cache_handles_nonexistent_file() {
    // Test that cleanup_file_cache gracefully handles files that were never analyzed
    let db = FixtureDatabase::new();

    // Should not panic when cleaning up a file that doesn't exist in cache
    let fake_path = PathBuf::from("/nonexistent/path/test.py");
    db.cleanup_file_cache(&fake_path);

    // Database should remain functional
    assert!(db.definitions.is_empty());
    assert!(db.line_index_cache.is_empty());
}

#[test]
#[timeout(10000)]
fn test_usage_by_fixture_reverse_index() {
    // Test that the usage_by_fixture reverse index is properly maintained
    let temp_dir = TempDir::new().unwrap();

    let content = r#"
import pytest

@pytest.fixture
def shared_fixture():
    return 42

@pytest.fixture
def another_fixture(shared_fixture):
    return shared_fixture + 1

def test_one(shared_fixture):
    assert shared_fixture == 42

def test_two(shared_fixture, another_fixture):
    assert shared_fixture == 42
"#;

    let file_path = create_temp_test_file(&temp_dir, "test_reverse_index.py", content);
    let canonical_path = file_path.canonicalize().unwrap();

    let db = FixtureDatabase::new();
    db.analyze_file(file_path.clone(), content);

    // Verify reverse index contains usages for shared_fixture
    assert!(
        db.usage_by_fixture.contains_key("shared_fixture"),
        "usage_by_fixture should track shared_fixture usages"
    );

    {
        // Scope the DashMap reference to avoid holding read lock across analyze_file
        let usages = db.usage_by_fixture.get("shared_fixture").unwrap();
        // shared_fixture is used in: another_fixture (dependency), test_one, test_two
        assert_eq!(
            usages.len(),
            3,
            "shared_fixture should have 3 usages (1 fixture dep + 2 tests)"
        );

        // Verify all usages point to the correct file
        for (path, usage) in usages.iter() {
            assert_eq!(*path, canonical_path);
            assert_eq!(usage.name, "shared_fixture");
        }
    }

    // Re-analyze with fewer usages - verify cleanup works
    let updated_content = r#"
import pytest

@pytest.fixture
def shared_fixture():
    return 42

def test_one(shared_fixture):
    assert shared_fixture == 42
"#;

    db.analyze_file(file_path.clone(), updated_content);

    // Now should only have 1 usage
    let usages = db.usage_by_fixture.get("shared_fixture").unwrap();
    assert_eq!(
        usages.len(),
        1,
        "shared_fixture should now have only 1 usage after re-analysis"
    );
}

#[test]
#[timeout(10000)]
fn test_available_fixtures_cache() {
    // Test that get_available_fixtures caches results and invalidates on changes
    let temp_dir = TempDir::new().unwrap();

    let conftest_content = r#"
import pytest

@pytest.fixture
def root_fixture():
    return "root"
"#;

    let test_content = r#"
def test_something(root_fixture):
    pass
"#;

    let conftest_path = create_temp_test_file(&temp_dir, "conftest.py", conftest_content);
    let test_path = create_temp_test_file(&temp_dir, "test_cache.py", test_content);
    let canonical_test_path = test_path.canonicalize().unwrap();

    let db = FixtureDatabase::new();
    db.analyze_file(conftest_path.clone(), conftest_content);
    db.analyze_file(test_path.clone(), test_content);

    // First call should compute and cache
    let fixtures1 = db.get_available_fixtures(&canonical_test_path);
    assert_eq!(fixtures1.len(), 1);
    assert_eq!(fixtures1[0].name, "root_fixture");

    // Cache should now contain this file
    assert!(
        db.available_fixtures_cache
            .contains_key(&canonical_test_path),
        "available_fixtures_cache should contain the file after get_available_fixtures"
    );

    // Second call should use cache (same result)
    let fixtures2 = db.get_available_fixtures(&canonical_test_path);
    assert_eq!(fixtures1.len(), fixtures2.len());
    assert_eq!(fixtures1[0].name, fixtures2[0].name);

    // Add a new fixture - this should invalidate the cache via version bump
    let updated_conftest = r#"
import pytest

@pytest.fixture
def root_fixture():
    return "root"

@pytest.fixture
def new_fixture():
    return "new"
"#;

    db.analyze_file(conftest_path.clone(), updated_conftest);

    // Cache should be invalidated, new fixtures should be returned
    let fixtures3 = db.get_available_fixtures(&canonical_test_path);
    assert_eq!(
        fixtures3.len(),
        2,
        "Should now have 2 fixtures after cache invalidation"
    );

    let fixture_names: Vec<&str> = fixtures3.iter().map(|f| f.name.as_str()).collect();
    assert!(fixture_names.contains(&"root_fixture"));
    assert!(fixture_names.contains(&"new_fixture"));
}

#[test]
#[timeout(10000)]
fn test_cleanup_file_cache_clears_available_fixtures_cache() {
    // Test that cleanup_file_cache also removes available_fixtures_cache entries
    let temp_dir = TempDir::new().unwrap();

    let content = r#"
import pytest

@pytest.fixture
def my_fixture():
    return 42

def test_one(my_fixture):
    pass
"#;

    let file_path = create_temp_test_file(&temp_dir, "test_cleanup_avail.py", content);
    let canonical_path = file_path.canonicalize().unwrap();

    let db = FixtureDatabase::new();
    db.analyze_file(file_path.clone(), content);

    // Trigger caching by calling get_available_fixtures
    let _ = db.get_available_fixtures(&canonical_path);

    // Verify cache is populated
    assert!(
        db.available_fixtures_cache.contains_key(&canonical_path),
        "available_fixtures_cache should contain the file"
    );

    // Clean up caches
    db.cleanup_file_cache(&file_path);

    // Verify available_fixtures_cache is cleared
    assert!(
        !db.available_fixtures_cache.contains_key(&canonical_path),
        "available_fixtures_cache should be empty after cleanup"
    );
}

#[test]
#[timeout(10000)]
fn test_find_references_uses_reverse_index() {
    // Test that find_references_for_definition uses the reverse index efficiently
    let temp_dir = TempDir::new().unwrap();

    let conftest = r#"
import pytest

@pytest.fixture
def base_fixture():
    return "base"
"#;

    // Create multiple test files that use the fixture
    let test1 = r#"
def test_a(base_fixture):
    pass
"#;
    let test2 = r#"
def test_b(base_fixture):
    pass
"#;
    let test3 = r#"
def test_c(base_fixture):
    pass
"#;

    let _conftest_path = create_temp_test_file(&temp_dir, "conftest.py", conftest);
    create_temp_test_file(&temp_dir, "test_a.py", test1);
    create_temp_test_file(&temp_dir, "test_b.py", test2);
    create_temp_test_file(&temp_dir, "test_c.py", test3);

    let db = FixtureDatabase::new();
    db.scan_workspace(temp_dir.path());

    // Get the fixture definition
    let definition = db.definitions.get("base_fixture").unwrap()[0].clone();

    // Find references using the reverse index
    let references = db.find_references_for_definition(&definition);

    // Should find 3 references (one in each test file)
    assert_eq!(
        references.len(),
        3,
        "Should find 3 references across test files"
    );

    // Verify the reverse index has the right data
    let usage_entries = db.usage_by_fixture.get("base_fixture").unwrap();
    assert_eq!(
        usage_entries.len(),
        3,
        "Reverse index should have 3 entries"
    );
}

#[test]
#[timeout(30000)]
fn test_cache_handles_many_files() {
    // Test that the cache can handle many files without issues
    // The eviction logic is triggered internally when analyzing files
    let db = FixtureDatabase::new();

    // Analyze many files to build up the cache
    let content = r#"
import pytest

@pytest.fixture
def fixture_{n}():
    return {n}
"#;

    // Add many files - the cache eviction is called internally after each analyze
    for i in 0..100 {
        let file_content = content.replace("{n}", &i.to_string());
        let path = PathBuf::from(format!("/tmp/test_cache_many/file_{}.py", i));
        db.analyze_file(path.clone(), &file_content);
    }

    // Verify files were analyzed and cached
    assert!(
        db.file_cache.len() >= 100,
        "Should have at least 100 files in cache"
    );

    // Verify all fixtures were detected
    for i in 0..100 {
        let fixture_name = format!("fixture_{}", i);
        assert!(
            db.definitions.contains_key(&fixture_name),
            "fixture_{} should be detected",
            i
        );
    }
}