rustdupe 0.2.0

Smart duplicate file finder with interactive TUI
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
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
//! Safe file deletion using trash crate.
//!
//! # Overview
//!
//! This module provides safe file deletion functionality:
//! - Move to system trash (default, recoverable)
//! - Permanent deletion (with explicit flag)
//! - Batch operations with progress reporting
//! - TOCTOU verification before deletion
//!
//! # Safety
//!
//! All deletion operations verify file existence before attempting deletion.
//! At least one copy is always preserved when deleting from duplicate groups.
//!
//! # Example
//!
//! ```no_run
//! use rustdupe::actions::delete::{delete_to_trash, DeleteConfig};
//! use std::path::PathBuf;
//!
//! // Single file deletion to trash
//! let path = PathBuf::from("/path/to/duplicate.txt");
//! match delete_to_trash(&path) {
//!     Ok(result) => println!("Deleted: {}", result.path.display()),
//!     Err(e) => eprintln!("Failed: {}", e),
//! }
//! ```

use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use thiserror::Error;

/// Error type for deletion operations.
#[derive(Debug, Error)]
pub enum DeleteError {
    /// File was not found (may have been deleted or moved).
    #[error("file not found: {0}")]
    NotFound(PathBuf),

    /// Permission denied when attempting to delete.
    #[error("permission denied: {0}")]
    PermissionDenied(PathBuf),

    /// File was modified since scan (TOCTOU protection).
    #[error("file modified since scan: {0}")]
    Modified(PathBuf),

    /// Trash operation failed.
    #[error("trash operation failed for {path}: {message}")]
    TrashFailed { path: PathBuf, message: String },

    /// Permanent delete operation failed.
    #[error("permanent delete failed for {path}: {message}")]
    PermanentDeleteFailed { path: PathBuf, message: String },

    /// Attempted to delete all copies (at least one must be preserved).
    #[error("cannot delete all copies - at least one file must be preserved")]
    AllCopiesWouldBeDeleted,

    /// General I/O error.
    #[error("I/O error for {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: io::Error,
    },
}

impl DeleteError {
    /// Get the path associated with this error (if any).
    #[must_use]
    pub fn path(&self) -> Option<&Path> {
        match self {
            Self::NotFound(p)
            | Self::PermissionDenied(p)
            | Self::Modified(p)
            | Self::TrashFailed { path: p, .. }
            | Self::PermanentDeleteFailed { path: p, .. }
            | Self::Io { path: p, .. } => Some(p),
            Self::AllCopiesWouldBeDeleted => None,
        }
    }
}

/// Result of a successful deletion operation.
#[derive(Debug, Clone)]
pub struct DeleteResult {
    /// Path that was deleted.
    pub path: PathBuf,
    /// Size of the deleted file in bytes.
    pub size: u64,
    /// Whether deletion was permanent (true) or to trash (false).
    pub permanent: bool,
}

impl DeleteResult {
    /// Create a new delete result.
    #[must_use]
    pub fn new(path: PathBuf, size: u64, permanent: bool) -> Self {
        Self {
            path,
            size,
            permanent,
        }
    }
}

/// Results of a batch deletion operation.
#[derive(Debug, Clone, Default)]
pub struct BatchDeleteResult {
    /// Successfully deleted files.
    pub successes: Vec<DeleteResult>,
    /// Failed deletions with their errors.
    pub failures: Vec<(PathBuf, String)>,
    /// Total bytes freed.
    pub bytes_freed: u64,
}

impl BatchDeleteResult {
    /// Number of successful deletions.
    #[must_use]
    pub fn success_count(&self) -> usize {
        self.successes.len()
    }

    /// Number of failed deletions.
    #[must_use]
    pub fn failure_count(&self) -> usize {
        self.failures.len()
    }

    /// Total number of attempted deletions.
    #[must_use]
    pub fn total_count(&self) -> usize {
        self.successes.len() + self.failures.len()
    }

    /// Check if all deletions succeeded.
    #[must_use]
    pub fn all_succeeded(&self) -> bool {
        self.failures.is_empty()
    }

    /// Human-readable summary of the operation.
    #[must_use]
    pub fn summary(&self) -> String {
        if self.all_succeeded() {
            format!(
                "Deleted {} file(s), freed {} bytes",
                self.success_count(),
                self.bytes_freed
            )
        } else {
            format!(
                "Deleted {} file(s), {} failed, freed {} bytes",
                self.success_count(),
                self.failure_count(),
                self.bytes_freed
            )
        }
    }
}

/// Configuration for deletion operations.
#[derive(Debug, Clone)]
pub struct DeleteConfig {
    /// Use permanent deletion instead of trash.
    pub permanent: bool,
    /// Verify file modification time before deletion (TOCTOU protection).
    pub verify_mtime: bool,
    /// Continue on error (process remaining files even if some fail).
    pub continue_on_error: bool,
}

impl Default for DeleteConfig {
    fn default() -> Self {
        Self {
            permanent: false,
            verify_mtime: true,
            continue_on_error: true,
        }
    }
}

impl DeleteConfig {
    /// Create config for trash deletion.
    #[must_use]
    pub fn trash() -> Self {
        Self::default()
    }

    /// Create config for permanent deletion.
    #[must_use]
    pub fn permanent() -> Self {
        Self {
            permanent: true,
            ..Self::default()
        }
    }

    /// Enable/disable TOCTOU verification.
    #[must_use]
    pub fn with_verify_mtime(mut self, verify: bool) -> Self {
        self.verify_mtime = verify;
        self
    }

    /// Enable/disable continue on error.
    #[must_use]
    pub fn with_continue_on_error(mut self, continue_on_error: bool) -> Self {
        self.continue_on_error = continue_on_error;
        self
    }
}

/// Callback trait for deletion progress reporting.
pub trait DeleteProgressCallback: Send + Sync {
    /// Called before each file deletion.
    fn on_before_delete(&self, path: &Path, index: usize, total: usize);

    /// Called after successful deletion.
    fn on_delete_success(&self, path: &Path, size: u64);

    /// Called after failed deletion.
    fn on_delete_failure(&self, path: &Path, error: &str);

    /// Called when batch operation completes.
    fn on_complete(&self, result: &BatchDeleteResult);
}

/// File metadata snapshot for TOCTOU verification.
#[derive(Debug, Clone)]
pub struct FileSnapshot {
    /// Path to the file.
    pub path: PathBuf,
    /// File size in bytes.
    pub size: u64,
    /// Last modification time.
    pub mtime: Option<SystemTime>,
}

impl FileSnapshot {
    /// Create a snapshot of a file's current state.
    ///
    /// # Errors
    ///
    /// Returns error if file doesn't exist or can't be accessed.
    pub fn capture(path: &Path) -> Result<Self, DeleteError> {
        let metadata = fs::metadata(path).map_err(|e| match e.kind() {
            io::ErrorKind::NotFound => DeleteError::NotFound(path.to_path_buf()),
            io::ErrorKind::PermissionDenied => DeleteError::PermissionDenied(path.to_path_buf()),
            _ => DeleteError::Io {
                path: path.to_path_buf(),
                source: e,
            },
        })?;

        Ok(Self {
            path: path.to_path_buf(),
            size: metadata.len(),
            mtime: metadata.modified().ok(),
        })
    }

    /// Verify that the file still matches this snapshot.
    ///
    /// # Errors
    ///
    /// Returns error if file was modified, deleted, or can't be accessed.
    pub fn verify(&self) -> Result<(), DeleteError> {
        let current = Self::capture(&self.path)?;

        // Check if mtime changed (if we have both mtimes)
        if let (Some(orig), Some(curr)) = (self.mtime, current.mtime) {
            if orig != curr {
                log::warn!(
                    "File modified since scan: {} (mtime changed)",
                    self.path.display()
                );
                return Err(DeleteError::Modified(self.path.clone()));
            }
        }

        // Check if size changed
        if self.size != current.size {
            log::warn!(
                "File modified since scan: {} (size changed from {} to {})",
                self.path.display(),
                self.size,
                current.size
            );
            return Err(DeleteError::Modified(self.path.clone()));
        }

        Ok(())
    }
}

/// Delete a single file to the system trash.
///
/// This is the safest deletion method - files can be recovered from trash.
///
/// # Arguments
///
/// * `path` - Path to the file to delete
///
/// # Returns
///
/// A `DeleteResult` on success, or `DeleteError` on failure.
///
/// # Errors
///
/// - `NotFound` if the file doesn't exist
/// - `PermissionDenied` if deletion is not allowed
/// - `TrashFailed` if the trash operation fails
///
/// # Example
///
/// ```no_run
/// use rustdupe::actions::delete::delete_to_trash;
/// use std::path::PathBuf;
///
/// let path = PathBuf::from("/path/to/file.txt");
/// match delete_to_trash(&path) {
///     Ok(result) => println!("Moved to trash: {}", result.path.display()),
///     Err(e) => eprintln!("Failed: {}", e),
/// }
/// ```
pub fn delete_to_trash(path: &Path) -> Result<DeleteResult, DeleteError> {
    // Get file size before deletion
    let metadata = fs::metadata(path).map_err(|e| match e.kind() {
        io::ErrorKind::NotFound => DeleteError::NotFound(path.to_path_buf()),
        io::ErrorKind::PermissionDenied => DeleteError::PermissionDenied(path.to_path_buf()),
        _ => DeleteError::Io {
            path: path.to_path_buf(),
            source: e,
        },
    })?;

    let size = metadata.len();

    // Move to trash
    trash::delete(path).map_err(|e| {
        log::error!("Trash operation failed for {}: {}", path.display(), e);
        DeleteError::TrashFailed {
            path: path.to_path_buf(),
            message: e.to_string(),
        }
    })?;

    log::info!("Moved to trash: {} ({} bytes)", path.display(), size);

    Ok(DeleteResult::new(path.to_path_buf(), size, false))
}

/// Permanently delete a single file.
///
/// **WARNING**: This operation cannot be undone. The file will be permanently removed.
///
/// # Arguments
///
/// * `path` - Path to the file to delete
///
/// # Returns
///
/// A `DeleteResult` on success, or `DeleteError` on failure.
///
/// # Errors
///
/// - `NotFound` if the file doesn't exist
/// - `PermissionDenied` if deletion is not allowed
/// - `PermanentDeleteFailed` if the delete operation fails
///
/// # Example
///
/// ```no_run
/// use rustdupe::actions::delete::permanent_delete;
/// use std::path::PathBuf;
///
/// let path = PathBuf::from("/path/to/file.txt");
/// match permanent_delete(&path) {
///     Ok(result) => println!("Permanently deleted: {}", result.path.display()),
///     Err(e) => eprintln!("Failed: {}", e),
/// }
/// ```
pub fn permanent_delete(path: &Path) -> Result<DeleteResult, DeleteError> {
    // Get file size before deletion
    let metadata = fs::metadata(path).map_err(|e| match e.kind() {
        io::ErrorKind::NotFound => DeleteError::NotFound(path.to_path_buf()),
        io::ErrorKind::PermissionDenied => DeleteError::PermissionDenied(path.to_path_buf()),
        _ => DeleteError::Io {
            path: path.to_path_buf(),
            source: e,
        },
    })?;

    let size = metadata.len();

    // Permanently delete
    fs::remove_file(path).map_err(|e| {
        log::error!("Permanent delete failed for {}: {}", path.display(), e);
        DeleteError::PermanentDeleteFailed {
            path: path.to_path_buf(),
            message: e.to_string(),
        }
    })?;

    log::info!("Permanently deleted: {} ({} bytes)", path.display(), size);

    Ok(DeleteResult::new(path.to_path_buf(), size, true))
}

/// Delete a single file with TOCTOU verification.
///
/// Verifies the file hasn't changed since it was scanned before deleting.
///
/// # Arguments
///
/// * `path` - Path to the file to delete
/// * `expected_mtime` - Expected modification time (from scan)
/// * `config` - Deletion configuration
///
/// # Errors
///
/// - `Modified` if the file was changed since scan
/// - Other errors from `delete_to_trash` or `permanent_delete`
pub fn delete_verified(
    path: &Path,
    expected_mtime: Option<SystemTime>,
    config: &DeleteConfig,
) -> Result<DeleteResult, DeleteError> {
    // TOCTOU verification
    if config.verify_mtime {
        // Get current metadata
        let current = FileSnapshot::capture(path)?;

        // Compare mtime if available
        if let (Some(expected), Some(actual)) = (expected_mtime, current.mtime) {
            if expected != actual {
                return Err(DeleteError::Modified(path.to_path_buf()));
            }
        }
    }

    // Perform deletion
    if config.permanent {
        permanent_delete(path)
    } else {
        delete_to_trash(path)
    }
}

/// Delete multiple files in batch.
///
/// Processes all files, continuing on error if configured to do so.
/// At least one file in each duplicate group should be preserved before calling this.
///
/// # Arguments
///
/// * `paths` - Slice of paths to delete
/// * `config` - Deletion configuration
/// * `callback` - Optional progress callback
///
/// # Returns
///
/// A `BatchDeleteResult` with success/failure information.
///
/// # Example
///
/// ```no_run
/// use rustdupe::actions::delete::{delete_batch, DeleteConfig, DeleteProgressCallback, BatchDeleteResult};
/// use std::path::{Path, PathBuf};
///
/// // Define a simple no-op callback (required for type inference)
/// struct NoCallback;
/// impl DeleteProgressCallback for NoCallback {
///     fn on_before_delete(&self, _: &Path, _: usize, _: usize) {}
///     fn on_delete_success(&self, _: &Path, _: u64) {}
///     fn on_delete_failure(&self, _: &Path, _: &str) {}
///     fn on_complete(&self, _: &BatchDeleteResult) {}
/// }
///
/// let paths = vec![
///     PathBuf::from("/dup1.txt"),
///     PathBuf::from("/dup2.txt"),
/// ];
///
/// let result = delete_batch::<NoCallback>(&paths, &DeleteConfig::default(), None);
/// println!("{}", result.summary());
/// ```
pub fn delete_batch<C: DeleteProgressCallback>(
    paths: &[PathBuf],
    config: &DeleteConfig,
    callback: Option<&C>,
) -> BatchDeleteResult {
    let mut result = BatchDeleteResult::default();
    let total = paths.len();

    for (index, path) in paths.iter().enumerate() {
        // Progress callback
        if let Some(cb) = callback {
            cb.on_before_delete(path, index, total);
        }

        // Attempt deletion
        let delete_result = if config.permanent {
            permanent_delete(path)
        } else {
            delete_to_trash(path)
        };

        match delete_result {
            Ok(del) => {
                result.bytes_freed += del.size;
                if let Some(cb) = callback {
                    cb.on_delete_success(path, del.size);
                }
                result.successes.push(del);
            }
            Err(e) => {
                let error_msg = e.to_string();
                log::warn!("Failed to delete {}: {}", path.display(), error_msg);

                if let Some(cb) = callback {
                    cb.on_delete_failure(path, &error_msg);
                }

                result.failures.push((path.clone(), error_msg));

                if !config.continue_on_error {
                    log::info!("Stopping batch deletion due to error (continue_on_error=false)");
                    break;
                }
            }
        }
    }

    // Completion callback
    if let Some(cb) = callback {
        cb.on_complete(&result);
    }

    log::info!("{}", result.summary());

    result
}

/// Validate that a selection doesn't delete all copies.
///
/// At least one copy of each duplicate group must be preserved.
///
/// # Arguments
///
/// * `selected_paths` - Paths selected for deletion
/// * `group_paths` - All paths in the duplicate group
///
/// # Errors
///
/// Returns `AllCopiesWouldBeDeleted` if all copies would be deleted.
///
/// # Example
///
/// ```
/// use rustdupe::actions::delete::validate_preserves_copy;
/// use std::path::PathBuf;
///
/// let group = vec![
///     PathBuf::from("/original.txt"),
///     PathBuf::from("/copy1.txt"),
///     PathBuf::from("/copy2.txt"),
/// ];
///
/// // This is OK - one copy preserved
/// let selected = vec![
///     PathBuf::from("/copy1.txt"),
///     PathBuf::from("/copy2.txt"),
/// ];
/// assert!(validate_preserves_copy(&selected, &group).is_ok());
///
/// // This would delete all copies - error
/// let all_selected = group.clone();
/// assert!(validate_preserves_copy(&all_selected, &group).is_err());
/// ```
pub fn validate_preserves_copy(
    selected_paths: &[PathBuf],
    group_paths: &[PathBuf],
) -> Result<(), DeleteError> {
    use std::collections::HashSet;

    let selected_set: HashSet<&PathBuf> = selected_paths.iter().collect();
    let preserved_count = group_paths
        .iter()
        .filter(|p| !selected_set.contains(p))
        .count();

    if preserved_count == 0 {
        log::error!(
            "Attempted to delete all {} copies of a duplicate group",
            group_paths.len()
        );
        Err(DeleteError::AllCopiesWouldBeDeleted)
    } else {
        log::debug!(
            "Deletion validated: {} files selected, {} preserved",
            selected_paths.len(),
            preserved_count
        );
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_temp_file(dir: &TempDir, name: &str, content: &[u8]) -> PathBuf {
        let path = dir.path().join(name);
        let mut file = fs::File::create(&path).expect("Failed to create temp file");
        file.write_all(content).expect("Failed to write content");
        path
    }

    // ==================== DeleteError Tests ====================

    #[test]
    fn test_delete_error_path() {
        let path = PathBuf::from("/test/path");

        assert_eq!(
            DeleteError::NotFound(path.clone()).path(),
            Some(path.as_path())
        );
        assert_eq!(
            DeleteError::PermissionDenied(path.clone()).path(),
            Some(path.as_path())
        );
        assert_eq!(
            DeleteError::Modified(path.clone()).path(),
            Some(path.as_path())
        );
        assert_eq!(DeleteError::AllCopiesWouldBeDeleted.path(), None);
    }

    #[test]
    fn test_delete_error_display() {
        let path = PathBuf::from("/test/file.txt");

        let err = DeleteError::NotFound(path.clone());
        assert!(err.to_string().contains("not found"));

        let err = DeleteError::Modified(path.clone());
        assert!(err.to_string().contains("modified"));

        let err = DeleteError::AllCopiesWouldBeDeleted;
        assert!(err.to_string().contains("at least one"));
    }

    // ==================== DeleteResult Tests ====================

    #[test]
    fn test_delete_result_new() {
        let result = DeleteResult::new(PathBuf::from("/test.txt"), 1024, false);

        assert_eq!(result.path, PathBuf::from("/test.txt"));
        assert_eq!(result.size, 1024);
        assert!(!result.permanent);
    }

    // ==================== BatchDeleteResult Tests ====================

    #[test]
    fn test_batch_delete_result_default() {
        let result = BatchDeleteResult::default();

        assert_eq!(result.success_count(), 0);
        assert_eq!(result.failure_count(), 0);
        assert_eq!(result.total_count(), 0);
        assert!(result.all_succeeded());
        assert_eq!(result.bytes_freed, 0);
    }

    #[test]
    fn test_batch_delete_result_with_successes() {
        let mut result = BatchDeleteResult::default();
        result
            .successes
            .push(DeleteResult::new(PathBuf::from("/a.txt"), 1000, false));
        result
            .successes
            .push(DeleteResult::new(PathBuf::from("/b.txt"), 2000, false));
        result.bytes_freed = 3000;

        assert_eq!(result.success_count(), 2);
        assert_eq!(result.failure_count(), 0);
        assert!(result.all_succeeded());
        assert!(result.summary().contains("2 file(s)"));
        assert!(result.summary().contains("3000"));
    }

    #[test]
    fn test_batch_delete_result_with_failures() {
        let mut result = BatchDeleteResult::default();
        result
            .successes
            .push(DeleteResult::new(PathBuf::from("/a.txt"), 1000, false));
        result
            .failures
            .push((PathBuf::from("/b.txt"), "permission denied".to_string()));

        assert_eq!(result.success_count(), 1);
        assert_eq!(result.failure_count(), 1);
        assert_eq!(result.total_count(), 2);
        assert!(!result.all_succeeded());
        assert!(result.summary().contains("1 failed"));
    }

    // ==================== DeleteConfig Tests ====================

    #[test]
    fn test_delete_config_default() {
        let config = DeleteConfig::default();

        assert!(!config.permanent);
        assert!(config.verify_mtime);
        assert!(config.continue_on_error);
    }

    #[test]
    fn test_delete_config_trash() {
        let config = DeleteConfig::trash();
        assert!(!config.permanent);
    }

    #[test]
    fn test_delete_config_permanent() {
        let config = DeleteConfig::permanent();
        assert!(config.permanent);
    }

    #[test]
    fn test_delete_config_builders() {
        let config = DeleteConfig::default()
            .with_verify_mtime(false)
            .with_continue_on_error(false);

        assert!(!config.verify_mtime);
        assert!(!config.continue_on_error);
    }

    // ==================== FileSnapshot Tests ====================

    #[test]
    fn test_file_snapshot_capture() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let path = create_temp_file(&dir, "test.txt", b"hello");

        let snapshot = FileSnapshot::capture(&path).expect("Failed to capture snapshot");

        assert_eq!(snapshot.path, path);
        assert_eq!(snapshot.size, 5);
        assert!(snapshot.mtime.is_some());
    }

    #[test]
    fn test_file_snapshot_capture_not_found() {
        let path = PathBuf::from("/nonexistent/file.txt");
        let result = FileSnapshot::capture(&path);

        assert!(matches!(result, Err(DeleteError::NotFound(_))));
    }

    #[test]
    fn test_file_snapshot_verify_unchanged() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let path = create_temp_file(&dir, "test.txt", b"hello");

        let snapshot = FileSnapshot::capture(&path).expect("Failed to capture snapshot");
        let result = snapshot.verify();

        assert!(result.is_ok());
    }

    #[test]
    fn test_file_snapshot_verify_deleted() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let path = create_temp_file(&dir, "test.txt", b"hello");

        let snapshot = FileSnapshot::capture(&path).expect("Failed to capture snapshot");

        // Delete the file
        fs::remove_file(&path).expect("Failed to delete file");

        let result = snapshot.verify();
        assert!(matches!(result, Err(DeleteError::NotFound(_))));
    }

    // ==================== validate_preserves_copy Tests ====================

    #[test]
    fn test_validate_preserves_copy_one_preserved() {
        let group = vec![
            PathBuf::from("/a.txt"),
            PathBuf::from("/b.txt"),
            PathBuf::from("/c.txt"),
        ];
        let selected = vec![PathBuf::from("/b.txt"), PathBuf::from("/c.txt")];

        let result = validate_preserves_copy(&selected, &group);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_preserves_copy_all_preserved() {
        let group = vec![PathBuf::from("/a.txt"), PathBuf::from("/b.txt")];
        let selected = vec![];

        let result = validate_preserves_copy(&selected, &group);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_preserves_copy_none_preserved() {
        let group = vec![PathBuf::from("/a.txt"), PathBuf::from("/b.txt")];
        let selected = vec![PathBuf::from("/a.txt"), PathBuf::from("/b.txt")];

        let result = validate_preserves_copy(&selected, &group);
        assert!(matches!(result, Err(DeleteError::AllCopiesWouldBeDeleted)));
    }

    #[test]
    fn test_validate_preserves_copy_single_file_group() {
        let group = vec![PathBuf::from("/a.txt")];
        let selected = vec![PathBuf::from("/a.txt")];

        let result = validate_preserves_copy(&selected, &group);
        assert!(matches!(result, Err(DeleteError::AllCopiesWouldBeDeleted)));
    }

    #[test]
    fn test_validate_preserves_copy_empty_selection() {
        let group = vec![PathBuf::from("/a.txt")];
        let selected: Vec<PathBuf> = vec![];

        let result = validate_preserves_copy(&selected, &group);
        assert!(result.is_ok());
    }

    // ==================== permanent_delete Tests ====================

    #[test]
    fn test_permanent_delete_success() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let path = create_temp_file(&dir, "delete_me.txt", b"test content");

        assert!(path.exists());

        let result = permanent_delete(&path).expect("Failed to delete");

        assert!(!path.exists());
        assert_eq!(result.size, 12); // "test content"
        assert!(result.permanent);
    }

    #[test]
    fn test_permanent_delete_not_found() {
        let path = PathBuf::from("/nonexistent/file.txt");
        let result = permanent_delete(&path);

        assert!(matches!(result, Err(DeleteError::NotFound(_))));
    }

    // ==================== delete_to_trash Tests ====================

    #[test]
    fn test_delete_to_trash_not_found() {
        let path = PathBuf::from("/nonexistent/file.txt");
        let result = delete_to_trash(&path);

        assert!(matches!(result, Err(DeleteError::NotFound(_))));
    }

    // Note: Actual trash tests are platform-dependent and may not work in all environments.
    // The trash crate handles the platform-specific implementation.

    // ==================== delete_batch Tests ====================

    #[test]
    fn test_delete_batch_empty() {
        let paths: Vec<PathBuf> = vec![];
        let config = DeleteConfig::permanent();

        let result = delete_batch::<NoOpCallback>(&paths, &config, None);

        assert_eq!(result.success_count(), 0);
        assert_eq!(result.failure_count(), 0);
        assert!(result.all_succeeded());
    }

    #[test]
    fn test_delete_batch_with_failures() {
        let paths = vec![
            PathBuf::from("/nonexistent1.txt"),
            PathBuf::from("/nonexistent2.txt"),
        ];
        let config = DeleteConfig::permanent();

        let result = delete_batch::<NoOpCallback>(&paths, &config, None);

        assert_eq!(result.success_count(), 0);
        assert_eq!(result.failure_count(), 2);
        assert!(!result.all_succeeded());
    }

    #[test]
    fn test_delete_batch_mixed() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let existing = create_temp_file(&dir, "exists.txt", b"content");
        let nonexistent = PathBuf::from("/nonexistent.txt");

        let paths = vec![existing.clone(), nonexistent];
        let config = DeleteConfig::permanent();

        let result = delete_batch::<NoOpCallback>(&paths, &config, None);

        assert_eq!(result.success_count(), 1);
        assert_eq!(result.failure_count(), 1);
        assert!(!existing.exists());
    }

    #[test]
    fn test_delete_batch_stop_on_error() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let nonexistent = PathBuf::from("/nonexistent.txt");
        let existing = create_temp_file(&dir, "exists.txt", b"content");

        // Put nonexistent first to trigger error
        let paths = vec![nonexistent, existing.clone()];
        let config = DeleteConfig::permanent().with_continue_on_error(false);

        let result = delete_batch::<NoOpCallback>(&paths, &config, None);

        // Should stop after first error
        assert_eq!(result.total_count(), 1);
        assert_eq!(result.failure_count(), 1);
        // Second file should still exist
        assert!(existing.exists());
    }

    #[test]
    fn test_delete_batch_with_callback() {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let path = create_temp_file(&dir, "test.txt", b"content");

        let paths = vec![path.clone()];
        let config = DeleteConfig::permanent();
        let callback = TestCallback::new();

        let result = delete_batch(&paths, &config, Some(&callback));

        assert_eq!(result.success_count(), 1);
        assert!(callback.before_count() >= 1);
        assert!(callback.success_count() >= 1);
        assert!(callback.complete_called());
    }

    // ==================== Test Helpers ====================

    /// No-op callback for tests that don't need progress reporting.
    struct NoOpCallback;

    impl DeleteProgressCallback for NoOpCallback {
        fn on_before_delete(&self, _path: &Path, _index: usize, _total: usize) {}
        fn on_delete_success(&self, _path: &Path, _size: u64) {}
        fn on_delete_failure(&self, _path: &Path, _error: &str) {}
        fn on_complete(&self, _result: &BatchDeleteResult) {}
    }

    /// Test callback that tracks calls.
    struct TestCallback {
        before: std::sync::atomic::AtomicUsize,
        success: std::sync::atomic::AtomicUsize,
        failure: std::sync::atomic::AtomicUsize,
        complete: std::sync::atomic::AtomicBool,
    }

    impl TestCallback {
        fn new() -> Self {
            Self {
                before: std::sync::atomic::AtomicUsize::new(0),
                success: std::sync::atomic::AtomicUsize::new(0),
                failure: std::sync::atomic::AtomicUsize::new(0),
                complete: std::sync::atomic::AtomicBool::new(false),
            }
        }

        fn before_count(&self) -> usize {
            self.before.load(std::sync::atomic::Ordering::SeqCst)
        }

        fn success_count(&self) -> usize {
            self.success.load(std::sync::atomic::Ordering::SeqCst)
        }

        fn complete_called(&self) -> bool {
            self.complete.load(std::sync::atomic::Ordering::SeqCst)
        }
    }

    impl DeleteProgressCallback for TestCallback {
        fn on_before_delete(&self, _path: &Path, _index: usize, _total: usize) {
            self.before
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }

        fn on_delete_success(&self, _path: &Path, _size: u64) {
            self.success
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }

        fn on_delete_failure(&self, _path: &Path, _error: &str) {
            self.failure
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }

        fn on_complete(&self, _result: &BatchDeleteResult) {
            self.complete
                .store(true, std::sync::atomic::Ordering::SeqCst);
        }
    }
}