fix-engine-core 0.0.1

Core types for the fix engine: text edits, fix plans, strategies
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
//! Fix engine core types.
//!
//! Defines the data model for planned fixes: text edits grouped by file,
//! with support for pattern-based (deterministic) and LLM-assisted fixes.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

/// Return type for [`load_strategies_and_families`]: `(strategies, family_entries)`.
pub type StrategiesAndFamilies = (
    BTreeMap<String, FixStrategy>,
    BTreeMap<String, FixStrategyEntry>,
);

// Re-export shared types from konveyor-core so existing code continues to compile.
pub use konveyor_core::fix::{
    FixConfidence, FixSource, FixStrategyEntry, MappingEntry as StrategyMappingEntry,
    MemberMappingEntry,
};

// Re-export konveyor-core types for convenience
pub use konveyor_core::incident;
pub use konveyor_core::report;
pub use konveyor_core::rule;

/// A single text replacement within a file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextEdit {
    /// 1-indexed line number where the edit applies.
    pub line: u32,
    /// The original text to find on this line.
    pub old_text: String,
    /// The replacement text.
    pub new_text: String,
    /// Rule ID that generated this fix.
    pub rule_id: String,
    /// Human-readable description of what this fix does.
    pub description: String,
    /// When true, replace ALL occurrences of old_text on this line (not just the first).
    /// Used for prefix replacements (e.g. CssVariablePrefix) where a single line
    /// may contain multiple instances of the old prefix.
    #[serde(default)]
    pub replace_all: bool,
}

/// A planned fix for a single incident.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlannedFix {
    /// The text edits to apply.
    pub edits: Vec<TextEdit>,
    /// Confidence level.
    pub confidence: FixConfidence,
    /// How the fix was generated.
    pub source: FixSource,
    /// The rule ID this fix addresses.
    pub rule_id: String,
    /// File URI from the incident.
    pub file_uri: String,
    /// Line number from the incident.
    pub line: u32,
    /// Description of what this fix does.
    pub description: String,
}

/// A fix plan: all planned fixes grouped by file.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FixPlan {
    /// Fixes grouped by file path.
    pub files: BTreeMap<PathBuf, Vec<PlannedFix>>,
    /// Incidents that could not be auto-fixed and need manual attention.
    pub manual: Vec<ManualFixItem>,
    /// Incidents pending LLM-assisted fix.
    pub pending_llm: Vec<LlmFixRequest>,
    /// Number of edits removed during plan-time deduplication because a more
    /// specific edit on the same line already covers the same text region.
    #[serde(default)]
    pub edits_subsumed: usize,
}

/// An incident that requires manual fixing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManualFixItem {
    pub rule_id: String,
    pub file_uri: String,
    pub line: u32,
    pub message: String,
    pub code_snip: Option<String>,
}

/// A request to send to the LLM for fix generation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmFixRequest {
    pub rule_id: String,
    pub file_uri: String,
    pub file_path: PathBuf,
    pub line: u32,
    pub message: String,
    pub code_snip: Option<String>,
    /// The full source content of the file (for context).
    pub source: Option<String>,
    /// Labels from the violation (e.g., "family=Modal", "change-type=prop-to-child").
    /// Used to coalesce related rules into coherent migration groups.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub labels: Vec<String>,
}

/// Result of applying a fix plan.
#[derive(Debug, Default)]
pub struct FixResult {
    /// Number of files modified.
    pub files_modified: usize,
    /// Number of edits applied.
    pub edits_applied: usize,
    /// Number of edits skipped (line out of bounds or text not found).
    pub edits_skipped: usize,
    /// Number of edits subsumed by a more specific edit on the same line.
    pub edits_subsumed: usize,
    /// Errors encountered.
    pub errors: Vec<String>,
    /// Paths of files that were actually modified on disk.
    pub modified_files: Vec<std::path::PathBuf>,
}

/// A rename mapping: old name -> new name.
/// Used for prop renames, component renames, import renames, etc.
#[derive(Debug, Clone)]
pub struct RenameMapping {
    pub old: String,
    pub new: String,
}

/// Known fix strategies keyed by rule ID.
/// Each entry defines how to transform incidents from that rule into text edits.
#[derive(Debug, Clone)]
pub enum FixStrategy {
    /// Simple text replacement: rename the matched text.
    /// The mapping is propName/componentName/importedName old -> new.
    Rename(Vec<RenameMapping>),
    /// Remove the matched attribute (e.g., delete the entire prop from a JSX tag,
    /// or remove a decorator in Python, etc.).
    RemoveAttribute,
    /// Replace an import/include source path.
    ImportPathChange { old_path: String, new_path: String },
    /// Replace a CSS variable/class prefix.
    CssVariablePrefix {
        old_prefix: String,
        new_prefix: String,
        /// CSS classes to exclude from this prefix swap (dead after swap).
        exclude_patterns: Vec<String>,
    },
    /// Ensure a dependency exists at the correct version in the project manifest
    /// (e.g., package.json for Node.js, Cargo.toml for Rust, go.mod for Go).
    EnsureDependency {
        package: String,
        new_version: String,
    },
    /// No auto-fix available -- flag for manual review.
    Manual,
    /// Send to LLM for fix generation.
    /// `context` carries pre-formatted strategy data (strategy type, from/to,
    /// mappings, target structure, etc.) from the `FixStrategyEntry`.
    /// `None` when the rule fell through to Llm via label inference or default.
    Llm { context: Option<String> },
}

/// Convert a `FixStrategyEntry` (from the shared `konveyor-core` crate) to
/// a runtime `FixStrategy`.
///
/// When `mappings` is populated (consolidated rule), builds a multi-mapping
/// `FixStrategy::Rename` or extracts multiple `RemoveProp` targets.
pub fn strategy_entry_to_fix_strategy(entry: &FixStrategyEntry) -> FixStrategy {
    match entry.strategy.as_str() {
        "Rename" => {
            let mut renames: Vec<RenameMapping> = Vec::new();
            // Collect from mappings array (consolidated rule)
            for m in &entry.mappings {
                if let (Some(from), Some(to)) = (&m.from, &m.to) {
                    renames.push(RenameMapping {
                        old: from.clone(),
                        new: to.clone(),
                    });
                }
            }
            // Fall back to top-level from/to (single-rule strategy)
            if renames.is_empty() {
                if let (Some(from), Some(to)) = (&entry.from, &entry.to) {
                    renames.push(RenameMapping {
                        old: from.clone(),
                        new: to.clone(),
                    });
                }
            }
            if renames.is_empty() {
                FixStrategy::Manual
            } else {
                FixStrategy::Rename(renames)
            }
        }
        "RemoveProp" => FixStrategy::RemoveAttribute,
        "CssVariablePrefix" => {
            if let (Some(from), Some(to)) = (&entry.from, &entry.to) {
                FixStrategy::CssVariablePrefix {
                    old_prefix: from.clone(),
                    new_prefix: to.clone(),
                    exclude_patterns: entry.exclude_patterns.clone(),
                }
            } else {
                FixStrategy::Manual
            }
        }
        "ImportPathChange" => {
            if let (Some(from), Some(to)) = (&entry.from, &entry.to) {
                FixStrategy::ImportPathChange {
                    old_path: from.clone(),
                    new_path: to.clone(),
                }
            } else {
                FixStrategy::Manual
            }
        }
        "EnsureDependency" => {
            if let (Some(package), Some(new_version)) = (&entry.package, &entry.new_version) {
                FixStrategy::EnsureDependency {
                    package: package.clone(),
                    new_version: new_version.clone(),
                }
            } else {
                FixStrategy::Manual
            }
        }
        "PropValueChange" | "PropTypeChange" => FixStrategy::Llm {
            context: Some(format_strategy_context(entry)),
        },
        "LlmAssisted" => FixStrategy::Llm {
            context: Some(format_strategy_context(entry)),
        },
        // v2 SD-pipeline strategies -- these require structural
        // transformations that only the LLM can handle.
        "ChildToProp"
        | "PropToChild"
        | "PropToChildren"
        | "CompositionChange"
        | "DeprecatedMigration" => FixStrategy::Llm {
            context: Some(format_strategy_context(entry)),
        },
        // Family-level migration: the entry carries the complete target
        // component structure. Format it as a rich context block.
        "FamilyMigration" => FixStrategy::Llm {
            context: Some(format_family_migration_context(entry)),
        },
        _ => FixStrategy::Manual,
    }
}

/// Format a per-rule `FixStrategyEntry` into a human-readable context block
/// for the LLM prompt. Includes the strategy type, from/to mappings,
/// component/prop targets, member mappings, etc.
fn format_strategy_context(entry: &FixStrategyEntry) -> String {
    let mut parts = Vec::new();
    parts.push(format!("Strategy: {}", entry.strategy));
    if let Some(ref c) = entry.component {
        parts.push(format!("Component: {}", c));
    }
    if let Some(ref p) = entry.prop {
        parts.push(format!("Prop: {}", p));
    }
    if let Some(ref from) = entry.from {
        parts.push(format!("From: {}", from));
    }
    if let Some(ref to) = entry.to {
        parts.push(format!("To: {}", to));
    }
    if let Some(ref repl) = entry.replacement {
        parts.push(format!("Replacement: {}", repl));
    }
    if !entry.mappings.is_empty() {
        let maps: Vec<String> = entry
            .mappings
            .iter()
            .filter_map(|m| match (&m.from, &m.to) {
                (Some(f), Some(t)) => Some(format!("  {} -> {}", f, t)),
                _ => None,
            })
            .collect();
        if !maps.is_empty() {
            parts.push(format!("Mappings:\n{}", maps.join("\n")));
        }
    }
    if !entry.member_mappings.is_empty() {
        let maps: Vec<String> = entry
            .member_mappings
            .iter()
            .map(|m| format!("  {} -> {}", m.old_name, m.new_name))
            .collect();
        parts.push(format!("Member mappings:\n{}", maps.join("\n")));
    }
    if !entry.removed_members.is_empty() {
        parts.push(format!(
            "Removed members: {}",
            entry.removed_members.join(", ")
        ));
    }
    parts.join("\n")
}

/// Format a family-level `FixStrategyEntry` (keyed `family:<Name>`) into a
/// rich context block for the LLM prompt.
fn format_family_migration_context(entry: &FixStrategyEntry) -> String {
    let mut parts = Vec::new();
    parts.push("Strategy: FamilyMigration".to_string());

    if let Some(ref target) = entry.target_structure {
        parts.push(format!(
            "\nTarget structure (correct v6 composition):\n```jsx\n{}\n```",
            target
        ));
    }
    if let Some(ref comp) = entry.component {
        parts.push(format!("Component: {}", comp));
    }
    if !entry.retained_props.is_empty() {
        parts.push(format!(
            "Props that stay on root: {}",
            entry.retained_props.join(", ")
        ));
    }
    if !entry.prop_to_child.is_empty() {
        let maps: Vec<String> = entry
            .prop_to_child
            .iter()
            .map(|(prop, child)| format!("  {} -> <{} />", prop, child))
            .collect();
        parts.push(format!(
            "Props that move to child components:\n{}",
            maps.join("\n")
        ));
    }
    if !entry.child_props_to_parent.is_empty() {
        let maps: Vec<String> = entry
            .child_props_to_parent
            .iter()
            .map(|(child_prop, parent_prop)| format!("  {} -> {}", child_prop, parent_prop))
            .collect();
        parts.push(format!(
            "Child props that move to parent:\n{}",
            maps.join("\n")
        ));
    }
    if !entry.removed_children.is_empty() {
        parts.push(format!(
            "Removed children: {}",
            entry.removed_children.join(", ")
        ));
    }
    if !entry.new_imports.is_empty() {
        let src = entry.import_source.as_deref().unwrap_or("(same package)");
        parts.push(format!(
            "Add imports: {} from '{}'",
            entry.new_imports.join(", "),
            src
        ));
    }
    if !entry.removed_imports.is_empty() {
        parts.push(format!(
            "Remove imports: {}",
            entry.removed_imports.join(", ")
        ));
    }
    if !entry.prop_value_changes.is_empty() {
        let mut lines = Vec::new();
        for (prop, mappings) in &entry.prop_value_changes {
            for m in mappings {
                if let (Some(from), Some(to)) = (&m.from, &m.to) {
                    lines.push(format!("  {}: {} -> {}", prop, from, to));
                }
            }
        }
        if !lines.is_empty() {
            parts.push(format!("Prop value changes:\n{}", lines.join("\n")));
        }
    }

    // Deprecated -> v6 migration context: complete prop mapping with types.
    if let Some(ref dm) = entry.deprecated_migration {
        parts.push(format!(
            "\nDeprecated -> v6 migration:\n  Old import: {}\n  New import: {}",
            dm.old_package, dm.new_package
        ));

        // Matching props (survived the migration, may have type changes)
        if !dm.matching_props.is_empty() {
            let mut lines = Vec::new();
            for p in &dm.matching_props {
                if p.type_changed {
                    lines.push(format!(
                        "  {} -> {} (TYPE CHANGED):\n    old: {}\n    new: {}",
                        p.old_name,
                        p.new_name,
                        p.old_type.as_deref().unwrap_or("?"),
                        p.new_type.as_deref().unwrap_or("?")
                    ));
                } else {
                    let typ = p.new_type.as_deref().unwrap_or("?");
                    if p.old_name == p.new_name {
                        lines.push(format!("  {}: {} (unchanged)", p.new_name, typ));
                    } else {
                        lines.push(format!(
                            "  {} -> {}: {} (renamed, type unchanged)",
                            p.old_name, p.new_name, typ
                        ));
                    }
                }
            }
            parts.push(format!("Matching props:\n{}", lines.join("\n")));
        }

        // New props (only on v6, not on deprecated)
        if !dm.new_props.is_empty() {
            let mut lines = Vec::new();
            for (name, typ) in &dm.new_props {
                let mut line = format!("  {}: {}", name, typ);
                // Auto-detect render-prop patterns
                if (typ.contains("=> React.ReactNode")
                    || typ.contains("=> ReactNode")
                    || typ.contains("=> React.ReactElement"))
                    && typ.contains("=>")
                {
                    line.push_str(
                        "\n    NOTE: This is a render function. Pass a function REFERENCE, \
                         not a function call.",
                    );
                }
                lines.push(line);
            }
            parts.push(format!(
                "New props on v6 (not on deprecated version):\n{}",
                lines.join("\n")
            ));
        }

        // Removed props (only on deprecated, no v6 equivalent)
        if !dm.removed_props.is_empty() {
            parts.push(format!(
                "Removed props (no v6 equivalent): {}",
                dm.removed_props.join(", ")
            ));
        }
    }

    parts.join("\n")
}

/// Load fix strategies from a JSON file and also extract raw family-level
/// `FixStrategyEntry` entries (keyed `family:*`) for use in family consolidation.
///
/// Returns `(strategies, family_entries)`.
pub fn load_strategies_and_families(
    path: &Path,
) -> Result<StrategiesAndFamilies, Box<dyn std::error::Error>> {
    let content = std::fs::read_to_string(path)?;
    let entries: BTreeMap<String, FixStrategyEntry> = serde_json::from_str(&content)?;
    let strategies = entries
        .iter()
        .map(|(rule_id, entry)| (rule_id.clone(), strategy_entry_to_fix_strategy(entry)))
        .collect();
    let families = entries
        .into_iter()
        .filter(|(k, _)| k.starts_with("family:"))
        .collect();
    Ok((strategies, families))
}

/// Load fix strategies from a JSON file.
///
/// Returns a map of rule_id -> FixStrategy.
pub fn load_strategies_from_json(
    path: &Path,
) -> Result<BTreeMap<String, FixStrategy>, Box<dyn std::error::Error>> {
    let content = std::fs::read_to_string(path)?;
    let entries: BTreeMap<String, FixStrategyEntry> = serde_json::from_str(&content)?;
    let strategies = entries
        .iter()
        .map(|(rule_id, entry)| (rule_id.clone(), strategy_entry_to_fix_strategy(entry)))
        .collect();
    Ok(strategies)
}

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

    fn make_strategy_entry(strategy: &str) -> FixStrategyEntry {
        FixStrategyEntry::new(strategy)
    }

    #[test]
    fn test_rename_with_top_level_from_to() {
        let mut entry = make_strategy_entry("Rename");
        entry.from = Some("Chip".to_string());
        entry.to = Some("Label".to_string());

        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Rename(mappings) => {
                assert_eq!(mappings.len(), 1);
                assert_eq!(mappings[0].old, "Chip");
                assert_eq!(mappings[0].new, "Label");
            }
            other => panic!("Expected Rename, got {:?}", other),
        }
    }

    #[test]
    fn test_rename_with_mappings_array() {
        let mut entry = make_strategy_entry("Rename");
        entry.mappings = vec![
            StrategyMappingEntry {
                from: Some("Chip".to_string()),
                to: Some("Label".to_string()),
                component: None,
                prop: None,
            },
            StrategyMappingEntry {
                from: Some("ChipGroup".to_string()),
                to: Some("LabelGroup".to_string()),
                component: None,
                prop: None,
            },
        ];

        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Rename(mappings) => {
                assert_eq!(mappings.len(), 2);
                assert_eq!(mappings[0].old, "Chip");
                assert_eq!(mappings[0].new, "Label");
                assert_eq!(mappings[1].old, "ChipGroup");
                assert_eq!(mappings[1].new, "LabelGroup");
            }
            other => panic!("Expected Rename, got {:?}", other),
        }
    }

    #[test]
    fn test_rename_mappings_take_precedence_over_top_level() {
        let mut entry = make_strategy_entry("Rename");
        entry.from = Some("TopLevel".to_string());
        entry.to = Some("ShouldBeIgnored".to_string());
        entry.mappings = vec![StrategyMappingEntry {
            from: Some("FromMapping".to_string()),
            to: Some("ToMapping".to_string()),
            component: None,
            prop: None,
        }];

        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Rename(mappings) => {
                assert_eq!(mappings.len(), 1);
                assert_eq!(mappings[0].old, "FromMapping");
            }
            other => panic!("Expected Rename, got {:?}", other),
        }
    }

    #[test]
    fn test_css_variable_prefix() {
        let mut entry = make_strategy_entry("CssVariablePrefix");
        entry.from = Some("pf-v5-".to_string());
        entry.to = Some("pf-v6-".to_string());

        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::CssVariablePrefix {
                old_prefix,
                new_prefix,
                exclude_patterns,
            } => {
                assert_eq!(old_prefix, "pf-v5-");
                assert_eq!(new_prefix, "pf-v6-");
                assert!(exclude_patterns.is_empty());
            }
            other => panic!("Expected CssVariablePrefix, got {:?}", other),
        }
    }

    #[test]
    fn test_css_variable_prefix_missing_fields_falls_to_manual() {
        let entry = make_strategy_entry("CssVariablePrefix");
        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Manual => {}
            other => panic!("Expected Manual, got {:?}", other),
        }
    }

    #[test]
    fn test_import_path_change() {
        let mut entry = make_strategy_entry("ImportPathChange");
        entry.from = Some("@patternfly/react-core/deprecated".to_string());
        entry.to = Some("@patternfly/react-core".to_string());

        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::ImportPathChange { old_path, new_path } => {
                assert_eq!(old_path, "@patternfly/react-core/deprecated");
                assert_eq!(new_path, "@patternfly/react-core");
            }
            other => panic!("Expected ImportPathChange, got {:?}", other),
        }
    }

    #[test]
    fn test_import_path_change_missing_fields_falls_to_manual() {
        let mut entry = make_strategy_entry("ImportPathChange");
        entry.from = Some("something".to_string());
        // missing `to`
        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Manual => {}
            other => panic!("Expected Manual, got {:?}", other),
        }
    }

    #[test]
    fn test_ensure_dependency() {
        let mut entry = make_strategy_entry("EnsureDependency");
        entry.package = Some("@patternfly/react-core".to_string());
        entry.new_version = Some("^6.0.0".to_string());

        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::EnsureDependency {
                package,
                new_version,
            } => {
                assert_eq!(package, "@patternfly/react-core");
                assert_eq!(new_version, "^6.0.0");
            }
            other => panic!("Expected EnsureDependency, got {:?}", other),
        }
    }

    #[test]
    fn test_ensure_dependency_missing_fields_falls_to_manual() {
        let mut entry = make_strategy_entry("EnsureDependency");
        entry.package = Some("something".to_string());
        // missing new_version
        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Manual => {}
            other => panic!("Expected Manual, got {:?}", other),
        }
    }

    #[test]
    fn test_remove_prop_maps_to_remove_attribute() {
        let entry = make_strategy_entry("RemoveProp");
        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::RemoveAttribute => {}
            other => panic!("Expected RemoveAttribute, got {:?}", other),
        }
    }

    #[test]
    fn test_prop_value_change_maps_to_llm() {
        let entry = make_strategy_entry("PropValueChange");
        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Llm { .. } => {}
            other => panic!("Expected Llm, got {:?}", other),
        }
    }

    #[test]
    fn test_unknown_strategy_maps_to_manual() {
        let entry = make_strategy_entry("SomethingUnknown");
        match strategy_entry_to_fix_strategy(&entry) {
            FixStrategy::Manual => {}
            other => panic!("Expected Manual, got {:?}", other),
        }
    }

    #[test]
    fn test_fix_plan_default_is_empty() {
        let plan = FixPlan::default();
        assert!(plan.files.is_empty());
        assert!(plan.manual.is_empty());
        assert!(plan.pending_llm.is_empty());
    }

    #[test]
    fn test_fix_result_default_is_zero() {
        let result = FixResult::default();
        assert_eq!(result.files_modified, 0);
        assert_eq!(result.edits_applied, 0);
        assert_eq!(result.edits_skipped, 0);
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_fix_confidence_serde() {
        assert_eq!(
            serde_json::to_string(&FixConfidence::Exact).unwrap(),
            "\"exact\""
        );
        assert_eq!(
            serde_json::to_string(&FixConfidence::High).unwrap(),
            "\"high\""
        );
        assert_eq!(
            serde_json::to_string(&FixConfidence::Medium).unwrap(),
            "\"medium\""
        );
        assert_eq!(
            serde_json::to_string(&FixConfidence::Low).unwrap(),
            "\"low\""
        );
    }

    #[test]
    fn test_fix_source_serde() {
        assert_eq!(
            serde_json::to_string(&FixSource::Pattern).unwrap(),
            "\"pattern\""
        );
        assert_eq!(serde_json::to_string(&FixSource::Llm).unwrap(), "\"llm\"");
        assert_eq!(
            serde_json::to_string(&FixSource::Manual).unwrap(),
            "\"manual\""
        );
    }

    #[test]
    fn test_strategy_entry_json_deserialization() {
        let json = r#"{
            "strategy": "Rename",
            "mappings": [
                {"from": "Chip", "to": "Label"},
                {"from": "ChipGroup", "to": "LabelGroup"}
            ]
        }"#;
        let entry: FixStrategyEntry = serde_json::from_str(json).unwrap();
        assert_eq!(entry.strategy, "Rename");
        assert_eq!(entry.mappings.len(), 2);
        assert_eq!(entry.mappings[0].from.as_deref(), Some("Chip"));
        assert_eq!(entry.mappings[0].to.as_deref(), Some("Label"));
    }
}