cflx 0.6.82

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
#![cfg(feature = "heavy-tests")]

//! End-to-End Tests for OpenSpec Orchestrator
//!
//! These tests verify the complete workflow:
//! 1. Single change apply → archive flow
//! 2. Multiple changes sequential processing
//! 3. Error handling scenarios
//!
//! Note: These tests use mock scripts instead of real openspec/opencode
//! to ensure reproducibility and fast execution.

use std::fs;
use std::io::Write;

use std::path::Path;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

// Global counter for unique script names across parallel tests
static SCRIPT_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Create a mock openspec script that returns predefined output
fn create_mock_openspec(temp_dir: &Path, list_output: &str, archive_behavior: &str) -> String {
    use std::os::unix::fs::OpenOptionsExt;

    let script_id = SCRIPT_COUNTER.fetch_add(1, Ordering::Relaxed);
    let script_path = temp_dir.join(format!("mock_openspec_{}.sh", script_id));

    let script_content = format!(
        r#"#!/bin/bash
case "$1" in
    list)
        echo '{}'
        ;;
    archive)
        {}
        ;;
    *)
        echo "Unknown command: $1" >&2
        exit 1
        ;;
esac
"#,
        list_output.replace('\n', "\\n"),
        archive_behavior
    );

    // Create file with executable permissions from the start
    let mut file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .mode(0o755)
        .open(&script_path)
        .unwrap();
    file.write_all(script_content.as_bytes()).unwrap();
    file.sync_all().unwrap();
    drop(file);

    // Small delay to ensure the file is fully written and executable
    std::thread::sleep(std::time::Duration::from_millis(10));

    script_path.to_string_lossy().to_string()
}

/// Create a mock opencode script that simulates apply/archive commands
fn create_mock_opencode(temp_dir: &Path, behavior: &str) -> String {
    use std::os::unix::fs::OpenOptionsExt;

    let script_id = SCRIPT_COUNTER.fetch_add(1, Ordering::Relaxed);
    let script_path = temp_dir.join(format!("mock_opencode_{}.sh", script_id));

    let script_content = format!(
        r#"#!/bin/bash
# Mock OpenCode for testing
# Command format: mock_opencode.sh run "/openspec-apply change-id"
{}
"#,
        behavior
    );

    // Create file with executable permissions from the start
    let mut file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .mode(0o755)
        .open(&script_path)
        .unwrap();
    file.write_all(script_content.as_bytes()).unwrap();
    file.sync_all().unwrap();
    drop(file);

    // Small delay to ensure the file is fully written and executable
    std::thread::sleep(std::time::Duration::from_millis(10));

    script_path.to_string_lossy().to_string()
}

/// Create test directory structure with openspec changes
fn setup_openspec_test_env(temp_dir: &Path, changes: &[(&str, u32, u32)]) {
    let changes_dir = temp_dir.join("openspec/changes");
    fs::create_dir_all(&changes_dir).unwrap();

    for (change_id, completed, total) in changes {
        let change_dir = changes_dir.join(change_id);
        fs::create_dir_all(&change_dir).unwrap();

        // Create tasks.md
        let mut tasks_content = String::from("# Tasks\n\n");
        for i in 0..*total {
            let checkbox = if i < *completed { "[x]" } else { "[ ]" };
            tasks_content.push_str(&format!("- {} Task {}\n", checkbox, i + 1));
        }
        fs::write(change_dir.join("tasks.md"), tasks_content).unwrap();

        // Create proposal.md
        fs::write(
            change_dir.join("proposal.md"),
            format!("# Proposal: {}\n\nTest proposal content.", change_id),
        )
        .unwrap();
    }
}

// ============================================================================
// Test 1: Single change apply → archive flow
// ============================================================================

#[test]
fn test_single_change_flow_mock_setup() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Set up a single change that is almost complete
    setup_openspec_test_env(temp_path, &[("test-change", 4, 5)]);

    // Create mock openspec that returns the test change
    let list_output = "Changes:\n  test-change     4/5 tasks   1m ago";
    let mock_openspec = create_mock_openspec(temp_path, list_output, "exit 0");

    // Create mock opencode that succeeds
    let mock_opencode = create_mock_opencode(
        temp_path,
        r#"
echo "Mock OpenCode: Received command: $@"
exit 0
"#,
    );

    // Verify mocks are created and executable
    assert!(Path::new(&mock_openspec).exists());
    assert!(Path::new(&mock_opencode).exists());

    // Test that the mock openspec works
    let output = Command::new(&mock_openspec).arg("list").output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("test-change"));
    assert!(stdout.contains("4/5"));
}

#[test]
fn test_single_change_complete_triggers_archive() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Set up a single change that is 100% complete (should trigger archive)
    setup_openspec_test_env(temp_path, &[("complete-change", 5, 5)]);

    // Create mock openspec that returns the complete change
    let list_output = "Changes:\n  complete-change     5/5 tasks   1m ago";
    let mock_openspec = create_mock_openspec(
        temp_path,
        list_output,
        r#"
echo "Archived: $2"
exit 0
"#,
    );

    // Verify archive command works
    let output = Command::new(&mock_openspec)
        .args(["archive", "complete-change", "--yes"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Archived"));
}

// ============================================================================
// Test 2: Multiple changes sequential processing
// ============================================================================

#[test]
fn test_multiple_changes_priority_complete_first() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Set up multiple changes with different progress
    setup_openspec_test_env(
        temp_path,
        &[
            ("change-a", 2, 5),  // 40% complete
            ("change-b", 5, 5),  // 100% complete - should be processed first (archive)
            ("change-c", 1, 10), // 10% complete
        ],
    );

    // Create mock openspec with multiple changes
    let list_output = r#"Changes:
  change-a     2/5 tasks   10m ago
  change-b     5/5 tasks   5m ago
  change-c     1/10 tasks   1h ago"#;

    let mock_openspec = create_mock_openspec(temp_path, list_output, "exit 0");

    // Verify all changes are returned
    let output = Command::new(&mock_openspec).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    assert!(stdout.contains("change-a"));
    assert!(stdout.contains("change-b"));
    assert!(stdout.contains("change-c"));
    assert!(stdout.contains("5/5")); // Complete change
}

#[test]
fn test_multiple_changes_fallback_to_progress_order() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Set up changes where none are complete
    // The orchestrator should fall back to highest progress
    setup_openspec_test_env(
        temp_path,
        &[
            ("low-progress", 1, 10),    // 10%
            ("high-progress", 8, 10),   // 80% - should be selected
            ("medium-progress", 5, 10), // 50%
        ],
    );

    let list_output = r#"Changes:
  low-progress     1/10 tasks   1h ago
  high-progress     8/10 tasks   30m ago
  medium-progress     5/10 tasks   45m ago"#;

    let mock_openspec = create_mock_openspec(temp_path, list_output, "exit 0");

    // Parse and verify the progress ordering
    let output = Command::new(&mock_openspec).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    // Verify all changes are present
    assert!(stdout.contains("low-progress"));
    assert!(stdout.contains("high-progress"));
    assert!(stdout.contains("medium-progress"));
}

// ============================================================================
// Test 3: Error handling scenarios
// ============================================================================

#[test]
fn test_openspec_list_failure() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Create mock openspec that fails on list
    use std::os::unix::fs::OpenOptionsExt;

    let script_id = SCRIPT_COUNTER.fetch_add(1, Ordering::Relaxed);
    let script_path = temp_path.join(format!("mock_openspec_fail_{}.sh", script_id));
    let script_content = r#"#!/bin/bash
echo "Error: openspec not configured" >&2
exit 1
"#;

    // Create file with executable permissions from the start
    let mut file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .mode(0o755)
        .open(&script_path)
        .unwrap();
    file.write_all(script_content.as_bytes()).unwrap();
    file.sync_all().unwrap();
    drop(file);

    // Small delay to ensure the file is fully written and executable
    std::thread::sleep(std::time::Duration::from_millis(10));

    // Verify the failure
    let output = Command::new(&script_path).arg("list").output().unwrap();
    assert!(!output.status.success());

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("Error"));
}

#[test]
fn test_opencode_apply_failure() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Create mock opencode that fails during apply
    let mock_opencode = create_mock_opencode(
        temp_path,
        r#"
echo "Error: Failed to apply change" >&2
exit 1
"#,
    );

    // Verify the failure
    let output = Command::new(&mock_opencode)
        .args(["run", "/openspec-apply test-change"])
        .output()
        .unwrap();
    assert!(!output.status.success());

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("Error"));
}

#[test]
fn test_archive_failure_handling() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Create mock openspec where archive fails
    let list_output = "Changes:\n  failing-archive     5/5 tasks   1m ago";
    let mock_openspec = create_mock_openspec(
        temp_path,
        list_output,
        r#"
echo "Error: Archive directory not writable" >&2
exit 1
"#,
    );

    // Verify archive failure
    let output = Command::new(&mock_openspec)
        .args(["archive", "failing-archive", "--yes"])
        .output()
        .unwrap();
    assert!(!output.status.success());

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("Error"));
}

#[test]
fn test_empty_changes_list() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Create mock openspec that returns empty list
    let mock_openspec = create_mock_openspec(temp_path, "Changes:", "exit 0");

    let output = Command::new(&mock_openspec).arg("list").output().unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should have header but no changes
    assert!(stdout.contains("Changes:"));
    assert!(!stdout.contains("tasks"));
}

#[test]
fn test_partial_failure_continues_with_others() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Set up multiple changes where one will fail
    setup_openspec_test_env(temp_path, &[("will-fail", 3, 5), ("will-succeed", 4, 5)]);

    // Create mock opencode that fails for specific change
    let mock_opencode = create_mock_opencode(
        temp_path,
        r#"
# Check if the command contains 'will-fail'
if echo "$@" | grep -q "will-fail"; then
    echo "Error: Failed to apply will-fail" >&2
    exit 1
fi
echo "Successfully applied"
exit 0
"#,
    );

    // Verify failure for specific change
    let output = Command::new(&mock_opencode)
        .args(["run", "/openspec-apply will-fail"])
        .output()
        .unwrap();
    assert!(!output.status.success());

    // Verify success for other change
    let output = Command::new(&mock_opencode)
        .args(["run", "/openspec-apply will-succeed"])
        .output()
        .unwrap();
    assert!(output.status.success());
}

// ============================================================================
// Integration with file system structure
// ============================================================================

#[test]
fn test_openspec_directory_structure() {
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Create full openspec directory structure
    let project_md = temp_path.join("openspec/project.md");
    fs::create_dir_all(project_md.parent().unwrap()).unwrap();
    fs::write(&project_md, "# Project\n\nTest project.").unwrap();

    // Create changes directory
    let changes_dir = temp_path.join("openspec/changes");
    fs::create_dir_all(&changes_dir).unwrap();

    // Create a test change
    let change_dir = changes_dir.join("test-feature");
    fs::create_dir_all(&change_dir).unwrap();

    fs::write(
        change_dir.join("proposal.md"),
        "# Proposal: Test Feature\n\nAdd a new feature.",
    )
    .unwrap();

    fs::write(
        change_dir.join("design.md"),
        "# Design: Test Feature\n\nImplementation details.",
    )
    .unwrap();

    fs::write(
        change_dir.join("tasks.md"),
        r#"# Tasks

- [x] Task 1: Setup
- [x] Task 2: Implementation
- [ ] Task 3: Testing
- [ ] Task 4: Documentation
"#,
    )
    .unwrap();

    // Verify structure
    assert!(project_md.exists());
    assert!(changes_dir.exists());
    assert!(change_dir.join("proposal.md").exists());
    assert!(change_dir.join("design.md").exists());
    assert!(change_dir.join("tasks.md").exists());

    // Verify task parsing from tasks.md
    let tasks_content = fs::read_to_string(change_dir.join("tasks.md")).unwrap();
    let completed_count = tasks_content.matches("[x]").count();
    let total_count = tasks_content.matches("- [").count();

    assert_eq!(completed_count, 2);
    assert_eq!(total_count, 4);
}

// ============================================================================
// Archive Priority Tests (fix-tui-archive-skip)
// ============================================================================

// OPENSPEC: openspec/specs/cli/spec.md#tui-archive-priority-processing/archive-before-next-apply
#[test]
fn test_archive_priority_complete_changes_first() {
    // Test scenario: Change A at 100%, Change B at 50%
    // Verify A should be archived before B is processed
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Set up multiple changes with different completion states
    setup_openspec_test_env(
        temp_path,
        &[
            ("change-a", 5, 5), // 100% complete - should be archived first
            ("change-b", 2, 4), // 50% complete - should be processed second
        ],
    );

    // Create mock openspec that tracks archive calls
    let archive_log = temp_path.join("archive_log.txt");
    let list_output = r#"Changes:
  change-a     5/5 tasks   5m ago
  change-b     2/4 tasks   10m ago"#;

    use std::os::unix::fs::OpenOptionsExt;

    let script_id = SCRIPT_COUNTER.fetch_add(1, Ordering::Relaxed);
    let script_path = temp_path.join(format!("mock_openspec_priority_{}.sh", script_id));
    let script_content = format!(
        r#"#!/bin/bash
case "$1" in
    list)
        echo '{}'
        ;;
    archive)
        echo "$(date +%s%N) archived: $2" >> {}
        echo "Archived: $2"
        ;;
    *)
        echo "Unknown command: $1" >&2
        exit 1
        ;;
esac
"#,
        list_output.replace('\n', "\\n"),
        archive_log.display()
    );

    // Create file with executable permissions from the start
    let mut file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .mode(0o755)
        .open(&script_path)
        .unwrap();
    file.write_all(script_content.as_bytes()).unwrap();
    file.sync_all().unwrap();
    drop(file);

    // Small delay to ensure the file is fully written and executable
    std::thread::sleep(std::time::Duration::from_millis(10));

    // Verify the complete change (5/5) appears in list
    let output = Command::new(&script_path).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("change-a"));
    assert!(stdout.contains("5/5")); // Complete change

    // Verify archive command works
    let output = Command::new(&script_path)
        .args(["archive", "change-a", "--yes"])
        .output()
        .unwrap();
    assert!(output.status.success());
}

// OPENSPEC: openspec/specs/cli/spec.md#tui-archive-priority-processing/multiple-complete-changes
#[test]
fn test_archive_priority_multiple_complete_changes() {
    // Test scenario: Multiple changes at 100% completion
    // Verify all complete changes are archived before any apply happens
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    setup_openspec_test_env(
        temp_path,
        &[
            ("complete-1", 3, 3),  // 100% complete
            ("complete-2", 5, 5),  // 100% complete
            ("incomplete", 1, 10), // 10% - needs apply
        ],
    );

    let list_output = r#"Changes:
  complete-1     3/3 tasks   5m ago
  complete-2     5/5 tasks   3m ago
  incomplete     1/10 tasks   15m ago"#;

    let mock_openspec = create_mock_openspec(
        temp_path,
        list_output,
        r#"
echo "Archived: $2"
exit 0
"#,
    );

    // Verify both complete changes appear in list
    let output = Command::new(&mock_openspec).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    // Count complete changes
    assert!(stdout.contains("3/3")); // complete-1
    assert!(stdout.contains("5/5")); // complete-2
    assert!(stdout.contains("1/10")); // incomplete
}

// OPENSPEC: openspec/specs/cli/spec.md#remove-retry-based-completion-check/completion-detected-on-next-iteration
#[test]
fn test_mid_apply_completion_detection() {
    // Test scenario: Change becomes 100% during another apply
    // Verify complete change is detected and archived on next loop iteration
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    // Initial state: both incomplete
    setup_openspec_test_env(
        temp_path,
        &[
            ("change-a", 4, 5), // 80% - will complete during apply
            ("change-b", 2, 5), // 40% - being applied
        ],
    );

    // Create stateful mock that simulates change-a completing mid-process
    let state_file = temp_path.join("state.txt");
    fs::write(&state_file, "0").unwrap(); // Initial state

    use std::os::unix::fs::OpenOptionsExt;

    let script_id = SCRIPT_COUNTER.fetch_add(1, Ordering::Relaxed);
    let script_path = temp_path.join(format!("mock_openspec_midapply_{}.sh", script_id));
    let script_content = format!(
        r#"#!/bin/bash
STATE_FILE="{}"
STATE=$(cat "$STATE_FILE" 2>/dev/null || echo "0")

case "$1" in
    list)
        if [ "$STATE" = "0" ]; then
            echo 'Changes:
  change-a     4/5 tasks   5m ago
  change-b     2/5 tasks   10m ago'
        else
            # After first apply, change-a is complete
            echo 'Changes:
  change-a     5/5 tasks   5m ago
  change-b     3/5 tasks   10m ago'
        fi
        ;;
    archive)
        echo "Archived: $2"
        ;;
    increment)
        echo "1" > "$STATE_FILE"
        echo "State incremented"
        ;;
    *)
        echo "Unknown command: $1" >&2
        exit 1
        ;;
esac
"#,
        state_file.display()
    );

    // Create file with executable permissions from the start
    let mut file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .mode(0o755)
        .open(&script_path)
        .unwrap();
    file.write_all(script_content.as_bytes()).unwrap();
    file.sync_all().unwrap();
    drop(file);

    // Small delay to ensure the file is fully written and executable
    std::thread::sleep(std::time::Duration::from_millis(10));

    // Initial list - both incomplete
    let output = Command::new(&script_path).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("4/5")); // change-a incomplete
    assert!(stdout.contains("2/5")); // change-b incomplete

    // Simulate apply completing (increment state)
    let output = Command::new(&script_path)
        .arg("increment")
        .output()
        .unwrap();
    assert!(output.status.success());

    // After state change - change-a is now complete
    let output = Command::new(&script_path).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("5/5")); // change-a now complete
    assert!(stdout.contains("3/5")); // change-b progressed

    // Verify archive works for newly complete change
    let output = Command::new(&script_path)
        .args(["archive", "change-a", "--yes"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Archived"));
}

#[test]
fn test_no_complete_changes_fallback() {
    // Test scenario: No changes at 100% completion
    // Verify orchestrator selects highest progress change for apply
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_path = temp_dir.path();

    setup_openspec_test_env(
        temp_path,
        &[
            ("low", 1, 10),    // 10%
            ("medium", 5, 10), // 50%
            ("high", 8, 10),   // 80% - should be selected
        ],
    );

    let list_output = r#"Changes:
  low     1/10 tasks   1h ago
  medium     5/10 tasks   30m ago
  high     8/10 tasks   15m ago"#;

    let mock_openspec = create_mock_openspec(temp_path, list_output, "exit 0");

    let output = Command::new(&mock_openspec).arg("list").output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    // All changes present, none complete
    assert!(stdout.contains("1/10"));
    assert!(stdout.contains("5/10"));
    assert!(stdout.contains("8/10"));
    assert!(!stdout.contains("/10 tasks") || !stdout.contains("10/10")); // No complete changes
}

// Git worktree / real external boundary E2E tests were moved to
// `tests/e2e_git_worktree_tests.rs` so this file remains focused on
// mock-driven orchestrator flow checks.