diskr 0.1.68

Lightweight terminal file explorer and disk/storage manager for macOS
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
//! Persisted scan baselines and diffing — "what grew since the last scan?".
//!
//! `save` records the immediate children of a path (each sized recursively via
//! [`crate::bulkstat`]) together with a timestamp, keyed by absolute path in a
//! single JSON file. `diff` re-scans the path now and compares against the saved
//! baseline. Diffing is kept pure ([`diff_records`]) so it can be tested without
//! touching the filesystem.

use anyhow::{bail, Context, Result};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::{
    bulkstat::{self, SizeInfo},
    state,
};

pub(crate) const HISTORY_MAX_RECORDS: usize = 512;

/// One immediate child of a scanned directory, with its recursive size.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChildSize {
    pub name: String,
    pub is_dir: bool,
    pub size: SizeInfo,
    /// Directories under this child that could not be read while sizing it;
    /// when non-zero the recorded size is a lower bound.
    pub inaccessible: u32,
}

/// A saved scan of a directory's immediate children at a point in time.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScanRecord {
    pub path: PathBuf,
    /// Seconds since the Unix epoch when the baseline was captured.
    pub timestamp: u64,
    pub children: Vec<ChildSize>,
}

impl ScanRecord {
    pub fn total(&self) -> SizeInfo {
        let mut total = SizeInfo::default();
        for child in &self.children {
            total.logical = total.logical.saturating_add(child.size.logical);
            total.allocated = total.allocated.saturating_add(child.size.allocated);
        }
        total
    }

    /// Total directories that were unreadable across all children; non-zero
    /// means the recorded sizes are lower bounds.
    pub fn inaccessible(&self) -> u32 {
        self.children
            .iter()
            .fold(0u32, |acc, child| acc.saturating_add(child.inaccessible))
    }
}

/// A single child's change between two scans. Either side may be `None` when the
/// child only exists in one of the scans (added or removed).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChildChange {
    pub name: String,
    pub before: Option<SizeInfo>,
    pub after: Option<SizeInfo>,
}

impl ChildChange {
    pub fn delta_allocated(&self) -> i128 {
        i128::from(self.after.map(|s| s.allocated).unwrap_or(0))
            - i128::from(self.before.map(|s| s.allocated).unwrap_or(0))
    }

    pub fn delta_logical(&self) -> i128 {
        i128::from(self.after.map(|s| s.logical).unwrap_or(0))
            - i128::from(self.before.map(|s| s.logical).unwrap_or(0))
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DiffReport {
    pub path: PathBuf,
    pub baseline_timestamp: u64,
    pub current_timestamp: u64,
    pub before_total: SizeInfo,
    pub after_total: SizeInfo,
    /// Unreadable-directory counts in the baseline and the current scan; either
    /// being non-zero means the totals and deltas are lower bounds.
    pub baseline_inaccessible: u32,
    pub current_inaccessible: u32,
    /// Children whose size changed, plus additions and removals, sorted by the
    /// magnitude of the allocated-size delta (largest movers first).
    pub changes: Vec<ChildChange>,
}

impl DiffReport {
    pub fn total_delta_allocated(&self) -> i128 {
        i128::from(self.after_total.allocated) - i128::from(self.before_total.allocated)
    }

    pub fn total_delta_logical(&self) -> i128 {
        i128::from(self.after_total.logical) - i128::from(self.before_total.logical)
    }
}

/// Scan `path` and persist the result as the new baseline, returning the record.
pub fn save(path: &Path) -> Result<ScanRecord> {
    validate_dir(path)?;
    let record = scan_record(path)?;
    store_record(&record)?;
    Ok(record)
}

/// Compare a fresh scan of `path` against the saved baseline.
pub fn diff(path: &Path) -> Result<DiffReport> {
    validate_dir(path)?;
    let Some(baseline) = load_record_for_path(path)? else {
        bail!(
            "no saved baseline for {}; run `diskr --save {}` first",
            path.display(),
            path.display()
        );
    };
    diff_from_record(&baseline, path)
}

/// Compare a fresh scan of `path` against an already-loaded baseline.
pub fn diff_from_record(baseline: &ScanRecord, path: &Path) -> Result<DiffReport> {
    validate_dir(path)?;
    let current = scan_record(path)?;
    Ok(diff_records(baseline, &current))
}

/// Like [`diff_from_record`], but the background scan aborts early when
/// `cancellation` fires, returning `Ok(None)`. Used by the TUI so a diff worker
/// for a directory the user already navigated away from stops walking instead
/// of running to completion only to be discarded.
pub fn diff_from_record_with_cancellation(
    baseline: &ScanRecord,
    path: &Path,
    cancellation: &bulkstat::ScanCancellation,
) -> Result<Option<DiffReport>> {
    validate_dir(path)?;
    let Some(current) = scan_record_with_cancellation(path, cancellation)? else {
        return Ok(None);
    };
    Ok(Some(diff_records(baseline, &current)))
}

/// Pure diff of two scan records. `before`/`after` need not be sorted.
pub fn diff_records(before: &ScanRecord, after: &ScanRecord) -> DiffReport {
    let mut changes: Vec<ChildChange> = Vec::new();
    let before_by_name: HashMap<&str, SizeInfo> = before
        .children
        .iter()
        .map(|child| (child.name.as_str(), child.size))
        .collect();
    let after_names: std::collections::HashSet<&str> = after
        .children
        .iter()
        .map(|child| child.name.as_str())
        .collect();

    for child in &after.children {
        let prior = before_by_name.get(child.name.as_str()).copied();
        let change = ChildChange {
            name: child.name.clone(),
            before: prior,
            after: Some(child.size),
        };
        if change.delta_allocated() != 0 || change.delta_logical() != 0 {
            changes.push(change);
        }
    }

    // Removed children: present before, absent now.
    for child in &before.children {
        if !after_names.contains(child.name.as_str()) {
            changes.push(ChildChange {
                name: child.name.clone(),
                before: Some(child.size),
                after: None,
            });
        }
    }

    changes.sort_by(|a, b| {
        b.delta_allocated()
            .abs()
            .cmp(&a.delta_allocated().abs())
            .then(a.name.cmp(&b.name))
    });

    DiffReport {
        path: after.path.clone(),
        baseline_timestamp: before.timestamp,
        current_timestamp: after.timestamp,
        before_total: before.total(),
        after_total: after.total(),
        baseline_inaccessible: before.inaccessible(),
        current_inaccessible: after.inaccessible(),
        changes,
    }
}

fn validate_dir(path: &Path) -> Result<()> {
    if !path.exists() {
        bail!("path does not exist: {}", path.display());
    }
    if !path.is_dir() {
        bail!("path is not a directory: {}", path.display());
    }
    Ok(())
}

pub(crate) fn scan_record(path: &Path) -> Result<ScanRecord> {
    let never = bulkstat::ScanCancellation::never();
    // `never()` cannot fire, so the cancellable scan always yields `Some`.
    Ok(
        scan_record_with_cancellation(path, &never)?.unwrap_or_else(|| ScanRecord {
            path: path.to_path_buf(),
            timestamp: now_secs(),
            children: Vec::new(),
        }),
    )
}

pub(crate) fn scan_record_with_cancellation(
    path: &Path,
    cancellation: &bulkstat::ScanCancellation,
) -> Result<Option<ScanRecord>> {
    validate_dir(path)?;
    if cancellation.is_cancelled() {
        return Ok(None);
    }
    let canonical = path
        .canonicalize()
        .with_context(|| format!("resolve {}", path.display()))?;
    let mut children = Vec::new();
    let read =
        std::fs::read_dir(&canonical).with_context(|| format!("read {}", canonical.display()))?;
    for entry in read.flatten() {
        if cancellation.is_cancelled() {
            return Ok(None);
        }
        let name = entry.file_name().to_string_lossy().into_owned();
        let meta = match std::fs::symlink_metadata(entry.path()) {
            Ok(meta) => meta,
            Err(_) => continue,
        };
        let file_type = meta.file_type();
        if file_type.is_symlink() {
            continue;
        }
        let (is_dir, size, inaccessible) = if file_type.is_dir() {
            let Some(scan) = bulkstat::scan_dir_with_cancellation(&entry.path(), 0, cancellation)
            else {
                return Ok(None);
            };
            (true, scan.size, scan.inaccessible)
        } else if file_type.is_file() {
            use std::os::unix::fs::MetadataExt;
            (
                false,
                SizeInfo::new(meta.len(), meta.blocks().saturating_mul(512)),
                0,
            )
        } else {
            continue;
        };
        children.push(ChildSize {
            name,
            is_dir,
            size,
            inaccessible,
        });
    }
    children.sort_by(|a, b| a.name.cmp(&b.name));

    Ok(Some(ScanRecord {
        path: canonical,
        timestamp: now_secs(),
        children,
    }))
}

fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

fn history_file() -> PathBuf {
    state::state_dir().join("history.json")
}

fn load_history_from_path(path: &Path) -> Result<serde_json::Map<String, serde_json::Value>> {
    let text = match std::fs::read_to_string(path) {
        Ok(text) => text,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(serde_json::Map::new()),
        Err(err) => return Err(err).with_context(|| format!("read {}", path.display())),
    };
    let value: serde_json::Value = serde_json::from_str(&text)
        .with_context(|| format!("parse {} (delete it to reset history)", path.display()))?;
    match value {
        serde_json::Value::Object(map) => Ok(map),
        _ => bail!(
            "unexpected history format in {} (delete it to reset history)",
            path.display()
        ),
    }
}

pub fn load_records() -> Result<HashMap<PathBuf, ScanRecord>> {
    load_records_from_path(&history_file())
}

/// Like [`load_records`], but converts a load failure (a corrupt or
/// unreadable `history.json`) into a user-facing warning string instead of
/// silently presenting "no baselines", mirroring how the size cache surfaces
/// its own load errors.
pub fn load_records_with_warning() -> (HashMap<PathBuf, ScanRecord>, Option<String>) {
    match load_records() {
        Ok(records) => (records, None),
        Err(err) => (HashMap::new(), Some(format!("history load failed: {err}"))),
    }
}

fn load_records_from_path(path: &Path) -> Result<HashMap<PathBuf, ScanRecord>> {
    let history = load_history_from_path(path)?;
    let mut records: HashMap<PathBuf, ScanRecord> = history
        .into_iter()
        .map(|(path, value)| {
            let path = PathBuf::from(path);
            let record = record_from_json(&path, &value);
            (path, record)
        })
        .collect();
    if prune_records(&mut records) {
        store_records_to_path(path, &records)?;
    }
    Ok(records)
}

/// Load the saved baseline for a path if one exists.
pub fn load_record_for_path(path: &Path) -> Result<Option<ScanRecord>> {
    let canonical = path
        .canonicalize()
        .with_context(|| format!("resolve {}", path.display()))?;
    let history = load_records()?;
    Ok(history.get(&canonical).cloned())
}

pub(crate) fn store_record(record: &ScanRecord) -> Result<()> {
    let dir = state::state_dir();
    std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
    let path = history_file();
    store_record_to_path(&path, record)
}

pub(crate) fn store_record_to_path(path: &Path, record: &ScanRecord) -> Result<()> {
    if let Some(dir) = path.parent() {
        std::fs::create_dir_all(dir).with_context(|| format!("create {}", dir.display()))?;
    }
    let mut records = load_records_from_path(path)?;
    records.insert(record.path.clone(), record.clone());
    prune_records(&mut records);
    store_records_to_path(path, &records)?;
    Ok(())
}

pub(crate) fn prune_records(records: &mut HashMap<PathBuf, ScanRecord>) -> bool {
    if records.len() <= HISTORY_MAX_RECORDS {
        return false;
    }
    let mut ordered: Vec<_> = records.drain().collect();
    ordered.sort_by(|(path_a, a), (path_b, b)| {
        b.timestamp
            .cmp(&a.timestamp)
            .then_with(|| path_a.cmp(path_b))
    });
    ordered.truncate(HISTORY_MAX_RECORDS);
    records.extend(ordered);
    true
}

fn store_records_to_path(path: &Path, records: &HashMap<PathBuf, ScanRecord>) -> Result<()> {
    let mut ordered: Vec<_> = records.iter().collect();
    ordered.sort_by(|(path_a, a), (path_b, b)| {
        b.timestamp
            .cmp(&a.timestamp)
            .then_with(|| path_a.cmp(path_b))
    });
    let mut history = serde_json::Map::with_capacity(ordered.len());
    for (path, record) in ordered {
        history.insert(path.to_string_lossy().into_owned(), record_to_json(record));
    }
    let text = serde_json::to_string_pretty(&serde_json::Value::Object(history))?;
    state::atomic_write(path, &text)
}

fn record_to_json(record: &ScanRecord) -> serde_json::Value {
    let children: Vec<serde_json::Value> = record
        .children
        .iter()
        .map(|child| {
            serde_json::json!({
                "name": child.name,
                "is_dir": child.is_dir,
                "logical": child.size.logical,
                "allocated": child.size.allocated,
                "inaccessible": child.inaccessible,
            })
        })
        .collect();
    serde_json::json!({
        "path": record.path.to_string_lossy(),
        "timestamp": record.timestamp,
        "children": children,
    })
}

fn record_from_json(path: &Path, value: &serde_json::Value) -> ScanRecord {
    let timestamp = value.get("timestamp").and_then(|v| v.as_u64()).unwrap_or(0);
    let children = value
        .get("children")
        .and_then(|v| v.as_array())
        .map(|items| {
            items
                .iter()
                .filter_map(|item| {
                    let name = item.get("name")?.as_str()?.to_string();
                    let is_dir = item
                        .get("is_dir")
                        .and_then(|v| v.as_bool())
                        .unwrap_or(false);
                    let logical = item.get("logical").and_then(|v| v.as_u64()).unwrap_or(0);
                    let allocated = item.get("allocated").and_then(|v| v.as_u64()).unwrap_or(0);
                    // Missing in pre-0.1.x baselines; default to 0 so old files
                    // load unchanged.
                    let inaccessible = item
                        .get("inaccessible")
                        .and_then(|v| v.as_u64())
                        .and_then(|n| u32::try_from(n).ok())
                        .unwrap_or(0);
                    Some(ChildSize {
                        name,
                        is_dir,
                        size: SizeInfo::new(logical, allocated),
                        inaccessible,
                    })
                })
                .collect()
        })
        .unwrap_or_default();
    ScanRecord {
        path: path.to_path_buf(),
        timestamp,
        children,
    }
}

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

    fn child(name: &str, allocated: u64) -> ChildSize {
        ChildSize {
            name: name.to_string(),
            is_dir: true,
            size: SizeInfo::new(allocated, allocated),
            inaccessible: 0,
        }
    }

    fn record(children: Vec<ChildSize>) -> ScanRecord {
        ScanRecord {
            path: PathBuf::from("/tmp/example"),
            timestamp: 1000,
            children,
        }
    }

    #[test]
    fn diff_detects_growth_addition_and_removal() {
        let before = record(vec![
            child("steady", 100),
            child("shrinks", 500),
            child("gone", 200),
        ]);
        let mut after = record(vec![
            child("steady", 100),
            child("shrinks", 300),
            child("grows", 900),
        ]);
        after.timestamp = 2000;

        let diff = diff_records(&before, &after);

        // "steady" is unchanged and should be absent.
        assert!(diff.changes.iter().all(|c| c.name != "steady"));
        // Largest mover first: "grows" (+900) over "shrinks" (-200) and "gone" (-200).
        assert_eq!(diff.changes[0].name, "grows");
        assert_eq!(diff.changes[0].before, None);
        assert_eq!(diff.changes[0].delta_allocated(), 900);

        let removed = diff
            .changes
            .iter()
            .find(|c| c.name == "gone")
            .expect("removed child present");
        assert_eq!(removed.after, None);
        assert_eq!(removed.delta_allocated(), -200);

        assert_eq!(diff.before_total.allocated, 800);
        assert_eq!(diff.after_total.allocated, 1300);
        assert_eq!(diff.total_delta_allocated(), 500);
        assert_eq!(diff.baseline_timestamp, 1000);
        assert_eq!(diff.current_timestamp, 2000);
    }

    #[test]
    fn record_round_trips_through_json() {
        let original = ScanRecord {
            path: PathBuf::from("/tmp/example"),
            timestamp: 4242,
            children: vec![
                ChildSize {
                    name: "dir".to_string(),
                    is_dir: true,
                    size: SizeInfo::new(10, 20),
                    inaccessible: 3,
                },
                ChildSize {
                    name: "file".to_string(),
                    is_dir: false,
                    size: SizeInfo::new(30, 40),
                    inaccessible: 0,
                },
            ],
        };

        let json = record_to_json(&original);
        let restored = record_from_json(&original.path, &json);
        assert_eq!(restored, original);
    }

    #[test]
    fn cancelled_diff_returns_none_without_scanning() {
        let dir = std::env::temp_dir().join(format!(
            "diskr_history_cancel_{}_{}",
            std::process::id(),
            now_secs()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let baseline = ScanRecord {
            path: dir.clone(),
            timestamp: 0,
            children: Vec::new(),
        };
        // A token whose generation has already moved past its expected value is
        // cancelled, so the diff aborts without walking.
        let generation = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(2));
        let cancellation = bulkstat::ScanCancellation::new(generation, 1);

        let result = diff_from_record_with_cancellation(&baseline, &dir, &cancellation).unwrap();
        assert!(result.is_none());

        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn corrupt_history_file_surfaces_as_error() {
        // load_records_with_warning relies on the load path returning Err so a
        // corrupt history.json becomes a visible status instead of "no
        // baselines".
        let path = std::env::temp_dir().join(format!(
            "diskr_history_corrupt_{}_{}.json",
            std::process::id(),
            now_secs()
        ));
        std::fs::write(&path, "{ not valid json").unwrap();

        assert!(load_records_from_path(&path).is_err());

        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn old_format_child_without_inaccessible_defaults_to_zero() {
        let value = serde_json::json!({
            "path": "/tmp/example",
            "timestamp": 1,
            "children": [
                {"name": "dir", "is_dir": true, "logical": 10, "allocated": 20}
            ],
        });
        let record = record_from_json(Path::new("/tmp/example"), &value);
        assert_eq!(record.children[0].inaccessible, 0);
        assert_eq!(record.inaccessible(), 0);
    }

    #[test]
    fn diff_reports_inaccessible_totals() {
        let mut before = record(vec![child("a", 100)]);
        before.children[0].inaccessible = 2;
        let mut after = record(vec![child("a", 150)]);
        after.children[0].inaccessible = 0;

        let diff = diff_records(&before, &after);
        assert_eq!(diff.baseline_inaccessible, 2);
        assert_eq!(diff.current_inaccessible, 0);
    }

    #[test]
    fn total_sums_children() {
        let rec = record(vec![child("a", 100), child("b", 250)]);
        assert_eq!(rec.total().allocated, 350);
    }

    #[test]
    fn load_records_uses_json_keys_for_paths() {
        let path = std::env::temp_dir().join(format!(
            "diskr_history_{}_{}.json",
            std::process::id(),
            now_secs()
        ));
        std::fs::write(
            &path,
            r#"{
  "/tmp/first": {"path":"/wrong","timestamp":1,"children":[]},
  "/tmp/second": {"timestamp":2,"children":[]}
}"#,
        )
        .unwrap();

        let records = load_records_from_path(&path).unwrap();

        assert_eq!(
            records.get(Path::new("/tmp/first")).unwrap().path,
            PathBuf::from("/tmp/first")
        );
        assert_eq!(
            records.get(Path::new("/tmp/second")).unwrap().path,
            PathBuf::from("/tmp/second")
        );

        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn load_records_prunes_older_entries() {
        let path = std::env::temp_dir().join(format!(
            "diskr_history_prune_{}_{}.json",
            std::process::id(),
            now_secs()
        ));
        let mut history = serde_json::Map::new();
        for idx in 0..(HISTORY_MAX_RECORDS + 2) {
            let path = PathBuf::from(format!("/tmp/history-{idx}"));
            history.insert(
                path.to_string_lossy().into_owned(),
                record_to_json(&ScanRecord {
                    path,
                    timestamp: idx as u64,
                    children: Vec::new(),
                }),
            );
        }
        std::fs::write(
            &path,
            serde_json::to_string_pretty(&serde_json::Value::Object(history)).unwrap(),
        )
        .unwrap();

        let records = load_records_from_path(&path).unwrap();
        let pruned_text = std::fs::read_to_string(&path).unwrap();

        assert_eq!(records.len(), HISTORY_MAX_RECORDS);
        assert!(!records.contains_key(Path::new("/tmp/history-0")));
        assert!(records.contains_key(Path::new(&format!(
            "/tmp/history-{}",
            HISTORY_MAX_RECORDS + 1
        ))));
        let pruned_value: serde_json::Value = serde_json::from_str(&pruned_text).unwrap();
        assert_eq!(pruned_value.as_object().unwrap().len(), HISTORY_MAX_RECORDS);

        let _ = std::fs::remove_file(path);
    }
}