cpd-reporter 0.1.21

Output format reporters for cpd
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
// shared.rs — common reporter utilities to keep output formatting DRY.

use cpd_core::models::{CloneKind, CpdClone, Fragment, SimilarityMethod, StatRow, Statistics};
use std::collections::BTreeMap;
use std::collections::HashMap;

/// ANSI terminal styling that respects `--no-colors`.
pub struct Style {
    no_colors: bool,
}

impl Style {
    pub fn new(no_colors: bool) -> Self {
        Self { no_colors }
    }

    pub fn dim(&self, text: &str) -> String {
        if self.no_colors {
            text.to_string()
        } else {
            format!("\x1b[90m{}\x1b[39m", text)
        }
    }

    pub fn red(&self, text: &str) -> String {
        if self.no_colors {
            text.to_string()
        } else {
            format!("\x1b[31m{}\x1b[39m", text)
        }
    }

    /// `text` in the ANSI foreground colour `code` (32 green, 33 yellow, ...).
    pub fn paint(&self, text: &str, code: u8) -> String {
        if self.no_colors {
            text.to_string()
        } else {
            format!("\x1b[{code}m{text}\x1b[39m")
        }
    }

    pub fn bold(&self, text: &str) -> String {
        if self.no_colors {
            text.to_string()
        } else {
            format!("\x1b[1m{}\x1b[22m", text)
        }
    }

    pub fn bold_green(&self, text: &str) -> String {
        if self.no_colors {
            text.to_string()
        } else {
            format!("\x1b[1m\x1b[32m{}\x1b[39m\x1b[22m", text)
        }
    }

    pub fn green_prefix(&self, message: &str) -> String {
        if self.no_colors {
            message.to_string()
        } else {
            format!("\x1b[32m{}\x1b[39m", message)
        }
    }

    /// `text` as a solid badge chip: `code` becomes the background, via
    /// reverse video, so it reads as a filled pill rather than coloured text.
    pub fn chip(&self, text: &str, code: u8) -> String {
        if self.no_colors {
            text.to_string()
        } else {
            format!("\x1b[1m\x1b[{code}m\x1b[7m{text}\x1b[27m\x1b[39m\x1b[22m")
        }
    }
}

pub fn make_row(fmt: &str, row: &StatRow) -> [String; 7] {
    [
        fmt.to_string(),
        row.sources.to_string(),
        row.lines.to_string(),
        row.tokens.to_string(),
        row.clones.to_string(),
        format!("{} ({:.2}%)", row.duplicated_lines, row.percentage),
        format!("{} ({:.2}%)", row.duplicated_tokens, row.percentage_tokens),
    ]
}

/// Iterate over format statistics sorted by format name.
///
/// `callback` receives `(format_name, stat_row)` for each known format, followed
/// by `("Total:", total_row)`.
pub fn for_each_sorted_format<F>(stats: &Statistics, mut callback: F)
where
    F: FnMut(&str, &StatRow),
{
    let mut format_names: Vec<&str> = stats.formats.keys().map(|s| s.as_str()).collect();
    format_names.sort();
    for fmt in &format_names {
        if let Some(row) = stats.formats.get(*fmt) {
            callback(fmt, row);
        }
    }
    callback("Total:", &stats.total);
}

/// Prints the statistics table in the jscpd console style.
pub fn print_table(stats: &Statistics, style: &Style, box_chars: BoxChars) {
    let headers = [
        "Format",
        "Files analyzed",
        "Total lines",
        "Total tokens",
        "Clones found",
        "Duplicated lines",
        "Duplicated tokens",
    ];

    let sorted_formats: BTreeMap<&str, &StatRow> =
        stats.formats.iter().map(|(k, v)| (k.as_str(), v)).collect();

    let format_rows: Vec<[String; 7]> = sorted_formats
        .iter()
        .map(|(fmt, row)| make_row(fmt, row))
        .collect();

    let total_row = make_row("Total:", &stats.total);

    let mut widths = [0usize; 7];
    for (i, h) in headers.iter().enumerate() {
        widths[i] = h.len();
    }
    for row in format_rows.iter().chain(std::iter::once(&total_row)) {
        for (i, cell) in row.iter().enumerate() {
            widths[i] = widths[i].max(cell.len());
        }
    }

    print_sep(
        &widths,
        box_chars.top_left,
        box_chars.top_mid,
        box_chars.top_right,
        style,
    );
    print_row(
        &headers.map(|h| h.to_string()),
        &widths,
        true,
        style,
        box_chars.vsep,
    );
    for row in &format_rows {
        print_sep(
            &widths,
            box_chars.mid_left,
            box_chars.mid_mid,
            box_chars.mid_right,
            style,
        );
        print_row(row, &widths, false, style, box_chars.vsep);
    }
    print_sep(
        &widths,
        box_chars.mid_left,
        box_chars.mid_mid,
        box_chars.mid_right,
        style,
    );
    print_row(&total_row, &widths, false, style, box_chars.vsep);
    print_sep(
        &widths,
        box_chars.bot_left,
        box_chars.bot_mid,
        box_chars.bot_right,
        style,
    );
}

/// Set of box-drawing characters used by `print_table`.
pub struct BoxChars {
    pub top_left: char,
    pub top_mid: char,
    pub top_right: char,
    pub mid_left: char,
    pub mid_mid: char,
    pub mid_right: char,
    pub bot_left: char,
    pub bot_mid: char,
    pub bot_right: char,
    pub hsep: char,
    pub vsep: char,
}

impl BoxChars {
    /// ASCII-friendly box drawing characters.
    pub fn ascii() -> Self {
        Self {
            top_left: '┌',
            top_mid: '┬',
            top_right: '┐',
            mid_left: '├',
            mid_mid: '┼',
            mid_right: '┤',
            bot_left: '└',
            bot_mid: '┴',
            bot_right: '┘',
            hsep: '─',
            vsep: '│',
        }
    }

    /// Unicode box drawing characters.
    pub fn unicode() -> Self {
        Self {
            top_left: '\u{250c}',
            top_mid: '\u{252c}',
            top_right: '\u{2510}',
            mid_left: '\u{251c}',
            mid_mid: '\u{253c}',
            mid_right: '\u{2524}',
            bot_left: '\u{2514}',
            bot_mid: '\u{2534}',
            bot_right: '\u{2518}',
            hsep: '\u{2500}',
            vsep: '\u{2502}',
        }
    }
}

fn print_sep(widths: &[usize], left: char, mid: char, right: char, style: &Style) {
    let mut s = String::new();
    s.push(left);
    for (i, &w) in widths.iter().enumerate() {
        for _ in 0..(w + 2) {
            s.push('─');
        }
        if i < widths.len() - 1 {
            s.push(mid);
        }
    }
    s.push(right);
    println!("{}", style.dim(&s));
}

fn print_row(cells: &[String; 7], widths: &[usize], header: bool, style: &Style, vsep: char) {
    let sep = style.dim(&vsep.to_string());
    print!("{}", sep);
    for (i, cell) in cells.iter().enumerate() {
        if header {
            let text = style.red(&format!(" {:width$} ", cell, width = widths[i]));
            print!("{}{}", text, sep);
        } else if i == 0 && cell == "Total:" {
            let padded = format!("{:<width$}", cell, width = widths[i]);
            print!(" {}{} {}", style.bold(cell), &padded[cell.len()..], sep);
        } else {
            print!(" {:width$} {}", cell, sep, width = widths[i]);
        }
    }
    println!();
}

// Canonical implementations live in cpd-core so cpd-finder (blame) resolves
// paths identically; re-exported here to keep the reporter-facing API stable.
pub use cpd_core::paths::{clean_source_id, resolve_fragment_path};

/// Read the text lines from `content` between `[start_line, end_line]` (1-indexed, inclusive).
pub fn extract_lines(content: &str, start_line: u32, end_line: u32) -> String {
    content
        .lines()
        .skip(start_line.saturating_sub(1) as usize)
        .take(end_line.saturating_sub(start_line.saturating_sub(1)) as usize)
        .collect::<Vec<_>>()
        .join("\n")
}

/// Load file contents once per resolved path. Returns the cached entry when available.
pub fn read_file_cached<'a>(
    cache: &'a mut HashMap<String, String>,
    fragment: &Fragment,
) -> &'a str {
    let resolved = resolve_fragment_path(fragment);
    cache
        .entry(resolved.clone())
        .or_insert_with(|| std::fs::read_to_string(&resolved).unwrap_or_default())
        .as_str()
}

/// Read the source text for a fragment from disk, if available.
pub fn fragment_text(cache: &mut HashMap<String, String>, fragment: &Fragment) -> String {
    let content = read_file_cached(cache, fragment).to_string();
    extract_lines(&content, fragment.start.line, fragment.end.line)
}

/// Content hash identifying a clone pair: the 16-hex-char `snippet_pair_hash`
/// of both snippet texts, insensitive to fragment order. This is the same
/// identity used by SARIF `partialFingerprints` and the baseline file. None
/// when neither snippet can be read from disk.
pub fn clone_pair_hash(cache: &mut HashMap<String, String>, clone: &CpdClone) -> Option<String> {
    let snippet_a = fragment_text(cache, &clone.fragment_a);
    let snippet_b = fragment_text(cache, &clone.fragment_b);
    if snippet_a.is_empty() && snippet_b.is_empty() {
        None
    } else {
        Some(format!(
            "{:016x}",
            cpd_core::hash::snippet_pair_hash(&snippet_a, &snippet_b)
        ))
    }
}

/// Print a source snippet for a fragment, with optional color dimming.
pub fn print_snippet(fragment: &Fragment, style: &Style, max_display: usize) {
    let resolved = resolve_fragment_path(fragment);
    let content = match std::fs::read_to_string(&resolved) {
        Ok(c) => c,
        Err(_) => return,
    };

    let start = fragment.start.line.saturating_sub(1) as usize;
    let end = fragment.end.line as usize;

    let lines: Vec<&str> = content.lines().collect();
    let snippet_lines = lines.get(start..end.min(lines.len())).unwrap_or(&[]);

    let truncated = snippet_lines.len() > max_display;
    let display_lines = if truncated {
        &snippet_lines[..max_display]
    } else {
        snippet_lines
    };

    for (i, line) in display_lines.iter().enumerate() {
        let line_num = fragment.start.line as usize + i;
        let prefix = format!("{:>4} │ ", line_num);
        println!("{}{}", style.dim(&prefix), line);
    }
    if truncated {
        let remaining = snippet_lines.len() - max_display;
        println!("{}", style.dim(&format!("     … {} more lines", remaining)));
    }
}

/// Format a clone location summary line, e.g. `file [1:1 - 10:10]`.
pub fn format_location(
    source_id: &str,
    start_line: u32,
    start_col: u32,
    end_line: u32,
    end_col: u32,
) -> String {
    format!(
        "{} [{}:{} - {}:{}]",
        source_id,
        start_line,
        start_col + 1,
        end_line,
        end_col + 1
    )
}

/// Label for a clone header: the format, plus `, renamed` for Type-2 clones,
/// `, similar (gap) ~0.91` / `, similar (ast) ~0.75` for near-miss clones —
/// the method matters because the two scores are not comparable — and
/// `, semantic ~0.78` for Type-4 clones, whose score is a cosine similarity.
pub fn clone_label(
    format: &str,
    kind: CloneKind,
    similarity: Option<f32>,
    method: Option<SimilarityMethod>,
) -> String {
    match (kind, similarity) {
        (CloneKind::Exact, _) => format.to_string(),
        (CloneKind::Renamed, _) => format!("{format}, renamed"),
        (CloneKind::Similar, Some(s)) => match method {
            Some(m) => format!("{format}, similar ({}) ~{s:.2}", m.as_str()),
            None => format!("{format}, similar ~{s:.2}"),
        },
        (CloneKind::Similar, None) => format!("{format}, similar"),
        (CloneKind::Semantic, Some(s)) => format!("{format}, semantic ~{s:.2}"),
        (CloneKind::Semantic, None) => format!("{format}, semantic"),
    }
}

/// Print a clone header line in console style: `Clone found (format)`, with a
/// `, renamed` / `, similar ~0.91` suffix for non-exact clones and a ` [NEW]`
/// marker for clones absent from the baseline.
pub fn print_clone_header(style: &Style, clone: &CpdClone) {
    let header = style.bold(&format!(
        "Clone found ({})",
        clone_label(
            &clone.format,
            clone.kind,
            clone.similarity,
            clone.similarity_method
        )
    ));
    let is_new = clone.is_new;
    if is_new {
        println!("{} {}", header, style.red("[NEW]"));
    } else {
        println!("{}", header);
    }
}

/// Print the two fragment location lines for a clone (console-full style).
pub fn print_clone_locations(_style: &Style, clone: &CpdClone) {
    let fa = &clone.fragment_a;
    let fb = &clone.fragment_b;
    println!(
        " - {}",
        format_location(
            &fa.source_id,
            fa.start.line,
            fa.start.column,
            fa.end.line,
            fa.end.column
        )
    );
    println!(
        "   {}",
        format_location(
            &fb.source_id,
            fb.start.line,
            fb.start.column,
            fb.end.line,
            fb.end.column
        )
    );
}

/// Build the standard duplication summary sentence used by silent/markdown reporters.
pub fn summary_line(
    clones_len: usize,
    total: &cpd_core::models::StatRow,
    format_count: usize,
) -> String {
    format!(
        "Duplications detection: Found {} exact clones with {}({:.2}%) duplicated lines in {} ({} formats) files.",
        clones_len, total.duplicated_lines, total.percentage, total.sources, format_count,
    )
}

/// Print the trailing "Found N clones" summary line. When a baseline marked
/// clones as new, the count is qualified with "(M new)".
pub fn print_found_count(clones: &[CpdClone], new_clones: u64, style: &Style) {
    let suffix = if new_clones > 0 {
        format!(" ({} new)", new_clones)
    } else {
        String::new()
    };
    println!(
        "{}",
        style.dim(&format!("Found {} clones{}.", clones.len(), suffix))
    );
}

/// Print the report trailer shared by console-style reporters.
pub fn print_report_trailer(
    clones: &[CpdClone],
    stats: &Statistics,
    style: &Style,
    box_chars: BoxChars,
) {
    print_table(stats, style, box_chars);
    print_found_count(clones, stats.total.new_clones, style);
}

/// Print the "No duplicates found." message used by console-style reporters.
pub fn print_no_duplicates(style: &Style) {
    println!("{}", style.dim("No duplicates found."));
}

/// Shared skeleton for console-style reporters.
///
/// Prints the empty-clones message, invokes `render_clone` for each clone, then prints the
/// statistics table and found-count summary.
pub fn report_console_style<F>(
    clones: &[CpdClone],
    stats: &Statistics,
    style: &Style,
    box_chars: BoxChars,
    mut render_clone: F,
) where
    F: FnMut(&CpdClone),
{
    if clones.is_empty() {
        print_no_duplicates(style);
    } else {
        for clone in clones {
            render_clone(clone);
        }
    }
    print_report_trailer(clones, stats, style, box_chars);
}

/// Print a green "X report saved to PATH" message, respecting `--no-colors`.
pub fn print_saved_report(style: &Style, report_name: &str, path: &std::path::Path) {
    let msg = format!("{} report saved to {}", report_name, path.display());
    println!("{}", style.green_prefix(&msg));
}

/// Write serialized report content to `output_dir/<filename>` and print the saved message.
pub fn write_report_file<C: AsRef<[u8]>>(
    output_dir: &std::path::Path,
    filename: &str,
    content: C,
    style: &Style,
    report_name: &str,
) -> Result<std::path::PathBuf, std::io::Error> {
    std::fs::create_dir_all(output_dir)?;
    let path = output_dir.join(filename);
    std::fs::write(&path, content)?;
    print_saved_report(style, report_name, &path);
    Ok(path)
}

/// Print the opt-in blocks that follow a console-style report: the
/// `--summary` tables and the `--history` chart. Shared by every reporter that
/// writes to stdout so the two features render the same everywhere.
pub fn print_appendices(ctx: &crate::context::ReportContext, style: &Style) {
    if let Some(summary) = ctx.summary {
        crate::summary_render::print_summary(summary, style);
    }
    if let Some(history) = ctx.history {
        crate::history_render::print_history(history, style);
    }
}

#[cfg(test)]
pub mod fixtures {
    use crate::context::ReportContext;
    use cpd_core::models::{CpdClone, Fragment, Location, StatRow, Statistics};
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::time::Duration;

    /// Build a minimal Statistics total with the given duplication percentage and duplicated lines.
    pub fn stats_with_pct(pct: f64, lines: u64) -> Statistics {
        Statistics {
            total: StatRow {
                lines: 100,
                tokens: 500,
                sources: 5,
                clones: 2,
                duplicated_lines: lines,
                duplicated_tokens: 50,
                percentage: pct,
                percentage_tokens: pct,
                new_duplicated_lines: 0,
                new_clones: 0,
            },
            formats: HashMap::new(),
            detection_date: "2026-01-01T00:00:00Z".to_string(),
        }
    }

    /// Generate a test asserting that `Reporter::report` succeeds on an empty clone list.
    #[macro_export]
    macro_rules! assert_empty_report_ok {
        ($test_name:ident, $reporter:ty) => {
            #[test]
            fn $test_name() {
                let output_dir = std::path::PathBuf::from("/tmp");
                let opts = $crate::reporter::ReporterOptions::new(output_dir.clone());
                let reporter = <$reporter>::new(&opts);
                let ctx = $crate::shared::fixtures::empty_ctx();
                assert!(
                    $crate::reporter::Reporter::report(&reporter, &[], &ctx, &output_dir,).is_ok()
                );
            }
        };
    }

    /// Generate a test asserting that the reporter `name()` returns the expected value.
    #[macro_export]
    macro_rules! assert_reporter_name {
        ($test_name:ident, $reporter:ty, $expected:expr) => {
            #[test]
            fn $test_name() {
                let opts = $crate::reporter::ReporterOptions::new(std::path::PathBuf::from("/tmp"));
                let reporter = <$reporter>::new(&opts);
                assert_eq!($crate::reporter::Reporter::name(&reporter), $expected);
            }
        };
    }

    /// Run a file-writing reporter over `clones` with `stats` into a fresh
    /// temp dir and return the content of `file_name`. `configure` adjusts the
    /// options before `make` builds the reporter from them.
    pub fn report_to_file<R: crate::reporter::Reporter>(
        prefix: &str,
        file_name: &str,
        clones: &[CpdClone],
        stats: &Statistics,
        configure: impl FnOnce(&mut crate::reporter::ReporterOptions),
        make: impl FnOnce(&crate::reporter::ReporterOptions) -> R,
    ) -> String {
        report_to_file_timed(
            prefix,
            file_name,
            clones,
            stats,
            Duration::ZERO,
            configure,
            make,
        )
    }

    /// [`report_to_file`] with an explicit detection duration, for reporters
    /// that print the elapsed time.
    pub fn report_to_file_timed<R: crate::reporter::Reporter>(
        prefix: &str,
        file_name: &str,
        clones: &[CpdClone],
        stats: &Statistics,
        duration: Duration,
        configure: impl FnOnce(&mut crate::reporter::ReporterOptions),
        make: impl FnOnce(&crate::reporter::ReporterOptions) -> R,
    ) -> String {
        let dir = tmp_dir(prefix);
        let mut opts = crate::reporter::ReporterOptions::new(dir.clone());
        configure(&mut opts);
        let reporter = make(&opts);
        let ctx = ReportContext::new(stats, duration);
        reporter.report(clones, &ctx, &dir).unwrap();
        std::fs::read_to_string(dir.join(file_name)).unwrap()
    }

    /// Run a stdout reporter over an empty clone list with `stats` and return
    /// its result, for reporters that write nothing to disk.
    pub fn report_result<R: crate::reporter::Reporter>(
        stats: &Statistics,
        configure: impl FnOnce(&mut crate::reporter::ReporterOptions),
        make: impl FnOnce(&crate::reporter::ReporterOptions) -> R,
    ) -> Result<(), crate::reporter::ReporterError> {
        let dir = PathBuf::from("/tmp");
        let mut opts = crate::reporter::ReporterOptions::new(dir.clone());
        configure(&mut opts);
        let reporter = make(&opts);
        let ctx = ReportContext::new(stats, Duration::ZERO);
        reporter.report(&[], &ctx, &dir)
    }

    /// Leaked empty statistics for tests that need a `ReportContext<'static>`.
    pub fn empty_ctx() -> ReportContext<'static> {
        let stats = Box::leak(Box::new(empty_stats()));
        ReportContext::new(stats, Duration::ZERO)
    }

    pub fn tmp_dir(prefix: &str) -> PathBuf {
        // A process-wide counter guarantees uniqueness across parallel test
        // threads; timestamps alone can collide and make tests share a dir.
        static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let dir = std::env::temp_dir().join(format!(
            "cpd-{}-{}-{}",
            prefix,
            std::process::id(),
            NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        std::fs::create_dir_all(&dir).ok();
        dir
    }

    pub fn empty_stats() -> Statistics {
        Statistics {
            total: StatRow::default(),
            formats: HashMap::new(),
            detection_date: "2026-01-01T00:00:00Z".to_string(),
        }
    }

    pub fn one_clone_stats() -> Statistics {
        let mut formats = HashMap::new();
        formats.insert(
            "javascript".to_string(),
            StatRow {
                lines: 100,
                tokens: 500,
                sources: 5,
                clones: 1,
                duplicated_lines: 10,
                duplicated_tokens: 50,
                percentage: 10.0,
                percentage_tokens: 10.0,
                new_duplicated_lines: 0,
                new_clones: 0,
            },
        );
        Statistics {
            total: StatRow {
                lines: 100,
                tokens: 500,
                sources: 5,
                clones: 1,
                duplicated_lines: 10,
                duplicated_tokens: 50,
                percentage: 10.0,
                percentage_tokens: 10.0,
                new_duplicated_lines: 0,
                new_clones: 0,
            },
            formats,
            detection_date: "2026-01-01T00:00:00Z".to_string(),
        }
    }

    pub fn make_clone(source_a: &str, source_b: &str, token_count: u32) -> CpdClone {
        make_clone_with_lines(source_a, source_b, 1, 10, token_count)
    }

    pub fn make_clone_with_lines(
        source_a: &str,
        source_b: &str,
        start_line: u32,
        end_line: u32,
        token_count: u32,
    ) -> CpdClone {
        let start = Location {
            line: start_line,
            column: 0,
            offset: 0,
        };
        let end = Location {
            line: end_line,
            column: 0,
            offset: 100,
        };
        make_clone_with_locations(source_a, source_b, start.clone(), end.clone(), token_count)
    }

    pub fn make_clone_with_locations(
        source_a: &str,
        source_b: &str,
        start: Location,
        end: Location,
        token_count: u32,
    ) -> CpdClone {
        let frag_a = Fragment {
            source_id: source_a.to_string(),
            source_root: None,
            start: start.clone(),
            end: end.clone(),
            range: [0, 100],
            blame: None,
        };
        let frag_b = Fragment {
            source_id: source_b.to_string(),
            source_root: None,
            start,
            end,
            range: [0, 100],
            blame: None,
        };
        CpdClone {
            format: "javascript".to_string(),
            fragment_a: frag_a,
            fragment_b: frag_b,
            token_count,
            is_new: false,
            kind: Default::default(),
            similarity: None,
            similarity_method: None,
            unmatched_lines: [0, 0],
        }
    }
}

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

    #[test]
    fn clone_label_shows_kind_method_and_score() {
        assert_eq!(
            clone_label("javascript", CloneKind::Exact, None, None),
            "javascript"
        );
        assert_eq!(
            clone_label("javascript", CloneKind::Renamed, None, None),
            "javascript, renamed"
        );
        assert_eq!(
            clone_label(
                "javascript",
                CloneKind::Similar,
                Some(0.91),
                Some(SimilarityMethod::Gap)
            ),
            "javascript, similar (gap) ~0.91"
        );
        assert_eq!(
            clone_label(
                "typescript",
                CloneKind::Similar,
                Some(0.752),
                Some(SimilarityMethod::Ast)
            ),
            "typescript, similar (ast) ~0.75"
        );
        assert_eq!(
            clone_label("java", CloneKind::Similar, None, None),
            "java, similar"
        );
        assert_eq!(
            clone_label("rust", CloneKind::Semantic, Some(0.7849), None),
            "rust, semantic ~0.78"
        );
    }
}