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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
//! Filesystem utilities — directory metadata, file types, project detection
//!
//! Fast immediate-directory scanning without recursing beyond the banner row.

use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

/// Maximum number of directory entries inspected by a normal banner scan.
///
/// A banner only displays a small sample of a directory. Keeping this bound
/// applies the same safety limit to the CLI, daemon, and shell hooks instead
/// of merely skipping metadata work after an unbounded `read_dir` walk.
pub const MAX_DIRECTORY_SCAN_ITEMS: usize = 500;

/// Project type detection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ProjectType {
    Rust,
    Node,
    Python,
    Go,
    Ruby,
    Java,
    Cpp,
    CMake,
    Generic,
}

impl ProjectType {
    /// Detect project type from directory contents, walking up to ancestors
    pub fn detect(path: &Path) -> Self {
        let mut current = Some(path);
        let mut depth = 0;
        while let Some(dir) = current {
            if depth >= 10 {
                break;
            } // Limit ancestor traversal
            depth += 1;

            if let Some(project_type) = Self::detect_from_direct_markers(dir) {
                return project_type;
            }

            if let Ok(d) = std::fs::read_dir(dir) {
                // The direct-marker check above handles normal project files.
                // Keep this case-insensitive fallback bounded so detecting a
                // project type cannot enumerate a huge directory such as
                // /tmp before the main scan starts.
                for entry in d.take(MAX_DIRECTORY_SCAN_ITEMS).flatten() {
                    let name = entry.file_name();
                    let name_str = name.to_string_lossy().to_lowercase();

                    match name_str.as_str() {
                        "cargo.toml" => return ProjectType::Rust,
                        "package.json" => return ProjectType::Node,
                        "pyproject.toml" | "setup.py" | "requirements.txt" | "pipfile" => {
                            return ProjectType::Python
                        }
                        "go.mod" => return ProjectType::Go,
                        "gemfile" => return ProjectType::Ruby,
                        "pom.xml" | "build.gradle" => return ProjectType::Java,
                        "cmakelists.txt" => return ProjectType::CMake,
                        _ => {}
                    }
                }
            }
            current = dir.parent();
        }

        ProjectType::Generic
    }

    fn detect_from_direct_markers(path: &Path) -> Option<Self> {
        let has = |name: &str| path.join(name).exists();
        if has("Cargo.toml") {
            Some(ProjectType::Rust)
        } else if has("package.json") {
            Some(ProjectType::Node)
        } else if has("pyproject.toml")
            || has("setup.py")
            || has("requirements.txt")
            || has("pipfile")
        {
            Some(ProjectType::Python)
        } else if has("go.mod") {
            Some(ProjectType::Go)
        } else if has("gemfile") {
            Some(ProjectType::Ruby)
        } else if has("pom.xml") || has("build.gradle") {
            Some(ProjectType::Java)
        } else if has("CMakeLists.txt") || has("cmakelists.txt") {
            Some(ProjectType::CMake)
        } else {
            None
        }
    }

    /// Get icon for project type
    pub fn icon(&self) -> &'static str {
        match self {
            ProjectType::Rust => "🦀",
            ProjectType::Node => "📦",
            ProjectType::Python => "🐍",
            ProjectType::Go => "🐹",
            ProjectType::Ruby => "💎",
            ProjectType::Java => "☕",
            ProjectType::Cpp | ProjectType::CMake => "⚙️",
            ProjectType::Generic => "📂",
        }
    }

    /// Get label for project type
    pub fn label(&self) -> &'static str {
        match self {
            ProjectType::Rust => "Rust",
            ProjectType::Node => "Node.js",
            ProjectType::Python => "Python",
            ProjectType::Go => "Go",
            ProjectType::Ruby => "Ruby",
            ProjectType::Java => "Java",
            ProjectType::Cpp => "C++",
            ProjectType::CMake => "CMake",
            ProjectType::Generic => "Generic",
        }
    }
}

/// Directory entry metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirEntry {
    pub name: String,
    pub path: PathBuf,
    pub is_dir: bool,
    pub is_file: bool,
    pub is_symlink: bool,
    #[serde(default)]
    pub is_exec: bool,
    pub size: u64,
    pub modified: Option<DateTime<Utc>>,
    pub perms: String,
    pub owner: String,
    pub group: String,
    #[serde(default)]
    pub symlink_target: Option<String>,
    #[serde(default = "default_true")]
    pub symlink_valid: bool,
    /// Cached per-file content-probe result populated by the daemon during
    /// a directory scan (e.g. `"1024x1536"` for a PNG, `"19"` for a ZIP
    /// entry count, `""` for files that don't have a probe). The client
    /// reads this directly instead of re-running the per-file I/O probe
    /// on every `f` invocation, which is the main reason `f ~/Downloads`
    /// was the slowest path in 0.6.25 (88 file I/O calls per render).
    /// `None` means the daemon hasn't populated it yet; the client should
    /// treat `None` and `Some("")` identically (no metadata available).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_probe: Option<String>,
}

fn default_true() -> bool {
    true
}

/// Directory summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirSummary {
    /// Number of entries observed. When `truncated` is true, this and the
    /// file/directory counts are lower bounds for the full directory.
    pub total_items: usize,
    pub total_size: u64,
    pub files: usize,
    pub dirs: usize,
    /// Whether the directory contained more than the sampled entry limit.
    /// Older daemon/cache payloads omit this field, so default it to false.
    #[serde(default)]
    pub truncated: bool,
    pub top_items: Vec<DirEntry>,
    pub project_type: ProjectType,
    pub last_modified: Option<DateTime<Utc>>,
    pub build_status: Option<crate::build_status::BuildStatus>,
    pub todo_info: Option<crate::todo_scanner::TodoInfo>,
    pub code_metrics: Option<crate::code_metrics::CodeMetrics>,
    pub port_info: Option<crate::port_usage::PortInfo>,
    pub docker_info: Option<crate::docker::DockerInfo>,
}

impl DirSummary {
    /// Scan a directory and gather metadata
    #[allow(dead_code)]
    pub fn scan(path: &Path) -> Result<Self> {
        Self::scan_with_options(path, true, true, true, true, true, &[])
    }

    /// Scan a directory with feature flags
    pub fn scan_with_options(
        path: &Path,
        check_build: bool,
        scan_todos: bool,
        check_ports: bool,
        check_docker: bool,
        check_metrics: bool,
        extra_skip_dirs: &[&str],
    ) -> Result<Self> {
        let project_type = ProjectType::detect(path);
        let mut total_size: u64 = 0;
        let mut files = 0;
        let mut dirs = 0;

        // Pre-load uid/gid caches to avoid reading /etc/passwd and /etc/group per file
        let uid_cache = load_uid_cache();
        let gid_cache = load_gid_cache();
        let mut top_items = Vec::new();
        let mut last_modified: Option<DateTime<Utc>> = None;

        let entries = std::fs::read_dir(path)?;

        // Cap the raw directory iterator, not just metadata collection. A
        // banner only displays a limited number of items, so consuming the
        // remaining 100K+ entries in /tmp is pure waste and can block a
        // synchronous shell hook. The first extra entry is used to tell
        // callers that the displayed sample is incomplete.
        let mut item_count = 0;
        let mut truncated = false;

        for entry_result in entries.take(MAX_DIRECTORY_SCAN_ITEMS + 1) {
            item_count += 1;
            if item_count > MAX_DIRECTORY_SCAN_ITEMS {
                truncated = true;
                break;
            }

            let Ok(entry) = entry_result else {
                continue;
            };

            let file_type = entry.file_type();
            let is_symlink = file_type
                .as_ref()
                .map(|ft| ft.is_symlink())
                .unwrap_or(false);

            // Classify a symlink by its target for filtering and aggregate
            // counts, while retaining `is_symlink` so it remains a separate
            // display type. This makes `--only-files` include symlinks to
            // files and keeps directory/file totals consistent with what is
            // rendered.
            let (is_dir, is_file) = if is_symlink {
                std::fs::metadata(entry.path())
                    .map(|metadata| (metadata.is_dir(), metadata.is_file()))
                    .unwrap_or((false, false))
            } else {
                (
                    file_type.as_ref().map(|ft| ft.is_dir()).unwrap_or(false),
                    file_type.as_ref().map(|ft| ft.is_file()).unwrap_or(false),
                )
            };

            if is_dir {
                dirs += 1;
            } else if is_file {
                files += 1;
            }

            // Try to get metadata — for symlinks, use symlink_metadata to read the link itself
            let metadata = if is_symlink {
                // For symlinks, try symlink_metadata first (reads the link itself)
                // If that fails, the symlink might be broken but we can still read permissions
                std::fs::symlink_metadata(entry.path()).ok().or_else(|| {
                    // Fallback: try to read metadata from the parent directory
                    entry.metadata().ok()
                })
            } else {
                entry.metadata().ok()
            };

            let size = metadata.as_ref().map(|m| m.len()).unwrap_or(0);
            total_size += size;

            // Get modified time
            let modified = metadata
                .as_ref()
                .and_then(|m| m.modified().ok())
                .map(DateTime::<Utc>::from);

            if let Some(mod_time) = modified {
                if last_modified.is_none() || mod_time > last_modified.unwrap() {
                    last_modified = Some(mod_time);
                }
            }

            // Unix permissions + owner/group
            let (perms, is_exec, owner, group) = if let Some(meta) = &metadata {
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    let mode = meta.permissions().mode();
                    let perms_str = format_mode(mode);
                    let exec = mode & 0o111 != 0;

                    // Resolve uid/gid to names via /etc/passwd and /etc/group
                    use std::os::unix::fs::MetadataExt;
                    let uid = meta.uid();
                    let gid = meta.gid();
                    let owner = uid_cache
                        .get(&uid)
                        .cloned()
                        .unwrap_or_else(|| uid.to_string());
                    let group = gid_cache
                        .get(&gid)
                        .cloned()
                        .unwrap_or_else(|| gid.to_string());

                    (perms_str, exec, owner, group)
                }
                #[cfg(not(unix))]
                {
                    let read_only = meta.permissions().readonly();
                    let perms_str = if read_only {
                        "r--r--r--".to_string()
                    } else {
                        "rw-rw-rw-".to_string()
                    };
                    (perms_str, false, "?".to_string(), "?".to_string())
                }
            } else if is_symlink {
                // For broken symlinks, show symlink permissions (usually lrwxrwxrwx)
                #[cfg(unix)]
                {
                    (
                        "lrwxrwxrwx".to_string(),
                        false,
                        "?".to_string(),
                        "?".to_string(),
                    )
                }
                #[cfg(not(unix))]
                {
                    (
                        "----------".to_string(),
                        false,
                        "?".to_string(),
                        "?".to_string(),
                    )
                }
            } else {
                (
                    "----------".to_string(),
                    false,
                    "?".to_string(),
                    "?".to_string(),
                )
            };

            // Symlink target and validity
            let (symlink_target, symlink_valid) = if is_symlink {
                let target = std::fs::read_link(entry.path()).ok().map(|p| {
                    // Try to resolve to absolute path for display
                    if p.is_absolute() {
                        p.to_string_lossy().to_string()
                    } else {
                        // Resolve relative path from parent directory
                        let parent = entry.path();
                        let parent = parent.parent().unwrap_or_else(|| Path::new("."));
                        let absolute = parent.join(&p);
                        if let Ok(canonical) = absolute.canonicalize() {
                            canonical.to_string_lossy().to_string()
                        } else {
                            // Can't resolve (dead symlink), show relative path
                            p.to_string_lossy().to_string()
                        }
                    }
                });
                let valid = target.is_some() && std::fs::metadata(entry.path()).is_ok();
                (target, valid)
            } else {
                (None, true)
            };

            top_items.push(DirEntry {
                name: entry.file_name().to_string_lossy().to_string(),
                path: entry.path().to_path_buf(),
                is_dir,
                is_file,
                is_symlink,
                is_exec,
                size,
                modified,
                perms,
                owner,
                group,
                symlink_target,
                symlink_valid,
                content_probe: None,
            });
        }

        // Sort: directories first, then by name.
        // `to_lowercase` allocates; cache the key once per item instead of
        // lowercasing on every comparison.
        top_items.sort_by_cached_key(|item| (!item.is_dir, item.name.to_lowercase()));

        // Run optional checks with caching
        let cache = crate::cache::Cache::new().ok();

        macro_rules! cached_check {
            ($enabled:expr, $cache:expr, $key:expr, $ttl:expr, $compute:expr) => {
                if $enabled {
                    if let Some(ref cache) = $cache {
                        let ck = crate::cache::cache_key(path, $key);
                        if let Some(cached) = cache.get(&ck, std::time::Duration::from_secs($ttl)) {
                            Some(cached)
                        } else {
                            let result = $compute;
                            if let Some(ref r) = result {
                                if let Err(e) = cache.set(&ck, r.clone()) {
                                    tracing::warn!("Failed to cache {}: {}", $key, e);
                                }
                            }
                            result
                        }
                    } else {
                        $compute
                    }
                } else {
                    None
                }
            };
        }

        let build_status = cached_check!(
            check_build,
            cache,
            "build",
            30,
            crate::build_status::check_build(path, &project_type)
        );
        let (todo_info, code_metrics) = if (scan_todos || check_metrics)
            && project_type != ProjectType::Generic
            && !truncated
        {
            // Cache the combined scan_insights result (TODO counts and
            // code metrics are computed in a single bounded tree walk;
            // there is no benefit to splitting the cache). TTL is 60s:
            // both insights are content-derived and don't change often
            // enough to justify recomputing on every cold scan. The
            // pre-fix code re-ran scan_insights on every call, which
            // was 60-65% of the cold-path time on /home/dracon/Dev
            // (127 ms of 198 ms total). See PROFILE_COLD_PATH.md.
            //
            // Skip for Generic (non-code) directories — scanning
            // Downloads or temp folders for TODOs/LOC is pure waste.
            let scan_closure =
                || crate::project_insights::scan_insights(path, extra_skip_dirs).ok();
            let insights_opt: Option<crate::project_insights::ProjectInsights> =
                if let Some(ref cache) = cache {
                    let ck = crate::cache::cache_key(path, "insights");
                    if let Some(cached) = cache.get(&ck, std::time::Duration::from_secs(60)) {
                        Some(cached)
                    } else {
                        let result = scan_closure();
                        if let Some(ref r) = result {
                            if let Err(e) = cache.set(&ck, r.clone()) {
                                tracing::warn!("Failed to cache insights: {}", e);
                            }
                        }
                        result
                    }
                } else {
                    scan_closure()
                };
            match insights_opt {
                Some(insights) => (
                    scan_todos.then_some(insights.todos),
                    check_metrics.then_some(insights.metrics),
                ),
                None => (None, None),
            }
        } else {
            (None, None)
        };

        let port_info = cached_check!(
            check_ports,
            cache,
            "ports",
            10,
            crate::port_usage::detect_ports(path).ok()
        );
        let docker_info = cached_check!(
            check_docker,
            cache,
            "docker",
            10,
            crate::docker::detect_docker(path).ok()
        );

        Ok(DirSummary {
            total_items: files + dirs,
            total_size,
            files,
            dirs,
            truncated,
            top_items,
            project_type,
            last_modified,
            build_status,
            todo_info,
            code_metrics,
            port_info,
            docker_info,
        })
    }

    /// Get top N items
    #[allow(dead_code)]
    pub fn top_items(&self, n: usize) -> &[DirEntry] {
        &self.top_items[..n.min(self.top_items.len())]
    }

    /// Get remaining count
    #[allow(dead_code)]
    pub fn remaining(&self, n: usize) -> usize {
        self.total_items.saturating_sub(n)
    }
}

/// Human-readable size — very compact (like exa: 4.3k, 1.1k, 983)
pub fn format_size_compact(bytes: u64) -> String {
    if bytes == 0 {
        "0".to_string()
    } else if bytes < 1024 {
        format!("{}", bytes)
    } else if bytes < 1024 * 1024 {
        let kb = bytes as f64 / 1024.0;
        if kb >= 10.0 {
            format!("{:.0}k", kb)
        } else {
            format!("{:.1}k", kb)
        }
    } else if bytes < 1024 * 1024 * 1024 {
        let mb = bytes as f64 / (1024.0 * 1024.0);
        if mb >= 10.0 {
            format!("{:.0}M", mb)
        } else {
            format!("{:.1}M", mb)
        }
    } else {
        let gb = bytes as f64 / (1024.0 * 1024.0 * 1024.0);
        if gb >= 10.0 {
            format!("{:.0}G", gb)
        } else {
            format!("{:.1}G", gb)
        }
    }
}

/// Format exact date/time — ISO-style with time: "2026-05-27 23:06:44"
pub fn format_exact_time(dt: &DateTime<Utc>) -> String {
    dt.format("%Y-%m-%d %H:%M:%S").to_string()
}

/// Format relative time
pub fn format_relative_time(dt: &DateTime<Utc>) -> String {
    let now = Utc::now();
    let duration = now.signed_duration_since(*dt);

    if duration.num_days() > 365 {
        format!("{} year(s) ago", duration.num_days() / 365)
    } else if duration.num_days() > 30 {
        format!("{} month(s) ago", duration.num_days() / 30)
    } else if duration.num_days() > 0 {
        format!("{} day(s) ago", duration.num_days())
    } else if duration.num_hours() > 0 {
        format!("{} hour(s) ago", duration.num_hours())
    } else if duration.num_minutes() > 0 {
        format!("{} minute(s) ago", duration.num_minutes())
    } else {
        "just now".to_string()
    }
}

impl DirSummary {
    /// Get file type breakdown
    #[allow(dead_code)]
    pub fn by_type(&self) -> Vec<(String, usize)> {
        let mut counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
        for entry in &self.top_items {
            let ext = if let Some(dot) = entry.name.rfind('.') {
                entry.name[dot + 1..].to_lowercase()
            } else {
                "other".to_string()
            };
            *counts.entry(ext).or_insert(0) += 1;
        }
        counts.into_iter().collect()
    }
}

/// Format Unix file mode to drwxr-xrwx string (10 chars like `ls -l`)
#[cfg(unix)]
fn format_mode(mode: u32) -> String {
    // File type
    let ft = match mode & 0o170000 {
        0o040000 => 'd',
        0o120000 => 'l',
        0o010000 => 'p',
        0o020000 => 'c',
        0o060000 => 'b',
        0o140000 => 's',
        _ => '-',
    };
    let user_r = if mode & 0o400 != 0 { 'r' } else { '-' };
    let user_w = if mode & 0o200 != 0 { 'w' } else { '-' };
    let user_x = if mode & 0o100 != 0 { 'x' } else { '-' };
    let group_r = if mode & 0o040 != 0 { 'r' } else { '-' };
    let group_w = if mode & 0o020 != 0 { 'w' } else { '-' };
    let group_x = if mode & 0o010 != 0 { 'x' } else { '-' };
    let other_r = if mode & 0o004 != 0 { 'r' } else { '-' };
    let other_w = if mode & 0o002 != 0 { 'w' } else { '-' };
    let other_x = if mode & 0o001 != 0 { 'x' } else { '-' };

    let mut perms = String::with_capacity(10);
    perms.push(ft);
    perms.push(user_r);
    perms.push(user_w);
    perms.push(user_x);
    perms.push(group_r);
    perms.push(group_w);
    perms.push(group_x);
    perms.push(other_r);
    perms.push(other_w);
    perms.push(other_x);
    perms
}

/// Load uid→username cache from /etc/passwd
#[cfg(unix)]
fn load_uid_cache() -> &'static std::collections::HashMap<u32, String> {
    static CACHE: OnceLock<std::collections::HashMap<u32, String>> = OnceLock::new();
    CACHE.get_or_init(load_uid_cache_inner)
}

#[cfg(unix)]
fn load_uid_cache_inner() -> std::collections::HashMap<u32, String> {
    let mut cache = std::collections::HashMap::new();
    if let Ok(content) = std::fs::read_to_string("/etc/passwd") {
        for line in content.lines() {
            let parts: Vec<&str> = line.split(':').collect();
            if parts.len() >= 3 {
                if let Ok(uid) = parts[2].parse::<u32>() {
                    cache.insert(uid, parts[0].to_string());
                }
            }
        }
    }
    cache
}

/// Load gid→groupname cache from /etc/group
#[cfg(unix)]
fn load_gid_cache() -> &'static std::collections::HashMap<u32, String> {
    static CACHE: OnceLock<std::collections::HashMap<u32, String>> = OnceLock::new();
    CACHE.get_or_init(load_gid_cache_inner)
}

#[cfg(unix)]
fn load_gid_cache_inner() -> std::collections::HashMap<u32, String> {
    let mut cache = std::collections::HashMap::new();
    if let Ok(content) = std::fs::read_to_string("/etc/group") {
        for line in content.lines() {
            let parts: Vec<&str> = line.split(':').collect();
            if parts.len() >= 3 {
                if let Ok(gid) = parts[2].parse::<u32>() {
                    cache.insert(gid, parts[0].to_string());
                }
            }
        }
    }
    cache
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    #[test]
    fn test_format_size_compact() {
        assert_eq!(format_size_compact(0), "0");
        assert_eq!(format_size_compact(500), "500");
        assert_eq!(format_size_compact(1024), "1.0k");
        assert_eq!(format_size_compact(10 * 1024), "10k");
        assert_eq!(format_size_compact(1024 * 1024), "1.0M");
        assert_eq!(format_size_compact(1024 * 1024 * 1024), "1.0G");
    }

    #[test]
    fn test_project_type_detect_rust() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("Cargo.toml"), "").unwrap();
        assert_eq!(ProjectType::detect(tmp.path()), ProjectType::Rust);
    }

    #[test]
    fn test_project_type_detect_node() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("package.json"), "").unwrap();
        assert_eq!(ProjectType::detect(tmp.path()), ProjectType::Node);
    }

    #[test]
    fn test_project_type_detect_python() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("pyproject.toml"), "").unwrap();
        assert_eq!(ProjectType::detect(tmp.path()), ProjectType::Python);
    }

    #[test]
    fn test_project_type_detect_go() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("go.mod"), "").unwrap();
        assert_eq!(ProjectType::detect(tmp.path()), ProjectType::Go);
    }

    #[test]
    fn test_project_type_generic() {
        // Create temp dir in /var/tmp to avoid project marker detection from ancestors
        let tmp = tempfile::Builder::new().tempdir_in("/var/tmp").unwrap();
        assert_eq!(ProjectType::detect(tmp.path()), ProjectType::Generic);
    }

    #[test]
    fn test_scan_stops_at_directory_entry_limit() {
        let tmp = tempfile::tempdir().unwrap();
        for index in 0..=MAX_DIRECTORY_SCAN_ITEMS {
            std::fs::write(tmp.path().join(format!("item-{index}")), "content").unwrap();
        }

        let summary =
            DirSummary::scan_with_options(tmp.path(), false, false, false, false, false, &[])
                .unwrap();

        assert!(
            summary.truncated,
            "the sentinel entry must mark the scan truncated"
        );
        assert_eq!(summary.top_items.len(), MAX_DIRECTORY_SCAN_ITEMS);
        assert_eq!(summary.total_items, MAX_DIRECTORY_SCAN_ITEMS);
        assert_eq!(summary.files, MAX_DIRECTORY_SCAN_ITEMS);
        assert_eq!(summary.dirs, 0);
    }

    #[cfg(unix)]
    #[test]
    fn test_symlink_to_file_is_classified_as_file() {
        use std::os::unix::fs::symlink;

        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("target.txt"), "content").unwrap();
        symlink(tmp.path().join("target.txt"), tmp.path().join("link.txt")).unwrap();

        let summary =
            DirSummary::scan_with_options(tmp.path(), false, false, false, false, false, &[])
                .unwrap();
        let link = summary
            .top_items
            .iter()
            .find(|item| item.name == "link.txt")
            .expect("symlink should be listed");
        assert!(link.is_symlink);
        assert!(link.is_file);
        assert!(!link.is_dir);
        assert_eq!(summary.files, 2);
    }

    #[test]
    fn test_project_type_icons() {
        assert_eq!(ProjectType::Rust.icon(), "🦀");
        assert_eq!(ProjectType::Node.icon(), "📦");
        assert_eq!(ProjectType::Python.icon(), "🐍");
        assert_eq!(ProjectType::Go.icon(), "🐹");
        assert_eq!(ProjectType::Generic.icon(), "📂");
    }

    #[test]
    fn test_project_type_labels() {
        assert_eq!(ProjectType::Rust.label(), "Rust");
        assert_eq!(ProjectType::Node.label(), "Node.js");
        assert_eq!(ProjectType::Python.label(), "Python");
    }

    #[test]
    fn test_format_exact_time() {
        let dt = chrono::Utc
            .with_ymd_and_hms(2024, 1, 15, 10, 30, 0)
            .unwrap();
        let formatted = format_exact_time(&dt);
        assert_eq!(formatted, "2024-01-15 10:30:00");
    }

    #[test]
    fn test_format_relative_time() {
        let now = chrono::Utc::now();
        let five_min_ago = now - chrono::Duration::minutes(5);
        let result = format_relative_time(&five_min_ago);
        assert!(result.contains("5"));
        assert!(result.contains("minute"));
    }

    #[test]
    fn test_dir_summary_by_type() {
        let summary = DirSummary {
            total_items: 3,
            total_size: 100,
            files: 3,
            dirs: 0,
            truncated: false,
            top_items: vec![
                DirEntry {
                    name: "test.rs".into(),
                    path: "".into(),
                    is_dir: false,
                    is_file: true,
                    is_symlink: false,
                    is_exec: false,
                    size: 50,
                    modified: None,
                    perms: String::new(),
                    owner: String::new(),
                    group: String::new(),
                    symlink_target: None,
                    symlink_valid: true,
                    content_probe: None,
                },
                DirEntry {
                    name: "main.rs".into(),
                    path: "".into(),
                    is_dir: false,
                    is_file: true,
                    is_symlink: false,
                    is_exec: false,
                    size: 30,
                    modified: None,
                    perms: String::new(),
                    owner: String::new(),
                    group: String::new(),
                    symlink_target: None,
                    symlink_valid: true,
                    content_probe: None,
                },
                DirEntry {
                    name: "readme.md".into(),
                    path: "".into(),
                    is_dir: false,
                    is_file: true,
                    is_symlink: false,
                    is_exec: false,
                    size: 20,
                    modified: None,
                    perms: String::new(),
                    owner: String::new(),
                    group: String::new(),
                    symlink_target: None,
                    symlink_valid: true,
                    content_probe: None,
                },
            ],
            project_type: ProjectType::Rust,
            last_modified: None,
            build_status: None,
            todo_info: None,
            code_metrics: None,
            port_info: None,
            docker_info: None,
        };

        let by_type = summary.by_type();
        assert_eq!(by_type.len(), 2); // rs and md
    }

    // ===== scan_insights cache tests (0.7.7) =====
    //
    // scan_insights is the dominant cost on a cold scan (60-65% of
    // total time on /home/dracon/Dev). It must hit a file cache on
    // the second call within the 60s TTL window. These tests run
    // the scanner twice on the same temp dir and assert that the
    // second call returns the cached value unchanged.

    fn make_insight_test_tree() -> tempfile::TempDir {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("main.rs"),
            "fn main() {}\n// TODO: finish\nfn other() {}\n",
        )
        .unwrap();
        std::fs::write(tmp.path().join("note.md"), "# Title\n- [ ] task\n").unwrap();
        std::fs::create_dir(tmp.path().join("target")).unwrap();
        std::fs::write(tmp.path().join("target/skip.rs"), "TODO: skipped\n").unwrap();
        tmp
    }

    #[test]
    fn test_scan_insights_cache_warm_returns_same_value() {
        use crate::cache::Cache;
        use crate::project_insights::ProjectInsights;

        // Wipe any prior cache for this synthetic key by using a
        // unique temp dir per run.
        let tmp = make_insight_test_tree();
        let path = tmp.path();
        let cache = Cache::new().unwrap();
        let ck = crate::cache::cache_key(path, "insights");

        // First call: cache miss.
        let first: ProjectInsights = match crate::project_insights::scan_insights(path, &[]) {
            Ok(p) => p,
            Err(_) => return, // bail if scan fails in test env
        };
        cache.set(&ck, &first).unwrap();
        assert_eq!(first.todos.count, 2, "fresh scan finds 2 TODOs");

        // Second call: cache hit. We must get back a value that
        // serializes to the same bytes (count, file_count, etc.).
        let cached: Option<ProjectInsights> = cache.get(&ck, std::time::Duration::from_secs(60));
        let cached = cached.expect("insights cache must hit on second call");
        assert_eq!(cached.todos.count, first.todos.count);
        assert_eq!(cached.metrics.file_count, first.metrics.file_count);
        assert_eq!(cached.metrics.total_loc, first.metrics.total_loc);
    }

    #[test]
    fn test_scan_insights_cache_expired_returns_none() {
        use crate::cache::Cache;
        use crate::project_insights::ProjectInsights;

        let tmp = make_insight_test_tree();
        let path = tmp.path();
        let cache = Cache::new().unwrap();
        let ck = crate::cache::cache_key(path, "insights");

        let first: ProjectInsights = crate::project_insights::scan_insights(path, &[]).unwrap();
        cache.set(&ck, &first).unwrap();

        // 0s TTL = always expired.
        let cached: Option<ProjectInsights> = cache.get(&ck, std::time::Duration::from_secs(0));
        assert!(cached.is_none(), "0s TTL must invalidate the entry");
    }

    #[test]
    fn test_project_insights_serializes() {
        // The cache requires Serialize + Deserialize. This test fails
        // at compile time if those derives are missing, which is the
        // exact regression we want to prevent.
        let tmp = make_insight_test_tree();
        let insights = crate::project_insights::scan_insights(tmp.path(), &[]).unwrap();
        let json = serde_json::to_string(&insights).expect("ProjectInsights must serialize");
        let back: crate::project_insights::ProjectInsights =
            serde_json::from_str(&json).expect("ProjectInsights must deserialize");
        assert_eq!(back.todos.count, insights.todos.count);
        assert_eq!(back.metrics.total_loc, insights.metrics.total_loc);
    }
}