sasurahime 0.1.14

macOS developer cache cleaner — scan and wipe stale caches from 40+ tools
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
use crate::command::CommandRunner;
use crate::format::format_bytes;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BaseDir {
    Caches,
    AppSupport,
    Logs,
}

pub struct HintEntry {
    pub base_dir: BaseDir,
    /// One or more subdirectory paths (relative to base_dir) to sum for size.
    pub path_suffixes: &'static [&'static str],
    pub display_name: &'static str,
    pub process_name: Option<&'static str>,
    /// One or more shell commands to show the user.
    pub commands: &'static [&'static str],
    pub threshold_bytes: u64,
    pub skip: bool,
    /// osascript app name for `quit app "..."`. None = cannot auto-quit.
    pub quit_app: Option<&'static str>,
    /// App name for `open -a "..."` after cache deletion. None = don't relaunch.
    pub relaunch_app: Option<&'static str>,
}

pub struct ProcessHint {
    pub entry: &'static HintEntry,
    /// Sum of all path_suffixes sizes.
    pub size_bytes: u64,
    pub running: bool,
}

const MB: u64 = 1024 * 1024;

pub static KNOWN_ENTRIES: &[HintEntry] = &[
    // ── ~/Library/Caches/ ────────────────────────────────────────────────────
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["Microsoft Edge"],
        display_name: "Microsoft Edge",
        process_name: Some("Microsoft Edge Helper"),
        commands: &["rm -rf ~/Library/Caches/Microsoft\\ Edge"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Microsoft Edge"),
        relaunch_app: Some("Microsoft Edge"),
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["com.microsoft.VSCode.ShipIt"],
        display_name: "VSCode ShipIt cache",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/com.microsoft.VSCode.ShipIt"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["ms-playwright"],
        display_name: "Playwright (Node)",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/ms-playwright"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["ms-playwright-go"],
        display_name: "Playwright (Go)",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/ms-playwright-go"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["electron"],
        display_name: "Electron",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/electron"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["BraveSoftware"],
        display_name: "Brave Browser",
        process_name: Some("Brave Browser"),
        commands: &["rm -rf ~/Library/Caches/BraveSoftware"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Brave Browser"),
        relaunch_app: Some("Brave Browser"),
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["typescript"],
        display_name: "TypeScript server cache",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/typescript"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["gopls"],
        display_name: "gopls cache",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/gopls"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["ort.pyke.io"],
        display_name: "ONNX Runtime cache",
        process_name: None,
        commands: &["rm -rf ~/Library/Caches/ort.pyke.io"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["GeoServices"],
        display_name: "GeoServices",
        process_name: Some("locationd"),
        commands: &[],
        threshold_bytes: 64 * MB,
        skip: true, // OS-managed
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Caches,
        path_suffixes: &["Homebrew"],
        display_name: "Homebrew",
        process_name: None,
        commands: &["brew cleanup -s --prune=all"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    // ── ~/Library/Application Support/ ───────────────────────────────────────
    HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["Slack/Cache"],
        display_name: "Slack",
        process_name: Some("Slack"),
        commands: &["rm -rf ~/Library/Application\\ Support/Slack/Cache"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Slack"),
        relaunch_app: None, // login flow is heavy; user relaunches manually
    },
    HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["Claude/Cache"],
        display_name: "Claude (desktop)",
        process_name: Some("Claude"),
        commands: &["rm -rf ~/Library/Application\\ Support/Claude/Cache"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Claude"),
        relaunch_app: None, // session state; user relaunches manually
    },
    HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["obsidian/Cache"],
        display_name: "Obsidian",
        process_name: Some("Obsidian"),
        commands: &["rm -rf ~/Library/Application\\ Support/obsidian/Cache"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Obsidian"),
        relaunch_app: Some("Obsidian"),
    },
    HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["Code/Cache", "Code/CachedExtensionVSIXs", "Code/CachedData"],
        display_name: "VSCode caches",
        process_name: Some("Code"),
        commands: &[
            "rm -rf ~/Library/Application\\ Support/Code/Cache",
            "rm -rf ~/Library/Application\\ Support/Code/CachedExtensionVSIXs",
            "rm -rf ~/Library/Application\\ Support/Code/CachedData",
        ],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Visual Studio Code"),
        relaunch_app: Some("Visual Studio Code"),
    },
    HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["Google/Chrome/Default/Cache_Data"],
        display_name: "Google Chrome",
        process_name: Some("Google Chrome"),
        commands: &["rm -rf ~/Library/Application\\ Support/Google/Chrome/Default/Cache_Data"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Google Chrome"),
        relaunch_app: Some("Google Chrome"),
    },
    // ── ~/Library/Logs/ ───────────────────────────────────────────────────────
    HintEntry {
        base_dir: BaseDir::Logs,
        path_suffixes: &["Claude"],
        display_name: "Claude logs",
        process_name: Some("Claude"),
        commands: &["rm -rf ~/Library/Logs/Claude"],
        threshold_bytes: MB,
        skip: false,
        quit_app: Some("Claude"),
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Logs,
        path_suffixes: &["zoom.us"],
        display_name: "Zoom logs",
        process_name: Some("zoom.us"),
        commands: &["rm -rf ~/Library/Logs/zoom.us"],
        threshold_bytes: MB,
        skip: false,
        quit_app: Some("zoom.us"),
        relaunch_app: None, // may be in a meeting
    },
    HintEntry {
        base_dir: BaseDir::Logs,
        path_suffixes: &["DiagnosticReports"],
        display_name: "Diagnostic Reports",
        process_name: None,
        commands: &["rm -rf ~/Library/Logs/DiagnosticReports"],
        threshold_bytes: MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Logs,
        path_suffixes: &["LM Studio"],
        display_name: "LM Studio logs",
        process_name: Some("LM Studio"),
        commands: &["rm -rf ~/Library/Logs/LM\\ Studio"],
        threshold_bytes: MB,
        skip: false,
        quit_app: Some("LM Studio"),
        relaunch_app: Some("LM Studio"),
    },
    HintEntry {
        base_dir: BaseDir::Logs,
        path_suffixes: &["CrashReporter"],
        display_name: "Crash Reporter logs",
        process_name: None,
        commands: &["rm -rf ~/Library/Logs/CrashReporter"],
        threshold_bytes: MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
    HintEntry {
        base_dir: BaseDir::Logs,
        path_suffixes: &["fsck_hfs.log"],
        display_name: "fsck_hfs log",
        process_name: None,
        commands: &["rm -f ~/Library/Logs/fsck_hfs.log"],
        threshold_bytes: MB,
        skip: false,
        quit_app: None,
        relaunch_app: None,
    },
];

fn base_path(home: &Path, base: BaseDir) -> PathBuf {
    match base {
        BaseDir::Caches => home.join("Library/Caches"),
        BaseDir::AppSupport => home.join("Library/Application Support"),
        BaseDir::Logs => home.join("Library/Logs"),
    }
}

fn dir_size_bytes(path: &Path, runner: &dyn CommandRunner) -> u64 {
    if !path.exists() {
        return 0;
    }
    let path_str = path.to_string_lossy();
    match runner.run("du", &["-sk", &path_str]) {
        Ok(out) => {
            let stdout = String::from_utf8_lossy(&out.stdout);
            stdout
                .lines()
                .next()
                .and_then(|l| l.split_whitespace().next())
                .and_then(|s| s.parse::<u64>().ok())
                .map(|kb| kb * 1024)
                .unwrap_or(0)
        }
        Err(_) => 0,
    }
}

fn is_running(process_name: &str, runner: &dyn CommandRunner) -> bool {
    runner
        .run("pgrep", &["-x", process_name])
        .map(|o| o.status.success())
        .unwrap_or(false)
}

pub fn collect_hints(home: &Path, runner: &dyn CommandRunner) -> Vec<ProcessHint> {
    let mut hints: Vec<ProcessHint> = KNOWN_ENTRIES
        .iter()
        .filter(|e| !e.skip)
        .filter_map(|entry| {
            let base = base_path(home, entry.base_dir);
            let size_bytes: u64 = entry
                .path_suffixes
                .iter()
                .map(|s| dir_size_bytes(&base.join(s), runner))
                .sum();
            if size_bytes <= entry.threshold_bytes {
                return None;
            }
            let running = entry
                .process_name
                .map(|name| is_running(name, runner))
                .unwrap_or(false);
            Some(ProcessHint {
                entry,
                size_bytes,
                running,
            })
        })
        .collect();

    hints.sort_by_key(|h| std::cmp::Reverse(h.size_bytes));
    hints.truncate(5);
    hints
}

pub trait PromptReader {
    /// Read one line from the user. Returns `None` on EOF or error.
    fn read_line(&self) -> Option<String>;
}

pub struct StdinPrompt;

impl PromptReader for StdinPrompt {
    fn read_line(&self) -> Option<String> {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).ok()?;
        Some(s)
    }
}

const QUIT_POLL_INTERVAL: std::time::Duration = std::time::Duration::from_secs(1);
const QUIT_TIMEOUT_SECS: usize = 10;

/// Quit the app, delete caches, optionally relaunch. Returns Err if quit timed out.
pub fn auto_clean_hint(
    hint: &ProcessHint,
    home: &Path,
    runner: &dyn CommandRunner,
) -> anyhow::Result<()> {
    let entry = hint.entry;
    let quit_app = match entry.quit_app {
        Some(a) => a,
        None => anyhow::bail!("{} cannot be auto-quit", entry.display_name),
    };

    // Ask the app to quit via osascript.
    eprintln!("  Quitting {}...", entry.display_name);
    runner.run("osascript", &["-e", &format!("quit app \"{quit_app}\"")])?;

    // Poll until the process is gone.
    let process_name = entry.process_name.unwrap_or(quit_app);
    for _ in 0..QUIT_TIMEOUT_SECS {
        std::thread::sleep(QUIT_POLL_INTERVAL);
        let still_running = runner
            .run("pgrep", &["-x", process_name])
            .map(|o| o.status.success())
            .unwrap_or(false);
        if !still_running {
            break;
        }
    }
    // Final check.
    let still_running = runner
        .run("pgrep", &["-x", process_name])
        .map(|o| o.status.success())
        .unwrap_or(false);
    if still_running {
        anyhow::bail!(
            "{} did not quit within {}s — skipping cache deletion",
            entry.display_name,
            QUIT_TIMEOUT_SECS
        );
    }

    // Delete all path_suffixes.
    let base = base_path(home, entry.base_dir);
    eprintln!("  Clearing cache...");
    for suffix in entry.path_suffixes {
        let path = base.join(suffix);
        if path.exists() {
            let path_str = path.to_string_lossy();
            runner.run("rm", &["-rf", &path_str])?;
        }
    }
    eprintln!("  [OK]");

    // Optionally relaunch.
    if let Some(app) = entry.relaunch_app {
        eprintln!("  Restarting {}...", entry.display_name);
        runner.run("open", &["-a", app])?;
        eprintln!("  [OK]");
    } else {
        eprintln!(
            "  ({} will not be restarted — relaunch manually if needed)",
            entry.display_name
        );
    }

    Ok(())
}

/// For each running hint that supports auto-quit, ask the user and clean.
pub fn offer_auto_clean(
    hints: &[ProcessHint],
    home: &Path,
    runner: &dyn CommandRunner,
    prompt: &dyn PromptReader,
) {
    let actionable: Vec<_> = hints
        .iter()
        .filter(|h| h.running && h.entry.quit_app.is_some())
        .collect();

    if actionable.is_empty() {
        return;
    }

    for hint in actionable {
        let size_str = format_bytes(hint.size_bytes);
        eprint!(
            "\nQuit {} and clear cache? ({size_str} will be freed) [y/N] ",
            hint.entry.display_name
        );
        match prompt.read_line() {
            Some(input) if input.trim().eq_ignore_ascii_case("y") => {
                if let Err(e) = auto_clean_hint(hint, home, runner) {
                    eprintln!("  Error: {e}");
                }
            }
            _ => {}
        }
    }
}

pub fn print_hints(hints: &[ProcessHint]) {
    if hints.is_empty() {
        return;
    }
    let sep = "".repeat(60);
    eprintln!("{sep}");
    eprintln!(" Tip: The following caches can be freed manually:");
    eprintln!("{sep}");
    for h in hints {
        let running_tag = if h.running {
            "  [running — quit first]"
        } else {
            ""
        };
        eprintln!(
            "  {:<26} {}{}",
            h.entry.display_name,
            format_bytes(h.size_bytes),
            running_tag
        );
        for cmd in h.entry.commands {
            eprintln!("    $ {cmd}");
        }
    }
    eprintln!("{sep}");
}

// ─── tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use std::os::unix::process::ExitStatusExt;
    use std::process::Output;

    fn make_output(exit_code: i32, stdout: &str) -> Output {
        Output {
            status: std::process::ExitStatus::from_raw(exit_code),
            stdout: stdout.as_bytes().to_vec(),
            stderr: vec![],
        }
    }

    struct FakeRunner {
        /// Maps path substrings to KB sizes returned by `du -sk`.
        sizes: Vec<(String, u64)>,
        /// Process names that are "running" (pgrep returns exit 0).
        running: Vec<String>,
    }

    impl FakeRunner {
        fn new(sizes: &[(&str, u64)], running: &[&str]) -> Self {
            Self {
                sizes: sizes.iter().map(|(p, k)| (p.to_string(), *k)).collect(),
                running: running.iter().map(|s| s.to_string()).collect(),
            }
        }
    }

    impl CommandRunner for FakeRunner {
        fn run(&self, program: &str, args: &[&str]) -> Result<Output> {
            match program {
                "du" => {
                    let path = args.last().copied().unwrap_or("");
                    let kb = self
                        .sizes
                        .iter()
                        .find(|(pat, _)| path.contains(pat.as_str()))
                        .map(|(_, kb)| *kb)
                        .unwrap_or(0);
                    Ok(make_output(0, &format!("{kb}\t{path}\n")))
                }
                "pgrep" => {
                    let name = args.last().copied().unwrap_or("");
                    let found = self.running.iter().any(|r| r == name);
                    Ok(make_output(if found { 0 } else { 1 }, ""))
                }
                _ => anyhow::bail!("FakeRunner: unexpected command {program}"),
            }
        }

        fn exists(&self, _program: &str) -> bool {
            true
        }
    }

    fn make_dirs(home: &Path, base: BaseDir, suffixes: &[&str]) {
        let base_path = match base {
            BaseDir::Caches => home.join("Library/Caches"),
            BaseDir::AppSupport => home.join("Library/Application Support"),
            BaseDir::Logs => home.join("Library/Logs"),
        };
        for s in suffixes {
            std::fs::create_dir_all(base_path.join(s)).unwrap();
        }
    }

    #[test]
    fn collect_hints_filters_below_threshold() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::Caches, &["Homebrew", "typescript"]);

        // Homebrew: 80 MB (above 64 MB threshold), typescript: 10 MB (below)
        let runner = FakeRunner::new(&[("Homebrew", 80 * 1024), ("typescript", 10 * 1024)], &[]);
        let hints = collect_hints(home, &runner);

        assert!(hints.iter().any(|h| h.entry.display_name == "Homebrew"));
        assert!(!hints
            .iter()
            .any(|h| h.entry.display_name == "TypeScript server cache"));
    }

    #[test]
    fn collect_hints_limits_to_top5() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        // Create 7 entries above threshold in Caches
        make_dirs(
            home,
            BaseDir::Caches,
            &[
                "Microsoft Edge",
                "com.microsoft.VSCode.ShipIt",
                "ms-playwright",
                "ms-playwright-go",
                "electron",
                "BraveSoftware",
                "Homebrew",
            ],
        );
        let runner = FakeRunner::new(
            &[
                ("Microsoft Edge", 1_800 * 1024),
                ("com.microsoft.VSCode.ShipIt", 900 * 1024),
                ("ms-playwright", 280 * 1024),
                ("ms-playwright-go", 130 * 1024),
                ("electron", 110 * 1024),
                ("BraveSoftware", 100 * 1024),
                ("Homebrew", 80 * 1024),
            ],
            &[],
        );
        let hints = collect_hints(home, &runner);
        assert_eq!(hints.len(), 5);
        // should be sorted descending
        assert!(hints[0].size_bytes >= hints[1].size_bytes);
        assert!(hints[1].size_bytes >= hints[2].size_bytes);
    }

    #[test]
    fn collect_hints_excludes_skip_entries() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::Caches, &["GeoServices"]);
        let runner = FakeRunner::new(&[("GeoServices", 100 * 1024)], &[]);
        let hints = collect_hints(home, &runner);
        assert!(!hints.iter().any(|h| h.entry.display_name == "GeoServices"));
    }

    #[test]
    fn collect_hints_sets_running_flag() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::AppSupport, &["Slack/Cache"]);
        let runner = FakeRunner::new(
            &[("Slack", 230 * 1024)],
            &["Slack"], // Slack is running
        );
        let hints = collect_hints(home, &runner);
        let slack = hints.iter().find(|h| h.entry.display_name == "Slack");
        assert!(slack.is_some(), "Slack hint should appear");
        assert!(slack.unwrap().running, "running flag should be true");
    }

    #[test]
    fn collect_hints_aggregates_vscode_dirs() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(
            home,
            BaseDir::AppSupport,
            &["Code/Cache", "Code/CachedExtensionVSIXs", "Code/CachedData"],
        );
        // Each dir: 200 MB → total 600 MB
        let runner = FakeRunner::new(
            &[
                ("Code/Cache", 200 * 1024),
                ("Code/CachedExtensionVSIXs", 200 * 1024),
                ("Code/CachedData", 200 * 1024),
            ],
            &[],
        );
        let hints = collect_hints(home, &runner);
        let vscode = hints
            .iter()
            .find(|h| h.entry.display_name == "VSCode caches");
        assert!(vscode.is_some(), "VSCode caches hint should appear");
        assert_eq!(
            vscode.unwrap().size_bytes,
            600 * 1024 * 1024,
            "size should be sum of 3 dirs"
        );
    }

    #[test]
    fn collect_hints_includes_logs() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::Logs, &["Claude"]);
        // 62 MB — above 1 MB threshold for logs
        let runner = FakeRunner::new(&[("Claude", 62 * 1024)], &["Claude"]);
        let hints = collect_hints(home, &runner);
        let log = hints.iter().find(|h| h.entry.display_name == "Claude logs");
        assert!(log.is_some(), "Claude logs hint should appear");
        assert!(log.unwrap().running);
    }

    #[test]
    fn print_hints_empty_produces_no_output() {
        // Just verify it doesn't panic and returns without printing the header.
        // (We can't easily capture stderr in a unit test, so we just assert no panic.)
        print_hints(&[]);
    }

    #[test]
    fn print_hints_running_shows_quit_first() {
        static ENTRY: HintEntry = HintEntry {
            base_dir: BaseDir::AppSupport,
            path_suffixes: &["Slack/Cache"],
            display_name: "Slack",
            process_name: Some("Slack"),
            commands: &["rm -rf ~/Library/Application\\ Support/Slack/Cache"],
            threshold_bytes: 64 * MB,
            skip: false,
            quit_app: Some("Slack"),
            relaunch_app: None,
        };
        let hint = ProcessHint {
            entry: &ENTRY,
            size_bytes: 230 * MB,
            running: true,
        };
        let tag = if hint.running {
            "  [running — quit first]"
        } else {
            ""
        };
        assert!(tag.contains("quit first"));
    }

    // ── auto_clean tests ─────────────────────────────────────────────────────

    use std::sync::Mutex;

    struct RecordingRunner {
        /// Sequence of calls: (program, args)
        calls: Mutex<Vec<(String, Vec<String>)>>,
        /// process names that are "running" at the *start* of the test
        initially_running: Vec<String>,
        /// after how many pgrep calls the process appears gone (simulates quit)
        gone_after_pgrep_calls: usize,
    }

    impl RecordingRunner {
        fn new(running: &[&str], gone_after: usize) -> Self {
            Self {
                calls: Mutex::new(vec![]),
                initially_running: running.iter().map(|s| s.to_string()).collect(),
                gone_after_pgrep_calls: gone_after,
            }
        }

        fn recorded_calls(&self) -> Vec<(String, Vec<String>)> {
            self.calls.lock().unwrap().clone()
        }
    }

    impl CommandRunner for RecordingRunner {
        fn run(&self, program: &str, args: &[&str]) -> Result<Output> {
            let mut calls = self.calls.lock().unwrap();
            calls.push((
                program.to_string(),
                args.iter().map(|s| s.to_string()).collect(),
            ));
            let call_count = calls.len();
            drop(calls);

            match program {
                "pgrep" => {
                    let name = args.last().copied().unwrap_or("");
                    let is_running = self.initially_running.iter().any(|r| r == name);
                    // count how many pgrep calls have been made so far
                    let pgrep_count = self
                        .calls
                        .lock()
                        .unwrap()
                        .iter()
                        .filter(|(p, _)| p == "pgrep")
                        .count();
                    let still_running = is_running && pgrep_count <= self.gone_after_pgrep_calls;
                    Ok(make_output(if still_running { 0 } else { 1 }, ""))
                }
                "du" => {
                    let path = args.last().copied().unwrap_or("");
                    Ok(make_output(0, &format!("102400\t{path}\n"))) // 100 MB
                }
                "osascript" | "open" | "rm" => Ok(make_output(0, "")),
                _ => anyhow::bail!("RecordingRunner: unexpected {program} (call #{call_count})"),
            }
        }

        fn exists(&self, _: &str) -> bool {
            true
        }
    }

    static VSCODE_ENTRY: HintEntry = HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["Code/Cache", "Code/CachedExtensionVSIXs", "Code/CachedData"],
        display_name: "VSCode caches",
        process_name: Some("Code"),
        commands: &[
            "rm -rf ~/Library/Application\\ Support/Code/Cache",
            "rm -rf ~/Library/Application\\ Support/Code/CachedExtensionVSIXs",
            "rm -rf ~/Library/Application\\ Support/Code/CachedData",
        ],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Visual Studio Code"),
        relaunch_app: Some("Visual Studio Code"),
    };

    static SLACK_ENTRY: HintEntry = HintEntry {
        base_dir: BaseDir::AppSupport,
        path_suffixes: &["Slack/Cache"],
        display_name: "Slack",
        process_name: Some("Slack"),
        commands: &["rm -rf ~/Library/Application\\ Support/Slack/Cache"],
        threshold_bytes: 64 * MB,
        skip: false,
        quit_app: Some("Slack"),
        relaunch_app: None,
    };

    #[test]
    fn auto_clean_skips_if_quit_times_out() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::AppSupport, &["Code/Cache"]);

        // Process never goes away (gone_after = usize::MAX)
        let runner = RecordingRunner::new(&["Code"], usize::MAX);
        let hint = ProcessHint {
            entry: &VSCODE_ENTRY,
            size_bytes: 600 * MB,
            running: true,
        };

        let result = auto_clean_hint(&hint, home, &runner);
        assert!(result.is_err(), "should return Err when quit times out");
        // osascript should have been called but rm should NOT
        let calls = runner.recorded_calls();
        assert!(
            calls.iter().any(|(p, _)| p == "osascript"),
            "should attempt quit"
        );
        assert!(
            !calls.iter().any(|(p, _)| p == "rm"),
            "should NOT delete while process alive"
        );
    }

    #[test]
    fn auto_clean_deletes_paths_after_quit() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(
            home,
            BaseDir::AppSupport,
            &["Code/Cache", "Code/CachedExtensionVSIXs", "Code/CachedData"],
        );

        // Process goes away after 1st pgrep poll
        let runner = RecordingRunner::new(&["Code"], 1);
        let hint = ProcessHint {
            entry: &VSCODE_ENTRY,
            size_bytes: 600 * MB,
            running: true,
        };

        auto_clean_hint(&hint, home, &runner).expect("should succeed");

        let calls = runner.recorded_calls();
        let rm_calls: Vec<_> = calls.iter().filter(|(p, _)| p == "rm").collect();
        // 3 path_suffixes → 3 rm -rf calls
        assert_eq!(rm_calls.len(), 3, "should delete all 3 VSCode cache dirs");
    }

    #[test]
    fn auto_clean_relaunches_when_configured() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::AppSupport, &["Code/Cache"]);

        let runner = RecordingRunner::new(&["Code"], 1);
        let hint = ProcessHint {
            entry: &VSCODE_ENTRY,
            size_bytes: 600 * MB,
            running: true,
        };

        auto_clean_hint(&hint, home, &runner).expect("should succeed");

        let calls = runner.recorded_calls();
        let open_calls: Vec<_> = calls.iter().filter(|(p, _)| p == "open").collect();
        assert_eq!(open_calls.len(), 1, "should relaunch VSCode once");
        assert!(
            open_calls[0]
                .1
                .iter()
                .any(|a| a.contains("Visual Studio Code")),
            "open -a should name the app"
        );
    }

    #[test]
    fn auto_clean_skips_relaunch_for_slack() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::AppSupport, &["Slack/Cache"]);

        let runner = RecordingRunner::new(&["Slack"], 1);
        let hint = ProcessHint {
            entry: &SLACK_ENTRY,
            size_bytes: 230 * MB,
            running: true,
        };

        auto_clean_hint(&hint, home, &runner).expect("should succeed");

        let calls = runner.recorded_calls();
        assert!(
            !calls.iter().any(|(p, _)| p == "open"),
            "Slack should NOT be relaunched"
        );
    }

    // ── offer_auto_clean interaction tests ───────────────────────────────────

    struct FakePrompt {
        responses: std::cell::RefCell<std::collections::VecDeque<String>>,
    }

    impl FakePrompt {
        fn new(responses: &[&str]) -> Self {
            Self {
                responses: std::cell::RefCell::new(
                    responses.iter().map(|s| s.to_string()).collect(),
                ),
            }
        }
    }

    impl PromptReader for FakePrompt {
        fn read_line(&self) -> Option<String> {
            self.responses.borrow_mut().pop_front()
        }
    }

    #[test]
    fn offer_auto_clean_calls_auto_clean_when_user_answers_y() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::AppSupport, &["Code/Cache"]);

        // VSCode is running, user answers "y"
        let runner = RecordingRunner::new(&["Code"], 1);
        let prompt = FakePrompt::new(&["y"]);
        let hint = ProcessHint {
            entry: &VSCODE_ENTRY,
            size_bytes: 600 * MB,
            running: true,
        };

        offer_auto_clean(&[hint], home, &runner, &prompt);

        let calls = runner.recorded_calls();
        assert!(
            calls.iter().any(|(p, _)| p == "osascript"),
            "should attempt quit when user says y"
        );
    }

    #[test]
    fn offer_auto_clean_skips_auto_clean_when_user_answers_n() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();
        make_dirs(home, BaseDir::AppSupport, &["Code/Cache"]);

        let runner = RecordingRunner::new(&["Code"], 1);
        let prompt = FakePrompt::new(&["n"]);
        let hint = ProcessHint {
            entry: &VSCODE_ENTRY,
            size_bytes: 600 * MB,
            running: true,
        };

        offer_auto_clean(&[hint], home, &runner, &prompt);

        let calls = runner.recorded_calls();
        assert!(
            !calls.iter().any(|(p, _)| p == "osascript"),
            "should NOT attempt quit when user says n"
        );
    }

    #[test]
    fn offer_auto_clean_skips_non_running_hints() {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path();

        let runner = RecordingRunner::new(&[], 0);
        let prompt = FakePrompt::new(&["y"]); // would say yes, but shouldn't be asked
        let hint = ProcessHint {
            entry: &VSCODE_ENTRY,
            size_bytes: 600 * MB,
            running: false, // not running
        };

        offer_auto_clean(&[hint], home, &runner, &prompt);

        let calls = runner.recorded_calls();
        assert!(
            !calls.iter().any(|(p, _)| p == "osascript"),
            "should not touch non-running hints"
        );
    }

    #[test]
    fn auto_clean_hint_errors_when_quit_app_is_none() {
        static NO_QUIT_ENTRY: HintEntry = HintEntry {
            base_dir: BaseDir::Caches,
            path_suffixes: &["Homebrew"],
            display_name: "Homebrew",
            process_name: None,
            commands: &["brew cleanup -s --prune=all"],
            threshold_bytes: 64 * MB,
            skip: false,
            quit_app: None,
            relaunch_app: None,
        };
        let tmp = tempfile::tempdir().unwrap();
        let runner = RecordingRunner::new(&[], 0);
        let hint = ProcessHint {
            entry: &NO_QUIT_ENTRY,
            size_bytes: 80 * MB,
            running: false,
        };
        let result = auto_clean_hint(&hint, tmp.path(), &runner);
        assert!(result.is_err(), "should return Err when quit_app is None");
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("cannot be auto-quit"),
            "error message should explain why"
        );
    }
}