folder-auto-banner 0.7.14

Folder Auto Banner — A directory listing with instant context
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
//! Per-directory on-disk cache of `BannerData`.
//!
//! The daemon's IPC `read4` (4-byte length prefix) roundtrip has a
//! kernel-scheduling floor of ~1–10 ms on Linux, even for a tiny
//! 4-byte read on a Unix socket. For the `f <path>` use case (a fresh
//! process on every invocation, often called several times per second
//! when browsing), that floor dominates the wall-clock cost of an
//! otherwise-fast call.
//!
//! To skip the IPC for cache hits, the daemon writes the serialized
//! `BannerData` to a per-path file under `banner_data/` after every
//! successful banner compute (both cache miss and cache hit), with the
//! file's mtime set to the compute time. The client, before opening a
//! Unix-socket connection, checks whether the file exists and whether
//! its mtime is within the daemon's `CACHE_TTL`. If so, it reads and
//! deserializes the file directly and skips the IPC entirely.
//!
//! A 4-byte read of a Unix-socket response is dominated by kernel
//! scheduling (1–10 ms); a stat + read of a 70 KB file is dominated by
//! page-cache hits (<0.1 ms). The disk path is therefore typically
//! 5–50× faster than the IPC path for a warm cache hit.
//!
//! The cache file is owned by the daemon, not the client, so cache
//! invalidation (TTL expiry, root-mtime change) is handled by the
//! daemon's existing banner-cache logic. The file's mtime is the
//! freshness signal; if the daemon's in-memory cache is invalidated,
//! the next banner compute re-writes the file with a new mtime.

use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::daemon_types::{BannerData, MAX_IPC_FRAME_SIZE};

/// Maximum age of a cache file before the client considers it stale.
/// Must match the daemon's `CACHE_TTL` (`Duration::from_secs(300)` in
/// `src/daemon.rs`). Exposed here as a constant so the client doesn't
/// need to read it from the daemon at startup.
pub const CACHE_TTL: Duration = Duration::from_secs(300);

/// Maximum number of raw descendant entries inspected for cache freshness.
const MAX_DESCENDANT_ENTRIES: usize = 8192;

/// Subdirectory under the data dir where per-path cache files live.
const CACHE_SUBDIR: &str = "banner_data";

/// Returns the cache file path for the given directory, e.g.
/// `~/.local/share/fab/banner_data/3f8a2b1c...d4e5.json`.
///
/// The hash is a stable FNV-1a 64-bit digest of the canonicalized path
/// (lowercased on Windows would be needed for cross-platform stability,
/// but Unix paths are case-sensitive so we hash as-is).
pub fn cache_file_path(path: &Path) -> Option<PathBuf> {
    let data_dir = data_dir()?;
    let cache_dir = data_dir.join(CACHE_SUBDIR);
    if let Ok(metadata) = std::fs::symlink_metadata(&cache_dir) {
        if metadata.file_type().is_symlink() || !metadata.is_dir() {
            return None;
        }
    }
    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    let hash = fnv1a_64(canonical.to_string_lossy().as_bytes());
    Some(cache_dir.join(format!("{:016x}.json", hash)))
}

/// Returns the cache directory, creating it if necessary.
fn data_dir() -> Option<PathBuf> {
    crate::state::get_data_dir().ok()
}

/// FNV-1a 64-bit hash. Stable across Rust versions and platforms
/// (unlike `DefaultHasher`).
fn fnv1a_64(bytes: &[u8]) -> u64 {
    const FNV_OFFSET: u64 = 0xcbf29ce484222325;
    const FNV_PRIME: u64 = 0x100000001b3;
    let mut h = FNV_OFFSET;
    for b in bytes {
        h ^= *b as u64;
        h = h.wrapping_mul(FNV_PRIME);
    }
    h
}

/// Returns the mtime of the cache file for `path`, or `None` if the
/// file does not exist, is a directory, or its mtime cannot be read.
pub fn cache_file_mtime(path: &Path) -> Option<SystemTime> {
    let file = cache_file_path(path)?;
    let meta = std::fs::symlink_metadata(&file).ok()?;
    if meta.file_type().is_symlink() || meta.is_dir() || meta.len() > MAX_IPC_FRAME_SIZE as u64 {
        return None;
    }
    meta.modified().ok()
}

/// Returns the mtime of the directory at `path`. Used to validate that
/// a cache file's mtime is not older than the directory it describes
/// (if a user changes files in the directory, the directory's mtime
/// advances; the cache file's mtime should track that).
pub fn directory_mtime(path: &Path) -> Option<SystemTime> {
    let meta = std::fs::metadata(path).ok()?;
    meta.modified().ok()
}

/// Returns true if the (lower-cased) file name has an extension that the
/// daemon runs a content probe on (text files, images, archives, etc.).
/// Used to limit the per-file mtime staleness check to files that could
/// actually have changed banner metadata. This is the canonical list —
/// the daemon's inotify watcher delegates to it.
pub fn is_content_probe_ext(lower_name: &str) -> bool {
    lower_name.ends_with(".txt")
        || lower_name.ends_with(".md")
        || lower_name.ends_with(".json")
        || lower_name.ends_with(".js")
        || lower_name.ends_with(".ts")
        || lower_name.ends_with(".jsx")
        || lower_name.ends_with(".tsx")
        || lower_name.ends_with(".rs")
        || lower_name.ends_with(".py")
        || lower_name.ends_with(".go")
        || lower_name.ends_with(".java")
        || lower_name.ends_with(".rb")
        || lower_name.ends_with(".c")
        || lower_name.ends_with(".cpp")
        || lower_name.ends_with(".h")
        || lower_name.ends_with(".hpp")
        || lower_name.ends_with(".sh")
        || lower_name.ends_with(".yaml")
        || lower_name.ends_with(".yml")
        || lower_name.ends_with(".toml")
        || lower_name.ends_with(".xml")
        || lower_name.ends_with(".html")
        || lower_name.ends_with(".css")
        || lower_name.ends_with(".scss")
        || lower_name.ends_with(".png")
        || lower_name.ends_with(".jpg")
        || lower_name.ends_with(".jpeg")
        || lower_name.ends_with(".gif")
        || lower_name.ends_with(".webp")
        || lower_name.ends_with(".bmp")
        || lower_name.ends_with(".tiff")
        || lower_name.ends_with(".zip")
        || lower_name.ends_with(".tar")
        || lower_name.ends_with(".gz")
        || lower_name.ends_with(".mp4")
        || lower_name.ends_with(".mov")
        || lower_name.ends_with(".mkv")
        || lower_name.ends_with(".webm")
        || lower_name.ends_with(".m4v")
}

/// Returns the maximum mtime of all files with content-probe
/// extensions under `path`, recursing into subdirectories with a
/// bounded walk (depth ≤ 8, ≤ 8192 entries visited, heavy dirs
/// skipped). Used to detect file content changes that don't advance
/// the directory's own mtime (e.g., editing a file in place). A
/// flat, one-level scan missed nested edits at depth ≥ 2, which
/// could keep banners stale for the full 300s cache TTL.
pub fn max_descendant_mtime(path: &Path) -> Option<SystemTime> {
    fn walk(path: &Path, depth: usize, visited: &mut usize) -> Option<SystemTime> {
        if depth > 8 || *visited >= MAX_DESCENDANT_ENTRIES {
            return None;
        }
        let entries = std::fs::read_dir(path).ok()?;
        let mut max: Option<SystemTime> = None;
        let remaining = MAX_DESCENDANT_ENTRIES.saturating_sub(*visited);
        for entry in entries.take(remaining).flatten() {
            if *visited >= MAX_DESCENDANT_ENTRIES {
                break;
            }
            *visited += 1;
            let name_str = entry.file_name().to_string_lossy().to_string();
            if let Ok(meta) = std::fs::symlink_metadata(entry.path()) {
                // The freshness walk must stay within the requested tree.
                // Following a symlink can traverse unrelated directories or
                // loop back into an ancestor.
                if meta.file_type().is_symlink() {
                    continue;
                }
                if meta.is_dir() {
                    // Skip heavyweight subtrees: probe-relevant content under
                    // them is not shown by the banner anyway.
                    if matches!(
                        name_str.as_ref(),
                        ".git" | "target" | "node_modules" | ".cache" | ".venv" | "build"
                    ) {
                        continue;
                    }
                    if let Some(mtime) = walk(&entry.path(), depth + 1, visited) {
                        max = Some(max.map_or(mtime, |m| m.max(mtime)));
                    }
                } else if meta.is_file() {
                    if !is_content_probe_ext(&name_str.to_ascii_lowercase()) {
                        continue;
                    }
                    if let Ok(mtime) = meta.modified() {
                        max = Some(max.map_or(mtime, |m| m.max(mtime)));
                    }
                }
            }
        }
        max
    }

    let mut visited = 0usize;
    walk(path, 0, &mut visited)
}

/// Returns `true` if the cache file for `path` exists, is younger than
/// `CACHE_TTL`, AND is not older than the directory or any of its
/// direct children. The last two checks guard against the case where
/// the user changed files in the directory (add/remove advances the
/// dir mtime; in-place edit advances the file's mtime but not the
/// dir mtime).
pub fn is_cache_fresh(path: &Path) -> bool {
    let Some(file_mtime) = cache_file_mtime(path) else {
        return false;
    };
    let Ok(age) = SystemTime::now().duration_since(file_mtime) else {
        return false;
    };
    if age >= CACHE_TTL {
        return false;
    }
    // A changed config can alter both the scan features and the rendered
    // columns. Do not serve a disk cache written before that change.
    if let Some(config_mtime) = crate::state::Config::config_path()
        .ok()
        .and_then(|config| std::fs::metadata(config).ok())
        .and_then(|metadata| metadata.modified().ok())
    {
        if config_mtime > file_mtime {
            return false;
        }
    }
    // Guard against stale data: if the directory's mtime is newer than
    // the cache file's mtime, the file is stale (e.g., a file was
    // added or removed).
    if let Some(dir_mtime) = directory_mtime(path) {
        if dir_mtime > file_mtime {
            return false;
        }
    }
    // Guard against in-place file edits that don't advance the dir
    // mtime: if any direct child's mtime is newer than the cache
    // file's mtime, the file is stale. O(N) stat calls, but page-cached
    // and fast (~0.6 ms for ~200 files).
    if let Some(max_child_mtime) = max_descendant_mtime(path) {
        if max_child_mtime > file_mtime {
            return false;
        }
    }
    true
}

/// Read and deserialize the cache file for `path`. Returns `None` if
/// the file is missing, unreadable, is a directory, or fails to
/// deserialize. If the path is a directory (corruption from a previous
/// bug, manual intervention, etc.), the directory is removed so the
/// daemon can write a fresh file on the next IPC call.
pub fn read_cache(path: &Path) -> Option<BannerData> {
    let file = cache_file_path(path)?;
    let meta = std::fs::symlink_metadata(&file).ok()?;
    if meta.file_type().is_symlink() {
        tracing::warn!(
            "Cache path is a symlink, refusing to read: {}",
            file.display()
        );
        return None;
    }
    if meta.is_dir() {
        tracing::warn!("Cache path is a directory, removing: {}", file.display());
        let _ = std::fs::remove_dir(&file);
        return None;
    }
    if meta.len() > MAX_IPC_FRAME_SIZE as u64 {
        tracing::warn!(
            "Cache file is too large, refusing to read: {}",
            file.display()
        );
        return None;
    }
    let bytes = std::fs::read(&file).ok()?;
    serde_json::from_slice(&bytes).ok()
}

/// Remove the per-path disk cache file. Called when the daemon invalidates
/// a banner: the file's mtime is the client's freshness signal, so a stale
/// file would keep serving pre-invalidation data on the client fast path.
pub fn remove_cache(path: &Path) {
    if let Some(file) = cache_file_path(path) {
        let _ = std::fs::remove_file(file);
    }
}

/// Serialize `data` and write it to the cache file for `path`. Sets
/// the file's mtime to `now`. Returns `Ok(())` on success.
///
/// This is a best-effort write: any I/O error is logged but does not
/// fail the caller's banner render, because the disk cache is a pure
/// performance optimization — the IPC path is still the fallback.
pub fn write_cache(path: &Path, data: &BannerData) -> std::io::Result<()> {
    let Some(file) = cache_file_path(path) else {
        return Ok(());
    };
    if let Some(parent) = file.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            tracing::warn!("Failed to create banner data cache directory: {}", e);
            return Ok(());
        }
    }
    let bytes = match serde_json::to_vec(data) {
        Ok(b) => b,
        Err(e) => {
            tracing::warn!("Failed to serialize banner data cache: {}", e);
            return Ok(());
        }
    };
    if bytes.len() > MAX_IPC_FRAME_SIZE {
        tracing::warn!(
            "Serialized banner data cache exceeds the maximum size: {} bytes",
            bytes.len()
        );
        return Ok(());
    }
    // If the path exists and is a directory (corruption from a previous
    // bug, manual intervention, or filesystem weirdness), remove it so
    // we can write a regular file. We only remove it if it's a directory
    // — never remove a regular file.
    if let Ok(meta) = std::fs::symlink_metadata(&file) {
        if meta.file_type().is_symlink() {
            tracing::warn!(
                "Cache path is a symlink, refusing to write: {}",
                file.display()
            );
            return Ok(());
        }
        if meta.is_dir() {
            tracing::warn!("Cache path is a directory, removing: {}", file.display());
            let _ = std::fs::remove_dir(&file);
        }
    }
    // Atomic write: temp file + rename in the same directory, so a
    // concurrent reader (client fast path) never observes a partially
    // written file.
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let tmp = file.with_extension(format!("json.{}.{}.tmp", std::process::id(), nonce));
    use std::io::Write;
    let mut tmp_file = match std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&tmp)
    {
        Ok(file) => file,
        Err(e) => {
            tracing::warn!("Failed to create banner data cache temp file: {}", e);
            return Ok(());
        }
    };
    if let Err(e) = tmp_file.write_all(&bytes) {
        let _ = std::fs::remove_file(&tmp);
        tracing::warn!("Failed to write banner data cache: {}", e);
        return Ok(());
    }
    if let Err(e) = tmp_file.sync_all() {
        let _ = std::fs::remove_file(&tmp);
        tracing::warn!("Failed to flush banner data cache: {}", e);
        return Ok(());
    }
    drop(tmp_file);
    if let Err(e) = std::fs::rename(&tmp, &file) {
        let _ = std::fs::remove_file(&tmp);
        tracing::warn!("Failed to rename banner data cache: {}", e);
        return Ok(());
    }
    // The mtime is implicitly set to "now" by the rename above; the
    // client's freshness check (banner_data_cache::is_cache_fresh) uses
    // std::fs::metadata().modified() to read it. No utimes/filetime call
    // is needed — std::fs::write + rename does the right thing.
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fs::{DirEntry, DirSummary, ProjectType};
    use std::fs;
    use std::time::Duration;

    fn make_test_banner_data() -> BannerData {
        BannerData {
            summary: DirSummary {
                total_items: 1,
                total_size: 42,
                files: 1,
                dirs: 0,
                truncated: false,
                top_items: vec![DirEntry {
                    name: "test.txt".to_string(),
                    path: PathBuf::from("/tmp/test.txt"),
                    is_dir: false,
                    is_file: true,
                    is_symlink: false,
                    is_exec: false,
                    size: 42,
                    modified: None,
                    perms: "rw-r--r--".to_string(),
                    owner: "dracon".to_string(),
                    group: "users".to_string(),
                    symlink_target: None,
                    symlink_valid: true,
                    content_probe: None,
                }],
                project_type: ProjectType::Generic,
                last_modified: None,
                build_status: None,
                todo_info: None,
                code_metrics: None,
                port_info: None,
                docker_info: None,
            },
            git_info: None,
        }
    }

    #[test]
    fn fnv1a_64_is_stable() {
        // Spot-check a few well-known values.
        assert_eq!(fnv1a_64(b""), 0xcbf29ce484222325);
        assert_eq!(fnv1a_64(b"a"), 0xaf63dc4c8601ec8c);
        assert_eq!(fnv1a_64(b"foobar"), 0x85944171f73967e8);
    }

    #[test]
    fn fnv1a_64_changes_with_input() {
        assert_ne!(fnv1a_64(b"a"), fnv1a_64(b"b"));
        assert_ne!(fnv1a_64(b"abc"), fnv1a_64(b"abd"));
    }

    #[test]
    fn cache_file_path_is_deterministic() {
        let p = Path::new("/tmp/some/path");
        let a = cache_file_path(p).unwrap();
        let b = cache_file_path(p).unwrap();
        assert_eq!(a, b);
    }

    #[test]
    fn cache_file_path_differs_per_input() {
        let a = cache_file_path(Path::new("/tmp/a")).unwrap();
        let b = cache_file_path(Path::new("/tmp/b")).unwrap();
        assert_ne!(a, b);
    }

    #[test]
    fn cache_file_path_uses_hex_hash() {
        let p = cache_file_path(Path::new("/tmp/test")).unwrap();
        let fname = p.file_name().unwrap().to_str().unwrap();
        // Must be 16 hex chars + .json
        assert_eq!(fname.len(), 21);
        assert!(fname.ends_with(".json"));
        let hash = &fname[..16];
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn nonexistent_cache_is_not_fresh() {
        // Use a path that almost certainly does not have a cache file.
        assert!(!is_cache_fresh(Path::new(
            "/tmp/this/path/should/not/exist/fab-test"
        )));
    }

    #[test]
    fn write_and_read_cache_roundtrip() {
        // Use a temp dir for the data dir by overriding the directories call.
        // We can't easily override directories::ProjectDirs, so we test
        // write_cache/read_cache with a real temp path.
        let tmp = std::env::temp_dir().join(format!("fab-test-{}", std::process::id()));
        let _ = fs::create_dir_all(&tmp);
        let test_path = tmp.join("test_dir");
        let _ = fs::create_dir_all(&test_path);

        // Write
        let data = make_test_banner_data();
        write_cache(&test_path, &data).unwrap();

        // The cache file should exist at the expected location
        let cache_file = cache_file_path(&test_path).unwrap();
        assert!(
            cache_file.exists(),
            "cache file should exist at {:?}",
            cache_file
        );

        // Read back
        let read_back = read_cache(&test_path).expect("read_cache should succeed");
        assert_eq!(read_back.summary.total_items, 1);
        assert_eq!(read_back.summary.total_size, 42);
        assert_eq!(read_back.summary.top_items.len(), 1);
        assert_eq!(read_back.summary.top_items[0].name, "test.txt");

        // Cleanup
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn old_cache_without_truncated_field_still_deserializes() {
        let data = make_test_banner_data();
        let mut value = serde_json::to_value(&data).unwrap();
        value
            .get_mut("summary")
            .and_then(serde_json::Value::as_object_mut)
            .expect("serialized banner data must contain a summary object")
            .remove("truncated");

        let restored: BannerData = serde_json::from_value(value).unwrap();
        assert!(!restored.summary.truncated);
    }

    #[test]
    fn write_cache_creates_parent_dir() {
        let tmp = std::env::temp_dir().join(format!("fab-test-parent-{}", std::process::id()));
        let test_path = tmp.join("nested/test_dir");
        let _ = fs::create_dir_all(&test_path);

        let cache_file = cache_file_path(&test_path).unwrap();
        let parent = cache_file.parent().unwrap();

        // Write should create the parent directory if missing
        let data = make_test_banner_data();
        write_cache(&test_path, &data).unwrap();
        assert!(parent.exists(), "parent dir should exist");
        assert!(cache_file.exists(), "cache file should exist");

        let _ = fs::remove_file(&cache_file);
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn read_cache_returns_none_for_missing_file() {
        // Path that doesn't have a cache file
        let result = read_cache(Path::new(
            "/tmp/this/path/should/not/exist/fab-test-missing",
        ));
        assert!(result.is_none());
    }

    #[test]
    fn read_cache_handles_corrupt_json() {
        let tmp = std::env::temp_dir().join(format!("fab-test-corrupt-{}", std::process::id()));
        let _ = fs::create_dir_all(&tmp);
        let test_path = tmp.join("test_dir");
        let _ = fs::create_dir_all(&test_path);

        // Write garbage to the cache file
        let cache_file = cache_file_path(&test_path).unwrap();
        if let Some(parent) = cache_file.parent() {
            let _ = fs::create_dir_all(parent);
        }
        fs::write(&cache_file, b"{ this is not valid json").unwrap();

        // Read should return None, not panic
        let result = read_cache(&test_path);
        assert!(result.is_none(), "corrupt JSON should return None");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn read_cache_handles_directory_at_cache_path() {
        let tmp = std::env::temp_dir().join(format!("fab-test-dirpath-{}", std::process::id()));
        let _ = fs::create_dir_all(&tmp);
        let test_path = tmp.join("test_dir");
        let _ = fs::create_dir_all(&test_path);

        // Create a directory at the cache file path
        let cache_file = cache_file_path(&test_path).unwrap();
        if let Some(parent) = cache_file.parent() {
            let _ = fs::create_dir_all(parent);
        }
        let _ = fs::remove_file(&cache_file);
        fs::create_dir(&cache_file).unwrap();

        // Read should remove the directory and return None
        let result = read_cache(&test_path);
        assert!(
            result.is_none(),
            "directory at cache path should return None"
        );
        assert!(!cache_file.exists(), "directory should be removed");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn write_cache_replaces_directory_at_cache_path() {
        let tmp = std::env::temp_dir().join(format!("fab-test-replace-{}", std::process::id()));
        let _ = fs::create_dir_all(&tmp);
        let test_path = tmp.join("test_dir");
        let _ = fs::create_dir_all(&test_path);

        // Create a directory at the cache file path
        let cache_file = cache_file_path(&test_path).unwrap();
        if let Some(parent) = cache_file.parent() {
            let _ = fs::create_dir_all(parent);
        }
        let _ = fs::remove_file(&cache_file);
        fs::create_dir(&cache_file).unwrap();

        // Write should remove the directory and create a file
        let data = make_test_banner_data();
        write_cache(&test_path, &data).unwrap();
        assert!(
            cache_file.is_file(),
            "directory should be replaced with file"
        );

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn is_cache_fresh_false_for_missing_file() {
        assert!(!is_cache_fresh(Path::new(
            "/tmp/this/path/should/not/exist/fab-fresh-test"
        )));
    }

    #[test]
    fn is_cache_fresh_true_for_recent_file() {
        // Use a real path that we just wrote
        let tmp = std::env::temp_dir().join(format!("fab-test-isfresh-{}", std::process::id()));
        let _ = fs::create_dir_all(&tmp);
        let test_path = tmp.join("test_dir");
        let _ = fs::create_dir_all(&test_path);

        let data = make_test_banner_data();
        write_cache(&test_path, &data).unwrap();

        // File was just written, so it should be fresh
        assert!(is_cache_fresh(&test_path));

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn is_cache_fresh_false_for_directory() {
        let tmp = std::env::temp_dir().join(format!("fab-test-isdir-{}", std::process::id()));
        let _ = fs::create_dir_all(&tmp);
        let test_path = tmp.join("test_dir");
        let _ = fs::create_dir_all(&test_path);

        // Create a directory at the cache file path
        let cache_file = cache_file_path(&test_path).unwrap();
        if let Some(parent) = cache_file.parent() {
            let _ = fs::create_dir_all(parent);
        }
        let _ = fs::remove_file(&cache_file);
        fs::create_dir(&cache_file).unwrap();

        // Directory at cache path should not be fresh
        assert!(!is_cache_fresh(&test_path));

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_max_descendant_mtime_finds_nested_files() {
        let tmp = std::env::temp_dir().join(format!("fab-test-nested-{}", std::process::id()));
        let nested_dir = tmp.join("src").join("nested");
        let _ = fs::create_dir_all(&nested_dir);
        let nested_file = nested_dir.join("test.rs");
        fs::write(&nested_file, "fn main() {}").unwrap();

        let mtime = max_descendant_mtime(&tmp);
        assert!(mtime.is_some(), "should find nested .rs file");

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn cache_ttl_is_5_minutes() {
        // Sanity check: the TTL must match the daemon's CACHE_TTL.
        assert_eq!(CACHE_TTL, Duration::from_secs(300));
    }
}