cflx 0.6.11

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
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
//! Spec delta parsing and conflict detection module
//!
//! This module provides functionality to:
//! - Parse spec delta files from changes
//! - Detect conflicts between spec deltas across multiple changes
//! - Generate human-readable and JSON output

use crate::error::{OrchestratorError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Represents a delta operation type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeltaType {
    Added,
    Modified,
    Removed,
    Renamed { from: String },
}

/// Represents a requirement delta in a spec file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequirementDelta {
    /// Name of the requirement
    pub name: String,
    /// Type of operation
    pub delta_type: DeltaType,
    /// Content of the requirement (if applicable)
    pub content: Option<String>,
    /// Source change ID
    pub change_id: String,
    /// Spec file path (relative to change)
    pub spec_path: PathBuf,
}

/// Represents a conflict between two requirement deltas
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Conflict {
    /// Requirement name
    pub requirement_name: String,
    /// First delta involved in conflict
    pub delta1: RequirementDelta,
    /// Second delta involved in conflict
    pub delta2: RequirementDelta,
    /// Conflict reason
    pub reason: ConflictReason,
}

/// Reason for conflict
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConflictReason {
    /// Same requirement modified with different content
    ContentMismatch,
    /// One change removes, another modifies/adds
    RemoveConflict,
    /// Conflicting rename operations
    RenameConflict,
}

/// Parse all spec delta files from a change directory
pub fn parse_change_deltas(change_id: &str) -> Result<Vec<RequirementDelta>> {
    let change_path = Path::new("openspec/changes").join(change_id);
    if !change_path.exists() {
        return Err(OrchestratorError::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            format!("Change directory not found: {}", change_id),
        )));
    }

    let specs_path = change_path.join("specs");
    if !specs_path.exists() {
        // No specs directory means no deltas
        return Ok(Vec::new());
    }

    let mut deltas = Vec::new();
    collect_deltas_recursive(&specs_path, change_id, &mut deltas)?;
    Ok(deltas)
}

/// Recursively collect deltas from spec files
fn collect_deltas_recursive(
    dir: &Path,
    change_id: &str,
    deltas: &mut Vec<RequirementDelta>,
) -> Result<()> {
    if !dir.is_dir() {
        return Ok(());
    }

    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            collect_deltas_recursive(&path, change_id, deltas)?;
        } else if path.extension().and_then(|s| s.to_str()) == Some("md") {
            if let Some(file_deltas) = parse_spec_file(&path, change_id)? {
                deltas.extend(file_deltas);
            }
        }
    }

    Ok(())
}

/// Parse a single spec file and extract requirement deltas
fn parse_spec_file(path: &Path, change_id: &str) -> Result<Option<Vec<RequirementDelta>>> {
    let content = fs::read_to_string(path)?;
    let mut deltas = Vec::new();

    let lines: Vec<&str> = content.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i].trim();

        // Check for section headers
        let delta_type = if line == "## ADDED Requirements" {
            Some(DeltaType::Added)
        } else if line == "## MODIFIED Requirements" {
            Some(DeltaType::Modified)
        } else if line == "## REMOVED Requirements" {
            Some(DeltaType::Removed)
        } else if line.starts_with("## RENAMED Requirements") {
            // For RENAMED, we need to extract the "from" name
            // Format: ### Requirement: NewName (from OldName)
            None // We'll handle this specially
        } else {
            None
        };

        if let Some(dtype) = delta_type {
            // Parse requirements under this section
            i += 1;
            while i < lines.len() {
                let req_line = lines[i].trim();
                if req_line.starts_with("## ") {
                    // New section
                    break;
                }

                if req_line.starts_with("### Requirement:") {
                    let req_name = req_line
                        .trim_start_matches("### Requirement:")
                        .trim()
                        .to_string();

                    // Collect content until next requirement or section
                    let mut content_lines = Vec::new();
                    i += 1;
                    while i < lines.len() {
                        let content_line = lines[i];
                        if content_line.trim().starts_with("### Requirement:")
                            || content_line.trim().starts_with("## ")
                        {
                            break;
                        }
                        content_lines.push(content_line);
                        i += 1;
                    }

                    let content = if matches!(dtype, DeltaType::Removed) {
                        None
                    } else {
                        Some(content_lines.join("\n"))
                    };

                    deltas.push(RequirementDelta {
                        name: req_name,
                        delta_type: dtype.clone(),
                        content,
                        change_id: change_id.to_string(),
                        spec_path: path.to_path_buf(),
                    });

                    continue;
                }

                i += 1;
            }
            continue;
        }

        // Handle RENAMED section specially
        if line.starts_with("## RENAMED Requirements") {
            i += 1;
            while i < lines.len() {
                let req_line = lines[i].trim();
                if req_line.starts_with("## ") {
                    break;
                }

                if req_line.starts_with("### Requirement:") {
                    // Format: ### Requirement: NewName (from OldName)
                    let req_text = req_line.trim_start_matches("### Requirement:").trim();
                    if let Some(from_pos) = req_text.find("(from ") {
                        let new_name = req_text[..from_pos].trim().to_string();
                        let from_text = &req_text[from_pos + 6..]; // Skip "(from "
                        let old_name = from_text.trim_end_matches(')').trim().to_string();

                        // Collect content
                        let mut content_lines = Vec::new();
                        i += 1;
                        while i < lines.len() {
                            let content_line = lines[i];
                            if content_line.trim().starts_with("### Requirement:")
                                || content_line.trim().starts_with("## ")
                            {
                                break;
                            }
                            content_lines.push(content_line);
                            i += 1;
                        }

                        deltas.push(RequirementDelta {
                            name: new_name,
                            delta_type: DeltaType::Renamed { from: old_name },
                            content: Some(content_lines.join("\n")),
                            change_id: change_id.to_string(),
                            spec_path: path.to_path_buf(),
                        });

                        continue;
                    }
                }

                i += 1;
            }
            continue;
        }

        i += 1;
    }

    if deltas.is_empty() {
        Ok(None)
    } else {
        Ok(Some(deltas))
    }
}

/// Detect conflicts between deltas from different changes
pub fn detect_conflicts(all_deltas: &[RequirementDelta]) -> Vec<Conflict> {
    let mut conflicts = Vec::new();

    // Group deltas by requirement name
    let mut by_name: HashMap<String, Vec<&RequirementDelta>> = HashMap::new();
    for delta in all_deltas {
        by_name.entry(delta.name.clone()).or_default().push(delta);
    }

    // Check for conflicts within each requirement name
    for (req_name, deltas) in by_name {
        if deltas.len() < 2 {
            continue;
        }

        // Check all pairs of deltas for this requirement
        for i in 0..deltas.len() {
            for j in (i + 1)..deltas.len() {
                let d1 = deltas[i];
                let d2 = deltas[j];

                // Skip if same change (shouldn't happen)
                if d1.change_id == d2.change_id {
                    continue;
                }

                // Check for conflicts
                if let Some(reason) = check_conflict_pair(d1, d2) {
                    conflicts.push(Conflict {
                        requirement_name: req_name.clone(),
                        delta1: d1.clone(),
                        delta2: d2.clone(),
                        reason,
                    });
                }
            }
        }
    }

    // Check for rename conflicts (renamed from the same source)
    let mut rename_sources: HashMap<String, Vec<&RequirementDelta>> = HashMap::new();
    for delta in all_deltas {
        if let DeltaType::Renamed { from } = &delta.delta_type {
            rename_sources.entry(from.clone()).or_default().push(delta);
        }
    }

    for (from_name, deltas) in rename_sources {
        if deltas.len() < 2 {
            continue;
        }

        // Multiple renames from the same source
        for i in 0..deltas.len() {
            for j in (i + 1)..deltas.len() {
                let d1 = deltas[i];
                let d2 = deltas[j];

                if d1.change_id == d2.change_id {
                    continue;
                }

                conflicts.push(Conflict {
                    requirement_name: from_name.clone(),
                    delta1: d1.clone(),
                    delta2: d2.clone(),
                    reason: ConflictReason::RenameConflict,
                });
            }
        }
    }

    conflicts
}

/// Check if two deltas conflict with each other
fn check_conflict_pair(d1: &RequirementDelta, d2: &RequirementDelta) -> Option<ConflictReason> {
    match (&d1.delta_type, &d2.delta_type) {
        // Both removed: no conflict (same intention)
        (DeltaType::Removed, DeltaType::Removed) => None,

        // One removed, one modified/added: conflict
        (DeltaType::Removed, DeltaType::Modified)
        | (DeltaType::Removed, DeltaType::Added)
        | (DeltaType::Modified, DeltaType::Removed)
        | (DeltaType::Added, DeltaType::Removed) => Some(ConflictReason::RemoveConflict),

        // Both added or both modified: check content
        (DeltaType::Added, DeltaType::Added) | (DeltaType::Modified, DeltaType::Modified) => {
            if d1.content != d2.content {
                Some(ConflictReason::ContentMismatch)
            } else {
                None
            }
        }

        // Added vs Modified: check content
        (DeltaType::Added, DeltaType::Modified) | (DeltaType::Modified, DeltaType::Added) => {
            if d1.content != d2.content {
                Some(ConflictReason::ContentMismatch)
            } else {
                None
            }
        }

        // Renamed: already handled separately
        _ => None,
    }
}

/// Format conflicts for human-readable output
pub fn format_conflicts_human(conflicts: &[Conflict]) -> String {
    if conflicts.is_empty() {
        return "No conflicts detected.".to_string();
    }

    let mut output = String::new();
    output.push_str(&format!("Found {} conflict(s):\n\n", conflicts.len()));

    for (idx, conflict) in conflicts.iter().enumerate() {
        output.push_str(&format!("Conflict {}:\n", idx + 1));
        output.push_str(&format!("  Requirement: {}\n", conflict.requirement_name));
        output.push_str(&format!("  Reason: {:?}\n", conflict.reason));
        output.push_str(&format!(
            "  Change 1: {} ({:?})\n",
            conflict.delta1.change_id, conflict.delta1.delta_type
        ));
        output.push_str(&format!(
            "  Change 2: {} ({:?})\n",
            conflict.delta2.change_id, conflict.delta2.delta_type
        ));
        output.push('\n');
    }

    output
}

/// Format conflicts for JSON output
pub fn format_conflicts_json(conflicts: &[Conflict]) -> Result<String> {
    serde_json::to_string_pretty(conflicts).map_err(OrchestratorError::Json)
}

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

    #[test]
    fn test_parse_spec_file_added_section() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut temp_file = NamedTempFile::new().unwrap();
        let content = r#"# Test Spec

## ADDED Requirements

### Requirement: New Feature

This is a new feature.
Additional line.

### Requirement: Another Feature

Another feature content.
"#;
        temp_file.write_all(content.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let deltas = parse_spec_file(temp_file.path(), "test-change")
            .unwrap()
            .unwrap();

        assert_eq!(deltas.len(), 2);
        assert_eq!(deltas[0].name, "New Feature");
        assert_eq!(deltas[0].delta_type, DeltaType::Added);
        assert!(deltas[0]
            .content
            .as_ref()
            .unwrap()
            .contains("This is a new feature"));

        assert_eq!(deltas[1].name, "Another Feature");
        assert_eq!(deltas[1].delta_type, DeltaType::Added);
    }

    #[test]
    fn test_parse_spec_file_modified_section() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut temp_file = NamedTempFile::new().unwrap();
        let content = r#"# Test Spec

## MODIFIED Requirements

### Requirement: Existing Feature

Modified content.
"#;
        temp_file.write_all(content.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let deltas = parse_spec_file(temp_file.path(), "test-change")
            .unwrap()
            .unwrap();

        assert_eq!(deltas.len(), 1);
        assert_eq!(deltas[0].name, "Existing Feature");
        assert_eq!(deltas[0].delta_type, DeltaType::Modified);
    }

    #[test]
    fn test_parse_spec_file_removed_section() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut temp_file = NamedTempFile::new().unwrap();
        let content = r#"# Test Spec

## REMOVED Requirements

### Requirement: Old Feature
"#;
        temp_file.write_all(content.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let deltas = parse_spec_file(temp_file.path(), "test-change")
            .unwrap()
            .unwrap();

        assert_eq!(deltas.len(), 1);
        assert_eq!(deltas[0].name, "Old Feature");
        assert_eq!(deltas[0].delta_type, DeltaType::Removed);
        assert_eq!(deltas[0].content, None);
    }

    #[test]
    fn test_parse_spec_file_renamed_section() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut temp_file = NamedTempFile::new().unwrap();
        let content = r#"# Test Spec

## RENAMED Requirements

### Requirement: NewName (from OldName)

Renamed content.
"#;
        temp_file.write_all(content.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let deltas = parse_spec_file(temp_file.path(), "test-change")
            .unwrap()
            .unwrap();

        assert_eq!(deltas.len(), 1);
        assert_eq!(deltas[0].name, "NewName");
        assert!(matches!(&deltas[0].delta_type, DeltaType::Renamed { from } if from == "OldName"));
    }

    #[test]
    fn test_parse_spec_file_multiple_sections() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut temp_file = NamedTempFile::new().unwrap();
        let content = r#"# Test Spec

## ADDED Requirements

### Requirement: Feature A

Content A.

## MODIFIED Requirements

### Requirement: Feature B

Content B.

## REMOVED Requirements

### Requirement: Feature C
"#;
        temp_file.write_all(content.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let deltas = parse_spec_file(temp_file.path(), "test-change")
            .unwrap()
            .unwrap();

        assert_eq!(deltas.len(), 3);
        assert_eq!(deltas[0].name, "Feature A");
        assert_eq!(deltas[1].name, "Feature B");
        assert_eq!(deltas[2].name, "Feature C");
    }
}

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

    #[test]
    fn test_content_mismatch_conflict() {
        let d1 = RequirementDelta {
            name: "Test Requirement".to_string(),
            delta_type: DeltaType::Modified,
            content: Some("Content A".to_string()),
            change_id: "change1".to_string(),
            spec_path: PathBuf::from("specs/test/spec.md"),
        };

        let d2 = RequirementDelta {
            name: "Test Requirement".to_string(),
            delta_type: DeltaType::Modified,
            content: Some("Content B".to_string()),
            change_id: "change2".to_string(),
            spec_path: PathBuf::from("specs/test/spec.md"),
        };

        let reason = check_conflict_pair(&d1, &d2);
        assert_eq!(reason, Some(ConflictReason::ContentMismatch));
    }

    #[test]
    fn test_remove_conflict() {
        let d1 = RequirementDelta {
            name: "Test Requirement".to_string(),
            delta_type: DeltaType::Removed,
            content: None,
            change_id: "change1".to_string(),
            spec_path: PathBuf::from("specs/test/spec.md"),
        };

        let d2 = RequirementDelta {
            name: "Test Requirement".to_string(),
            delta_type: DeltaType::Modified,
            content: Some("New content".to_string()),
            change_id: "change2".to_string(),
            spec_path: PathBuf::from("specs/test/spec.md"),
        };

        let reason = check_conflict_pair(&d1, &d2);
        assert_eq!(reason, Some(ConflictReason::RemoveConflict));
    }

    #[test]
    fn test_no_conflict_same_content() {
        let d1 = RequirementDelta {
            name: "Test Requirement".to_string(),
            delta_type: DeltaType::Modified,
            content: Some("Same content".to_string()),
            change_id: "change1".to_string(),
            spec_path: PathBuf::from("specs/test/spec.md"),
        };

        let d2 = RequirementDelta {
            name: "Test Requirement".to_string(),
            delta_type: DeltaType::Modified,
            content: Some("Same content".to_string()),
            change_id: "change2".to_string(),
            spec_path: PathBuf::from("specs/test/spec.md"),
        };

        let reason = check_conflict_pair(&d1, &d2);
        assert_eq!(reason, None);
    }

    #[test]
    fn test_detect_conflicts() {
        let deltas = vec![
            RequirementDelta {
                name: "Req1".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content A".to_string()),
                change_id: "change1".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            RequirementDelta {
                name: "Req1".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content B".to_string()),
                change_id: "change2".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
        ];

        let conflicts = detect_conflicts(&deltas);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].requirement_name, "Req1");
    }

    #[test]
    fn test_detect_rename_conflict() {
        let deltas = vec![
            RequirementDelta {
                name: "NewName1".to_string(),
                delta_type: DeltaType::Renamed {
                    from: "OldName".to_string(),
                },
                content: Some("Content 1".to_string()),
                change_id: "change1".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            RequirementDelta {
                name: "NewName2".to_string(),
                delta_type: DeltaType::Renamed {
                    from: "OldName".to_string(),
                },
                content: Some("Content 2".to_string()),
                change_id: "change2".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
        ];

        let conflicts = detect_conflicts(&deltas);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].requirement_name, "OldName");
        assert!(matches!(
            conflicts[0].reason,
            ConflictReason::RenameConflict
        ));
    }

    #[test]
    fn test_no_conflict_different_requirements() {
        let deltas = vec![
            RequirementDelta {
                name: "Req1".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content A".to_string()),
                change_id: "change1".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            RequirementDelta {
                name: "Req2".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content B".to_string()),
                change_id: "change2".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
        ];

        let conflicts = detect_conflicts(&deltas);
        assert_eq!(conflicts.len(), 0);
    }

    #[test]
    fn test_no_conflict_both_removed() {
        let deltas = vec![
            RequirementDelta {
                name: "Req1".to_string(),
                delta_type: DeltaType::Removed,
                content: None,
                change_id: "change1".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            RequirementDelta {
                name: "Req1".to_string(),
                delta_type: DeltaType::Removed,
                content: None,
                change_id: "change2".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
        ];

        let conflicts = detect_conflicts(&deltas);
        assert_eq!(conflicts.len(), 0);
    }

    #[test]
    fn test_format_conflicts_human() {
        let conflict = Conflict {
            requirement_name: "Test Req".to_string(),
            delta1: RequirementDelta {
                name: "Test Req".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content A".to_string()),
                change_id: "change1".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            delta2: RequirementDelta {
                name: "Test Req".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content B".to_string()),
                change_id: "change2".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            reason: ConflictReason::ContentMismatch,
        };

        let output = format_conflicts_human(&[conflict]);
        assert!(output.contains("Found 1 conflict"));
        assert!(output.contains("Test Req"));
        assert!(output.contains("change1"));
        assert!(output.contains("change2"));
    }

    #[test]
    fn test_format_conflicts_json() {
        let conflict = Conflict {
            requirement_name: "Test Req".to_string(),
            delta1: RequirementDelta {
                name: "Test Req".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content A".to_string()),
                change_id: "change1".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            delta2: RequirementDelta {
                name: "Test Req".to_string(),
                delta_type: DeltaType::Modified,
                content: Some("Content B".to_string()),
                change_id: "change2".to_string(),
                spec_path: PathBuf::from("specs/test/spec.md"),
            },
            reason: ConflictReason::ContentMismatch,
        };

        let json = format_conflicts_json(&[conflict]).unwrap();
        assert!(json.contains("Test Req"));
        assert!(json.contains("change1"));
        assert!(json.contains("ContentMismatch"));
    }
}