panoptico 1.1.1

AI-powered code review CLI for Pull Requests using Claude
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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
// Author: Julian Bolivar
// Version: 1.1.0
// Date: 2026-02-17

//! Deterministic finding ID generation for stable deduplication.
//!
//! Provides [`Category`] classification, path normalization, and SHA256-based
//! ID generation for code review findings. IDs are stable across runs even
//! when AI-generated titles change, enabling reliable deduplication in CI pipelines.
//!
//! # Algorithm
//!
//! ```text
//! findingId = SHA256(normalize(file) + ":" + line + ":" + category)[:16]
//! ```
//!
//! The 16-character hex prefix provides 64 bits of entropy, sufficient to
//! avoid collisions within a single PR's findings.

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::backend::CodeReview;

/// Length of the hex-encoded finding ID prefix.
pub const FINDING_ID_HEX_LENGTH: usize = 16;

/// Controlled-vocabulary classification for code review findings.
///
/// Used as part of the deterministic [`generate_finding_id`] hash input,
/// ensuring stable IDs even when AI-generated titles vary between runs.
///
/// # Serialization
///
/// Serializes to/from kebab-case slugs (e.g., `buffer-overflow`, `null-deref`).
///
/// # Examples
///
/// ```ignore
/// use panoptico::finding_id::Category;
///
/// let cat: Category = serde_json::from_str("\"buffer-overflow\"").unwrap();
/// assert_eq!(cat, Category::BufferOverflow);
/// assert_eq!(serde_json::to_string(&cat).unwrap(), "\"buffer-overflow\"");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Category {
    /// Out-of-bounds read/write.
    BufferOverflow,
    /// Null/dangling pointer dereference.
    NullDeref,
    /// Unclosed file, socket, memory, handle.
    ResourceLeak,
    /// Missing input validation or sanitization.
    UnvalidatedInput,
    /// Data race or TOCTOU.
    RaceCondition,
    /// Missing or incorrect error handling.
    ErrorHandling,
    /// Credentials, keys, tokens in source.
    HardcodedSecret,
    /// Arithmetic overflow/underflow.
    IntegerOverflow,
    /// Command, SQL, or format-string injection.
    Injection,
    /// Incorrect control flow or business logic.
    LogicError,
    /// Type confusion or incorrect cast.
    TypeMismatch,
    /// Use of deprecated or removed API.
    DeprecatedApi,
    /// Unnecessary allocation, O(n^2) in hot path, etc.
    Performance,
    /// Naming, formatting, convention violations.
    Style,
    /// Missing, incorrect, or incomplete documentation.
    Documentation,
    /// Fallback for unclassified findings.
    ///
    /// `#[serde(other)]` causes any unrecognized slug to deserialize
    /// as `Other` instead of failing. This is essential because LLMs
    /// may invent category names not in the enum (e.g., `"memory-leak"`).
    #[serde(other)]
    Other,
}

impl Category {
    /// Return the kebab-case slug for this category variant.
    ///
    /// Matches the serde `rename_all = "kebab-case"` serialization,
    /// but avoids the round-trip through `serde_json`.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use panoptico::finding_id::Category;
    ///
    /// assert_eq!(Category::BufferOverflow.slug(), "buffer-overflow");
    /// assert_eq!(Category::Other.slug(), "other");
    /// ```
    pub fn slug(&self) -> &'static str {
        match self {
            Category::BufferOverflow => "buffer-overflow",
            Category::NullDeref => "null-deref",
            Category::ResourceLeak => "resource-leak",
            Category::UnvalidatedInput => "unvalidated-input",
            Category::RaceCondition => "race-condition",
            Category::ErrorHandling => "error-handling",
            Category::HardcodedSecret => "hardcoded-secret",
            Category::IntegerOverflow => "integer-overflow",
            Category::Injection => "injection",
            Category::LogicError => "logic-error",
            Category::TypeMismatch => "type-mismatch",
            Category::DeprecatedApi => "deprecated-api",
            Category::Performance => "performance",
            Category::Style => "style",
            Category::Documentation => "documentation",
            Category::Other => "other",
        }
    }
}

/// All category slugs in canonical order.
///
/// Single source of truth for the complete list of kebab-case
/// category identifiers. Used by `review_tool()` in `backend/mod.rs`
/// and `ClaudeCodeBackend::build_prompt()` in `backend/claude_code.rs`
/// to avoid duplicating the slug list.
pub const CATEGORY_SLUGS: &[&str] = &[
    "buffer-overflow",
    "null-deref",
    "resource-leak",
    "unvalidated-input",
    "race-condition",
    "error-handling",
    "hardcoded-secret",
    "integer-overflow",
    "injection",
    "logic-error",
    "type-mismatch",
    "deprecated-api",
    "performance",
    "style",
    "documentation",
    "other",
];

impl Default for Category {
    /// Returns [`Category::Other`] as the default.
    fn default() -> Self {
        Category::Other
    }
}

/// Normalize a file path for deterministic hashing.
///
/// Converts backslashes to forward slashes, strips leading `./`,
/// and collapses consecutive slashes.
///
/// # Arguments
///
/// * `path` - The file path to normalize.
///
/// # Returns
///
/// A normalized path string suitable for hash input.
///
/// # Examples
///
/// ```ignore
/// use panoptico::finding_id::normalize_path;
///
/// assert_eq!(normalize_path("src\\main.rs"), "src/main.rs");
/// assert_eq!(normalize_path("./src/lib.rs"), "src/lib.rs");
/// ```
pub fn normalize_path(path: &str) -> String {
    let mut result = String::with_capacity(path.len());
    let mut prev_slash = false;
    let mut chars = path.chars().peekable();

    // Strip leading "./" or ".\" (after implicit backslash→slash conversion).
    if chars.peek() == Some(&'.') {
        let mut probe = chars.clone();
        probe.next(); // consume '.'
        match probe.peek() {
            Some('/' | '\\') => {
                chars.next(); // skip '.'
                chars.next(); // skip separator
            }
            None => return String::new(), // bare "."
            _ => {}
        }
    }

    for ch in chars {
        let ch = if ch == '\\' { '/' } else { ch };
        if ch == '/' {
            if !prev_slash || result.is_empty() {
                result.push('/');
            }
            prev_slash = true;
        } else {
            result.push(ch);
            prev_slash = false;
        }
    }
    result
}

/// Generate a deterministic finding ID from file, line, and category.
///
/// Computes `SHA256(normalize(file) + ":" + line + ":" + category)` and
/// returns the first [`FINDING_ID_HEX_LENGTH`] hex characters.
///
/// # Arguments
///
/// * `file` - Source file path (will be normalized).
/// * `line` - Line number (use 0 for file-level findings).
/// * `category` - The finding category.
///
/// # Returns
///
/// A lowercase hex string of length [`FINDING_ID_HEX_LENGTH`].
pub fn generate_finding_id(file: &str, line: u32, category: &Category) -> String {
    let normalized = normalize_path(file);
    let input = format!("{}:{}:{}", normalized, line, category.slug());
    let hash = Sha256::digest(input.as_bytes());
    hex_encode(&hash[..FINDING_ID_HEX_LENGTH / 2])
}

/// Encode bytes as lowercase hexadecimal.
fn hex_encode(bytes: &[u8]) -> String {
    use std::fmt::Write;
    let mut s = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        let _ = write!(s, "{:02x}", b);
    }
    s
}

/// Assign deterministic IDs to all findings in a code review.
///
/// Iterates over each finding and sets its `finding_id` field using
/// [`generate_finding_id`] with the finding's file, line, and category.
///
/// # Arguments
///
/// * `review` - The code review whose findings will receive IDs.
pub fn assign_finding_ids(review: &mut CodeReview) {
    for finding in &mut review.findings {
        finding.finding_id = generate_finding_id(&finding.file, finding.line, &finding.category);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::{CodeReview, Finding, Severity};

    // ── A. Category serde (29 tests — all PASS) ──────────────────

    #[test]
    fn category_serialize_buffer_overflow() {
        assert_eq!(
            serde_json::to_string(&Category::BufferOverflow).unwrap(),
            "\"buffer-overflow\""
        );
    }

    #[test]
    fn category_serialize_null_deref() {
        assert_eq!(
            serde_json::to_string(&Category::NullDeref).unwrap(),
            "\"null-deref\""
        );
    }

    #[test]
    fn category_serialize_resource_leak() {
        assert_eq!(
            serde_json::to_string(&Category::ResourceLeak).unwrap(),
            "\"resource-leak\""
        );
    }

    #[test]
    fn category_serialize_unvalidated_input() {
        assert_eq!(
            serde_json::to_string(&Category::UnvalidatedInput).unwrap(),
            "\"unvalidated-input\""
        );
    }

    #[test]
    fn category_serialize_race_condition() {
        assert_eq!(
            serde_json::to_string(&Category::RaceCondition).unwrap(),
            "\"race-condition\""
        );
    }

    #[test]
    fn category_serialize_error_handling() {
        assert_eq!(
            serde_json::to_string(&Category::ErrorHandling).unwrap(),
            "\"error-handling\""
        );
    }

    #[test]
    fn category_serialize_hardcoded_secret() {
        assert_eq!(
            serde_json::to_string(&Category::HardcodedSecret).unwrap(),
            "\"hardcoded-secret\""
        );
    }

    #[test]
    fn category_serialize_integer_overflow() {
        assert_eq!(
            serde_json::to_string(&Category::IntegerOverflow).unwrap(),
            "\"integer-overflow\""
        );
    }

    #[test]
    fn category_serialize_injection() {
        assert_eq!(
            serde_json::to_string(&Category::Injection).unwrap(),
            "\"injection\""
        );
    }

    #[test]
    fn category_serialize_logic_error() {
        assert_eq!(
            serde_json::to_string(&Category::LogicError).unwrap(),
            "\"logic-error\""
        );
    }

    #[test]
    fn category_serialize_type_mismatch() {
        assert_eq!(
            serde_json::to_string(&Category::TypeMismatch).unwrap(),
            "\"type-mismatch\""
        );
    }

    #[test]
    fn category_serialize_deprecated_api() {
        assert_eq!(
            serde_json::to_string(&Category::DeprecatedApi).unwrap(),
            "\"deprecated-api\""
        );
    }

    #[test]
    fn category_serialize_performance() {
        assert_eq!(
            serde_json::to_string(&Category::Performance).unwrap(),
            "\"performance\""
        );
    }

    #[test]
    fn category_serialize_style() {
        assert_eq!(
            serde_json::to_string(&Category::Style).unwrap(),
            "\"style\""
        );
    }

    #[test]
    fn category_serialize_documentation() {
        assert_eq!(
            serde_json::to_string(&Category::Documentation).unwrap(),
            "\"documentation\""
        );
    }

    #[test]
    fn category_serialize_other() {
        assert_eq!(
            serde_json::to_string(&Category::Other).unwrap(),
            "\"other\""
        );
    }

    #[test]
    fn category_deserialize_buffer_overflow() {
        let cat: Category = serde_json::from_str("\"buffer-overflow\"").unwrap();
        assert_eq!(cat, Category::BufferOverflow);
    }

    #[test]
    fn category_deserialize_null_deref() {
        let cat: Category = serde_json::from_str("\"null-deref\"").unwrap();
        assert_eq!(cat, Category::NullDeref);
    }

    #[test]
    fn category_deserialize_error_handling() {
        let cat: Category = serde_json::from_str("\"error-handling\"").unwrap();
        assert_eq!(cat, Category::ErrorHandling);
    }

    #[test]
    fn category_deserialize_documentation() {
        let cat: Category = serde_json::from_str("\"documentation\"").unwrap();
        assert_eq!(cat, Category::Documentation);
    }

    #[test]
    fn category_deserialize_other() {
        let cat: Category = serde_json::from_str("\"other\"").unwrap();
        assert_eq!(cat, Category::Other);
    }

    #[test]
    fn category_unknown_slug_falls_back_to_other() {
        let cat: Category = serde_json::from_str("\"unknown-category\"").unwrap();
        assert_eq!(
            cat,
            Category::Other,
            "Unknown slug should fall back to Other"
        );
    }

    #[test]
    fn category_empty_string_falls_back_to_other() {
        let cat: Category = serde_json::from_str("\"\"").unwrap();
        assert_eq!(
            cat,
            Category::Other,
            "Empty string should fall back to Other"
        );
    }

    #[test]
    fn category_rejects_numeric() {
        let result = serde_json::from_str::<Category>("42");
        assert!(result.is_err(), "Numeric value should fail deserialization");
    }

    #[test]
    fn category_clone() {
        let cat = Category::BufferOverflow;
        let cloned = cat;
        assert_eq!(cat, cloned);
    }

    #[test]
    fn category_debug() {
        let debug = format!("{:?}", Category::NullDeref);
        assert!(
            debug.contains("NullDeref"),
            "Debug should contain variant name"
        );
    }

    #[test]
    fn category_equality() {
        assert_eq!(Category::Style, Category::Style);
    }

    #[test]
    fn category_inequality() {
        assert_ne!(Category::Style, Category::Performance);
    }

    #[test]
    fn category_default_is_other() {
        assert_eq!(Category::default(), Category::Other);
    }

    #[test]
    fn category_roundtrip_all_variants() {
        let variants = [
            Category::BufferOverflow,
            Category::NullDeref,
            Category::ResourceLeak,
            Category::UnvalidatedInput,
            Category::RaceCondition,
            Category::ErrorHandling,
            Category::HardcodedSecret,
            Category::IntegerOverflow,
            Category::Injection,
            Category::LogicError,
            Category::TypeMismatch,
            Category::DeprecatedApi,
            Category::Performance,
            Category::Style,
            Category::Documentation,
            Category::Other,
        ];
        for variant in &variants {
            let json = serde_json::to_string(variant).unwrap();
            let deserialized: Category = serde_json::from_str(&json).unwrap();
            assert_eq!(variant, &deserialized, "Roundtrip failed for {:?}", variant);
        }
    }

    #[test]
    fn category_slug_matches_serde_for_all_variants() {
        let variants = [
            Category::BufferOverflow,
            Category::NullDeref,
            Category::ResourceLeak,
            Category::UnvalidatedInput,
            Category::RaceCondition,
            Category::ErrorHandling,
            Category::HardcodedSecret,
            Category::IntegerOverflow,
            Category::Injection,
            Category::LogicError,
            Category::TypeMismatch,
            Category::DeprecatedApi,
            Category::Performance,
            Category::Style,
            Category::Documentation,
            Category::Other,
        ];
        for variant in &variants {
            let serde_slug = serde_json::to_string(variant).unwrap();
            let serde_slug = serde_slug.trim_matches('"');
            assert_eq!(
                variant.slug(),
                serde_slug,
                "slug() mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn category_slugs_constant_matches_all_variants() {
        let variants = [
            Category::BufferOverflow,
            Category::NullDeref,
            Category::ResourceLeak,
            Category::UnvalidatedInput,
            Category::RaceCondition,
            Category::ErrorHandling,
            Category::HardcodedSecret,
            Category::IntegerOverflow,
            Category::Injection,
            Category::LogicError,
            Category::TypeMismatch,
            Category::DeprecatedApi,
            Category::Performance,
            Category::Style,
            Category::Documentation,
            Category::Other,
        ];
        assert_eq!(
            CATEGORY_SLUGS.len(),
            variants.len(),
            "CATEGORY_SLUGS length must match variant count"
        );
        for (i, variant) in variants.iter().enumerate() {
            assert_eq!(
                CATEGORY_SLUGS[i],
                variant.slug(),
                "CATEGORY_SLUGS[{}] mismatch for {:?}",
                i,
                variant
            );
        }
    }

    #[test]
    fn category_variant_count_is_sixteen() {
        let variants = [
            Category::BufferOverflow,
            Category::NullDeref,
            Category::ResourceLeak,
            Category::UnvalidatedInput,
            Category::RaceCondition,
            Category::ErrorHandling,
            Category::HardcodedSecret,
            Category::IntegerOverflow,
            Category::Injection,
            Category::LogicError,
            Category::TypeMismatch,
            Category::DeprecatedApi,
            Category::Performance,
            Category::Style,
            Category::Documentation,
            Category::Other,
        ];
        assert_eq!(
            variants.len(),
            16,
            "Category should have exactly 16 variants"
        );
    }

    // ── B. Path normalization (15 tests) ─────────────────────────

    #[test]
    fn normalize_converts_backslashes_to_forward_slashes() {
        assert_eq!(normalize_path("src\\main.rs"), "src/main.rs");
    }

    #[test]
    fn normalize_converts_nested_backslashes() {
        assert_eq!(normalize_path("src\\backend\\mod.rs"), "src/backend/mod.rs");
    }

    #[test]
    fn normalize_strips_leading_dot_slash() {
        assert_eq!(normalize_path("./src/main.rs"), "src/main.rs");
    }

    #[test]
    fn normalize_strips_leading_dot_backslash() {
        assert_eq!(normalize_path(".\\src\\main.rs"), "src/main.rs");
    }

    #[test]
    fn normalize_preserves_already_normalized_path() {
        assert_eq!(normalize_path("src/main.rs"), "src/main.rs");
    }

    #[test]
    fn normalize_empty_path_returns_empty() {
        assert_eq!(normalize_path(""), "");
    }

    #[test]
    fn normalize_preserves_leading_slash() {
        assert_eq!(normalize_path("/usr/local/bin"), "/usr/local/bin");
    }

    #[test]
    fn normalize_collapses_double_slashes() {
        assert_eq!(normalize_path("src//main.rs"), "src/main.rs");
    }

    #[test]
    fn normalize_collapses_multiple_slashes() {
        assert_eq!(
            normalize_path("src///backend///mod.rs"),
            "src/backend/mod.rs"
        );
    }

    #[test]
    fn normalize_handles_trailing_slash() {
        assert_eq!(normalize_path("src/backend/"), "src/backend/");
    }

    #[test]
    fn normalize_preserves_spaces_in_path() {
        assert_eq!(
            normalize_path("my project/src/main.rs"),
            "my project/src/main.rs"
        );
    }

    #[test]
    fn normalize_handles_unicode_path() {
        assert_eq!(
            normalize_path("src/módulo/archivo.rs"),
            "src/módulo/archivo.rs"
        );
    }

    #[test]
    fn normalize_windows_drive_path() {
        assert_eq!(
            normalize_path("C:\\Users\\dev\\main.rs"),
            "C:/Users/dev/main.rs"
        );
    }

    #[test]
    fn normalize_dot_only_returns_empty() {
        assert_eq!(normalize_path("."), "");
    }

    #[test]
    fn normalize_mixed_separators() {
        assert_eq!(normalize_path("src\\backend/mod.rs"), "src/backend/mod.rs");
    }

    // ── C. Finding ID generation (15 tests) ──────────────────────

    #[test]
    fn finding_id_is_deterministic() {
        let id1 = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        let id2 = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        assert_eq!(id1, id2, "Same inputs must produce same ID");
    }

    #[test]
    fn finding_id_has_sixteen_hex_chars() {
        let id = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        assert_eq!(
            id.len(),
            FINDING_ID_HEX_LENGTH,
            "ID must be {} chars",
            FINDING_ID_HEX_LENGTH
        );
    }

    #[test]
    fn finding_id_contains_only_hex_chars() {
        let id = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        assert!(
            id.chars().all(|c| c.is_ascii_hexdigit()),
            "ID '{}' must contain only hex characters",
            id
        );
    }

    #[test]
    fn finding_id_is_lowercase() {
        let id = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        assert_eq!(id, id.to_lowercase(), "ID must be lowercase hex");
    }

    #[test]
    fn finding_id_differs_for_different_files() {
        let id1 = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        let id2 = generate_finding_id("src/lib.rs", 42, &Category::BufferOverflow);
        assert_ne!(id1, id2, "Different files must produce different IDs");
    }

    #[test]
    fn finding_id_differs_for_different_lines() {
        let id1 = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        let id2 = generate_finding_id("src/main.rs", 100, &Category::BufferOverflow);
        assert_ne!(id1, id2, "Different lines must produce different IDs");
    }

    #[test]
    fn finding_id_differs_for_different_categories() {
        let id1 = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        let id2 = generate_finding_id("src/main.rs", 42, &Category::NullDeref);
        assert_ne!(id1, id2, "Different categories must produce different IDs");
    }

    #[test]
    fn finding_id_line_zero_for_no_line_findings() {
        let id = generate_finding_id("src/main.rs", 0, &Category::Style);
        assert_eq!(
            id.len(),
            FINDING_ID_HEX_LENGTH,
            "Line 0 should produce valid ID"
        );
    }

    #[test]
    fn finding_id_normalizes_backslash_input() {
        let id_fwd = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        let id_back = generate_finding_id("src\\main.rs", 42, &Category::BufferOverflow);
        assert_eq!(
            id_fwd, id_back,
            "Backslash path should normalize to same ID"
        );
    }

    #[test]
    fn finding_id_normalizes_dot_prefix() {
        let id_clean = generate_finding_id("src/main.rs", 42, &Category::BufferOverflow);
        let id_dot = generate_finding_id("./src/main.rs", 42, &Category::BufferOverflow);
        assert_eq!(
            id_clean, id_dot,
            "Dot-prefix path should normalize to same ID"
        );
    }

    #[test]
    fn finding_id_empty_file_path() {
        let id = generate_finding_id("", 0, &Category::Other);
        assert_eq!(
            id.len(),
            FINDING_ID_HEX_LENGTH,
            "Empty file path should produce valid ID"
        );
    }

    #[test]
    fn finding_id_very_long_file_path() {
        let long_path = "a/".repeat(500) + "main.rs";
        let id = generate_finding_id(&long_path, 1, &Category::Style);
        assert_eq!(
            id.len(),
            FINDING_ID_HEX_LENGTH,
            "Long path should produce valid ID"
        );
    }

    #[test]
    fn finding_id_special_chars_in_path() {
        let id = generate_finding_id("src/my file (2).rs", 10, &Category::LogicError);
        assert_eq!(
            id.len(),
            FINDING_ID_HEX_LENGTH,
            "Special chars should produce valid ID"
        );
    }

    #[test]
    fn finding_id_max_line_number() {
        let id = generate_finding_id("src/main.rs", u32::MAX, &Category::Style);
        assert_eq!(
            id.len(),
            FINDING_ID_HEX_LENGTH,
            "Max line number should produce valid ID"
        );
    }

    #[test]
    fn finding_id_all_categories_produce_unique_ids_same_file_line() {
        let variants = [
            Category::BufferOverflow,
            Category::NullDeref,
            Category::ResourceLeak,
            Category::UnvalidatedInput,
            Category::RaceCondition,
            Category::ErrorHandling,
            Category::HardcodedSecret,
            Category::IntegerOverflow,
            Category::Injection,
            Category::LogicError,
            Category::TypeMismatch,
            Category::DeprecatedApi,
            Category::Performance,
            Category::Style,
            Category::Other,
        ];
        let ids: Vec<String> = variants
            .iter()
            .map(|cat| generate_finding_id("src/main.rs", 42, cat))
            .collect();
        let unique: std::collections::HashSet<&String> = ids.iter().collect();
        assert_eq!(
            ids.len(),
            unique.len(),
            "All categories should produce unique IDs for same file:line"
        );
    }

    // ── D. assign_finding_ids (8 tests) ──────────────────────────

    fn make_finding(file: &str, line: u32, category: Category) -> Finding {
        let mut f = crate::backend::mock::make_test_finding(file, line, "Test finding");
        f.category = category;
        f
    }

    #[test]
    fn assign_ids_empty_findings_unchanged() {
        let mut review = CodeReview {
            summary: "No findings".to_string(),
            findings: vec![],
        };
        assign_finding_ids(&mut review);
        assert!(review.findings.is_empty());
    }

    #[test]
    fn assign_ids_single_finding_gets_id() {
        let mut review = CodeReview {
            summary: "One finding".to_string(),
            findings: vec![make_finding("src/main.rs", 42, Category::BufferOverflow)],
        };
        assign_finding_ids(&mut review);
        assert!(
            !review.findings[0].finding_id.is_empty(),
            "Finding should receive an ID"
        );
        assert_eq!(review.findings[0].finding_id.len(), FINDING_ID_HEX_LENGTH);
    }

    #[test]
    fn assign_ids_multiple_findings_get_unique_ids() {
        let mut review = CodeReview {
            summary: "Multiple findings".to_string(),
            findings: vec![
                make_finding("src/main.rs", 42, Category::BufferOverflow),
                make_finding("src/main.rs", 100, Category::NullDeref),
                make_finding("src/lib.rs", 10, Category::Style),
            ],
        };
        assign_finding_ids(&mut review);
        let ids: Vec<&str> = review
            .findings
            .iter()
            .map(|f| f.finding_id.as_str())
            .collect();
        let unique: std::collections::HashSet<&str> = ids.iter().copied().collect();
        assert_eq!(ids.len(), unique.len(), "All finding IDs should be unique");
    }

    #[test]
    fn assign_ids_deterministic_across_calls() {
        let make_review = || CodeReview {
            summary: "Deterministic test".to_string(),
            findings: vec![make_finding("src/main.rs", 42, Category::BufferOverflow)],
        };
        let mut review1 = make_review();
        let mut review2 = make_review();
        assign_finding_ids(&mut review1);
        assign_finding_ids(&mut review2);
        assert_eq!(
            review1.findings[0].finding_id, review2.findings[0].finding_id,
            "Same input must produce same ID across calls"
        );
    }

    #[test]
    fn assign_ids_different_category_same_file_line_different_id() {
        let mut review = CodeReview {
            summary: "Category test".to_string(),
            findings: vec![
                make_finding("src/main.rs", 42, Category::BufferOverflow),
                make_finding("src/main.rs", 42, Category::NullDeref),
            ],
        };
        assign_finding_ids(&mut review);
        assert_ne!(
            review.findings[0].finding_id, review.findings[1].finding_id,
            "Different categories at same file:line should produce different IDs"
        );
    }

    #[test]
    fn assign_ids_preserves_summary() {
        let mut review = CodeReview {
            summary: "Important summary".to_string(),
            findings: vec![make_finding("src/main.rs", 1, Category::Style)],
        };
        assign_finding_ids(&mut review);
        assert_eq!(
            review.summary, "Important summary",
            "Summary must not be modified"
        );
    }

    #[test]
    fn assign_ids_preserves_other_finding_fields() {
        let mut review = CodeReview {
            summary: "test".to_string(),
            findings: vec![Finding {
                severity: Severity::Critical,
                file: "src/main.rs".to_string(),
                line: 42,
                title: "Original title".to_string(),
                description: "Original desc".to_string(),
                suggestion: "Original fix".to_string(),
                category: Category::BufferOverflow,
                finding_id: String::new(),
                reasoning: String::new(),
            }],
        };
        assign_finding_ids(&mut review);
        let f = &review.findings[0];
        assert_eq!(f.severity, Severity::Critical);
        assert_eq!(f.file, "src/main.rs");
        assert_eq!(f.line, 42);
        assert_eq!(f.title, "Original title");
        assert_eq!(f.description, "Original desc");
        assert_eq!(f.suggestion, "Original fix");
        assert_eq!(f.category, Category::BufferOverflow);
    }

    #[test]
    fn assign_ids_independent_of_title() {
        let mut review1 = CodeReview {
            summary: "test".to_string(),
            findings: vec![Finding {
                severity: Severity::Warning,
                file: "src/main.rs".to_string(),
                line: 42,
                title: "Buffer overflow in parse_input".to_string(),
                description: "desc".to_string(),
                suggestion: "fix".to_string(),
                category: Category::BufferOverflow,
                finding_id: String::new(),
                reasoning: String::new(),
            }],
        };
        let mut review2 = CodeReview {
            summary: "test".to_string(),
            findings: vec![Finding {
                severity: Severity::Warning,
                file: "src/main.rs".to_string(),
                line: 42,
                title: "Possible buffer overrun detected".to_string(),
                description: "different description".to_string(),
                suggestion: "different suggestion".to_string(),
                category: Category::BufferOverflow,
                finding_id: String::new(),
                reasoning: String::new(),
            }],
        };
        assign_finding_ids(&mut review1);
        assign_finding_ids(&mut review2);
        assert_eq!(
            review1.findings[0].finding_id, review2.findings[0].finding_id,
            "Findings with different titles but same file:line:category must produce the same ID"
        );
    }

    #[test]
    fn assign_ids_overwrites_preexisting_ids() {
        let mut review = CodeReview {
            summary: "test".to_string(),
            findings: vec![Finding {
                severity: Severity::Warning,
                file: "src/main.rs".to_string(),
                line: 42,
                title: "Finding".to_string(),
                description: "Desc".to_string(),
                suggestion: "Fix".to_string(),
                category: Category::BufferOverflow,
                finding_id: "old-id-value".to_string(),
                reasoning: String::new(),
            }],
        };
        assign_finding_ids(&mut review);
        assert_ne!(
            review.findings[0].finding_id, "old-id-value",
            "Pre-existing ID should be overwritten"
        );
        assert_eq!(review.findings[0].finding_id.len(), FINDING_ID_HEX_LENGTH);
    }

    // --- Phase 1 (G2): Chain-of-thought reasoning tests ---

    #[test]
    fn finding_id_excludes_reasoning() {
        let mut review1 = CodeReview {
            summary: "test".to_string(),
            findings: vec![Finding {
                severity: Severity::Warning,
                file: "src/main.rs".to_string(),
                line: 42,
                title: "Same title".to_string(),
                description: "Same desc".to_string(),
                suggestion: "Same fix".to_string(),
                category: Category::LogicError,
                finding_id: String::new(),
                reasoning: "Short reasoning".to_string(),
            }],
        };
        let mut review2 = CodeReview {
            summary: "test".to_string(),
            findings: vec![Finding {
                severity: Severity::Warning,
                file: "src/main.rs".to_string(),
                line: 42,
                title: "Same title".to_string(),
                description: "Same desc".to_string(),
                suggestion: "Same fix".to_string(),
                category: Category::LogicError,
                finding_id: String::new(),
                reasoning: "Completely different and much longer reasoning text".to_string(),
            }],
        };
        assign_finding_ids(&mut review1);
        assign_finding_ids(&mut review2);
        assert_eq!(
            review1.findings[0].finding_id, review2.findings[0].finding_id,
            "Findings with different reasoning but same file:line:category must produce the same ID"
        );
    }
}