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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//! Comprehensive tests for status reporting (FR-STA)
//!
//! **WHITE-BOX TEST**: This test uses internal module APIs (`artifact::ArtifactManager`,
//! `receipt::ReceiptManager`, `status::StatusManager`, `types::{...}`) and may break with
//! internal refactors. These tests are intentionally white-box to validate internal
//! implementation details. See FR-TEST-4 for white-box test policy.
//!
//! This module tests all aspects of status generation and reporting:
//! - FR-STA-001: Status generation with effective_config
//! - FR-STA-002: Source attribution (cli/config/default)
//! - FR-STA-003: Artifact enumeration with blake3 hashes
//! - FR-STA-004: Fresh spec (no prior receipts)
//! - FR-STA-005: Lock drift reporting
//! - JCS emission for status outputs

use anyhow::Result;
use std::collections::BTreeMap;
use std::collections::HashMap;
use xchecker::artifact::ArtifactManager;
use xchecker::paths;
use xchecker::receipt::ReceiptManager;
use xchecker::status::status::StatusManager;
use xchecker::types::{ConfigSource, DriftPair, FileHash, LockDrift, PacketEvidence, PhaseId};

/// Helper to generate status using the internal method
fn generate_status(
    artifact_manager: &ArtifactManager,
    receipt_manager: &ReceiptManager,
    effective_config: BTreeMap<String, (String, String)>,
    lock_drift: Option<LockDrift>,
) -> Result<xchecker::types::StatusOutput> {
    StatusManager::generate_status_internal(
        artifact_manager,
        receipt_manager,
        effective_config,
        lock_drift,
        None, // pending_fixups
        None, // secret_redactor
    )
}

/// Helper to create test managers with isolated environment
fn create_test_managers(spec_id: &str) -> (ArtifactManager, ReceiptManager, tempfile::TempDir) {
    let temp_dir = paths::with_isolated_home();
    let artifact_manager = ArtifactManager::new(spec_id).unwrap();
    let base_path = paths::spec_root(spec_id);
    let receipt_manager = ReceiptManager::new(&base_path);
    (artifact_manager, receipt_manager, temp_dir)
}

/// Helper to create a test receipt
fn create_test_receipt(
    receipt_manager: &ReceiptManager,
    spec_id: &str,
    phase: PhaseId,
    runner: &str,
    runner_distro: Option<String>,
) {
    let packet = PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let receipt = receipt_manager.create_receipt(
        spec_id,
        phase,
        0,
        vec![],
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,          // stderr_tail
        None,          // stderr_redacted
        vec![],        // warnings
        Some(false),   // fallback_used
        runner,        // runner
        runner_distro, // runner_distro
        None,          // error_kind
        None,          // error_reason
        None,          // diff_context,
        None,          // pipeline
    );

    receipt_manager.write_receipt(&receipt).unwrap();
}

/// Test FR-STA-001: Status generation with effective_config
#[test]
fn test_status_generation_with_effective_config() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-effective-config");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-effective-config",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config with multiple sources
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );
    effective_config.insert(
        "max_turns".to_string(),
        ("10".to_string(), "cli".to_string()),
    );
    effective_config.insert(
        "timeout".to_string(),
        ("600".to_string(), "config".to_string()),
    );

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify effective_config is populated
    assert!(
        status.effective_config.len() >= 3,
        "Should have at least 3 config values"
    );

    // Verify config values are present
    assert!(status.effective_config.contains_key("model"));
    assert!(status.effective_config.contains_key("max_turns"));
    assert!(status.effective_config.contains_key("timeout"));

    println!("✓ Status generation with effective_config test passed");
    Ok(())
}

/// Test FR-STA-002: Source attribution (cli/config/default)
#[test]
fn test_source_attribution() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-source-attribution");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-source-attribution",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config with different sources
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );
    effective_config.insert(
        "max_turns".to_string(),
        ("10".to_string(), "cli".to_string()),
    );
    effective_config.insert(
        "timeout".to_string(),
        ("600".to_string(), "config".to_string()),
    );

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify source attribution
    let model_config = status.effective_config.get("model").unwrap();
    assert_eq!(
        model_config.source,
        ConfigSource::Default,
        "Model should be from defaults"
    );

    let max_turns_config = status.effective_config.get("max_turns").unwrap();
    assert_eq!(
        max_turns_config.source,
        ConfigSource::Cli,
        "Max turns should be from CLI"
    );

    let timeout_config = status.effective_config.get("timeout").unwrap();
    assert_eq!(
        timeout_config.source,
        ConfigSource::Config,
        "Timeout should be from config file"
    );

    // Verify values are correct types
    assert_eq!(model_config.value.as_str().unwrap(), "haiku");
    assert_eq!(max_turns_config.value.as_i64().unwrap(), 10);
    assert_eq!(timeout_config.value.as_i64().unwrap(), 600);

    println!("✓ Source attribution test passed");
    Ok(())
}

/// Test FR-STA-003: Artifact enumeration with blake3 hashes
#[test]
fn test_artifact_enumeration_with_blake3() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-artifacts");

    // Create a test receipt with outputs
    let packet = PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let outputs = vec![
        FileHash {
            path: "artifacts/00-requirements.md".to_string(),
            blake3_canonicalized:
                "abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab".to_string(),
        },
        FileHash {
            path: "artifacts/00-requirements.core.yaml".to_string(),
            blake3_canonicalized:
                "1234abcd567890abcdef1234567890abcdef1234567890abcdef1234567890cd".to_string(),
        },
    ];

    let receipt = receipt_manager.create_receipt(
        "test-status-artifacts",
        PhaseId::Requirements,
        0,
        outputs,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,        // stderr_tail
        None,        // stderr_redacted
        vec![],      // warnings
        Some(false), // fallback_used
        "native",    // runner
        None,        // runner_distro
        None,        // error_kind
        None,        // error_reason
        None,        // diff_context,
        None,        // pipeline
    );

    receipt_manager.write_receipt(&receipt)?;

    // Create the actual artifact files so they can be enumerated
    let artifacts_dir = artifact_manager.base_path().join("artifacts");
    std::fs::create_dir_all(&artifacts_dir)?;
    std::fs::write(artifacts_dir.join("00-requirements.md"), "# Requirements\n")?;
    std::fs::write(
        artifacts_dir.join("00-requirements.core.yaml"),
        "version: 1\n",
    )?;

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify artifacts are enumerated
    assert_eq!(
        status.artifacts.len(),
        2,
        "Should have 2 artifacts enumerated"
    );

    // Verify artifacts are sorted by path
    assert_eq!(
        status.artifacts[0].path,
        "artifacts/00-requirements.core.yaml"
    );
    assert_eq!(status.artifacts[1].path, "artifacts/00-requirements.md");

    // Verify blake3_first8 is populated (first 8 chars of hash)
    assert_eq!(status.artifacts[0].blake3_first8, "1234abcd");
    assert_eq!(status.artifacts[1].blake3_first8, "abcd1234");

    println!("✓ Artifact enumeration with blake3 hashes test passed");
    Ok(())
}

/// Test FR-STA-004: Fresh spec (no prior receipts)
///
/// Fresh specs with no receipts should return a valid status with sensible defaults.
/// This allows users to check status even before any phases have been run.
#[test]
fn test_fresh_spec_no_receipts() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-fresh-spec");

    // Don't create any receipts - this is a fresh spec

    // Create minimal effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status - should succeed with sensible defaults
    let result = generate_status(&artifact_manager, &receipt_manager, effective_config, None);

    // Fresh specs should return a valid status with defaults, not an error
    assert!(
        result.is_ok(),
        "Fresh spec with no receipts should return valid status, got error: {:?}",
        result.as_ref().err()
    );

    let status = result.unwrap();

    // Verify sensible defaults for fresh spec
    assert!(
        status.artifacts.is_empty(),
        "Fresh spec should have no artifacts"
    );
    assert!(
        status.last_receipt_path.is_empty(),
        "Fresh spec should have empty receipt path"
    );
    assert_eq!(
        status.runner, "native",
        "Fresh spec should default to native runner"
    );
    assert_eq!(
        status.canonicalization_version, "1.0.0",
        "Should use default canonicalization version"
    );
    assert_eq!(
        status.canonicalization_backend, "jcs-rfc8785",
        "Should use default canonicalization backend"
    );
    assert!(
        !status.fallback_used,
        "Fresh spec should not have used fallback"
    );

    // Verify effective config is present
    assert!(
        status.effective_config.contains_key("model"),
        "Effective config should contain model"
    );

    println!("✓ Fresh spec (no prior receipts) test passed");
    Ok(())
}

/// Test FR-STA-005: Lock drift reporting
#[test]
fn test_lock_drift_reporting() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-lock-drift");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-lock-drift",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Create lock drift with all three fields
    let lock_drift = Some(LockDrift {
        model_full_name: Some(DriftPair {
            locked: "haiku".to_string(),
            current: "sonnet-20250101".to_string(),
        }),
        claude_cli_version: Some(DriftPair {
            locked: "0.8.0".to_string(),
            current: "0.8.1".to_string(),
        }),
        schema_version: Some(DriftPair {
            locked: "1".to_string(),
            current: "2".to_string(),
        }),
    });

    // Generate status with drift
    let status = generate_status(
        &artifact_manager,
        &receipt_manager,
        effective_config,
        lock_drift,
    )?;

    // Verify lock drift is present
    assert!(
        status.lock_drift.is_some(),
        "Lock drift should be present in status"
    );

    let drift = status.lock_drift.unwrap();

    // Verify model drift
    assert!(drift.model_full_name.is_some());
    let model_drift = drift.model_full_name.unwrap();
    assert_eq!(model_drift.locked, "haiku");
    assert_eq!(model_drift.current, "sonnet-20250101");

    // Verify CLI version drift
    assert!(drift.claude_cli_version.is_some());
    let cli_drift = drift.claude_cli_version.unwrap();
    assert_eq!(cli_drift.locked, "0.8.0");
    assert_eq!(cli_drift.current, "0.8.1");

    // Verify schema version drift
    assert!(drift.schema_version.is_some());
    let schema_drift = drift.schema_version.unwrap();
    assert_eq!(schema_drift.locked, "1");
    assert_eq!(schema_drift.current, "2");

    println!("✓ Lock drift reporting test passed");
    Ok(())
}

/// Test JCS emission for status outputs
#[test]
fn test_status_jcs_emission() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) = create_test_managers("test-status-jcs");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-jcs",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Emit as canonical JSON
    let json = StatusManager::emit_json(&status)?;

    // Verify it's valid JSON
    let parsed: serde_json::Value = serde_json::from_str(&json)?;
    assert_eq!(parsed["schema_version"], "1");
    assert_eq!(parsed["runner"], "native");

    // Verify canonical JSON properties (no extra whitespace, no newlines)
    assert!(!json.contains("  "), "JCS should not have extra spaces");
    assert!(!json.contains("\n"), "JCS should not have newlines");

    // Verify re-serialization produces identical output
    let parsed2: serde_json::Value = serde_json::from_str(&json)?;
    let json_bytes2 = serde_json_canonicalizer::to_vec(&parsed2)?;
    let json2 = String::from_utf8(json_bytes2)?;

    assert_eq!(
        json, json2,
        "Re-serialization should produce identical output"
    );

    println!("✓ Status JCS emission test passed");
    Ok(())
}

/// Test status with WSL runner
#[test]
fn test_status_with_wsl_runner() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) = create_test_managers("test-status-wsl");

    // Create a test receipt with WSL runner
    create_test_receipt(
        &receipt_manager,
        "test-status-wsl",
        PhaseId::Requirements,
        "wsl",
        Some("Ubuntu-22.04".to_string()),
    );

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify WSL runner info
    assert_eq!(status.runner, "wsl");
    assert_eq!(status.runner_distro, Some("Ubuntu-22.04".to_string()));

    println!("✓ Status with WSL runner test passed");
    Ok(())
}

/// Test status with no lock drift
#[test]
fn test_status_no_lock_drift() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-no-drift");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-no-drift",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status without drift
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify no lock drift
    assert!(
        status.lock_drift.is_none(),
        "Lock drift should be None when not provided"
    );

    println!("✓ Status with no lock drift test passed");
    Ok(())
}

/// Test status artifacts are sorted by path
#[test]
fn test_status_artifacts_sorted() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) = create_test_managers("test-status-sorted");

    // Create a test receipt with multiple outputs in non-alphabetical order
    let packet = PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let outputs = vec![
        FileHash {
            path: "artifacts/99-final.md".to_string(),
            blake3_canonicalized:
                "zzzz1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab".to_string(),
        },
        FileHash {
            path: "artifacts/00-requirements.md".to_string(),
            blake3_canonicalized:
                "aaaa1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab".to_string(),
        },
        FileHash {
            path: "artifacts/50-middle.md".to_string(),
            blake3_canonicalized:
                "mmmm1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab".to_string(),
        },
    ];

    let receipt = receipt_manager.create_receipt(
        "test-status-sorted",
        PhaseId::Requirements,
        0,
        outputs,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,        // stderr_tail
        None,        // stderr_redacted
        vec![],      // warnings
        Some(false), // fallback_used
        "native",    // runner
        None,        // runner_distro
        None,        // error_kind
        None,        // error_reason
        None,        // diff_context,
        None,        // pipeline
    );

    receipt_manager.write_receipt(&receipt)?;

    // Create the actual artifact files
    let artifacts_dir = artifact_manager.base_path().join("artifacts");
    std::fs::create_dir_all(&artifacts_dir)?;
    std::fs::write(artifacts_dir.join("99-final.md"), "# Final\n")?;
    std::fs::write(artifacts_dir.join("00-requirements.md"), "# Requirements\n")?;
    std::fs::write(artifacts_dir.join("50-middle.md"), "# Middle\n")?;

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify artifacts are sorted by path
    assert_eq!(status.artifacts.len(), 3);
    assert_eq!(status.artifacts[0].path, "artifacts/00-requirements.md");
    assert_eq!(status.artifacts[1].path, "artifacts/50-middle.md");
    assert_eq!(status.artifacts[2].path, "artifacts/99-final.md");

    println!("✓ Status artifacts sorted test passed");
    Ok(())
}

/// Test status with empty effective config
#[test]
fn test_status_empty_effective_config() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-empty-config");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-empty-config",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create empty effective config
    let effective_config = BTreeMap::new();

    // Generate status
    let status = generate_status(&artifact_manager, &receipt_manager, effective_config, None)?;

    // Verify status is generated successfully with empty config
    assert_eq!(status.schema_version, "1");
    assert_eq!(status.effective_config.len(), 0);

    println!("✓ Status with empty effective config test passed");
    Ok(())
}

/// Test AT-STA-004: Pending fixup summary (counts only)
#[test]
fn test_pending_fixup_summary() -> Result<()> {
    use xchecker::types::PendingFixupsSummary;

    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-pending-fixups");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-pending-fixups",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Create pending fixups summary
    let pending_fixups = Some(PendingFixupsSummary {
        targets: 3,
        est_added: 42,
        est_removed: 15,
    });

    // Generate status with pending fixups
    let status = StatusManager::generate_status_internal(
        &artifact_manager,
        &receipt_manager,
        effective_config,
        None,
        pending_fixups,
        None,
    )?;

    // Verify pending fixups are present
    assert!(
        status.pending_fixups.is_some(),
        "Pending fixups should be present in status"
    );

    let fixups = status.pending_fixups.as_ref().unwrap();
    assert_eq!(fixups.targets, 3, "Should have 3 target files");
    assert_eq!(fixups.est_added, 42, "Should have 42 estimated added lines");
    assert_eq!(
        fixups.est_removed, 15,
        "Should have 15 estimated removed lines"
    );

    // Verify JCS emission includes pending_fixups
    let json = StatusManager::emit_json(&status)?;
    assert!(
        json.contains("pending_fixups"),
        "JSON should contain pending_fixups field"
    );
    assert!(
        json.contains("\"targets\":3"),
        "JSON should contain targets count"
    );
    assert!(
        json.contains("\"est_added\":42"),
        "JSON should contain est_added count"
    );
    assert!(
        json.contains("\"est_removed\":15"),
        "JSON should contain est_removed count"
    );

    println!("✓ Pending fixup summary test passed");
    Ok(())
}

/// Test that pending_fixups is omitted when None
#[test]
fn test_pending_fixups_omitted_when_none() -> Result<()> {
    let (artifact_manager, receipt_manager, _temp_dir) =
        create_test_managers("test-status-no-pending-fixups");

    // Create a test receipt
    create_test_receipt(
        &receipt_manager,
        "test-status-no-pending-fixups",
        PhaseId::Requirements,
        "native",
        None,
    );

    // Create effective config
    let mut effective_config = BTreeMap::new();
    effective_config.insert(
        "model".to_string(),
        ("haiku".to_string(), "default".to_string()),
    );

    // Generate status without pending fixups
    let status = StatusManager::generate_status_internal(
        &artifact_manager,
        &receipt_manager,
        effective_config,
        None,
        None, // No pending fixups
        None,
    )?;

    // Verify pending fixups are not present
    assert!(
        status.pending_fixups.is_none(),
        "Pending fixups should be None when not provided"
    );

    // Verify JCS emission omits pending_fixups field
    let json = StatusManager::emit_json(&status)?;
    assert!(
        !json.contains("pending_fixups"),
        "JSON should not contain pending_fixups field when None"
    );

    println!("✓ Pending fixups omitted when None test passed");
    Ok(())
}

/// Meta-test that runs all status tests sequentially.
///
/// NOTE: This test is ignored because:
/// 1. All individual tests already run as part of the test suite
/// 2. This meta-test can cause race conditions when run in parallel
///    because several tests manipulate process-wide state (XCHECKER_HOME, env vars)
/// 3. To run manually: cargo test run_all_status_tests -- --ignored
#[test]
#[ignore = "meta-test - individual tests already run; can cause races in parallel execution"]
fn run_all_status_tests() {
    println!("\n=== Running Status Reporting Tests (FR-STA) ===\n");

    test_status_generation_with_effective_config().unwrap();
    test_source_attribution().unwrap();
    test_artifact_enumeration_with_blake3().unwrap();
    test_fresh_spec_no_receipts().unwrap();
    test_lock_drift_reporting().unwrap();
    test_status_jcs_emission().unwrap();
    test_status_with_wsl_runner().unwrap();
    test_status_no_lock_drift().unwrap();
    test_status_artifacts_sorted().unwrap();
    test_status_empty_effective_config().unwrap();
    test_pending_fixup_summary().unwrap();
    test_pending_fixups_omitted_when_none().unwrap();

    println!("\n=== All Status Reporting Tests Passed ===\n");
}