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
#![cfg(feature = "test-utils")]
//! Smoke tests for xchecker CLI commands
//!
//! These tests validate that all xchecker commands execute successfully
//! and produce valid output. They test the integration of all components
//! without requiring Claude CLI or API keys.

use std::env;
use std::fs;
use std::io::Write;
use std::process::Command;
use tempfile::TempDir;
use xchecker::test_support;

/// Get the xchecker binary path
fn get_xchecker_bin() -> std::path::PathBuf {
    let manifest_dir =
        env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should be set by cargo");

    // Try to get the binary from CARGO_BIN_EXE_xchecker first (set during test runs)
    if let Ok(bin_path) = env::var("CARGO_BIN_EXE_xchecker") {
        return std::path::PathBuf::from(bin_path);
    }

    // Otherwise, assume it's in target/debug
    let mut bin_path = std::path::PathBuf::from(manifest_dir);
    bin_path.push("target");
    bin_path.push("debug");
    bin_path.push("xchecker");
    if cfg!(windows) {
        bin_path.set_extension("exe");
    }
    bin_path
}

/// Helper to run xchecker via cargo (for tests that don't need specific working directory)
fn run_xchecker(args: &[&str]) -> Command {
    let manifest_dir =
        env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should be set by cargo");

    let mut cmd = Command::new("cargo");
    cmd.current_dir(&manifest_dir);
    cmd.arg("run").arg("--bin").arg("xchecker").arg("--");
    for arg in args {
        cmd.arg(arg);
    }
    cmd
}

/// Helper to run xchecker binary in a specific working directory
fn run_xchecker_in_dir(args: &[&str], work_dir: &std::path::Path) -> Command {
    let bin_path = get_xchecker_bin();

    let mut cmd = Command::new(&bin_path);
    cmd.current_dir(work_dir);
    for arg in args {
        cmd.arg(arg);
    }
    cmd
}

fn real_llm_tests_enabled() -> bool {
    if test_support::llm_tests_enabled() {
        true
    } else {
        println!(
            "Skipping real LLM smoke tests. Set XCHECKER_REAL_LLM_TESTS=1 to enable (and ensure XCHECKER_SKIP_LLM_TESTS is unset)."
        );
        false
    }
}

/// Smoke test: xchecker doctor --json
///
/// Validates that the doctor command runs successfully and produces valid JSON output
#[test]
fn test_smoke_doctor_json() {
    println!("🔥 Smoke test: xchecker doctor --json");

    let output = run_xchecker(&["doctor", "--json"])
        .output()
        .expect("Failed to execute xchecker doctor --json");

    // Doctor should always succeed (exit 0) unless there are critical failures
    // It may exit 1 if checks fail, but the command itself should execute
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Validate JSON output
    let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
    assert!(
        json_result.is_ok(),
        "Doctor output should be valid JSON: {}",
        stdout
    );

    let json = json_result.unwrap();

    // Validate required fields (FR-OBS)
    assert!(
        json.get("checks").is_some(),
        "Doctor JSON should have 'checks' field"
    );
    assert!(
        json.get("ok").is_some(),
        "Doctor JSON should have 'ok' field"
    );

    println!("✓ Doctor command produces valid JSON output");
}

/// Smoke test: xchecker init demo --create-lock
///
/// Validates that the init command creates spec directory structure and lockfile
#[test]
fn test_smoke_init_with_lock() {
    println!("🔥 Smoke test: xchecker init demo --create-lock");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let spec_id = "smoke-test-init";

    let output = run_xchecker_in_dir(&["init", spec_id, "--create-lock"], temp_dir.path())
        .output()
        .expect("Failed to execute xchecker init");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Init should succeed
    assert!(
        output.status.success(),
        "Init command should succeed: {}",
        stderr
    );

    // Verify directory structure created (FR-LOCK)
    let spec_dir = temp_dir.path().join(".xchecker/specs").join(spec_id);
    assert!(
        spec_dir.exists(),
        "Spec directory should be created: {}",
        spec_dir.display()
    );
    assert!(
        spec_dir.join("artifacts").exists(),
        "Artifacts directory should be created"
    );
    assert!(
        spec_dir.join("receipts").exists(),
        "Receipts directory should be created"
    );
    assert!(
        spec_dir.join("context").exists(),
        "Context directory should be created"
    );

    // Verify lockfile created (FR-LOCK-006)
    let lock_path = spec_dir.join("lock.json");
    assert!(
        lock_path.exists(),
        "Lockfile should be created: {}",
        lock_path.display()
    );

    // Validate lockfile content
    let lock_content = fs::read_to_string(&lock_path).expect("Failed to read lockfile");
    let lock_json: serde_json::Value =
        serde_json::from_str(&lock_content).expect("Lockfile should be valid JSON");

    assert!(
        lock_json.get("model_full_name").is_some(),
        "Lockfile should have model_full_name"
    );
    assert!(
        lock_json.get("claude_cli_version").is_some(),
        "Lockfile should have claude_cli_version"
    );
    assert!(
        lock_json.get("schema_version").is_some(),
        "Lockfile should have schema_version"
    );

    println!("✓ Init command creates directory structure and lockfile");
}

/// Smoke test: xchecker spec demo --dry-run
///
/// Validates that the spec command runs in dry-run mode without making Claude calls
#[test]
fn test_smoke_spec_dry_run() {
    println!("🔥 Smoke test: xchecker spec demo --dry-run");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let spec_id = "smoke-test-spec";

    // Create a test input file
    let input_content = "Create a simple calculator application with basic arithmetic operations";

    let mut child = run_xchecker_in_dir(&["spec", spec_id, "--dry-run"], temp_dir.path())
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("Failed to spawn xchecker spec");

    // Write input to stdin
    if let Some(stdin) = child.stdin.as_mut() {
        stdin
            .write_all(input_content.as_bytes())
            .expect("Failed to write to stdin");
    }

    let output = child
        .wait_with_output()
        .expect("Failed to wait for xchecker spec");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Dry-run should succeed (FR-ORC)
    assert!(
        output.status.success(),
        "Spec dry-run should succeed: {}",
        stderr
    );

    // Verify output indicates dry-run mode
    assert!(
        stdout.contains("dry-run") || stdout.contains("Requirements phase completed"),
        "Output should indicate dry-run execution or completion"
    );

    println!("✓ Spec command runs successfully in dry-run mode");
}

/// Smoke test: xchecker status demo --json
///
/// Validates that the status command produces valid JSON output
#[test]
fn test_smoke_status_json() {
    println!("🔥 Smoke test: xchecker status demo --json");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let spec_id = "smoke-test-status";

    // First initialize the spec
    let init_output = run_xchecker_in_dir(&["init", spec_id], temp_dir.path())
        .output()
        .expect("Failed to execute xchecker init");

    assert!(
        init_output.status.success(),
        "Init should succeed before status check"
    );

    // Now check status
    let output = run_xchecker_in_dir(&["status", spec_id, "--json"], temp_dir.path())
        .output()
        .expect("Failed to execute xchecker status");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Status should succeed (FR-STA)
    assert!(
        output.status.success(),
        "Status command should succeed: {}",
        stderr
    );

    // Validate JSON output (FR-STA-001)
    let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
    assert!(
        json_result.is_ok(),
        "Status output should be valid JSON: {}",
        stdout
    );

    let json = json_result.unwrap();

    // Validate required fields per status-json.v1 schema (FR-Claude Code-CLI, Requirements 4.1.2)
    assert!(
        json.get("schema_version").is_some(),
        "Status JSON should have schema_version"
    );
    assert!(
        json.get("spec_id").is_some(),
        "Status JSON should have spec_id"
    );
    assert!(
        json.get("phase_statuses").is_some(),
        "Status JSON should have phase_statuses"
    );
    assert!(
        json.get("pending_fixups").is_some(),
        "Status JSON should have pending_fixups"
    );
    assert!(
        json.get("has_errors").is_some(),
        "Status JSON should have has_errors"
    );

    println!("✓ Status command produces valid JSON output");
}

/// Smoke test: xchecker clean demo --hard
///
/// Validates that the clean command removes spec artifacts
#[test]
fn test_smoke_clean_hard() {
    println!("🔥 Smoke test: xchecker clean demo --hard");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let spec_id = "smoke-test-clean";

    // First initialize the spec
    let init_output = run_xchecker_in_dir(&["init", spec_id], temp_dir.path())
        .output()
        .expect("Failed to execute xchecker init");

    assert!(
        init_output.status.success(),
        "Init should succeed before clean"
    );

    let spec_dir = temp_dir.path().join(".xchecker/specs").join(spec_id);
    assert!(
        spec_dir.exists(),
        "Spec directory should exist before clean"
    );

    // Now clean the spec
    let output = run_xchecker_in_dir(&["clean", spec_id, "--hard"], temp_dir.path())
        .output()
        .expect("Failed to execute xchecker clean");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Clean should succeed
    assert!(
        output.status.success(),
        "Clean command should succeed: {}",
        stderr
    );

    // Verify spec directory is removed
    assert!(
        !spec_dir.exists(),
        "Spec directory should be removed after clean"
    );

    println!("✓ Clean command removes spec artifacts");
}

/// Smoke test: xchecker benchmark
///
/// Validates that the benchmark command runs and produces valid output
#[test]
#[ignore = "flaky in CI - environment-dependent timing"]
fn test_smoke_benchmark() {
    println!("🔥 Smoke test: xchecker benchmark");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    let output = run_xchecker_in_dir(
        &[
            "benchmark",
            "--file-count",
            "10",
            "--iterations",
            "2",
            "--json",
        ],
        temp_dir.path(),
    )
    .output()
    .expect("Failed to execute xchecker benchmark");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Benchmark should succeed (FR-BENCH)
    assert!(
        output.status.success(),
        "Benchmark command should succeed: {}",
        stderr
    );

    // Validate JSON output (FR-BENCH-004)
    let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
    assert!(
        json_result.is_ok(),
        "Benchmark output should be valid JSON: {}",
        stdout
    );

    let json = json_result.unwrap();

    // Validate required fields (FR-BENCH-004)
    assert!(
        json.get("ok").is_some(),
        "Benchmark JSON should have 'ok' field"
    );
    assert!(
        json.get("timings_ms").is_some(),
        "Benchmark JSON should have 'timings_ms' field"
    );
    assert!(
        json.get("rss_mb").is_some(),
        "Benchmark JSON should have 'rss_mb' field"
    );

    println!("✓ Benchmark command produces valid output");
}

/// Smoke test: Verify all commands succeed with correct exit codes
///
/// This test runs all commands and verifies they produce expected exit codes
#[test]
fn test_smoke_all_commands_exit_codes() {
    println!("🔥 Smoke test: Verify all commands exit codes");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Test 1: --help should exit 0
    let help_output = run_xchecker(&["--help"])
        .output()
        .expect("Failed to execute --help");
    assert!(
        help_output.status.success(),
        "--help should exit with code 0"
    );

    // Test 2: --version should exit 0
    let version_output = run_xchecker(&["--version"])
        .output()
        .expect("Failed to execute --version");
    assert!(
        version_output.status.success(),
        "--version should exit with code 0"
    );

    // Test 3: doctor should exit 0 or 1 (depending on checks)
    let doctor_output = run_xchecker(&["doctor"])
        .output()
        .expect("Failed to execute doctor");
    let doctor_code = doctor_output.status.code().unwrap_or(-1);
    assert!(
        doctor_code == 0 || doctor_code == 1,
        "doctor should exit with code 0 or 1, got {}",
        doctor_code
    );

    // Test 4: init should exit 0
    let init_output = run_xchecker_in_dir(&["init", "exit-code-test"], temp_dir.path())
        .output()
        .expect("Failed to execute init");
    assert!(init_output.status.success(), "init should exit with code 0");

    // Test 5: status should exit 0
    let status_output = run_xchecker_in_dir(&["status", "exit-code-test"], temp_dir.path())
        .output()
        .expect("Failed to execute status");
    assert!(
        status_output.status.success(),
        "status should exit with code 0"
    );

    // Test 6: clean should exit 0
    let clean_output = run_xchecker_in_dir(&["clean", "exit-code-test", "--hard"], temp_dir.path())
        .output()
        .expect("Failed to execute clean");
    assert!(
        clean_output.status.success(),
        "clean should exit with code 0"
    );

    // Test 7: benchmark should exit 0
    let benchmark_output = run_xchecker_in_dir(
        &["benchmark", "--file-count", "5", "--iterations", "1"],
        temp_dir.path(),
    )
    .output()
    .expect("Failed to execute benchmark");
    assert!(
        benchmark_output.status.success(),
        "benchmark should exit with code 0"
    );

    println!("✓ All commands produce correct exit codes");
}

/// Smoke test: Verify JSON output is valid for all commands that support --json
///
/// This test validates that all JSON outputs are properly formatted
#[test]
fn test_smoke_json_output_validity() {
    println!("🔥 Smoke test: Verify JSON output validity");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");

    // Initialize a test spec
    run_xchecker_in_dir(&["init", "json-test"], temp_dir.path())
        .output()
        .expect("Failed to init spec");

    // Test 1: doctor --json
    let doctor_output = run_xchecker(&["doctor", "--json"])
        .output()
        .expect("Failed to execute doctor --json");
    let doctor_json = String::from_utf8_lossy(&doctor_output.stdout);
    assert!(
        serde_json::from_str::<serde_json::Value>(&doctor_json).is_ok(),
        "doctor --json should produce valid JSON"
    );

    // Test 2: status --json
    let status_output = run_xchecker_in_dir(&["status", "json-test", "--json"], temp_dir.path())
        .output()
        .expect("Failed to execute status --json");
    let status_json = String::from_utf8_lossy(&status_output.stdout);
    assert!(
        serde_json::from_str::<serde_json::Value>(&status_json).is_ok(),
        "status --json should produce valid JSON"
    );

    // Test 3: benchmark --json
    let benchmark_output = run_xchecker_in_dir(
        &[
            "benchmark",
            "--file-count",
            "5",
            "--iterations",
            "1",
            "--json",
        ],
        temp_dir.path(),
    )
    .output()
    .expect("Failed to execute benchmark --json");
    let benchmark_json = String::from_utf8_lossy(&benchmark_output.stdout);
    assert!(
        serde_json::from_str::<serde_json::Value>(&benchmark_json).is_ok(),
        "benchmark --json should produce valid JSON"
    );

    println!("✓ All JSON outputs are valid");
}

// ============================================================================
// Tests with Claude CLI (ignored by default, run in CI with --ignored)
// ============================================================================

/// Smoke test to verify Claude CLI is accessible
///
/// This test is marked as ignored and only runs in CI with the --ignored flag
/// when ANTHROPIC_API_KEY secret is available.
#[test]
#[ignore = "requires_real_claude"]
fn test_claude_cli_available() {
    if !real_llm_tests_enabled() {
        return;
    }

    // Check if API key is available
    if env::var("ANTHROPIC_API_KEY").is_err() {
        println!("Skipping smoke test: ANTHROPIC_API_KEY not set");
        return;
    }

    println!("Running smoke test with real Claude CLI...");

    // Try to run claude --version
    let output = std::process::Command::new("claude")
        .arg("--version")
        .output();

    match output {
        Ok(result) => {
            if result.status.success() {
                let version = String::from_utf8_lossy(&result.stdout);
                println!("Claude CLI version: {}", version.trim());
                println!("✓ Claude CLI is accessible");
            } else {
                let stderr = String::from_utf8_lossy(&result.stderr);
                panic!(
                    "Claude CLI failed with exit code {:?}: {}",
                    result.status.code(),
                    stderr
                );
            }
        }
        Err(e) => {
            panic!("Failed to execute claude command: {}", e);
        }
    }
}

/// Smoke test to verify basic xchecker functionality with real Claude CLI
///
/// This test is marked as ignored and only runs in CI with the --ignored flag
/// when ANTHROPIC_API_KEY secret is available.
#[test]
#[ignore = "requires_real_claude"]
fn test_xchecker_with_real_claude() {
    if !real_llm_tests_enabled() {
        return;
    }

    // Check if API key is available
    if env::var("ANTHROPIC_API_KEY").is_err() {
        println!("Skipping smoke test: ANTHROPIC_API_KEY not set");
        return;
    }

    println!("Smoke test: xchecker with real Claude CLI");

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let spec_id = "real-claude-test";

    // Create a test input
    let input_content = "Create a simple hello world application";

    let mut child = run_xchecker_in_dir(&["spec", spec_id], temp_dir.path())
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("Failed to spawn xchecker spec");

    // Write input to stdin
    if let Some(stdin) = child.stdin.as_mut() {
        stdin
            .write_all(input_content.as_bytes())
            .expect("Failed to write to stdin");
    }

    let output = child
        .wait_with_output()
        .expect("Failed to wait for xchecker spec");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Exit code: {:?}", output.status.code());
    println!("Stdout: {}", stdout);
    if !stderr.is_empty() {
        println!("Stderr: {}", stderr);
    }

    // Spec execution with real Claude should succeed
    assert!(
        output.status.success(),
        "Spec execution with real Claude should succeed: {}",
        stderr
    );

    // Verify artifacts were created
    let spec_dir = temp_dir.path().join(".xchecker/specs").join(spec_id);
    let artifacts_dir = spec_dir.join("artifacts");
    assert!(
        artifacts_dir.exists(),
        "Artifacts directory should exist after execution"
    );

    // Verify receipt was created
    let receipts_dir = spec_dir.join("receipts");
    assert!(
        receipts_dir.exists(),
        "Receipts directory should exist after execution"
    );

    println!("✓ xchecker works with real Claude CLI");
}