zccache 1.9.1

Local-first compiler cache for C/C++/Rust/Emscripten
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
//! Filesystem verification for cached metadata.
//!
//! Bridges the in-memory `MetadataCache` with the real filesystem:
//! stat files, verify cached entries, and compute content hashes on demand.

use super::metadata::{Confidence, FileMetadata, MetadataCache};
use crate::core::NormalizedPath;
use crate::core::Result;
use crate::hash::ContentHash;
use std::path::Path;
use std::time::Instant;

/// Result of verifying cached metadata against the current filesystem state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VerifyResult {
    /// File metadata matches: `(mtime, size)` unchanged.
    Fresh,
    /// File metadata changed: `mtime` or `size` differs.
    Stale,
    /// File no longer exists at this path.
    Gone,
}

impl MetadataCache {
    /// Stat a file and return its current metadata at `High` confidence.
    ///
    /// This does NOT insert into the cache — it only reads from the filesystem.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be stat'd (not found, permission denied, etc.).
    pub fn stat_file(path: &Path) -> Result<FileMetadata> {
        let fs_meta = std::fs::metadata(path)?;
        let mtime = fs_meta.modified()?;
        let size = fs_meta.len();

        Ok(FileMetadata {
            mtime,
            size,
            confidence: Confidence::High,
            last_verified: Instant::now(),
            content_hash: None,
        })
    }

    /// Verify whether the cached entry for `path` still matches the filesystem.
    ///
    /// Compares `(mtime, size)` from the cache against a fresh stat.
    /// Does NOT update the cache — callers decide what to do with the result.
    ///
    /// Returns `Gone` if the file no longer exists.
    /// Returns `Err` if the path is not in the cache (use `stat_file` instead).
    pub fn verify(&self, path: &Path) -> Result<VerifyResult> {
        let normalized = NormalizedPath::from(path);
        let cached = self
            .get(&normalized)
            .ok_or_else(|| crate::core::Error::Cache {
                message: format!("path not in cache: {}", path.display()),
            })?;

        let fresh = match Self::stat_file(path) {
            Ok(m) => m,
            Err(crate::core::Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => {
                return Ok(VerifyResult::Gone);
            }
            Err(e) => return Err(e),
        };

        if cached.mtime != fresh.mtime || cached.size != fresh.size {
            return Ok(VerifyResult::Stale);
        }

        Ok(VerifyResult::Fresh)
    }

    /// Full cache lookup: check cache, stat-verify, hash if needed.
    ///
    /// Implements the lookup flow from the design doc:
    /// - Cache miss → stat, hash, insert at High, return hash.
    /// - High/Medium confidence + metadata match → return cached hash.
    /// - Low confidence + metadata match → re-hash anyway (low trust).
    /// - Metadata mismatch → re-hash.
    ///
    /// Handles the TOCTOU race: stats before and after hashing, retries if unstable.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read.
    pub fn lookup(&self, path: &Path) -> Result<ContentHash> {
        let normalized = NormalizedPath::from(path);
        let cached = self.get(&normalized);

        // Cache miss → stat, hash, insert.
        let entry = match cached {
            None => return self.hash_and_insert(path),
            Some(e) => e,
        };

        // Always stat-verify before returning a hit.
        // "A wrong cache hit is catastrophic; an extra stat is cheap."
        let fresh = match Self::stat_file(path) {
            Ok(m) => m,
            Err(crate::core::Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => {
                self.remove(&normalized);
                return Err(crate::core::Error::FileNotFound(path.into()));
            }
            Err(e) => return Err(e),
        };

        let metadata_matches = entry.mtime == fresh.mtime && entry.size == fresh.size;

        if metadata_matches {
            match entry.confidence {
                Confidence::High | Confidence::Medium => {
                    // Metadata matches → promote to High, reuse cached hash if present.
                    if let Some(hash_bytes) = entry.content_hash {
                        self.insert(
                            path.into(),
                            FileMetadata {
                                confidence: Confidence::High,
                                last_verified: Instant::now(),
                                ..entry
                            },
                        );
                        return Ok(ContentHash::from_bytes(hash_bytes));
                    }
                    // No cached hash — hash now.
                }
                Confidence::Low => {
                    // Low trust: metadata matches but re-hash anyway (per design doc).
                }
            }
        }

        // Stale, Low, or no cached hash — re-hash.
        self.hash_and_insert(path)
    }

    /// Stat the file, hash it, insert at High confidence, return hash.
    fn hash_and_insert(&self, path: &Path) -> Result<ContentHash> {
        let pre_stat = Self::stat_file(path)?;
        let hash = crate::hash::hash_file(path)?;
        let post_stat = Self::stat_file(path)?;

        // TOCTOU check: if file changed during hashing, retry up to 3 times.
        if pre_stat.mtime != post_stat.mtime || pre_stat.size != post_stat.size {
            for _ in 0..3 {
                let pre = Self::stat_file(path)?;
                let h = crate::hash::hash_file(path)?;
                let post = Self::stat_file(path)?;
                if pre.mtime == post.mtime && pre.size == post.size {
                    self.insert(
                        path.into(),
                        FileMetadata {
                            content_hash: Some(*h.as_bytes()),
                            ..post
                        },
                    );
                    return Ok(h);
                }
            }
            // Still unstable after retries — return last hash but at Low confidence.
            let meta = Self::stat_file(path)?;
            self.insert(
                path.into(),
                FileMetadata {
                    confidence: Confidence::Low,
                    content_hash: Some(*hash.as_bytes()),
                    ..meta
                },
            );
            return Ok(hash);
        }

        self.insert(
            path.into(),
            FileMetadata {
                content_hash: Some(*hash.as_bytes()),
                ..post_stat
            },
        );
        Ok(hash)
    }
}

#[cfg(test)]
mod tests {
    use super::Confidence;
    use super::*;
    use crate::core::NormalizedPath;
    use std::fs;
    use std::thread;
    use std::time::Duration;
    use tempfile::TempDir;

    /// Helper: create a file with given content, return its path.
    fn create_file(dir: &TempDir, name: &str, content: &str) -> NormalizedPath {
        let path = dir.path().join(name);
        fs::write(&path, content).expect("failed to create test file");
        path.into()
    }

    /// Helper: sleep enough for mtime to differ.
    /// Filesystem timestamps have varying granularity (FAT32: 2s, NTFS: 100ns,
    /// ext4: 1ns, HFS+: 1s). We sleep 1.1s to be safe across all platforms.
    fn sleep_for_mtime() {
        thread::sleep(Duration::from_millis(1100));
    }

    // ---------------------------------------------------------------
    // stat_file
    // ---------------------------------------------------------------

    #[test]
    fn stat_file_returns_metadata_for_existing_file() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "hello.c", "int main() { return 0; }");

        let meta = MetadataCache::stat_file(&path).unwrap();

        assert_eq!(meta.size, 24);
        assert_eq!(meta.confidence, Confidence::High);
        assert!(meta.content_hash.is_none()); // stat doesn't hash
    }

    #[test]
    fn stat_file_fails_for_missing_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("nonexistent.c");

        let result = MetadataCache::stat_file(&path);
        assert!(result.is_err());
    }

    #[test]
    fn stat_file_captures_correct_size() {
        let dir = TempDir::new().unwrap();
        let content = "a".repeat(4096);
        let path = create_file(&dir, "big.c", &content);

        let meta = MetadataCache::stat_file(&path).unwrap();
        assert_eq!(meta.size, 4096);
    }

    // ---------------------------------------------------------------
    // verify: unchanged file
    // ---------------------------------------------------------------

    #[test]
    fn verify_unchanged_file_returns_fresh() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "stable.c", "#include <stdio.h>");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Fresh);
    }

    // ---------------------------------------------------------------
    // verify: touch (mtime change, same content)
    // ---------------------------------------------------------------

    #[test]
    fn verify_detects_touch() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "touched.c", "void f() {}");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        // Touch: rewrite same content after a delay so mtime changes
        sleep_for_mtime();
        fs::write(&path, "void f() {}").unwrap();

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Stale);
    }

    // ---------------------------------------------------------------
    // verify: edit (content + size change)
    // ---------------------------------------------------------------

    #[test]
    fn verify_detects_content_edit() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "edited.c", "int x = 1;");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        sleep_for_mtime();
        fs::write(&path, "int x = 42; // changed").unwrap();

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Stale);
    }

    #[test]
    fn verify_detects_size_change_same_mtime_granularity() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "sized.c", "short");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        // Write different-length content immediately (mtime might not change on coarse FS)
        fs::write(&path, "much longer content than before").unwrap();

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Stale);
    }

    // ---------------------------------------------------------------
    // verify: file removal
    // ---------------------------------------------------------------

    #[test]
    fn verify_detects_removed_file() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "doomed.c", "goodbye");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        fs::remove_file(&path).unwrap();

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Gone);
    }

    // ---------------------------------------------------------------
    // verify: file replacement (delete + recreate at same path)
    // ---------------------------------------------------------------

    #[test]
    fn verify_detects_file_replacement() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "replaced.c", "original");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        // Replace: delete and recreate — mtime will differ
        fs::remove_file(&path).unwrap();
        sleep_for_mtime();
        fs::write(&path, "original").unwrap(); // same content!

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Stale);
    }

    // ---------------------------------------------------------------
    // verify: uncached path returns error
    // ---------------------------------------------------------------

    #[test]
    fn verify_uncached_path_returns_error() {
        let cache = MetadataCache::new();
        let result = cache.verify(Path::new("/no/such/entry"));
        assert!(result.is_err());
    }

    // ---------------------------------------------------------------
    // lookup: cache miss → stat + hash + insert
    // ---------------------------------------------------------------

    #[test]
    fn lookup_miss_stats_hashes_and_caches() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "new.c", "int main() {}");

        let cache = MetadataCache::new();
        assert!(cache.is_empty());

        let hash = cache.lookup(&path).unwrap();

        assert_eq!(cache.len(), 1);
        let entry = cache.get(&path).unwrap();
        assert_eq!(entry.confidence, Confidence::High);
        assert!(entry.content_hash.is_some());

        let expected = crate::hash::hash_file(&path).unwrap();
        assert_eq!(hash, expected);
    }

    // ---------------------------------------------------------------
    // lookup: cache hit, file unchanged → returns cached hash
    // ---------------------------------------------------------------

    #[test]
    fn lookup_hit_unchanged_returns_cached_hash() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "stable.c", "const int N = 42;");

        let cache = MetadataCache::new();
        let hash1 = cache.lookup(&path).unwrap();
        let hash2 = cache.lookup(&path).unwrap();

        assert_eq!(hash1, hash2);
    }

    // ---------------------------------------------------------------
    // lookup: cache hit, file edited → re-hashes
    // ---------------------------------------------------------------

    #[test]
    fn lookup_rehashes_after_edit() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "evolving.c", "v1");

        let cache = MetadataCache::new();
        let hash_v1 = cache.lookup(&path).unwrap();

        sleep_for_mtime();
        fs::write(&path, "v2 with more content").unwrap();

        let hash_v2 = cache.lookup(&path).unwrap();
        assert_ne!(hash_v1, hash_v2);

        let expected = crate::hash::hash_bytes(b"v2 with more content");
        assert_eq!(hash_v2, expected);
    }

    // ---------------------------------------------------------------
    // lookup: file removed between lookups → error
    // ---------------------------------------------------------------

    #[test]
    fn lookup_fails_after_removal() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "ephemeral.c", "here today");

        let cache = MetadataCache::new();
        let _hash = cache.lookup(&path).unwrap();

        fs::remove_file(&path).unwrap();

        let result = cache.lookup(&path);
        assert!(result.is_err());
    }

    // ---------------------------------------------------------------
    // lookup: downgraded entry forces re-verification
    // ---------------------------------------------------------------

    #[test]
    fn lookup_after_downgrade_reverifies() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "watched.c", "stable content");

        let cache = MetadataCache::new();
        let hash1 = cache.lookup(&path).unwrap();

        cache.downgrade_all();

        let hash2 = cache.lookup(&path).unwrap();
        assert_eq!(hash1, hash2);

        let entry = cache.get(&path).unwrap();
        assert_eq!(entry.confidence, Confidence::High);
    }

    // ---------------------------------------------------------------
    // lookup: downgraded entry + file changed → detects change
    // ---------------------------------------------------------------

    #[test]
    fn lookup_after_downgrade_detects_change() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "sneaky.c", "before");

        let cache = MetadataCache::new();
        let hash_before = cache.lookup(&path).unwrap();

        cache.downgrade(&path);

        sleep_for_mtime();
        fs::write(&path, "after").unwrap();

        let hash_after = cache.lookup(&path).unwrap();
        assert_ne!(hash_before, hash_after);
    }

    // ---------------------------------------------------------------
    // multiple files: independence
    // ---------------------------------------------------------------

    #[test]
    fn changes_to_one_file_dont_affect_others() {
        let dir = TempDir::new().unwrap();
        let path_a = create_file(&dir, "a.c", "file a");
        let path_b = create_file(&dir, "b.c", "file b");

        let cache = MetadataCache::new();
        let hash_a = cache.lookup(&path_a).unwrap();
        let hash_b = cache.lookup(&path_b).unwrap();

        sleep_for_mtime();
        fs::write(&path_b, "file b modified").unwrap();

        let hash_a2 = cache.lookup(&path_a).unwrap();
        assert_eq!(hash_a, hash_a2);

        let hash_b2 = cache.lookup(&path_b).unwrap();
        assert_ne!(hash_b, hash_b2);
    }

    // ---------------------------------------------------------------
    // content hash is cleared on re-stat after change
    // ---------------------------------------------------------------

    #[test]
    fn content_hash_invalidated_on_change() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "hashme.c", "original");

        let cache = MetadataCache::new();
        let _hash = cache.lookup(&path).unwrap();

        let entry = cache.get(&path).unwrap();
        assert!(entry.content_hash.is_some());

        sleep_for_mtime();
        fs::write(&path, "changed content").unwrap();

        let new_hash = cache.lookup(&path).unwrap();
        let expected = crate::hash::hash_bytes(b"changed content");
        assert_eq!(new_hash, expected);
    }

    // ---------------------------------------------------------------
    // sequential edits: cache tracks the full lifecycle
    // ---------------------------------------------------------------

    #[test]
    fn full_lifecycle_create_edit_edit_remove() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("lifecycle.c");

        let cache = MetadataCache::new();

        // 1. File doesn't exist yet
        assert!(cache.lookup(&path).is_err());

        // 2. Create the file
        fs::write(&path, "version 1").unwrap();
        let hash_v1 = cache.lookup(&path).unwrap();
        assert_eq!(hash_v1, crate::hash::hash_bytes(b"version 1"));

        // 3. First edit
        sleep_for_mtime();
        fs::write(&path, "version 2").unwrap();
        let hash_v2 = cache.lookup(&path).unwrap();
        assert_ne!(hash_v1, hash_v2);
        assert_eq!(hash_v2, crate::hash::hash_bytes(b"version 2"));

        // 4. Second edit
        sleep_for_mtime();
        fs::write(&path, "version 3 with extra stuff").unwrap();
        let hash_v3 = cache.lookup(&path).unwrap();
        assert_ne!(hash_v2, hash_v3);
        assert_eq!(
            hash_v3,
            crate::hash::hash_bytes(b"version 3 with extra stuff")
        );

        // 5. Remove
        fs::remove_file(&path).unwrap();
        assert!(cache.lookup(&path).is_err());

        // 6. Cache entry should be gone (or at least lookup returns error)
        assert!(cache.lookup(&path).is_err());
    }

    // ---------------------------------------------------------------
    // append-only edit (size grows, mtime changes)
    // ---------------------------------------------------------------

    #[test]
    fn verify_detects_append() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "growing.c", "line 1\n");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        sleep_for_mtime();
        use std::io::Write;
        let mut f = fs::OpenOptions::new().append(true).open(&path).unwrap();
        f.write_all(b"line 2\n").unwrap();
        drop(f);

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Stale);
    }

    // ---------------------------------------------------------------
    // truncate to empty
    // ---------------------------------------------------------------

    #[test]
    fn verify_detects_truncation() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "truncated.c", "lots of content here");

        let cache = MetadataCache::new();
        let meta = MetadataCache::stat_file(&path).unwrap();
        cache.insert(path.clone(), meta);

        sleep_for_mtime();
        fs::write(&path, "").unwrap();

        let result = cache.verify(&path).unwrap();
        assert_eq!(result, VerifyResult::Stale);
    }

    // ---------------------------------------------------------------
    // lookup: empty file
    // ---------------------------------------------------------------

    #[test]
    fn lookup_empty_file() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "empty.c", "");

        let cache = MetadataCache::new();
        let hash = cache.lookup(&path).unwrap();
        assert_eq!(hash, crate::hash::hash_bytes(b""));
    }

    // ---------------------------------------------------------------
    // lookup: file not found on cache miss
    // ---------------------------------------------------------------

    #[test]
    fn lookup_cache_miss_nonexistent() {
        let cache = MetadataCache::new();
        let result = cache.lookup(Path::new("/no/such/file.c"));
        assert!(result.is_err());
    }

    // ---------------------------------------------------------------
    // rescan_all: Low entries promoted if filesystem matches
    // ---------------------------------------------------------------

    #[test]
    fn rescan_all_promotes_matching_entries() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "stable.c", "unchanged");

        let cache = MetadataCache::new();
        cache.lookup(&path).unwrap();

        // Simulate overflow: downgrade to Low.
        cache.downgrade_all();
        assert_eq!(cache.get(&path).unwrap().confidence, Confidence::Low);

        // Rescan: file unchanged → promoted back to High.
        let promoted = cache.rescan_all();
        assert_eq!(promoted, 1);
        assert_eq!(cache.get(&path).unwrap().confidence, Confidence::High);
    }

    #[test]
    fn rescan_all_leaves_changed_entries_low() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "changed.c", "v1");

        let cache = MetadataCache::new();
        cache.lookup(&path).unwrap();
        cache.downgrade_all();

        sleep_for_mtime();
        fs::write(&path, "v2 longer content").unwrap();

        let promoted = cache.rescan_all();
        assert_eq!(promoted, 0);
        assert_eq!(cache.get(&path).unwrap().confidence, Confidence::Low);
    }

    #[test]
    fn rescan_all_skips_high_entries() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "high.c", "content");

        let cache = MetadataCache::new();
        cache.lookup(&path).unwrap();

        // Entry is High — rescan should not count it.
        let promoted = cache.rescan_all();
        assert_eq!(promoted, 0);
    }

    #[test]
    fn rescan_all_handles_removed_files() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "gone.c", "bye");

        let cache = MetadataCache::new();
        cache.lookup(&path).unwrap();
        cache.downgrade_all();

        fs::remove_file(&path).unwrap();

        let promoted = cache.rescan_all();
        assert_eq!(promoted, 0);
        assert_eq!(cache.get(&path).unwrap().confidence, Confidence::Low);
    }

    #[test]
    fn rescan_all_mixed_entries() {
        let dir = TempDir::new().unwrap();
        let path_unchanged = create_file(&dir, "same.c", "same");
        let path_changed = create_file(&dir, "diff.c", "old");
        let path_gone = create_file(&dir, "gone.c", "bye");

        let cache = MetadataCache::new();
        cache.lookup(&path_unchanged).unwrap();
        cache.lookup(&path_changed).unwrap();
        cache.lookup(&path_gone).unwrap();

        cache.downgrade_all();

        sleep_for_mtime();
        fs::write(&path_changed, "new content").unwrap();
        fs::remove_file(&path_gone).unwrap();

        let promoted = cache.rescan_all();
        assert_eq!(promoted, 1); // only path_unchanged
        assert_eq!(
            cache.get(&path_unchanged).unwrap().confidence,
            Confidence::High
        );
        assert_eq!(
            cache.get(&path_changed).unwrap().confidence,
            Confidence::Low
        );
        assert_eq!(cache.get(&path_gone).unwrap().confidence, Confidence::Low);
    }

    #[test]
    fn rescan_all_empty_cache() {
        let cache = MetadataCache::new();
        assert_eq!(cache.rescan_all(), 0);
    }

    // ---------------------------------------------------------------
    // lookup: High confidence with cached hash returns it directly
    // ---------------------------------------------------------------

    #[test]
    fn lookup_high_confidence_with_hash_returns_cached() {
        let dir = TempDir::new().unwrap();
        let path = create_file(&dir, "cached.c", "content");

        let cache = MetadataCache::new();
        let hash1 = cache.lookup(&path).unwrap();

        // Entry should be High with hash. Second lookup should reuse it.
        let entry = cache.get(&path).unwrap();
        assert_eq!(entry.confidence, Confidence::High);
        assert!(entry.content_hash.is_some());

        let hash2 = cache.lookup(&path).unwrap();
        assert_eq!(hash1, hash2);
    }

    // ---------------------------------------------------------------
    // verify: all three result variants
    // ---------------------------------------------------------------

    #[test]
    fn verify_result_variants() {
        assert_eq!(VerifyResult::Fresh, VerifyResult::Fresh);
        assert_ne!(VerifyResult::Fresh, VerifyResult::Stale);
        assert_ne!(VerifyResult::Fresh, VerifyResult::Gone);
        assert_ne!(VerifyResult::Stale, VerifyResult::Gone);
    }

    // ---------------------------------------------------------------
    // many files: bulk operations
    // ---------------------------------------------------------------

    #[test]
    fn bulk_lookup_and_selective_invalidation() {
        let dir = TempDir::new().unwrap();
        let mut paths = Vec::new();
        let mut original_hashes = Vec::new();

        for i in 0..20 {
            let path = create_file(&dir, &format!("file_{i}.c"), &format!("content {i}"));
            paths.push(path);
        }

        let cache = MetadataCache::new();

        for path in &paths {
            let hash = cache.lookup(path).unwrap();
            original_hashes.push(hash);
        }
        assert_eq!(cache.len(), 20);

        sleep_for_mtime();
        for (i, path) in paths.iter().enumerate() {
            if i % 3 == 0 {
                fs::write(path, format!("modified content {i}")).unwrap();
            }
        }

        for (i, path) in paths.iter().enumerate() {
            let hash = cache.lookup(path).unwrap();
            if i % 3 == 0 {
                assert_ne!(hash, original_hashes[i], "file {i} should have changed");
            } else {
                assert_eq!(hash, original_hashes[i], "file {i} should be unchanged");
            }
        }
    }
}