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
#![cfg(feature = "test-utils")]
//! Integration tests for PacketBuilder and Phase system wiring (Task 9.2)
//!
//! **WHITE-BOX TEST**: This test uses internal module APIs (`orchestrator::{OrchestratorConfig,
//! PhaseOrchestrator}`, `types::PhaseId`) 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.
//!
//! Tests the complete integration of PacketBuilder, Phase trait system,
//! and orchestrator with proper packet evidence tracking.

use anyhow::Result;
use std::fs;
use tempfile::TempDir;
use xchecker::orchestrator::{OrchestratorConfig, PhaseOrchestrator};
use xchecker::test_support;
use xchecker::types::PhaseId;

/// Helper to set up test environment with sample files
fn setup_test_environment_with_files(test_name: &str) -> (PhaseOrchestrator, TempDir) {
    // Use isolated home for each test to avoid conflicts
    let temp_dir = xchecker::paths::with_isolated_home();

    // Create spec directory structure
    let spec_id = format!("test-packet-phase-{}", test_name);
    let orchestrator = PhaseOrchestrator::new(&spec_id).unwrap();

    // Create some sample files in the spec directory for packet building
    let spec_dir = orchestrator.artifact_manager().base_path();

    // Create a README file (medium priority)
    fs::write(
        spec_dir.join("README.md"),
        "# Test Spec\n\nThis is a test specification for packet building.",
    )
    .unwrap();

    // Create a SPEC file (high priority)
    fs::write(
        spec_dir.join("SPEC-001.md"),
        "# Specification Document\n\nDetailed specification content.",
    )
    .unwrap();

    // Create a core YAML file (upstream priority - non-evictable)
    fs::write(
        spec_dir.join("config.core.yaml"),
        "version: 1.0\nname: test-spec\n",
    )
    .unwrap();

    (orchestrator, temp_dir)
}

/// Test that PacketBuilder is properly integrated into phase execution
#[tokio::test]
async fn test_packet_builder_integration() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("builder-integration");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase
    let result = orchestrator.execute_requirements_phase(&config).await?;

    assert!(result.success, "Requirements phase should succeed");
    assert_eq!(result.exit_code, 0, "Exit code should be 0 for success");

    // Verify receipt was written
    assert!(result.receipt_path.is_some(), "Receipt should be written");

    // Read the receipt and verify packet evidence is populated
    let receipt_path = result.receipt_path.unwrap();
    let receipt_content = fs::read_to_string(&receipt_path)?;

    // Receipt should contain packet evidence
    assert!(
        receipt_content.contains("packet_evidence") || receipt_content.contains("files"),
        "Receipt should contain packet evidence"
    );

    Ok(())
}

/// Test that packet evidence includes file information
#[tokio::test]
async fn test_packet_evidence_includes_files() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("evidence-files");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase
    let result = orchestrator.execute_requirements_phase(&config).await?;
    assert!(result.success);

    // Read the receipt
    let receipt_manager =
        xchecker::receipt::ReceiptManager::new(orchestrator.artifact_manager().base_path());

    let receipt = receipt_manager.read_latest_receipt(PhaseId::Requirements)?;
    assert!(receipt.is_some(), "Receipt should exist");

    let receipt = receipt.unwrap();

    // Verify packet evidence has files
    assert!(
        !receipt.packet.files.is_empty(),
        "Packet evidence should include files"
    );

    // Verify files have required fields
    for file_evidence in &receipt.packet.files {
        assert!(
            !file_evidence.path.is_empty(),
            "File path should not be empty"
        );
        assert!(
            !file_evidence.blake3_pre_redaction.is_empty(),
            "BLAKE3 hash should not be empty"
        );
        // Priority should be set
        assert!(
            matches!(
                file_evidence.priority,
                xchecker::types::Priority::Upstream
                    | xchecker::types::Priority::High
                    | xchecker::types::Priority::Medium
                    | xchecker::types::Priority::Low
            ),
            "Priority should be valid"
        );
    }

    Ok(())
}

/// Test that secret scanning is integrated before Claude invocation
#[tokio::test]
async fn test_secret_scanning_integration() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("secret-scan");

    // Create a file with a fake secret
    let spec_dir = orchestrator.artifact_manager().base_path();
    let token = test_support::github_pat();
    fs::write(spec_dir.join("secrets.txt"), format!("API Key: {}", token))?;

    let config = OrchestratorConfig {
        dry_run: true, // Use dry_run to avoid nested runtime issues
        ..Default::default()
    };

    // Execute Requirements phase - should fail due to secret detection
    let result = orchestrator.execute_requirements_phase(&config).await;

    // Should fail with secret detection error
    assert!(
        result.is_err(),
        "Phase should fail when secrets are detected"
    );

    let err = result.unwrap_err();
    let err_msg = err.to_string();

    // Error should mention secret detection
    assert!(
        err_msg.contains("secret") || err_msg.contains("Secret"),
        "Error should mention secret detection: {}",
        err_msg
    );

    Ok(())
}

/// Test that packet overflow is detected before Claude invocation
#[tokio::test]
async fn test_packet_overflow_detection_requires_future_api() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("overflow");

    // Create upstream files that collectively exceed packet limits
    let spec_dir = orchestrator.artifact_manager().base_path();
    let large_content = "x".repeat(600); // Keep under single-file max, overflow in aggregate
    fs::write(spec_dir.join("config.core.yaml"), &large_content)?;
    fs::write(spec_dir.join("extra.core.yaml"), &large_content)?;

    let mut config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Set very small packet limits to trigger overflow
    config
        .config
        .insert("packet_max_bytes".to_string(), "1000".to_string());
    config
        .config
        .insert("packet_max_lines".to_string(), "10".to_string());

    // Execute Requirements phase - should fail due to packet overflow
    let result = orchestrator.execute_requirements_phase(&config).await;

    // Should fail with packet overflow error
    assert!(
        result.is_err(),
        "Phase should fail when packet exceeds limits"
    );

    let err = result.unwrap_err();
    let err_msg = err.to_string();

    // Error should mention packet overflow
    assert!(
        err_msg.contains("overflow")
            || err_msg.contains("Overflow")
            || err_msg.contains("exceeded")
            || err_msg.contains("limit"),
        "Error should mention packet overflow: {}",
        err_msg
    );

    Ok(())
}

/// Test end-to-end Requirements -> Design -> Tasks progression
#[tokio::test]
async fn test_end_to_end_phase_progression() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("e2e-progression");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase
    let result1 = orchestrator.execute_requirements_phase(&config).await?;
    assert!(result1.success, "Requirements phase should succeed");
    assert!(
        !result1.artifact_paths.is_empty(),
        "Should create artifacts"
    );

    // Verify Requirements artifacts were created
    assert!(
        result1
            .artifact_paths
            .iter()
            .any(|p| p.to_string_lossy().contains("requirements")),
        "Should create requirements artifacts"
    );

    // Execute Design phase
    let result2 = orchestrator.execute_design_phase(&config).await?;
    assert!(result2.success, "Design phase should succeed");
    assert!(
        !result2.artifact_paths.is_empty(),
        "Should create artifacts"
    );

    // Verify Design artifacts were created
    assert!(
        result2
            .artifact_paths
            .iter()
            .any(|p| p.to_string_lossy().contains("design")),
        "Should create design artifacts"
    );

    // Execute Tasks phase
    let result3 = orchestrator.execute_tasks_phase(&config).await?;
    assert!(result3.success, "Tasks phase should succeed");
    assert!(
        !result3.artifact_paths.is_empty(),
        "Should create artifacts"
    );

    // Verify Tasks artifacts were created
    assert!(
        result3
            .artifact_paths
            .iter()
            .any(|p| p.to_string_lossy().contains("tasks")),
        "Should create tasks artifacts"
    );

    Ok(())
}

/// Test that phase dependency enforcement works
#[tokio::test]
async fn test_phase_dependency_enforcement() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("dependency");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

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

    // Execute Requirements first
    orchestrator.execute_requirements_phase(&config).await?;

    // Now Design should succeed
    let result = orchestrator.execute_design_phase(&config).await;
    assert!(result.is_ok(), "Design should succeed after Requirements");

    // Try to execute Tasks without Design - should fail
    // (Requirements is done but Design is not)
    // Actually, we just executed Design above, so let's test a different scenario

    // Create a new orchestrator for a fresh test
    let (orchestrator2, _temp_dir2) = setup_test_environment_with_files("dependency2");

    // Execute only Requirements
    orchestrator2.execute_requirements_phase(&config).await?;

    // Try to execute Tasks without Design - should fail
    let result = orchestrator2.execute_tasks_phase(&config).await;
    assert!(result.is_err(), "Tasks should fail without Design");

    Ok(())
}

/// Test that packet preview is written to context directory
#[tokio::test]
async fn test_packet_preview_written() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("preview");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase
    orchestrator.execute_requirements_phase(&config).await?;

    // Check that packet preview was written
    let context_dir = orchestrator.artifact_manager().context_path();
    let preview_path = context_dir.join("requirements-packet.txt");

    assert!(
        preview_path.exists(),
        "Packet preview should be written to context directory"
    );

    // Verify preview contains file content markers
    let preview_content = fs::read_to_string(&preview_path)?;
    assert!(
        preview_content.contains("===") || !preview_content.is_empty(),
        "Packet preview should contain content"
    );

    Ok(())
}

/// Test that artifacts are generated (markdown + core YAML)
#[tokio::test]
async fn test_artifacts_generated() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("artifacts");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase
    let result = orchestrator.execute_requirements_phase(&config).await?;

    // Verify both markdown and YAML artifacts were created
    let has_markdown = result
        .artifact_paths
        .iter()
        .any(|p| p.to_string_lossy().ends_with(".md"));
    let has_yaml = result
        .artifact_paths
        .iter()
        .any(|p| p.to_string_lossy().ends_with(".yaml"));

    assert!(has_markdown, "Should create markdown artifact");
    assert!(has_yaml, "Should create YAML artifact");

    // Verify artifacts exist on disk
    for artifact_path in &result.artifact_paths {
        assert!(
            artifact_path.exists(),
            "Artifact should exist: {:?}",
            artifact_path
        );
    }

    Ok(())
}

/// Test that receipts include accurate packet evidence
#[tokio::test]
async fn test_receipt_packet_evidence_accuracy() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment_with_files("evidence-accuracy");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase
    orchestrator.execute_requirements_phase(&config).await?;

    // Read the receipt
    let receipt_manager =
        xchecker::receipt::ReceiptManager::new(orchestrator.artifact_manager().base_path());

    let receipt = receipt_manager
        .read_latest_receipt(PhaseId::Requirements)?
        .expect("Receipt should exist");

    // Verify packet evidence has correct structure
    assert!(receipt.packet.max_bytes > 0, "Max bytes should be set");
    assert!(receipt.packet.max_lines > 0, "Max lines should be set");

    // Verify files in evidence match files in spec directory
    let expected_files = vec!["README.md", "SPEC-001.md", "config.core.yaml"];

    for expected_file in expected_files {
        let found = receipt
            .packet
            .files
            .iter()
            .any(|f| f.path.contains(expected_file));
        assert!(
            found,
            "Packet evidence should include file: {}",
            expected_file
        );
    }

    Ok(())
}