oximedia-dedup 0.1.5

Media deduplication and duplicate detection for OxiMedia
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
//! Network-aware deduplication for distributed media libraries.
//!
//! This module provides mechanisms to deduplicate media across multiple nodes in
//! a distributed system.  Rather than requiring every node to download every file,
//! nodes exchange compact **fingerprint manifests** and only transfer content when
//! necessary.
//!
//! # Design
//!
//! Each node maintains a local [`NodeManifest`] containing fingerprint summaries
//! (Blake3 hex digest, perceptual hash bits, duration, file size) for its local
//! media files.  Manifests are serialisable as JSON so they can be transmitted over
//! HTTP or any byte channel without coupling to a particular transport.
//!
//! The [`NetworkDedupEngine`] accepts manifests from multiple remote nodes and
//! computes cross-node duplicate groups by:
//!
//! 1. **Exact match** – identical Blake3 digests → definite duplicate.
//! 2. **Perceptual match** – Hamming distance on 64-bit pHash ≤ configured
//!    threshold → near-duplicate candidate.
//! 3. **Duration guard** – files with very different durations (> `duration_tolerance_s`)
//!    are excluded from perceptual matching to reduce false positives.
//!
//! # Example
//!
//! ```rust
//! use oximedia_dedup::network_dedup::{
//!     NetworkDedupEngine, NetworkDedupConfig, NodeManifest, FileRecord,
//! };
//!
//! let mut engine = NetworkDedupEngine::new(NetworkDedupConfig::default());
//!
//! let mut manifest_a = NodeManifest::new("node-a".to_string());
//! manifest_a.add_file(FileRecord::new(
//!     "node-a:/videos/movie.mp4".to_string(),
//!     "abcdef01".repeat(8),
//!     Some(0xDEAD_BEEF_1234_5678),
//!     Some(7200.0),
//!     Some(4_000_000_000),
//! ));
//!
//! let mut manifest_b = NodeManifest::new("node-b".to_string());
//! manifest_b.add_file(FileRecord::new(
//!     "node-b:/archive/movie_copy.mp4".to_string(),
//!     "abcdef01".repeat(8),
//!     Some(0xDEAD_BEEF_1234_5678),
//!     Some(7200.0),
//!     Some(4_000_000_000),
//! ));
//!
//! engine.add_manifest(manifest_a);
//! engine.add_manifest(manifest_b);
//!
//! let groups = engine.find_cross_node_duplicates();
//! assert!(!groups.is_empty());
//! ```

#![allow(dead_code)]
#![allow(clippy::cast_precision_loss)]

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// FileRecord
// ---------------------------------------------------------------------------

/// A single file entry within a node's manifest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileRecord {
    /// Logical URI for this file (e.g. `"node-a:/path/to/file.mp4"`).
    pub uri: String,
    /// Lower-case hexadecimal BLAKE3 digest (64 hex characters).
    pub blake3_hex: String,
    /// Optional 64-bit perceptual hash.
    pub phash: Option<u64>,
    /// Optional duration in seconds.
    pub duration_s: Option<f64>,
    /// Optional file size in bytes.
    pub file_size: Option<u64>,
}

impl FileRecord {
    /// Create a new `FileRecord`.
    #[must_use]
    pub fn new(
        uri: String,
        blake3_hex: String,
        phash: Option<u64>,
        duration_s: Option<f64>,
        file_size: Option<u64>,
    ) -> Self {
        Self {
            uri,
            blake3_hex,
            phash,
            duration_s,
            file_size,
        }
    }

    /// Return `true` if this record has a valid-looking Blake3 hex digest.
    ///
    /// BLAKE3 produces 32 bytes → 64 hex characters.
    #[must_use]
    pub fn has_valid_digest(&self) -> bool {
        self.blake3_hex.len() == 64
            && self
                .blake3_hex
                .chars()
                .all(|c| c.is_ascii_hexdigit())
    }

    /// Compute the Hamming distance to another record's perceptual hash.
    ///
    /// Returns `None` if either record lacks a perceptual hash.
    #[must_use]
    pub fn phash_distance(&self, other: &Self) -> Option<u32> {
        match (self.phash, other.phash) {
            (Some(a), Some(b)) => Some((a ^ b).count_ones()),
            _ => None,
        }
    }

    /// Return the logical node name extracted from the URI prefix
    /// (`"node-a:/foo"` → `"node-a"`).
    #[must_use]
    pub fn node_name(&self) -> Option<&str> {
        self.uri.split(':').next()
    }
}

// ---------------------------------------------------------------------------
// NodeManifest
// ---------------------------------------------------------------------------

/// Fingerprint manifest for a single node.
///
/// A manifest holds all [`FileRecord`]s known to a particular node and can be
/// serialised/deserialised as JSON for transport.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeManifest {
    /// Human-readable node identifier.
    pub node_id: String,
    /// The file records.
    pub records: Vec<FileRecord>,
    /// Creation timestamp (Unix seconds) — informational only.
    pub created_at: u64,
}

impl NodeManifest {
    /// Create an empty manifest for `node_id`.
    #[must_use]
    pub fn new(node_id: String) -> Self {
        Self {
            node_id,
            records: Vec::new(),
            created_at: 0,
        }
    }

    /// Add a file record to the manifest.
    pub fn add_file(&mut self, record: FileRecord) {
        self.records.push(record);
    }

    /// Return the number of records.
    #[must_use]
    pub fn len(&self) -> usize {
        self.records.len()
    }

    /// Return `true` if the manifest has no records.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }

    /// Serialise the manifest to a JSON string.
    ///
    /// # Errors
    ///
    /// Returns a `serde_json::Error` if serialisation fails (which should never
    /// happen for this type).
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Deserialise a manifest from a JSON string.
    ///
    /// # Errors
    ///
    /// Returns a `serde_json::Error` if the JSON is malformed or the schema
    /// doesn't match.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for the [`NetworkDedupEngine`].
#[derive(Debug, Clone)]
pub struct NetworkDedupConfig {
    /// Maximum Hamming distance for perceptual hash matching.
    pub phash_max_distance: u32,
    /// Maximum difference in duration (seconds) for two files to be considered
    /// near-duplicate candidates during perceptual matching.
    pub duration_tolerance_s: f64,
    /// Minimum file size (bytes) to include a record in perceptual matching.
    /// Very small files are excluded to avoid spurious perceptual matches.
    pub min_file_size: u64,
}

impl Default for NetworkDedupConfig {
    fn default() -> Self {
        Self {
            phash_max_distance: 10,
            duration_tolerance_s: 5.0,
            min_file_size: 65_536, // 64 KiB
        }
    }
}

// ---------------------------------------------------------------------------
// DuplicateGroup
// ---------------------------------------------------------------------------

/// A group of cross-node duplicate files.
#[derive(Debug, Clone)]
pub struct CrossNodeGroup {
    /// The URIs of all files in this duplicate group.
    pub uris: Vec<String>,
    /// How the duplicates were detected.
    pub method: DuplicateMethod,
    /// Perceptual hash distance (0 for exact duplicates, None if method != Perceptual).
    pub phash_distance: Option<u32>,
}

/// Detection method for cross-node duplicates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DuplicateMethod {
    /// Identical BLAKE3 cryptographic digest.
    ExactHash,
    /// Perceptual hash Hamming distance within configured threshold.
    PerceptualHash,
}

// ---------------------------------------------------------------------------
// NetworkDedupEngine
// ---------------------------------------------------------------------------

/// Engine for detecting duplicates across distributed media nodes.
///
/// Add manifests from each remote node then call [`find_cross_node_duplicates`]
/// to get a list of [`CrossNodeGroup`]s.
#[derive(Debug)]
pub struct NetworkDedupEngine {
    config: NetworkDedupConfig,
    manifests: Vec<NodeManifest>,
}

impl NetworkDedupEngine {
    /// Create a new engine with the given configuration.
    #[must_use]
    pub fn new(config: NetworkDedupConfig) -> Self {
        Self {
            config,
            manifests: Vec::new(),
        }
    }

    /// Add a [`NodeManifest`] to the engine.
    pub fn add_manifest(&mut self, manifest: NodeManifest) {
        self.manifests.push(manifest);
    }

    /// Return the number of manifests registered.
    #[must_use]
    pub fn manifest_count(&self) -> usize {
        self.manifests.len()
    }

    /// Return the total number of file records across all manifests.
    #[must_use]
    pub fn total_records(&self) -> usize {
        self.manifests.iter().map(|m| m.records.len()).sum()
    }

    /// Find duplicate groups across all registered node manifests.
    ///
    /// The algorithm runs two passes:
    ///
    /// 1. **Exact pass** – groups records by Blake3 hex digest.  Only groups
    ///    that span at least two *different* nodes are returned.
    /// 2. **Perceptual pass** – for records with pHash and not already grouped
    ///    as exact duplicates, applies Hamming-distance comparison with the
    ///    configured threshold and duration guard.
    #[must_use]
    pub fn find_cross_node_duplicates(&self) -> Vec<CrossNodeGroup> {
        let mut groups = Vec::new();

        // Flatten all records with their node id attached.
        let all: Vec<(&str, &FileRecord)> = self
            .manifests
            .iter()
            .flat_map(|m| m.records.iter().map(move |r| (m.node_id.as_str(), r)))
            .collect();

        // --- Pass 1: exact hash ---
        let mut by_digest: HashMap<&str, Vec<(&str, &FileRecord)>> = HashMap::new();
        for &(node, rec) in &all {
            by_digest
                .entry(rec.blake3_hex.as_str())
                .or_default()
                .push((node, rec));
        }

        let mut exact_uris: std::collections::HashSet<String> =
            std::collections::HashSet::new();

        for (_digest, records) in &by_digest {
            if records.len() < 2 {
                continue;
            }
            // Check that at least two *different* nodes are represented.
            let nodes: std::collections::HashSet<&str> =
                records.iter().map(|(n, _)| *n).collect();
            if nodes.len() < 2 {
                continue;
            }
            let uris: Vec<String> = records.iter().map(|(_, r)| r.uri.clone()).collect();
            for u in &uris {
                exact_uris.insert(u.clone());
            }
            groups.push(CrossNodeGroup {
                uris,
                method: DuplicateMethod::ExactHash,
                phash_distance: Some(0),
            });
        }

        // --- Pass 2: perceptual hash ---
        let phash_candidates: Vec<(&str, &FileRecord)> = all
            .iter()
            .filter(|(_, r)| {
                r.phash.is_some()
                    && !exact_uris.contains(&r.uri)
                    && r.file_size
                        .map(|s| s >= self.config.min_file_size)
                        .unwrap_or(true)
            })
            .copied()
            .collect();

        let n = phash_candidates.len();
        let mut grouped = vec![false; n];

        for i in 0..n {
            if grouped[i] {
                continue;
            }
            let (node_i, rec_i) = phash_candidates[i];
            let mut grp_uris = vec![rec_i.uri.clone()];
            let mut min_dist = u32::MAX;

            for j in (i + 1)..n {
                if grouped[j] {
                    continue;
                }
                let (node_j, rec_j) = phash_candidates[j];
                // Only match across different nodes.
                if node_i == node_j {
                    continue;
                }
                // Duration guard.
                if let (Some(d1), Some(d2)) =
                    (rec_i.duration_s, rec_j.duration_s)
                {
                    if (d1 - d2).abs() > self.config.duration_tolerance_s {
                        continue;
                    }
                }
                if let Some(dist) = rec_i.phash_distance(rec_j) {
                    if dist <= self.config.phash_max_distance {
                        grp_uris.push(rec_j.uri.clone());
                        grouped[j] = true;
                        if dist < min_dist {
                            min_dist = dist;
                        }
                    }
                }
            }

            if grp_uris.len() >= 2 {
                grouped[i] = true;
                groups.push(CrossNodeGroup {
                    uris: grp_uris,
                    method: DuplicateMethod::PerceptualHash,
                    phash_distance: if min_dist == u32::MAX {
                        None
                    } else {
                        Some(min_dist)
                    },
                });
            }
        }

        groups
    }

    /// Return a summary of how many duplicates span how many nodes.
    #[must_use]
    pub fn cross_node_summary(&self) -> CrossNodeSummary {
        let groups = self.find_cross_node_duplicates();
        let exact_groups = groups
            .iter()
            .filter(|g| g.method == DuplicateMethod::ExactHash)
            .count();
        let perceptual_groups = groups
            .iter()
            .filter(|g| g.method == DuplicateMethod::PerceptualHash)
            .count();
        let total_duplicate_files: usize = groups.iter().map(|g| g.uris.len()).sum();
        CrossNodeSummary {
            total_groups: groups.len(),
            exact_groups,
            perceptual_groups,
            total_duplicate_files,
        }
    }
}

/// Summary of cross-node deduplication results.
#[derive(Debug, Clone)]
pub struct CrossNodeSummary {
    /// Total number of duplicate groups found.
    pub total_groups: usize,
    /// Groups detected via exact hash.
    pub exact_groups: usize,
    /// Groups detected via perceptual hash.
    pub perceptual_groups: usize,
    /// Total number of file URIs across all duplicate groups.
    pub total_duplicate_files: usize,
}

impl Default for NetworkDedupEngine {
    fn default() -> Self {
        Self::new(NetworkDedupConfig::default())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_record(uri: &str, digest: &str, phash: Option<u64>, dur: Option<f64>) -> FileRecord {
        FileRecord::new(
            uri.to_string(),
            digest.to_string(),
            phash,
            dur,
            Some(1_000_000),
        )
    }

    /// Build an engine with two nodes that share one exact duplicate.
    fn two_node_exact() -> NetworkDedupEngine {
        let mut engine = NetworkDedupEngine::new(NetworkDedupConfig::default());

        let digest = "a".repeat(64);
        let mut ma = NodeManifest::new("node-a".to_string());
        ma.add_file(make_record("node-a:/movie.mp4", &digest, None, Some(3600.0)));

        let mut mb = NodeManifest::new("node-b".to_string());
        mb.add_file(make_record(
            "node-b:/backup/movie.mp4",
            &digest,
            None,
            Some(3600.0),
        ));

        engine.add_manifest(ma);
        engine.add_manifest(mb);
        engine
    }

    #[test]
    fn test_exact_cross_node_duplicate() {
        let engine = two_node_exact();
        let groups = engine.find_cross_node_duplicates();
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].method, DuplicateMethod::ExactHash);
        assert_eq!(groups[0].uris.len(), 2);
    }

    #[test]
    fn test_no_duplicate_same_node() {
        // Same digest but same node — should NOT appear in cross-node groups.
        let mut engine = NetworkDedupEngine::new(NetworkDedupConfig::default());
        let digest = "b".repeat(64);
        let mut ma = NodeManifest::new("node-a".to_string());
        ma.add_file(make_record("node-a:/v1.mp4", &digest, None, None));
        ma.add_file(make_record("node-a:/v2.mp4", &digest, None, None));
        engine.add_manifest(ma);

        let groups = engine.find_cross_node_duplicates();
        assert!(groups.is_empty(), "same-node duplicates must be excluded");
    }

    #[test]
    fn test_perceptual_cross_node_match() {
        let mut engine = NetworkDedupEngine::new(NetworkDedupConfig::default());
        // pHash distance of 2 — well within default threshold of 10.
        let base: u64 = 0xFF00_FF00_FF00_FF00;
        let close: u64 = base ^ 0b11; // 2 bits different

        let mut ma = NodeManifest::new("node-a".to_string());
        ma.add_file(make_record(
            "node-a:/clip.mp4",
            &"c".repeat(64),
            Some(base),
            Some(60.0),
        ));
        let mut mb = NodeManifest::new("node-b".to_string());
        mb.add_file(make_record(
            "node-b:/clip_re.mp4",
            &"d".repeat(64),
            Some(close),
            Some(60.0),
        ));
        engine.add_manifest(ma);
        engine.add_manifest(mb);

        let groups = engine.find_cross_node_duplicates();
        let perceptual: Vec<_> = groups
            .iter()
            .filter(|g| g.method == DuplicateMethod::PerceptualHash)
            .collect();
        assert_eq!(perceptual.len(), 1);
        assert_eq!(perceptual[0].phash_distance, Some(2));
    }

    #[test]
    fn test_duration_guard_excludes_mismatch() {
        let mut engine = NetworkDedupEngine::new(NetworkDedupConfig::default());
        let base: u64 = 0xAAAA_AAAA_AAAA_AAAA;

        let mut ma = NodeManifest::new("node-a".to_string());
        ma.add_file(make_record(
            "node-a:/short.mp4",
            &"e".repeat(64),
            Some(base),
            Some(30.0),
        ));
        let mut mb = NodeManifest::new("node-b".to_string());
        mb.add_file(make_record(
            "node-b:/long.mp4",
            &"f".repeat(64),
            Some(base ^ 1), // distance 1, but durations differ by 60 s
            Some(90.0),
        ));
        engine.add_manifest(ma);
        engine.add_manifest(mb);

        let groups = engine.find_cross_node_duplicates();
        // Duration differs by 60 s > tolerance 5 s → no perceptual match.
        let perceptual: Vec<_> = groups
            .iter()
            .filter(|g| g.method == DuplicateMethod::PerceptualHash)
            .collect();
        assert!(perceptual.is_empty(), "duration guard should exclude this pair");
    }

    #[test]
    fn test_manifest_serialise_roundtrip() {
        let mut m = NodeManifest::new("node-z".to_string());
        m.add_file(FileRecord::new(
            "node-z:/test.mp4".to_string(),
            "0".repeat(64),
            Some(12345),
            Some(99.9),
            Some(1024),
        ));
        let json = m.to_json().expect("serialise should succeed");
        let m2 = NodeManifest::from_json(&json).expect("deserialise should succeed");
        assert_eq!(m2.node_id, "node-z");
        assert_eq!(m2.records.len(), 1);
        assert_eq!(m2.records[0].phash, Some(12345));
    }

    #[test]
    fn test_file_record_valid_digest() {
        let good = FileRecord::new(
            "n:/f.mp4".to_string(),
            "a1b2c3".repeat(10) + "a1b2c3",  // 66 chars — invalid
            None,
            None,
            None,
        );
        // 66 hex chars is not 64 → invalid.
        assert!(!good.has_valid_digest());

        let valid = FileRecord::new(
            "n:/f.mp4".to_string(),
            "0".repeat(64),
            None,
            None,
            None,
        );
        assert!(valid.has_valid_digest());
    }

    #[test]
    fn test_phash_distance_calculation() {
        let a = FileRecord::new("n:/a.mp4".to_string(), "0".repeat(64), Some(0xFF), None, None);
        let b = FileRecord::new("n:/b.mp4".to_string(), "0".repeat(64), Some(0xFE), None, None);
        // 0xFF ^ 0xFE = 0x01 → 1 bit
        assert_eq!(a.phash_distance(&b), Some(1));

        let no_hash = FileRecord::new("n:/c.mp4".to_string(), "0".repeat(64), None, None, None);
        assert_eq!(a.phash_distance(&no_hash), None);
    }

    #[test]
    fn test_cross_node_summary() {
        let engine = two_node_exact();
        let summary = engine.cross_node_summary();
        assert_eq!(summary.total_groups, 1);
        assert_eq!(summary.exact_groups, 1);
        assert_eq!(summary.perceptual_groups, 0);
        assert_eq!(summary.total_duplicate_files, 2);
    }

    #[test]
    fn test_empty_engine() {
        let engine = NetworkDedupEngine::new(NetworkDedupConfig::default());
        assert_eq!(engine.manifest_count(), 0);
        assert_eq!(engine.total_records(), 0);
        let groups = engine.find_cross_node_duplicates();
        assert!(groups.is_empty());
    }

    #[test]
    fn test_node_name_extraction() {
        let rec = FileRecord::new(
            "node-alpha:/path/to/file.mp4".to_string(),
            "0".repeat(64),
            None,
            None,
            None,
        );
        assert_eq!(rec.node_name(), Some("node-alpha"));
    }

    #[test]
    fn test_three_node_perceptual_cluster() {
        // Three nodes all have pHashes within distance 10 of each other.
        // The greedy algorithm may group different pairs; we only require that
        // at least two of them end up in a perceptual group.
        let base: u64 = 0x0F0F_0F0F_0F0F_0F0F;
        let mut engine = NetworkDedupEngine::new(NetworkDedupConfig::default());

        for (node, delta) in [("n1", 0u64), ("n2", 0b1), ("n3", 0b11)] {
            let mut m = NodeManifest::new(node.to_string());
            m.add_file(make_record(
                &format!("{node}:/clip.mp4"),
                // Use distinct digests so exact-hash pass doesn't fire.
                &format!("{:0>64}", node),
                Some(base ^ delta),
                Some(120.0),
            ));
            engine.add_manifest(m);
        }

        let groups = engine.find_cross_node_duplicates();
        // At least one perceptual group should exist — n1/n2 and n1/n3 are
        // all within distance 3 which is well below the default threshold of 10.
        // The greedy pass guarantees at least the first pair is found.
        let perceptual_total: usize = groups
            .iter()
            .filter(|g| g.method == DuplicateMethod::PerceptualHash)
            .map(|g| g.uris.len())
            .sum();
        assert!(
            perceptual_total >= 2,
            "expected at least 2 files in perceptual groups, got {perceptual_total}"
        );
    }
}