xchecker 1.2.0

Spec pipeline with receipts and gateable JSON contracts
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
//! Integration Tests for Full Workflows
//!
//! **WHITE-BOX TEST**: This test uses internal module APIs (`orchestrator::{OrchestratorConfig,
//! PhaseOrchestrator}`, `types::{PhaseId, Receipt}`) and may break with internal refactors.
//! These tests are intentionally white-box to validate internal implementation details.
//! Prefer `OrchestratorHandle` for new tests. See FR-TEST-4 for white-box test policy.
//!
//! This module tests complete spec generation flows end-to-end, verifying
//! resume scenarios, failure recovery, and determinism with identical inputs.
//!
//! Requirements tested:
//! - R1.1: Complete spec generation flows (Requirements → Design → Tasks)
//! - R2.2: Deterministic outputs with identical inputs
//! - R2.5: Structure determinism for canonicalized outputs
//! - R4.2: Resume scenarios and failure recovery

use anyhow::Result;
use serial_test::serial;
use std::path::PathBuf;
use tempfile::TempDir;

use xchecker::orchestrator::{OrchestratorConfig, PhaseOrchestrator};
use xchecker::types::{PhaseId, Receipt};

#[allow(clippy::duplicate_mod)]
#[path = "test_support/mod.rs"]
mod test_support;

fn normalize_core_yaml_for_determinism(content: &str) -> String {
    content
        .lines()
        .map(|line| {
            if line.starts_with("spec_id:") {
                "spec_id: \"deterministic\"".to_string()
            } else if line.starts_with("generated_at:") {
                "generated_at: \"1970-01-01T00:00:00Z\"".to_string()
            } else if line.starts_with("# Core requirements data for spec") {
                "# Core requirements data for spec deterministic".to_string()
            } else {
                line.to_string()
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Test environment setup for full workflow validation
///
/// Note: Field order matters for drop semantics. Fields drop in declaration order,
/// so `_cwd_guard` must be declared first to restore CWD before `temp_dir` is deleted.
struct WorkflowTestEnvironment {
    #[allow(dead_code)]
    _cwd_guard: test_support::CwdGuard,
    temp_dir: TempDir,
    orchestrator: PhaseOrchestrator,
    spec_id: String,
}

impl WorkflowTestEnvironment {
    fn new(test_name: &str) -> Result<Self> {
        let temp_dir = TempDir::new()?;
        let cwd_guard = test_support::CwdGuard::new(temp_dir.path())?;

        // Create .xchecker directory structure
        std::fs::create_dir_all(temp_dir.path().join(".xchecker/specs"))?;

        let spec_id = format!("workflow-{test_name}");
        let orchestrator = PhaseOrchestrator::new(&spec_id)?;

        Ok(Self {
            _cwd_guard: cwd_guard,
            temp_dir,
            orchestrator,
            spec_id,
        })
    }

    fn spec_dir(&self) -> PathBuf {
        self.temp_dir
            .path()
            .join(".xchecker/specs")
            .join(&self.spec_id)
    }

    fn create_success_config(&self) -> OrchestratorConfig {
        OrchestratorConfig {
            dry_run: false,
            config: {
                let mut map = std::collections::HashMap::new();
                map.insert("runner_mode".to_string(), "native".to_string());
                let stub_path = test_support::claude_stub_path()
                    .expect("claude-stub path is required for workflow tests");
                map.insert("claude_cli_path".to_string(), stub_path);
                map.insert("claude_scenario".to_string(), "success".to_string());
                map.insert("verbose".to_string(), "true".to_string());
                map
            },
            full_config: None,
            selectors: None,
            strict_validation: false,
            redactor: Default::default(),
            hooks: None,
        }
    }

    fn create_error_config(&self) -> OrchestratorConfig {
        OrchestratorConfig {
            dry_run: false,
            config: {
                let mut map = std::collections::HashMap::new();
                map.insert("runner_mode".to_string(), "native".to_string());
                let stub_path = test_support::claude_stub_path()
                    .expect("claude-stub path is required for workflow tests");
                map.insert("claude_cli_path".to_string(), stub_path);
                map.insert("claude_scenario".to_string(), "error".to_string());
                map
            },
            full_config: None,
            selectors: None,
            strict_validation: false,
            redactor: Default::default(),
            hooks: None,
        }
    }
}

/// Test 1: Complete spec generation flow (Requirements → Design → Tasks)
/// Validates R1.1 requirements for full workflow execution
#[tokio::test]
#[serial]
#[ignore = "requires_claude_stub"]
async fn test_complete_spec_generation_flow() -> Result<()> {
    let env = WorkflowTestEnvironment::new("complete-flow")?;
    let config = env.create_success_config();

    // Execute Requirements phase
    println!("Executing Requirements phase...");
    let req_result = env.orchestrator.execute_requirements_phase(&config).await?;
    assert!(req_result.success, "Requirements phase should succeed");
    assert_eq!(req_result.phase, PhaseId::Requirements);
    assert_eq!(
        req_result.artifact_paths.len(),
        2,
        "Should create .md and .core.yaml"
    );

    // Verify Requirements artifacts exist and have content
    let artifacts_dir = env.spec_dir().join("artifacts");
    let req_md = artifacts_dir.join("00-requirements.md");
    let req_yaml = artifacts_dir.join("00-requirements.core.yaml");

    assert!(req_md.exists(), "Requirements markdown should exist");
    assert!(req_yaml.exists(), "Requirements YAML should exist");

    let req_content = std::fs::read_to_string(&req_md)?;
    assert!(
        req_content.contains("# Requirements Document"),
        "Should have proper structure"
    );
    assert!(
        req_content.contains("**User Story:**"),
        "Should have user stories"
    );
    assert!(req_content.contains("WHEN"), "Should have EARS format");

    // Execute Design phase
    println!("Executing Design phase...");
    let design_result = env.orchestrator.execute_design_phase(&config).await?;
    assert!(design_result.success, "Design phase should succeed");
    assert_eq!(design_result.phase, PhaseId::Design);
    assert_eq!(
        design_result.artifact_paths.len(),
        2,
        "Should create .md and .core.yaml"
    );

    // Verify Design artifacts exist and have content
    let design_md = artifacts_dir.join("10-design.md");
    let design_yaml = artifacts_dir.join("10-design.core.yaml");

    assert!(design_md.exists(), "Design markdown should exist");
    assert!(design_yaml.exists(), "Design YAML should exist");

    let design_content = std::fs::read_to_string(&design_md)?;
    assert!(
        design_content.contains("# Design Document"),
        "Should have proper structure"
    );
    assert!(
        design_content.contains("## Architecture"),
        "Should have architecture section"
    );

    // Execute Tasks phase
    println!("Executing Tasks phase...");
    let tasks_result = env.orchestrator.execute_tasks_phase(&config).await?;
    assert!(tasks_result.success, "Tasks phase should succeed");
    assert_eq!(tasks_result.phase, PhaseId::Tasks);
    assert_eq!(
        tasks_result.artifact_paths.len(),
        2,
        "Should create .md and .core.yaml"
    );

    // Verify Tasks artifacts exist and have content
    let tasks_md = artifacts_dir.join("20-tasks.md");
    let tasks_yaml = artifacts_dir.join("20-tasks.core.yaml");

    assert!(tasks_md.exists(), "Tasks markdown should exist");
    assert!(tasks_yaml.exists(), "Tasks YAML should exist");

    let tasks_content = std::fs::read_to_string(&tasks_md)?;
    assert!(
        tasks_content.contains("# Implementation Plan"),
        "Should have proper structure"
    );
    assert!(
        tasks_content.contains("- [ ]"),
        "Should have task checkboxes"
    );

    // Verify all receipts were created
    let receipts_dir = env.spec_dir().join("receipts");
    assert!(receipts_dir.exists(), "Receipts directory should exist");

    let receipt_files: Vec<_> = std::fs::read_dir(&receipts_dir)?
        .filter_map(std::result::Result::ok)
        .filter(|entry| entry.path().extension().is_some_and(|ext| ext == "json"))
        .collect();

    assert_eq!(
        receipt_files.len(),
        3,
        "Should have 3 receipts (one per phase)"
    );

    // Verify receipt contents
    for receipt_file in receipt_files {
        let receipt_content = std::fs::read_to_string(receipt_file.path())?;
        let receipt: Receipt = serde_json::from_str(&receipt_content)?;

        assert_eq!(receipt.spec_id, env.spec_id);
        assert_eq!(receipt.exit_code, 0);
        assert!(!receipt.xchecker_version.is_empty());
        assert!(!receipt.claude_cli_version.is_empty());
        assert!(!receipt.model_full_name.is_empty());
        assert!(!receipt.outputs.is_empty());
    }

    println!("✓ Complete spec generation flow test passed");
    Ok(())
}

/// Test 2: Resume scenarios and failure recovery
/// Validates R4.2 requirements for phase resumption and failure handling
#[tokio::test]
#[serial]
#[ignore = "requires_claude_stub"]
async fn test_resume_scenarios_and_failure_recovery() -> Result<()> {
    let env = WorkflowTestEnvironment::new("resume-recovery")?;

    // First, execute Requirements phase successfully
    let success_config = env.create_success_config();
    let req_result = env
        .orchestrator
        .execute_requirements_phase(&success_config)
        .await?;
    assert!(req_result.success, "Requirements phase should succeed");

    // Now try Design phase with error config to simulate failure
    let error_config = env.create_error_config();
    let design_result = env.orchestrator.execute_design_phase(&error_config).await?;
    assert!(
        !design_result.success,
        "Design phase should fail with error config"
    );
    assert_ne!(design_result.exit_code, 0, "Should have non-zero exit code");

    // Verify partial artifact was created (R4.3)
    let artifacts_dir = env.spec_dir().join("artifacts");
    let partial_files: Vec<_> = std::fs::read_dir(&artifacts_dir)?
        .filter_map(std::result::Result::ok)
        .filter(|entry| {
            entry
                .path()
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|name| name.contains(".partial."))
        })
        .collect();

    assert!(
        !partial_files.is_empty(),
        "Should have created partial artifact on failure"
    );

    // Verify failure receipt was created with proper error information
    assert!(
        design_result.receipt_path.is_some(),
        "Should create receipt even on failure"
    );
    let receipt_path = design_result.receipt_path.unwrap();
    let receipt_content = std::fs::read_to_string(&receipt_path)?;
    let receipt: Receipt = serde_json::from_str(&receipt_content)?;

    assert_ne!(
        receipt.exit_code, 0,
        "Receipt should record failure exit code"
    );
    assert!(
        receipt.stderr_tail.is_some(),
        "Receipt should capture stderr"
    );
    assert!(!receipt.warnings.is_empty(), "Receipt should have warnings");

    // Now resume Design phase with success config
    println!("Resuming Design phase after failure...");
    let resume_result = env
        .orchestrator
        .resume_from_phase(PhaseId::Design, &success_config)
        .await?;
    assert!(resume_result.success, "Resume should succeed");
    assert_eq!(resume_result.phase, PhaseId::Design);

    // Verify Design artifacts were created successfully
    let design_md = artifacts_dir.join("10-design.md");
    let design_yaml = artifacts_dir.join("10-design.core.yaml");

    assert!(
        design_md.exists(),
        "Design markdown should exist after resume"
    );
    assert!(
        design_yaml.exists(),
        "Design YAML should exist after resume"
    );

    // Verify partial artifacts were cleaned up (R4.5)
    let _remaining_partials: Vec<_> = std::fs::read_dir(&artifacts_dir)?
        .filter_map(std::result::Result::ok)
        .filter(|entry| {
            entry
                .path()
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|name| name.contains(".partial."))
        })
        .collect();

    // Note: Partial cleanup happens on success, so there might still be partials
    // from the failed run, but the successful run should have created final artifacts

    println!("✓ Resume scenarios and failure recovery test passed");
    Ok(())
}

/// Test 3: Determinism with identical inputs producing same outputs
/// Validates R2.2 and R2.5 requirements for deterministic behavior
#[tokio::test]
#[serial]
#[ignore = "requires_claude_stub"]
async fn test_determinism_with_identical_inputs() -> Result<()> {
    // Create two separate environments with identical configurations
    let env1 = WorkflowTestEnvironment::new("determinism-1")?;
    let env2 = WorkflowTestEnvironment::new("determinism-2")?;

    let config = OrchestratorConfig {
        dry_run: true, // Use dry-run for deterministic simulation
        config: {
            let mut map = std::collections::HashMap::new();
            map.insert("model".to_string(), "haiku".to_string());
            map.insert("verbose".to_string(), "false".to_string());
            map
        },
        full_config: None,
        selectors: None,
        strict_validation: false,
        redactor: Default::default(),
        hooks: None,
    };

    // Execute Requirements phase in both environments
    let req_result1 = env1
        .orchestrator
        .execute_requirements_phase(&config)
        .await?;
    let req_result2 = env2
        .orchestrator
        .execute_requirements_phase(&config)
        .await?;

    assert!(
        req_result1.success && req_result2.success,
        "Both executions should succeed"
    );

    // Read the generated artifacts from both environments
    let artifacts1_dir = env1.spec_dir().join("artifacts");
    let artifacts2_dir = env2.spec_dir().join("artifacts");

    let req_md1 = std::fs::read_to_string(artifacts1_dir.join("00-requirements.md"))?;
    let req_md2 = std::fs::read_to_string(artifacts2_dir.join("00-requirements.md"))?;

    let req_yaml1 = std::fs::read_to_string(artifacts1_dir.join("00-requirements.core.yaml"))?;
    let req_yaml2 = std::fs::read_to_string(artifacts2_dir.join("00-requirements.core.yaml"))?;
    let req_yaml1 = normalize_core_yaml_for_determinism(&req_yaml1);
    let req_yaml2 = normalize_core_yaml_for_determinism(&req_yaml2);

    // In dry-run mode with identical configs, outputs should be identical
    assert_eq!(
        req_md1, req_md2,
        "Markdown outputs should be identical with same inputs"
    );
    assert_eq!(
        req_yaml1, req_yaml2,
        "YAML outputs should be identical with same inputs"
    );

    // Verify canonicalized hashes are identical (R2.5)
    let canonicalizer = xchecker::canonicalization::Canonicalizer::new();

    let hash1_md =
        canonicalizer.hash_canonicalized(&req_md1, xchecker::types::FileType::Markdown)?;
    let hash2_md =
        canonicalizer.hash_canonicalized(&req_md2, xchecker::types::FileType::Markdown)?;

    let hash1_yaml =
        canonicalizer.hash_canonicalized(&req_yaml1, xchecker::types::FileType::Yaml)?;
    let hash2_yaml =
        canonicalizer.hash_canonicalized(&req_yaml2, xchecker::types::FileType::Yaml)?;

    assert_eq!(
        hash1_md, hash2_md,
        "Canonicalized markdown hashes should be identical"
    );
    assert_eq!(
        hash1_yaml, hash2_yaml,
        "Canonicalized YAML hashes should be identical"
    );

    // Verify receipts have identical packet hashes and model information
    let receipt1_path = req_result1.receipt_path.unwrap();
    let receipt2_path = req_result2.receipt_path.unwrap();

    let receipt1_content = std::fs::read_to_string(&receipt1_path)?;
    let receipt2_content = std::fs::read_to_string(&receipt2_path)?;

    let receipt1: Receipt = serde_json::from_str(&receipt1_content)?;
    let receipt2: Receipt = serde_json::from_str(&receipt2_content)?;

    // Model and configuration should be identical
    assert_eq!(
        receipt1.model_full_name, receipt2.model_full_name,
        "Model names should be identical"
    );
    assert_eq!(
        receipt1.canonicalization_version, receipt2.canonicalization_version,
        "Canonicalization versions should be identical"
    );

    // Output hashes should be identical (R2.2)
    let outputs1: Vec<_> = receipt1
        .outputs
        .iter()
        .filter(|output| !output.path.ends_with(".core.yaml"))
        .collect();
    let outputs2: Vec<_> = receipt2
        .outputs
        .iter()
        .filter(|output| !output.path.ends_with(".core.yaml"))
        .collect();

    assert_eq!(
        outputs1.len(),
        outputs2.len(),
        "Should have same number of deterministic outputs"
    );

    for (output1, output2) in outputs1.iter().zip(outputs2.iter()) {
        assert_eq!(output1.path, output2.path, "Output paths should match");
        assert_eq!(
            output1.blake3_canonicalized, output2.blake3_canonicalized,
            "Canonicalized hashes should be identical for same inputs"
        );
    }

    println!("✓ Determinism with identical inputs test passed");
    Ok(())
}

/// Test 4: Multi-phase workflow with dependency validation
/// Validates proper phase dependency checking and artifact propagation
#[tokio::test]
#[serial]
#[ignore = "requires_claude_stub"]
async fn test_multi_phase_workflow_with_dependencies() -> Result<()> {
    let env = WorkflowTestEnvironment::new("dependencies")?;
    let config = env.create_success_config();

    // Try to execute Design phase without Requirements (should fail)
    let design_result = env.orchestrator.execute_design_phase(&config).await;
    assert!(
        design_result.is_err(),
        "Design phase should fail without Requirements dependency"
    );

    // Execute Requirements phase first
    let req_result = env.orchestrator.execute_requirements_phase(&config).await?;
    assert!(req_result.success, "Requirements phase should succeed");

    // Now Design phase should succeed
    let design_result = env.orchestrator.execute_design_phase(&config).await?;
    assert!(
        design_result.success,
        "Design phase should succeed after Requirements"
    );

    // Try Tasks phase without Design (should fail if we had a fresh environment)
    // But since we have Design now, it should succeed
    let tasks_result = env.orchestrator.execute_tasks_phase(&config).await?;
    assert!(
        tasks_result.success,
        "Tasks phase should succeed after Design"
    );

    // Verify all artifacts exist in proper sequence
    let artifacts_dir = env.spec_dir().join("artifacts");

    assert!(
        artifacts_dir.join("00-requirements.md").exists(),
        "Requirements artifacts should exist"
    );
    assert!(
        artifacts_dir.join("00-requirements.core.yaml").exists(),
        "Requirements YAML should exist"
    );

    assert!(
        artifacts_dir.join("10-design.md").exists(),
        "Design artifacts should exist"
    );
    assert!(
        artifacts_dir.join("10-design.core.yaml").exists(),
        "Design YAML should exist"
    );

    assert!(
        artifacts_dir.join("20-tasks.md").exists(),
        "Tasks artifacts should exist"
    );
    assert!(
        artifacts_dir.join("20-tasks.core.yaml").exists(),
        "Tasks YAML should exist"
    );

    // Verify receipts show proper phase sequence
    let receipts_dir = env.spec_dir().join("receipts");
    let receipt_files: Vec<_> = std::fs::read_dir(&receipts_dir)?
        .filter_map(std::result::Result::ok)
        .filter(|entry| entry.path().extension().is_some_and(|ext| ext == "json"))
        .collect();

    assert_eq!(
        receipt_files.len(),
        3,
        "Should have receipts for all three phases"
    );

    // Read and verify receipt timestamps show proper ordering
    let mut receipts = Vec::new();
    for receipt_file in receipt_files {
        let receipt_content = std::fs::read_to_string(receipt_file.path())?;
        let receipt: Receipt = serde_json::from_str(&receipt_content)?;
        receipts.push(receipt);
    }

    // Sort by emitted_at timestamp
    receipts.sort_by(|a, b| a.emitted_at.cmp(&b.emitted_at));

    assert_eq!(
        receipts[0].phase, "requirements",
        "First receipt should be requirements"
    );
    assert_eq!(
        receipts[1].phase, "design",
        "Second receipt should be design"
    );
    assert_eq!(receipts[2].phase, "tasks", "Third receipt should be tasks");

    println!("✓ Multi-phase workflow with dependencies test passed");
    Ok(())
}

/// Test 5: Artifact content validation and structure verification
/// Validates that generated artifacts have proper structure and content
#[tokio::test]
#[serial]
#[ignore = "requires_claude_stub"]
async fn test_artifact_content_validation() -> Result<()> {
    let env = WorkflowTestEnvironment::new("content-validation")?;
    let config = env.create_success_config();

    // Execute complete workflow
    let req_result = env.orchestrator.execute_requirements_phase(&config).await?;
    let design_result = env.orchestrator.execute_design_phase(&config).await?;
    let tasks_result = env.orchestrator.execute_tasks_phase(&config).await?;

    assert!(
        req_result.success && design_result.success && tasks_result.success,
        "All phases should succeed"
    );

    let artifacts_dir = env.spec_dir().join("artifacts");

    // Validate Requirements artifacts
    let req_md = std::fs::read_to_string(artifacts_dir.join("00-requirements.md"))?;
    assert!(
        req_md.contains("# Requirements Document"),
        "Requirements should have proper title"
    );
    assert!(
        req_md.contains("## Introduction"),
        "Requirements should have introduction"
    );
    assert!(
        req_md.contains("## Requirements"),
        "Requirements should have requirements section"
    );
    assert!(
        req_md.contains("**User Story:**"),
        "Requirements should have user stories"
    );
    assert!(
        req_md.contains("#### Acceptance Criteria"),
        "Requirements should have acceptance criteria"
    );
    assert!(
        req_md.contains("WHEN") && req_md.contains("THEN") && req_md.contains("SHALL"),
        "Requirements should use EARS format"
    );

    let req_yaml = std::fs::read_to_string(artifacts_dir.join("00-requirements.core.yaml"))?;
    assert!(
        !req_yaml.is_empty(),
        "Requirements YAML should not be empty"
    );

    // Validate Design artifacts
    let design_md = std::fs::read_to_string(artifacts_dir.join("10-design.md"))?;
    assert!(
        design_md.contains("# Design Document"),
        "Design should have proper title"
    );
    assert!(
        design_md.contains("## Overview"),
        "Design should have overview"
    );
    assert!(
        design_md.contains("## Architecture"),
        "Design should have architecture section"
    );

    let design_yaml = std::fs::read_to_string(artifacts_dir.join("10-design.core.yaml"))?;
    assert!(!design_yaml.is_empty(), "Design YAML should not be empty");

    // Validate Tasks artifacts
    let tasks_md = std::fs::read_to_string(artifacts_dir.join("20-tasks.md"))?;
    assert!(
        tasks_md.contains("# Implementation Plan"),
        "Tasks should have proper title"
    );
    assert!(tasks_md.contains("- [ ]"), "Tasks should have checkboxes");
    assert!(
        tasks_md.contains("Milestone"),
        "Tasks should have milestones"
    );

    let tasks_yaml = std::fs::read_to_string(artifacts_dir.join("20-tasks.core.yaml"))?;
    assert!(!tasks_yaml.is_empty(), "Tasks YAML should not be empty");

    // Validate YAML structure can be parsed
    let _req_parsed: serde_yaml::Value = serde_yaml::from_str(&req_yaml)?;
    let _design_parsed: serde_yaml::Value = serde_yaml::from_str(&design_yaml)?;
    let _tasks_parsed: serde_yaml::Value = serde_yaml::from_str(&tasks_yaml)?;

    println!("✓ Artifact content validation test passed");
    Ok(())
}

/// Test 6: Error propagation and recovery across phases
/// Validates proper error handling in multi-phase scenarios
#[tokio::test]
#[serial]
#[ignore = "requires_claude_stub"]
async fn test_error_propagation_and_recovery() -> Result<()> {
    if !test_support::should_run_e2e() {
        eprintln!("(skipped) E2E test requires Claude CLI or XCHECKER_E2E=1");
        return Ok(());
    }

    let env = WorkflowTestEnvironment::new("error-propagation")?;

    // Execute Requirements successfully
    let success_config = env.create_success_config();
    let req_result = env
        .orchestrator
        .execute_requirements_phase(&success_config)
        .await?;
    assert!(req_result.success, "Requirements should succeed");

    // Execute Design with error config
    let error_config = env.create_error_config();
    let design_result = env.orchestrator.execute_design_phase(&error_config).await?;
    assert!(
        !design_result.success,
        "Design should fail with error config"
    );

    // Try Tasks phase - should fail due to missing Design dependency
    let tasks_result = env.orchestrator.execute_tasks_phase(&success_config).await;
    assert!(
        tasks_result.is_err(),
        "Tasks should fail without successful Design"
    );

    // Recover Design phase
    let design_recovery = env
        .orchestrator
        .resume_from_phase(PhaseId::Design, &success_config)
        .await?;
    assert!(design_recovery.success, "Design recovery should succeed");

    // Now Tasks should succeed
    let tasks_result = env
        .orchestrator
        .execute_tasks_phase(&success_config)
        .await?;
    assert!(
        tasks_result.success,
        "Tasks should succeed after Design recovery"
    );

    // Verify final state has all successful artifacts
    let artifacts_dir = env.spec_dir().join("artifacts");

    assert!(
        artifacts_dir.join("00-requirements.md").exists(),
        "Requirements should exist"
    );
    assert!(
        artifacts_dir.join("10-design.md").exists(),
        "Design should exist after recovery"
    );
    assert!(
        artifacts_dir.join("20-tasks.md").exists(),
        "Tasks should exist after recovery"
    );

    // Verify receipts show the error and recovery pattern
    let receipts_dir = env.spec_dir().join("receipts");
    let receipt_files: Vec<_> = std::fs::read_dir(&receipts_dir)?
        .filter_map(std::result::Result::ok)
        .filter(|entry| entry.path().extension().is_some_and(|ext| ext == "json"))
        .collect();

    // Should have receipts for: Requirements (success), Design (failure), Design (success), Tasks (success)
    assert!(
        receipt_files.len() >= 3,
        "Should have multiple receipts including failure and recovery"
    );

    println!("✓ Error propagation and recovery test passed");
    Ok(())
}

/// Integration test runner for full workflow validation
/// This function provides a summary of workflow integration test coverage.
///
/// **Note**: Individual tests are run via `cargo test --test integration_full_workflows`.
/// This runner is used for documentation and summary purposes when called from
/// the comprehensive test suite.
pub async fn run_full_workflow_validation() -> Result<()> {
    println!("🚀 Full workflow tests require claude-stub binary.");
    println!("   Run with: cargo test --test integration_full_workflows -- --include-ignored");
    println!();
    println!("Full Workflow Requirements Coverage:");
    println!("  ✓ R1.1: Complete spec generation flows (Requirements → Design → Tasks)");
    println!("  ✓ R2.2: Deterministic outputs with identical inputs");
    println!("  ✓ R2.5: Structure determinism for canonicalized outputs");
    println!("  ✓ R4.2: Resume scenarios and failure recovery");
    println!();
    println!("Key Features Covered:");
    println!("  ✓ End-to-end workflow execution with proper phase dependencies");
    println!("  ✓ Resume capability from failed phases with partial artifact handling");
    println!("  ✓ Deterministic behavior with identical inputs producing same outputs");
    println!("  ✓ Proper error propagation and recovery across multiple phases");
    println!("  ✓ Artifact content validation and structure verification");
    println!("  ✓ Receipt generation and audit trail maintenance");

    Ok(())
}