le-change 0.3.0

Ultra-fast Git change detection library with zero-cost abstractions
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
//! Computed output categories from ProcessedResult

use crate::interner::StringInterner;
use crate::types::{
    ChangeType, GroupDeployAction, GroupDeployDecision, GroupDeployReason, GroupResult,
    InternedString, ProcessedResult, RebuildReasonKind, VanishedFile,
};
use std::collections::{HashMap, HashSet};

/// All derived output categories computed in a single pass
pub struct ComputedOutputs {
    /// Filtered added file indices
    pub filtered_added: Vec<u32>,
    /// Filtered copied file indices
    pub filtered_copied: Vec<u32>,
    /// Filtered deleted file indices
    pub filtered_deleted: Vec<u32>,
    /// Filtered modified file indices
    pub filtered_modified: Vec<u32>,
    /// Filtered renamed file indices
    pub filtered_renamed: Vec<u32>,
    /// Filtered type-changed file indices
    pub filtered_type_changed: Vec<u32>,
    /// Filtered unmerged file indices
    pub filtered_unmerged: Vec<u32>,
    /// Filtered unknown file indices
    pub filtered_unknown: Vec<u32>,
    /// "Other changed" indices: ACMR not in filter
    pub other_changed: Vec<u32>,
    /// "Other modified" indices: ACMRD not in filter
    pub other_modified: Vec<u32>,
    /// "Other deleted" indices: D not in filter
    pub other_deleted: Vec<u32>,
    /// All changed and modified superset
    pub all_changed_and_modified: Vec<u32>,
    /// Rename mapping: (index, previous_path)
    pub renamed_mapping: Vec<(u32, InternedString)>,
    /// When output_renamed_as_deleted_added is true, contains (index, previous_path)
    /// for renamed files split into deleted entries. The new name goes to filtered_added.
    pub rename_split_deletions: Vec<(u32, InternedString)>,
    /// YAML group keys that had modified matches
    pub modified_keys: Vec<InternedString>,
    /// YAML group keys that had any changed matches
    pub changed_keys: Vec<InternedString>,
    /// Per-group deploy decisions (populated when YAML groups are present)
    pub group_deploy_decisions: Vec<GroupDeployDecision>,
}

impl ComputedOutputs {
    /// Single-pass computation from a ProcessedResult
    ///
    /// When `output_renamed_as_deleted_added` is true, renamed files are split:
    /// the new path goes to `filtered_added` and the old path is stored in
    /// `rename_split_deletions` (the consumer should include these in deleted output).
    ///
    /// The `interner` parameter is used to look up group keys for concurrency tracking.
    pub fn compute(result: &ProcessedResult, output_renamed_as_deleted_added: bool) -> Self {
        Self::compute_with_concurrency(result, output_renamed_as_deleted_added, None, None)
    }

    /// Compute with concurrency information from workflow check.
    ///
    /// `blocked_groups` maps group keys to blocking run IDs.
    /// `interner` resolves InternedString keys for matching.
    pub fn compute_with_concurrency(
        result: &ProcessedResult,
        output_renamed_as_deleted_added: bool,
        blocked_groups: Option<&HashMap<InternedString, Vec<u64>>>,
        interner: Option<&StringInterner>,
    ) -> Self {
        Self::compute_full(
            result,
            output_renamed_as_deleted_added,
            blocked_groups,
            interner,
            false,
        )
    }

    /// Full computation including Destroy deploy decisions.
    ///
    /// `deleted_to_destroy` maps groups whose only membership is
    /// endpoint-Deleted files to a Destroy action with
    /// `reconstruct_sha = result.base_sha`.
    pub fn compute_full(
        result: &ProcessedResult,
        output_renamed_as_deleted_added: bool,
        blocked_groups: Option<&HashMap<InternedString, Vec<u64>>>,
        _interner: Option<&StringInterner>,
        deleted_to_destroy: bool,
    ) -> Self {
        let filtered_set: HashSet<u32> = result.filtered_indices.iter().copied().collect();
        let unmatched_set: HashSet<u32> = result.unmatched_indices.iter().copied().collect();

        let mut out = Self {
            filtered_added: Vec::new(),
            filtered_copied: Vec::new(),
            filtered_deleted: Vec::new(),
            filtered_modified: Vec::new(),
            filtered_renamed: Vec::new(),
            filtered_type_changed: Vec::new(),
            filtered_unmerged: Vec::new(),
            filtered_unknown: Vec::new(),
            other_changed: Vec::new(),
            other_modified: Vec::new(),
            other_deleted: Vec::new(),
            all_changed_and_modified: Vec::new(),
            renamed_mapping: Vec::new(),
            rename_split_deletions: Vec::new(),
            modified_keys: Vec::new(),
            changed_keys: Vec::new(),
            group_deploy_decisions: Vec::new(),
        };

        for (i, file) in result.all_files.iter().enumerate() {
            let idx = i as u32;
            let in_filter = filtered_set.contains(&idx);
            let in_unmatched = unmatched_set.contains(&idx);

            // All changed and modified includes everything
            out.all_changed_and_modified.push(idx);

            if in_filter {
                match file.change_type {
                    ChangeType::Added => out.filtered_added.push(idx),
                    ChangeType::Copied => out.filtered_copied.push(idx),
                    ChangeType::Deleted => out.filtered_deleted.push(idx),
                    ChangeType::Modified => out.filtered_modified.push(idx),
                    ChangeType::Renamed => {
                        if output_renamed_as_deleted_added {
                            // Split: new path → added, old path → deleted (stored separately)
                            out.filtered_added.push(idx);
                            if let Some(prev) = file.previous_path {
                                out.rename_split_deletions.push((idx, prev));
                            }
                        } else {
                            out.filtered_renamed.push(idx);
                            if let Some(prev) = file.previous_path {
                                out.renamed_mapping.push((idx, prev));
                            }
                        }
                    }
                    ChangeType::TypeChanged => out.filtered_type_changed.push(idx),
                    ChangeType::Unmerged => out.filtered_unmerged.push(idx),
                    ChangeType::Unknown => out.filtered_unknown.push(idx),
                }
            }

            if in_unmatched {
                // "Other changed" = ACMR not in filter
                match file.change_type {
                    ChangeType::Added
                    | ChangeType::Copied
                    | ChangeType::Modified
                    | ChangeType::Renamed => {
                        out.other_changed.push(idx);
                    }
                    _ => {}
                }

                // "Other modified" = ACMRD not in filter
                match file.change_type {
                    ChangeType::Added
                    | ChangeType::Copied
                    | ChangeType::Modified
                    | ChangeType::Renamed
                    | ChangeType::Deleted => {
                        out.other_modified.push(idx);
                    }
                    _ => {}
                }

                // "Other deleted" = D not in filter
                if file.change_type == ChangeType::Deleted {
                    out.other_deleted.push(idx);
                }
            }
        }

        // Compute group keys (InternedString is Copy — no allocation)
        for group in &result.group_results {
            if !group.matched_indices.is_empty() {
                out.changed_keys.push(group.key);

                let has_modified = group.matched_indices.iter().any(|&idx| {
                    result
                        .all_files
                        .get(idx as usize)
                        .map(|f| f.change_type == ChangeType::Modified)
                        .unwrap_or(false)
                });

                if has_modified {
                    out.modified_keys.push(group.key);
                }
            }
        }

        // Helper: look up concurrency info for a group key
        let concurrency_for = |key: InternedString| -> (bool, u32) {
            blocked_groups
                .and_then(|bg| bg.get(&key))
                .map(|ids| (true, ids.len() as u32))
                .unwrap_or((false, 0))
        };

        // Helper: resolve group matched indices to file paths
        let resolve_group_paths = |group: &GroupResult| -> Vec<InternedString> {
            group
                .matched_indices
                .iter()
                .filter_map(|&idx| result.all_files.get(idx as usize).map(|f| f.path))
                .collect()
        };

        // A group's vanished members: rides along on every decision for
        // visibility and drives Destroy classification.
        let vanished_of = |group: &GroupResult| -> Vec<VanishedFile> {
            group
                .vanished_indices
                .iter()
                .map(|&i| result.vanished_files[i as usize])
                .collect()
        };

        // Compute group deploy decisions
        // Destroy classification, shared by both decision branches. Returns
        // Some(decision) when the group must be a Destroy: it has vanished
        // members and no live (non-deleted) changes — or, with
        // deleted_to_destroy, only endpoint-deleted members.
        let destroy_decision =
            |group: &GroupResult, cb: bool, cb_by: u32| -> Option<GroupDeployDecision> {
                let group_vanished = vanished_of(group);
                let live_changes = group
                    .matched_indices
                    .iter()
                    .any(|&i| result.all_files[i as usize].change_type != ChangeType::Deleted);
                let all_deleted = !group.matched_indices.is_empty() && !live_changes;
                let deleted_paths: Vec<InternedString> = group
                    .matched_indices
                    .iter()
                    .filter_map(|&i| {
                        let f = &result.all_files[i as usize];
                        (f.change_type == ChangeType::Deleted).then_some(f.path)
                    })
                    .collect();

                if live_changes {
                    return None; // group still deploys; vanished info rides along
                }
                if !group_vanished.is_empty() && (!all_deleted || deleted_to_destroy) {
                    // vanished-only, or vanished + endpoint-deleted with the flag
                    let reconstruct = group_vanished.first().map(|v| v.last_seen_sha);
                    return Some(GroupDeployDecision {
                        key: group.key,
                        action: GroupDeployAction::Destroy,
                        reason: Some(GroupDeployReason::Vanished),
                        files_to_rebuild: deleted_paths,
                        files_to_skip: Vec::new(),
                        total_files: (group.matched_indices.len() + group_vanished.len()) as u32,
                        concurrency_blocked: cb,
                        concurrency_blocked_by: cb_by,
                        vanished_files: group_vanished,
                        reconstruct_sha: reconstruct,
                    });
                }
                if deleted_to_destroy && all_deleted && group_vanished.is_empty() {
                    return Some(GroupDeployDecision {
                        key: group.key,
                        action: GroupDeployAction::Destroy,
                        reason: Some(GroupDeployReason::EndpointDeleted),
                        files_to_rebuild: deleted_paths,
                        files_to_skip: Vec::new(),
                        total_files: group.matched_indices.len() as u32,
                        concurrency_blocked: cb,
                        concurrency_blocked_by: cb_by,
                        vanished_files: Vec::new(),
                        reconstruct_sha: result.base_sha,
                    });
                }
                None
            };

        if !result.group_results.is_empty() {
            if let Some(ref ci) = result.ci_decision {
                // Build lookup sets from CiDecision
                let rebuild_set: HashSet<InternedString> =
                    ci.files_to_rebuild.iter().copied().collect();
                let skip_set: HashSet<InternedString> = ci.files_to_skip.iter().copied().collect();
                let reasons_map: HashMap<InternedString, RebuildReasonKind> = ci
                    .rebuild_reasons
                    .iter()
                    .map(|r| (r.file, r.kind))
                    .collect();

                for group in &result.group_results {
                    let group_paths = resolve_group_paths(group);
                    let (cb0, cb_by0) = concurrency_for(group.key);
                    if let Some(decision) = destroy_decision(group, cb0, cb_by0) {
                        out.group_deploy_decisions.push(decision);
                        continue;
                    }

                    if group_paths.is_empty() {
                        continue;
                    }

                    // Partition into rebuild/skip
                    let mut group_rebuild = Vec::new();
                    let mut group_skip = Vec::new();
                    for &path in &group_paths {
                        if rebuild_set.contains(&path) {
                            group_rebuild.push(path);
                        } else if skip_set.contains(&path) {
                            group_skip.push(path);
                        } else {
                            // File not in CI decision — treat as needing rebuild
                            group_rebuild.push(path);
                        }
                    }

                    let total_files = group_paths.len() as u32;

                    let (cb, cb_by) = concurrency_for(group.key);

                    if group_rebuild.is_empty() {
                        out.group_deploy_decisions.push(GroupDeployDecision {
                            key: group.key,
                            action: GroupDeployAction::Skip,
                            reason: None,
                            files_to_rebuild: Vec::new(),
                            files_to_skip: group_skip,
                            total_files,
                            concurrency_blocked: cb,
                            concurrency_blocked_by: cb_by,
                            vanished_files: vanished_of(group),
                            reconstruct_sha: None,
                        });
                    } else {
                        let has_new = group_rebuild.iter().any(|p| {
                            matches!(
                                reasons_map.get(p),
                                Some(RebuildReasonKind::NewChange)
                                    | Some(RebuildReasonKind::BothNewAndFailed)
                            )
                        });
                        let has_failure = group_rebuild.iter().any(|p| {
                            matches!(
                                reasons_map.get(p),
                                Some(RebuildReasonKind::PreviousFailure)
                                    | Some(RebuildReasonKind::BothNewAndFailed)
                            )
                        });

                        let reason = match (has_new, has_failure) {
                            (true, true) => GroupDeployReason::BothNewAndFailed,
                            (false, true) => GroupDeployReason::PreviousFailure,
                            _ => GroupDeployReason::NewChange,
                        };

                        out.group_deploy_decisions.push(GroupDeployDecision {
                            key: group.key,
                            action: GroupDeployAction::Deploy,
                            reason: Some(reason),
                            files_to_rebuild: group_rebuild,
                            files_to_skip: group_skip,
                            total_files,
                            concurrency_blocked: cb,
                            concurrency_blocked_by: cb_by,
                            vanished_files: vanished_of(group),
                            reconstruct_sha: None,
                        });
                    }
                }
            } else {
                // No CI decision — all groups with files get Deploy/NewChange
                for group in &result.group_results {
                    let group_paths = resolve_group_paths(group);
                    let (cb0, cb_by0) = concurrency_for(group.key);
                    if let Some(decision) = destroy_decision(group, cb0, cb_by0) {
                        out.group_deploy_decisions.push(decision);
                        continue;
                    }

                    if group_paths.is_empty() {
                        continue;
                    }

                    let total_files = group_paths.len() as u32;
                    let (cb, cb_by) = concurrency_for(group.key);
                    out.group_deploy_decisions.push(GroupDeployDecision {
                        key: group.key,
                        action: GroupDeployAction::Deploy,
                        reason: Some(GroupDeployReason::NewChange),
                        files_to_rebuild: group_paths,
                        files_to_skip: Vec::new(),
                        total_files,
                        concurrency_blocked: cb,
                        concurrency_blocked_by: cb_by,
                        vanished_files: vanished_of(group),
                        reconstruct_sha: None,
                    });
                }
            }
        }

        out
    }

    /// Any files in the changed category (filtered)
    pub fn any_changed(&self) -> bool {
        !self.filtered_added.is_empty()
            || !self.filtered_copied.is_empty()
            || !self.filtered_modified.is_empty()
            || !self.filtered_renamed.is_empty()
    }

    /// Only one file in the changed category
    pub fn only_changed(&self) -> bool {
        let count = self.filtered_added.len()
            + self.filtered_copied.len()
            + self.filtered_modified.len()
            + self.filtered_renamed.len();
        count == 1
    }

    /// Any modified files (filtered)
    pub fn any_modified(&self) -> bool {
        !self.filtered_modified.is_empty()
    }

    /// Only one modified file
    pub fn only_modified(&self) -> bool {
        self.filtered_modified.len() == 1
            && self.filtered_added.is_empty()
            && self.filtered_copied.is_empty()
            && self.filtered_renamed.is_empty()
            && self.filtered_deleted.is_empty()
    }

    /// Any groups with Deploy action
    pub fn has_deployable_groups(&self) -> bool {
        self.group_deploy_decisions
            .iter()
            .any(|d| d.action == GroupDeployAction::Deploy)
    }

    /// Whether any group carries a Destroy deploy decision
    pub fn has_destroyable_groups(&self) -> bool {
        self.group_deploy_decisions
            .iter()
            .any(|d| d.action == GroupDeployAction::Destroy)
    }

    /// Any deleted files (filtered)
    pub fn any_deleted(&self) -> bool {
        !self.filtered_deleted.is_empty()
    }

    /// Only one deleted file
    pub fn only_deleted(&self) -> bool {
        self.filtered_deleted.len() == 1
            && self.filtered_added.is_empty()
            && self.filtered_copied.is_empty()
            && self.filtered_modified.is_empty()
            && self.filtered_renamed.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::interner::StringInterner;
    use crate::types::{ChangedFile, CiDecision, FileOrigin, GroupResult, RebuildReason};

    fn make_file(change_type: ChangeType, idx: u32) -> ChangedFile {
        ChangedFile {
            path: InternedString(idx),
            change_type,
            previous_path: None,
            is_symlink: false,
            submodule_depth: 0,
            origin: FileOrigin::default(),
        }
    }

    #[test]
    fn test_compute_basic() {
        let result = ProcessedResult {
            all_files: vec![
                make_file(ChangeType::Added, 0),
                make_file(ChangeType::Modified, 1),
                make_file(ChangeType::Deleted, 2),
            ],
            filtered_indices: vec![0, 1],
            unmatched_indices: vec![2],
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: Vec::new(),
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: None,
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.filtered_added, vec![0]);
        assert_eq!(outputs.filtered_modified, vec![1]);
        assert!(outputs.filtered_deleted.is_empty());
        assert_eq!(outputs.other_deleted, vec![2]);
        assert!(outputs.any_changed());
        assert!(!outputs.only_changed());
    }

    #[test]
    fn test_compute_unfiltered() {
        let result = ProcessedResult {
            all_files: vec![make_file(ChangeType::Modified, 0)],
            filtered_indices: vec![0],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: false,
            group_results: Vec::new(),
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: None,
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert!(outputs.any_modified());
        assert!(outputs.only_modified());
    }

    #[test]
    fn test_compute_rename_splitting() {
        let result = ProcessedResult {
            all_files: vec![
                ChangedFile {
                    path: InternedString(0), // new name
                    change_type: ChangeType::Renamed,
                    previous_path: Some(InternedString(10)), // old name
                    is_symlink: false,
                    submodule_depth: 0,
                    origin: FileOrigin::default(),
                },
                make_file(ChangeType::Modified, 1),
            ],
            filtered_indices: vec![0, 1],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: Vec::new(),
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: None,
        };

        // Without splitting
        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.filtered_renamed, vec![0]);
        assert!(outputs.filtered_added.is_empty());
        assert!(outputs.rename_split_deletions.is_empty());
        assert_eq!(outputs.renamed_mapping.len(), 1);

        // With splitting
        let outputs = ComputedOutputs::compute(&result, true);
        assert!(outputs.filtered_renamed.is_empty());
        assert_eq!(outputs.filtered_added, vec![0]); // new name → added
        assert_eq!(outputs.rename_split_deletions.len(), 1); // old name → deleted
        assert_eq!(outputs.rename_split_deletions[0], (0, InternedString(10)));
        assert!(outputs.renamed_mapping.is_empty());
    }

    #[test]
    fn test_group_keys() {
        let interner = StringInterner::new();
        let frontend_key = interner.intern("frontend");
        let backend_key = interner.intern("backend");

        let result = ProcessedResult {
            all_files: vec![
                make_file(ChangeType::Modified, 0),
                make_file(ChangeType::Added, 1),
            ],
            filtered_indices: vec![0, 1],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: vec![
                GroupResult {
                    key: frontend_key,
                    matched_indices: vec![0],
                    vanished_indices: Vec::new(),
                },
                GroupResult {
                    key: backend_key,
                    matched_indices: vec![1],
                    vanished_indices: Vec::new(),
                },
            ],
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: None,
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.changed_keys, vec![frontend_key, backend_key]);
        assert_eq!(outputs.modified_keys, vec![frontend_key]); // Only Modified type
    }

    #[test]
    fn test_deploy_decisions_with_ci_decision_mixed() {
        let interner = StringInterner::new();
        let dev_key = interner.intern("dev");
        let staging_key = interner.intern("staging");
        let prod_key = interner.intern("prod");

        let result = ProcessedResult {
            all_files: vec![
                make_file(ChangeType::Modified, 0),
                make_file(ChangeType::Modified, 1),
                make_file(ChangeType::Modified, 2),
            ],
            filtered_indices: vec![0, 1, 2],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: vec![
                GroupResult {
                    key: dev_key,
                    matched_indices: vec![0],
                    vanished_indices: Vec::new(),
                },
                GroupResult {
                    key: staging_key,
                    matched_indices: vec![1],
                    vanished_indices: Vec::new(),
                },
                GroupResult {
                    key: prod_key,
                    matched_indices: vec![2],
                    vanished_indices: Vec::new(),
                },
            ],
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: Some(CiDecision {
                files_to_rebuild: vec![InternedString(0), InternedString(2)],
                files_to_skip: vec![InternedString(1)],
                failed_jobs: Vec::new(),
                successful_jobs: Vec::new(),
                rebuild_reasons: vec![
                    RebuildReason {
                        file: InternedString(0),
                        kind: RebuildReasonKind::NewChange,
                        failed_run_id: None,
                        failed_job_name: None,
                    },
                    RebuildReason {
                        file: InternedString(2),
                        kind: RebuildReasonKind::PreviousFailure,
                        failed_run_id: Some(100),
                        failed_job_name: None,
                    },
                ],
            }),
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.group_deploy_decisions.len(), 3);

        assert_eq!(outputs.group_deploy_decisions[0].key, dev_key);
        assert_eq!(
            outputs.group_deploy_decisions[0].action,
            GroupDeployAction::Deploy
        );
        assert_eq!(
            outputs.group_deploy_decisions[0].reason,
            Some(GroupDeployReason::NewChange)
        );

        assert_eq!(outputs.group_deploy_decisions[1].key, staging_key);
        assert_eq!(
            outputs.group_deploy_decisions[1].action,
            GroupDeployAction::Skip
        );
        assert!(outputs.group_deploy_decisions[1].reason.is_none());

        assert_eq!(outputs.group_deploy_decisions[2].key, prod_key);
        assert_eq!(
            outputs.group_deploy_decisions[2].action,
            GroupDeployAction::Deploy
        );
        assert_eq!(
            outputs.group_deploy_decisions[2].reason,
            Some(GroupDeployReason::PreviousFailure)
        );

        assert!(outputs.has_deployable_groups());
    }

    #[test]
    fn test_deploy_decisions_without_ci_decision() {
        let interner = StringInterner::new();
        let dev_key = interner.intern("dev");
        let prod_key = interner.intern("prod");

        let result = ProcessedResult {
            all_files: vec![
                make_file(ChangeType::Added, 0),
                make_file(ChangeType::Modified, 1),
            ],
            filtered_indices: vec![0, 1],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: vec![
                GroupResult {
                    key: dev_key,
                    matched_indices: vec![0],
                    vanished_indices: Vec::new(),
                },
                GroupResult {
                    key: prod_key,
                    matched_indices: vec![1],
                    vanished_indices: Vec::new(),
                },
            ],
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: None,
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.group_deploy_decisions.len(), 2);

        for d in &outputs.group_deploy_decisions {
            assert_eq!(d.action, GroupDeployAction::Deploy);
            assert_eq!(d.reason, Some(GroupDeployReason::NewChange));
        }
        assert!(outputs.has_deployable_groups());
    }

    #[test]
    fn test_deploy_decisions_empty_groups() {
        let result = ProcessedResult {
            all_files: vec![make_file(ChangeType::Modified, 0)],
            filtered_indices: vec![0],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: false,
            group_results: Vec::new(),
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: None,
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert!(outputs.group_deploy_decisions.is_empty());
        assert!(!outputs.has_deployable_groups());
    }

    #[test]
    fn test_deploy_decisions_both_new_and_failed() {
        let interner = StringInterner::new();
        let mixed_key = interner.intern("mixed");

        let result = ProcessedResult {
            all_files: vec![
                make_file(ChangeType::Modified, 0),
                make_file(ChangeType::Modified, 1),
            ],
            filtered_indices: vec![0, 1],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: vec![GroupResult {
                key: mixed_key,
                matched_indices: vec![0, 1],
                vanished_indices: Vec::new(),
            }],
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: Some(CiDecision {
                files_to_rebuild: vec![InternedString(0), InternedString(1)],
                files_to_skip: Vec::new(),
                failed_jobs: Vec::new(),
                successful_jobs: Vec::new(),
                rebuild_reasons: vec![
                    RebuildReason {
                        file: InternedString(0),
                        kind: RebuildReasonKind::NewChange,
                        failed_run_id: None,
                        failed_job_name: None,
                    },
                    RebuildReason {
                        file: InternedString(1),
                        kind: RebuildReasonKind::PreviousFailure,
                        failed_run_id: Some(200),
                        failed_job_name: None,
                    },
                ],
            }),
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.group_deploy_decisions.len(), 1);
        assert_eq!(
            outputs.group_deploy_decisions[0].action,
            GroupDeployAction::Deploy
        );
        assert_eq!(
            outputs.group_deploy_decisions[0].reason,
            Some(GroupDeployReason::BothNewAndFailed)
        );
        assert_eq!(outputs.group_deploy_decisions[0].files_to_rebuild.len(), 2);
    }

    #[test]
    fn test_deploy_decisions_all_skip() {
        let interner = StringInterner::new();
        let staging_key = interner.intern("staging");

        let result = ProcessedResult {
            all_files: vec![make_file(ChangeType::Modified, 0)],
            filtered_indices: vec![0],
            unmatched_indices: Vec::new(),
            vanished_files: Vec::new(),
            base_sha: None,
            head_sha: None,
            pattern_applied: true,
            group_results: vec![GroupResult {
                key: staging_key,
                matched_indices: vec![0],
                vanished_indices: Vec::new(),
            }],
            additions: 0,
            deletions: 0,
            diagnostics: Vec::new(),
            workflow_result: None,
            ci_decision: Some(CiDecision {
                files_to_rebuild: Vec::new(),
                files_to_skip: vec![InternedString(0)],
                failed_jobs: Vec::new(),
                successful_jobs: Vec::new(),
                rebuild_reasons: Vec::new(),
            }),
        };

        let outputs = ComputedOutputs::compute(&result, false);
        assert_eq!(outputs.group_deploy_decisions.len(), 1);
        assert_eq!(
            outputs.group_deploy_decisions[0].action,
            GroupDeployAction::Skip
        );
        assert!(outputs.group_deploy_decisions[0].reason.is_none());
        assert!(!outputs.has_deployable_groups());
    }
}

#[cfg(test)]
mod vanished_decision_tests {
    use super::*;
    use crate::interner::StringInterner;
    use crate::types::{ChangedFile, FileOrigin, GroupResult, VanishedFile};

    fn deleted_file(path: InternedString) -> ChangedFile {
        ChangedFile {
            path,
            change_type: ChangeType::Deleted,
            previous_path: None,
            is_symlink: false,
            submodule_depth: 0,
            origin: FileOrigin {
                in_current_changes: true,
                in_previous_failure: false,
                in_previous_success: false,
            },
        }
    }

    fn modified_file(path: InternedString) -> ChangedFile {
        ChangedFile {
            change_type: ChangeType::Modified,
            ..deleted_file(path)
        }
    }

    fn base_result(interner: &StringInterner) -> ProcessedResult {
        ProcessedResult {
            base_sha: Some(interner.intern("basesha")),
            head_sha: Some(interner.intern("headsha")),
            ..Default::default()
        }
    }

    #[test]
    fn test_vanished_only_group_destroys_with_last_seen() {
        let interner = StringInterner::new();
        let mut result = base_result(&interner);
        result.vanished_files = vec![VanishedFile {
            path: interner.intern("stacks/gone/Pulumi.yaml"),
            last_seen_sha: interner.intern("cafe1234"),
        }];
        result.group_results = vec![GroupResult {
            key: interner.intern("gone"),
            matched_indices: Vec::new(),
            vanished_indices: vec![0],
        }];

        let out = ComputedOutputs::compute_full(&result, false, None, None, false);
        assert_eq!(out.group_deploy_decisions.len(), 1);
        let d = &out.group_deploy_decisions[0];
        assert_eq!(d.action, GroupDeployAction::Destroy);
        assert_eq!(d.reason, Some(GroupDeployReason::Vanished));
        assert_eq!(
            interner.resolve(d.reconstruct_sha.unwrap()),
            Some("cafe1234")
        );
        assert!(out.has_destroyable_groups());
        assert!(!out.has_deployable_groups());
    }

    #[test]
    fn test_endpoint_deleted_group_destroys_only_with_flag() {
        let interner = StringInterner::new();
        let mut result = base_result(&interner);
        result.all_files = vec![deleted_file(interner.intern("stacks/old/Pulumi.yaml"))];
        result.filtered_indices = vec![0];
        result.group_results = vec![GroupResult {
            key: interner.intern("old"),
            matched_indices: vec![0],
            vanished_indices: Vec::new(),
        }];

        // Flag off: legacy behavior (Deploy — the deletion rides the build path)
        let out = ComputedOutputs::compute_full(&result, false, None, None, false);
        assert_eq!(
            out.group_deploy_decisions[0].action,
            GroupDeployAction::Deploy
        );

        // Flag on: Destroy with reconstruct_sha = base
        let out = ComputedOutputs::compute_full(&result, false, None, None, true);
        let d = &out.group_deploy_decisions[0];
        assert_eq!(d.action, GroupDeployAction::Destroy);
        assert_eq!(d.reason, Some(GroupDeployReason::EndpointDeleted));
        assert_eq!(
            interner.resolve(d.reconstruct_sha.unwrap()),
            Some("basesha")
        );
        assert_eq!(d.files_to_rebuild.len(), 1);
    }

    #[test]
    fn test_mixed_group_with_live_changes_stays_deploy() {
        let interner = StringInterner::new();
        let mut result = base_result(&interner);
        result.all_files = vec![modified_file(interner.intern("stacks/live/Pulumi.yaml"))];
        result.filtered_indices = vec![0];
        result.vanished_files = vec![VanishedFile {
            path: interner.intern("stacks/live/old.yaml"),
            last_seen_sha: interner.intern("cafe1234"),
        }];
        result.group_results = vec![GroupResult {
            key: interner.intern("live"),
            matched_indices: vec![0],
            vanished_indices: vec![0],
        }];

        let out = ComputedOutputs::compute_full(&result, false, None, None, true);
        let d = &out.group_deploy_decisions[0];
        assert_eq!(d.action, GroupDeployAction::Deploy);
        assert!(d.reconstruct_sha.is_none());
    }

    #[test]
    fn test_vanished_plus_deleted_destroy_gated_by_flag() {
        let interner = StringInterner::new();
        let mut result = base_result(&interner);
        result.all_files = vec![deleted_file(interner.intern("stacks/g/Pulumi.yaml"))];
        result.filtered_indices = vec![0];
        result.vanished_files = vec![VanishedFile {
            path: interner.intern("stacks/g/schema.json"),
            last_seen_sha: interner.intern("beef5678"),
        }];
        result.group_results = vec![GroupResult {
            key: interner.intern("g"),
            matched_indices: vec![0],
            vanished_indices: vec![0],
        }];

        // Without the flag the endpoint-deleted member keeps legacy Deploy
        let out = ComputedOutputs::compute_full(&result, false, None, None, false);
        assert_eq!(
            out.group_deploy_decisions[0].action,
            GroupDeployAction::Deploy
        );

        // With the flag: Destroy, newest vanished sha wins for reconstruction
        let out = ComputedOutputs::compute_full(&result, false, None, None, true);
        let d = &out.group_deploy_decisions[0];
        assert_eq!(d.action, GroupDeployAction::Destroy);
        assert_eq!(d.reason, Some(GroupDeployReason::Vanished));
        assert_eq!(
            interner.resolve(d.reconstruct_sha.unwrap()),
            Some("beef5678")
        );
        assert_eq!(d.total_files, 2);
    }
}