cruftkill 0.2.3

Polyglot dev-cache reaper — find and delete node_modules, .venv, target, DerivedData and the rest of your build cruft from a fast terminal UI
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
//! TUI application state and pure-function reducer.
//!
//! Keeps render code and event handling decoupled: the main loop
//! ([`super::run`]) translates `KeyEvent` / scan-result / size-result
//! signals into [`Action`]s; this module applies them to [`AppState`] and
//! returns a small set of side effects that the loop performs (e.g. "delete
//! folder at index N").
//!
//! Why a reducer-style design? It makes the UI logic unit-testable without a
//! real terminal — see the `tests` module below.

use std::path::PathBuf;
use std::time::SystemTime;

use crate::core::sort::sort_results;
use crate::core::types::{FolderResult, ScanFoundFolder, SortBy, SortDirection};

/// What the TUI is currently doing. Most of the time it's `Browse`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mode {
    Browse,
    /// Awaiting Y/N on deleting the row at this cursor index.
    Confirm(usize),
}

/// All UI-visible state.
#[derive(Debug)]
pub struct AppState {
    pub root: PathBuf,
    pub targets: Vec<String>,
    pub dry_run: bool,

    pub results: Vec<FolderResult>,
    pub cursor: usize,
    pub mode: Mode,
    pub sort: SortBy,
    pub sort_direction: SortDirection,

    /// Set true when the scanner channel closes — used in the status bar.
    pub scan_finished: bool,

    /// `true` once the user has pressed ↑/↓ at least once. Until then we
    /// keep the cursor pinned to row 0 across re-sorts so the "top hit"
    /// stays visible while results stream in. After the user moves the
    /// cursor we switch to "preserve by path" behaviour.
    pub user_navigated: bool,

    /// Live progress counter — number of directories whose contents have
    /// been read. Sourced from `ScanStats::completed` and refreshed by the
    /// main loop on each tick.
    pub dirs_scanned: u64,

    /// Last status / error message shown to the user. Cleared on next action.
    pub last_message: Option<String>,
}

impl AppState {
    /// Create a new state with the default sort (`Size` desc).
    pub fn new(root: PathBuf, targets: Vec<String>, dry_run: bool, sort: SortBy) -> Self {
        Self::with_sort(root, targets, dry_run, sort, SortDirection::default())
    }

    pub fn with_sort(
        root: PathBuf,
        targets: Vec<String>,
        dry_run: bool,
        sort: SortBy,
        sort_direction: SortDirection,
    ) -> Self {
        Self {
            root,
            targets,
            dry_run,
            results: Vec::new(),
            cursor: 0,
            mode: Mode::Browse,
            sort,
            sort_direction,
            scan_finished: false,
            user_navigated: false,
            dirs_scanned: 0,
            last_message: None,
        }
    }

    /// Total size across all rows that have a known size — backwards compat
    /// alias for [`releasable_bytes`] + [`saved_bytes`].
    pub fn total_size(&self) -> u64 {
        self.results.iter().filter_map(|r| r.size_bytes).sum()
    }

    /// Bytes still on disk that the user could reclaim by deleting them.
    /// Excludes rows already deleted in this session.
    pub fn releasable_bytes(&self) -> u64 {
        self.results.iter().filter(|r| !r.deleted).filter_map(|r| r.size_bytes).sum()
    }

    /// Bytes the user has actually reclaimed (or simulated reclaiming, in
    /// dry-run mode) during this session.
    pub fn saved_bytes(&self) -> u64 {
        self.results.iter().filter(|r| r.deleted).filter_map(|r| r.size_bytes).sum()
    }

    /// Currently-highlighted row, if any.
    pub fn selected(&self) -> Option<&FolderResult> {
        self.results.get(self.cursor)
    }
}

/// User intent. Translated by `apply` into state changes + optional side effect.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
    Up,
    Down,
    /// Open the confirm prompt for the currently-selected row.
    RequestDelete,
    ConfirmYes,
    ConfirmNo,
    /// Toggle sort by size. Pressing again flips direction. When switching
    /// FROM another sort, the direction resets to the default for that key
    /// (Size→Desc, Name→Asc, LastUsed→Desc).
    ToggleSortBySize,
    ToggleSortByName,
    ToggleSortByLastUsed,
    /// Cancel the current scan, clear results, and start a fresh scan with
    /// the same options. Useful after the user has just deleted folders and
    /// wants a clean state.
    Rescan,
    Quit,
    Noop,
}

/// Things the reducer asks the main loop to do that aren't pure state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Effect {
    /// Perform the actual delete on the FS, then call back into the reducer
    /// via `record_delete_outcome`.
    DeleteFolder {
        index: usize,
        path: PathBuf,
    },
    /// Tear down the TUI and exit.
    Quit,
    /// Cancel the current scan and start a new one with the same options.
    /// Main loop is responsible for the actual scanner lifecycle.
    Rescan,
    None,
}

impl AppState {
    /// Apply an action, returning any side effect for the main loop to execute.
    pub fn apply(&mut self, action: Action) -> Effect {
        self.last_message = None;
        match action {
            Action::Up => {
                self.user_navigated = true;
                if self.cursor > 0 {
                    self.cursor -= 1;
                }
                Effect::None
            }
            Action::Down => {
                self.user_navigated = true;
                if !self.results.is_empty() && self.cursor + 1 < self.results.len() {
                    self.cursor += 1;
                }
                Effect::None
            }
            Action::RequestDelete => {
                if self.mode == Mode::Browse
                    && let Some(r) = self.results.get(self.cursor)
                    && !r.deleted
                {
                    self.mode = Mode::Confirm(self.cursor);
                }
                Effect::None
            }
            Action::ConfirmYes => match self.mode.clone() {
                Mode::Confirm(idx) => {
                    self.mode = Mode::Browse;
                    if let Some(r) = self.results.get(idx) {
                        Effect::DeleteFolder { index: idx, path: r.path.clone() }
                    } else {
                        Effect::None
                    }
                }
                _ => Effect::None,
            },
            Action::ConfirmNo => {
                if matches!(self.mode, Mode::Confirm(_)) {
                    self.mode = Mode::Browse;
                }
                Effect::None
            }
            Action::ToggleSortBySize => {
                self.toggle_or_switch_sort(SortBy::Size, SortDirection::Desc);
                Effect::None
            }
            Action::ToggleSortByName => {
                self.toggle_or_switch_sort(SortBy::Path, SortDirection::Asc);
                Effect::None
            }
            Action::ToggleSortByLastUsed => {
                self.toggle_or_switch_sort(SortBy::Age, SortDirection::Desc);
                Effect::None
            }
            Action::Rescan => {
                if matches!(self.mode, Mode::Browse) {
                    self.clear_for_rescan();
                    Effect::Rescan
                } else {
                    Effect::None
                }
            }
            Action::Quit => Effect::Quit,
            Action::Noop => Effect::None,
        }
    }

    /// Reset everything that depends on a specific scan run. Keeps the
    /// user's chosen sort + direction + targets + root + dry_run flag.
    pub fn clear_for_rescan(&mut self) {
        self.results.clear();
        self.cursor = 0;
        self.user_navigated = false;
        self.scan_finished = false;
        self.dirs_scanned = 0;
        self.last_message = Some("rescanning…".into());
    }

    fn toggle_or_switch_sort(&mut self, by: SortBy, default_direction: SortDirection) {
        if self.sort == by {
            self.sort_direction = self.sort_direction.toggle();
        } else {
            self.sort = by;
            self.sort_direction = default_direction;
        }
        self.resort();
    }

    /// Sort `results` by the current `sort` + `sort_direction`.
    ///
    /// Cursor policy:
    /// - If the user has not navigated yet (`!user_navigated`), pin to row 0
    ///   so the top hit stays visible while results stream in or get re-sorted.
    /// - Otherwise, preserve the cursor on whichever row was selected
    ///   (by path), so a sort-toggle keeps your item in view.
    /// - Final clamp to keep the cursor in range.
    pub fn resort(&mut self) {
        let selected_path =
            if self.user_navigated { self.selected().map(|r| r.path.clone()) } else { None };
        sort_results(&mut self.results, self.sort, self.sort_direction);
        if let Some(p) = selected_path
            && let Some(idx) = self.results.iter().position(|r| r.path == p)
        {
            self.cursor = idx;
        } else if !self.user_navigated {
            self.cursor = 0;
        } else if self.cursor >= self.results.len() && !self.results.is_empty() {
            self.cursor = self.results.len() - 1;
        }
    }

    /// Push a result coming off the scanner channel. Caller can preset
    /// `last_modified` if they have it (the TUI does this synchronously
    /// from `fs::metadata` so the `Age` sort is meaningful immediately).
    pub fn push_result(&mut self, found: ScanFoundFolder) {
        self.results.push(FolderResult::from_scan(found));
        self.resort();
    }

    /// Push a result and seed its `last_modified` in one call.
    pub fn push_result_with_mtime(
        &mut self,
        found: ScanFoundFolder,
        last_modified: Option<SystemTime>,
    ) {
        let mut row = FolderResult::from_scan(found);
        row.last_modified = last_modified;
        self.results.push(row);
        self.resort();
    }

    /// Update a row's size after the size-calc task completes.
    pub fn record_size(&mut self, path: &std::path::Path, size: u64) {
        let changed = if let Some(row) = self.results.iter_mut().find(|r| r.path == path) {
            row.size_bytes = Some(size);
            true
        } else {
            false
        };
        // Only re-sort if the change can affect ordering.
        if changed && self.sort == SortBy::Size {
            self.resort();
        }
    }

    /// Update a row after a delete attempt completes.
    pub fn record_delete_outcome(&mut self, index: usize, success: bool, error: Option<String>) {
        if let Some(row) = self.results.get_mut(index) {
            row.deleted = success;
            if let Some(e) = error {
                self.last_message = Some(format!("delete failed: {e}"));
            } else if self.dry_run {
                self.last_message = Some("(dry-run) would have deleted".into());
            } else {
                self.last_message = Some("deleted".into());
            }
        }
    }

    /// Called when the scanner channel closes. Per user request: always
    /// snap the cursor to the first row when the scan completes — even if
    /// the user has been navigating — so the "top hit" is immediately
    /// visible. The user can still scroll afterwards.
    pub fn mark_scan_finished(&mut self) {
        let was_already_finished = self.scan_finished;
        self.scan_finished = true;
        if !was_already_finished && !self.results.is_empty() {
            self.cursor = 0;
            self.user_navigated = false;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;
    use std::time::Duration;

    fn fresh_state() -> AppState {
        AppState::new(PathBuf::from("/root"), vec!["node_modules".into()], false, SortBy::Size)
    }

    fn push(state: &mut AppState, p: &str) {
        state.push_result(ScanFoundFolder::new(PathBuf::from(p), None));
    }

    #[test]
    fn default_sort_is_size_desc() {
        let s = fresh_state();
        assert_eq!(s.sort, SortBy::Size);
        assert_eq!(s.sort_direction, SortDirection::Desc);
    }

    #[test]
    fn navigation_stays_in_bounds() {
        let mut s = fresh_state();
        push(&mut s, "/a");
        push(&mut s, "/b");
        push(&mut s, "/c");
        assert_eq!(s.cursor, 0);
        s.apply(Action::Up);
        assert_eq!(s.cursor, 0); // can't go below 0
        s.apply(Action::Down);
        s.apply(Action::Down);
        s.apply(Action::Down); // tries to go past last
        assert_eq!(s.cursor, 2);
    }

    #[test]
    fn delete_flow_yes_emits_effect_and_returns_to_browse() {
        let mut s = fresh_state();
        push(&mut s, "/a/node_modules");
        s.apply(Action::RequestDelete);
        assert_eq!(s.mode, Mode::Confirm(0));
        let eff = s.apply(Action::ConfirmYes);
        match eff {
            Effect::DeleteFolder { index, path } => {
                assert_eq!(index, 0);
                assert_eq!(path, PathBuf::from("/a/node_modules"));
            }
            other => panic!("expected DeleteFolder, got {other:?}"),
        }
        assert_eq!(s.mode, Mode::Browse);
    }

    #[test]
    fn delete_flow_no_returns_to_browse_without_effect() {
        let mut s = fresh_state();
        push(&mut s, "/a/node_modules");
        s.apply(Action::RequestDelete);
        assert_eq!(s.mode, Mode::Confirm(0));
        let eff = s.apply(Action::ConfirmNo);
        assert_eq!(eff, Effect::None);
        assert_eq!(s.mode, Mode::Browse);
    }

    #[test]
    fn cannot_request_delete_when_no_rows() {
        let mut s = fresh_state();
        s.apply(Action::RequestDelete);
        assert_eq!(s.mode, Mode::Browse);
    }

    #[test]
    fn cannot_redelete_a_deleted_row() {
        let mut s = fresh_state();
        push(&mut s, "/a/node_modules");
        s.record_delete_outcome(0, true, None);
        s.apply(Action::RequestDelete);
        assert_eq!(s.mode, Mode::Browse, "should not open confirm for deleted row");
    }

    #[test]
    fn record_size_updates_matching_row() {
        let mut s = fresh_state();
        push(&mut s, "/a/node_modules");
        push(&mut s, "/b/node_modules");
        s.record_size(Path::new("/a/node_modules"), 42_000);
        let row_a = s.results.iter().find(|r| r.path == Path::new("/a/node_modules")).unwrap();
        let row_b = s.results.iter().find(|r| r.path == Path::new("/b/node_modules")).unwrap();
        assert_eq!(row_a.size_bytes, Some(42_000));
        assert_eq!(row_b.size_bytes, None);
    }

    #[test]
    fn total_size_sums_known_sizes() {
        let mut s = fresh_state();
        push(&mut s, "/a");
        push(&mut s, "/b");
        s.record_size(Path::new("/a"), 1_000);
        s.record_size(Path::new("/b"), 2_500);
        assert_eq!(s.total_size(), 3_500);
    }

    #[test]
    fn quit_returns_quit_effect() {
        let mut s = fresh_state();
        assert_eq!(s.apply(Action::Quit), Effect::Quit);
    }

    #[test]
    fn dry_run_message_after_delete() {
        let mut s = fresh_state();
        s.dry_run = true;
        push(&mut s, "/a/node_modules");
        s.record_delete_outcome(0, true, None);
        assert!(s.last_message.as_deref().unwrap().contains("dry-run"));
    }

    // ─── Sort toggle behaviour ──────────────────────────────────────────────

    #[test]
    fn pressing_size_again_flips_direction() {
        let mut s = fresh_state();
        assert_eq!(s.sort_direction, SortDirection::Desc);
        s.apply(Action::ToggleSortBySize);
        assert_eq!(s.sort, SortBy::Size);
        assert_eq!(s.sort_direction, SortDirection::Asc);
        s.apply(Action::ToggleSortBySize);
        assert_eq!(s.sort_direction, SortDirection::Desc);
    }

    #[test]
    fn switching_from_size_to_name_uses_default_asc() {
        let mut s = fresh_state(); // Size + Desc
        s.apply(Action::ToggleSortByName);
        assert_eq!(s.sort, SortBy::Path);
        assert_eq!(s.sort_direction, SortDirection::Asc);
    }

    #[test]
    fn switching_to_last_used_uses_default_desc() {
        let mut s = fresh_state();
        s.apply(Action::ToggleSortByName); // somewhere else
        s.apply(Action::ToggleSortByLastUsed);
        assert_eq!(s.sort, SortBy::Age);
        assert_eq!(s.sort_direction, SortDirection::Desc);
        // Toggling again flips.
        s.apply(Action::ToggleSortByLastUsed);
        assert_eq!(s.sort_direction, SortDirection::Asc);
    }

    #[test]
    fn resort_keeps_cursor_on_same_row_after_user_navigates() {
        let mut s = fresh_state(); // sort=Size desc
        push(&mut s, "/aaa");
        push(&mut s, "/bbb");
        push(&mut s, "/ccc");
        s.record_size(Path::new("/aaa"), 100);
        s.record_size(Path::new("/bbb"), 999);
        s.record_size(Path::new("/ccc"), 500);
        // Order under Size+Desc: bbb(999), ccc(500), aaa(100).
        // Move cursor down to the middle (ccc) — this also flips user_navigated.
        s.apply(Action::Down);
        assert!(s.user_navigated);
        let ccc_idx = s.results.iter().position(|r| r.path == Path::new("/ccc")).unwrap();
        assert_eq!(s.cursor, ccc_idx);
        // Flip to Asc.
        s.apply(Action::ToggleSortBySize);
        // New order: aaa(100), ccc(500), bbb(999). Cursor should still point to ccc.
        let new_idx = s.results.iter().position(|r| r.path == Path::new("/ccc")).unwrap();
        assert_eq!(s.cursor, new_idx);
    }

    // ─── Auto-top + rescan behaviour ────────────────────────────────────────

    #[test]
    fn cursor_pinned_at_top_during_streaming_until_user_navigates() {
        // Simulate live streaming: rows arrive one-by-one with sizes filling in.
        let mut s = fresh_state();
        let scan = |p: &str| ScanFoundFolder::new(PathBuf::from(p), None);
        s.push_result(scan("/small"));
        s.record_size(Path::new("/small"), 100);
        // Now a bigger row arrives — it should sort to the top under Size+Desc,
        // and because user hasn't navigated, the cursor must move to the new top row.
        s.push_result(scan("/big"));
        s.record_size(Path::new("/big"), 10_000);
        assert_eq!(s.cursor, 0);
        assert_eq!(s.selected().unwrap().path, PathBuf::from("/big"));

        // User presses Down → user_navigated flips → cursor follows the row.
        s.apply(Action::Down);
        assert!(s.user_navigated);
        let small_idx = s.results.iter().position(|r| r.path == Path::new("/small")).unwrap();
        assert_eq!(s.cursor, small_idx);

        // Another row arrives mid-stream; cursor should STAY on /small now.
        s.push_result(scan("/medium"));
        s.record_size(Path::new("/medium"), 1_000);
        assert_eq!(s.selected().unwrap().path, PathBuf::from("/small"));
    }

    #[test]
    fn scan_finished_snaps_cursor_to_top_even_if_user_navigated() {
        let mut s = fresh_state();
        push(&mut s, "/a");
        push(&mut s, "/b");
        push(&mut s, "/c");
        // User moved around mid-scan.
        s.apply(Action::Down);
        s.apply(Action::Down);
        assert!(s.cursor > 0);
        // Scan completes.
        s.mark_scan_finished();
        assert_eq!(s.cursor, 0, "post-scan cursor must snap to top");
        assert!(s.scan_finished);
        // user_navigated also resets so further streaming-style pushes pin to top
        // (in case the user later rescans).
        assert!(!s.user_navigated);
    }

    #[test]
    fn scan_finished_is_idempotent_does_not_reset_cursor_on_redundant_calls() {
        let mut s = fresh_state();
        push(&mut s, "/a");
        push(&mut s, "/b");
        push(&mut s, "/c");
        s.mark_scan_finished();
        assert_eq!(s.cursor, 0);
        // Now user navigates AFTER scan finished.
        s.apply(Action::Down);
        assert_eq!(s.cursor, 1);
        // A second mark_scan_finished call (e.g., main loop double-checks) must
        // NOT yank the cursor back to 0 again.
        s.mark_scan_finished();
        assert_eq!(s.cursor, 1, "redundant mark_scan_finished should be a no-op");
    }

    #[test]
    fn rescan_clears_results_and_emits_effect() {
        let mut s = fresh_state();
        push(&mut s, "/a");
        push(&mut s, "/b");
        s.apply(Action::Down);
        s.mark_scan_finished();
        // Sanity.
        assert_eq!(s.results.len(), 2);
        assert!(s.scan_finished);

        let eff = s.apply(Action::Rescan);
        assert_eq!(eff, Effect::Rescan);
        assert!(s.results.is_empty());
        assert_eq!(s.cursor, 0);
        assert!(!s.scan_finished);
        assert!(!s.user_navigated);
        // status message acknowledges the action
        assert!(s.last_message.as_deref().unwrap_or("").contains("rescan"));
    }

    #[test]
    fn rescan_ignored_in_confirm_mode() {
        let mut s = fresh_state();
        push(&mut s, "/a/node_modules");
        s.apply(Action::RequestDelete);
        assert!(matches!(s.mode, Mode::Confirm(_)));
        let eff = s.apply(Action::Rescan);
        assert_eq!(eff, Effect::None);
        // Modal is still active and results unchanged.
        assert!(matches!(s.mode, Mode::Confirm(_)));
        assert_eq!(s.results.len(), 1);
    }

    #[test]
    fn push_keeps_results_sorted() {
        let mut s = fresh_state();
        // Push in random order with sizes preset via push then record_size.
        let scan = |p: &str| ScanFoundFolder::new(PathBuf::from(p), None);
        s.push_result(scan("/small"));
        s.record_size(Path::new("/small"), 100);
        s.push_result(scan("/big"));
        s.record_size(Path::new("/big"), 10_000);
        s.push_result(scan("/medium"));
        s.record_size(Path::new("/medium"), 1_000);
        // Default Size + Desc → big, medium, small.
        let paths: Vec<_> =
            s.results.iter().map(|r| r.path.to_string_lossy().into_owned()).collect();
        assert_eq!(paths, vec!["/big", "/medium", "/small"]);
    }

    #[test]
    fn age_sort_uses_last_modified() {
        let mut s = fresh_state();
        let now = SystemTime::now();
        s.push_result_with_mtime(
            ScanFoundFolder::new(PathBuf::from("/recent"), None),
            Some(now - Duration::from_secs(10)),
        );
        s.push_result_with_mtime(
            ScanFoundFolder::new(PathBuf::from("/ancient"), None),
            Some(now - Duration::from_secs(10_000)),
        );
        s.push_result_with_mtime(ScanFoundFolder::new(PathBuf::from("/no-mtime"), None), None);
        s.apply(Action::ToggleSortByLastUsed); // Age + Desc (newest first)
        let paths: Vec<_> =
            s.results.iter().map(|r| r.path.to_string_lossy().into_owned()).collect();
        assert_eq!(paths, vec!["/recent", "/ancient", "/no-mtime"]);
    }
}