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
//! Integration tests for lockfile system (FR-LOCK-006, FR-LOCK-007, FR-LOCK-008)
//!
//! **WHITE-BOX TEST**: This test uses internal module APIs (`lock::{RunContext, XCheckerLock}`)
//! 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:
//! - Lockfile creation with --create-lock flag
//! - Drift detection for each field (model, CLI version, schema)
//! - --strict-lock enforcement
//! - Lockfile loading and validation

use anyhow::Result;
use std::fs;
use tempfile::TempDir;
use xchecker::lock::{RunContext, XCheckerLock};

/// Setup test environment with isolated home directory
fn setup_test_env() -> TempDir {
    xchecker::paths::with_isolated_home()
}

/// Test FR-LOCK-006: Lockfile creation with init --create-lock
#[test]
fn test_lockfile_creation_with_init() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-lockfile-creation";

    // Create lockfile
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());

    lock.save(spec_id)?;

    // Verify lockfile exists
    let lock_path = xchecker::paths::spec_root(spec_id)
        .as_std_path()
        .join("lock.json");
    assert!(lock_path.exists(), "Lockfile should be created");

    // Verify lockfile content
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    assert_eq!(loaded.schema_version, "1");
    assert_eq!(loaded.model_full_name, "haiku");
    assert_eq!(loaded.claude_cli_version, "0.8.1");

    println!("✓ Lockfile creation test passed");
    Ok(())
}

/// Test FR-LOCK-007: Drift detection for model field
#[test]
fn test_drift_detection_model_field() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-drift-model";

    // Create lockfile with specific model
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    // Load and check drift with different model
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    let context = RunContext {
        model_full_name: "sonnet-20250101".to_string(),
        claude_cli_version: "0.8.1".to_string(),
        schema_version: "1".to_string(),
    };

    let drift = loaded.detect_drift(&context);
    assert!(drift.is_some(), "Should detect model drift");

    let drift = drift.unwrap();
    assert!(
        drift.model_full_name.is_some(),
        "Model drift should be detected"
    );
    assert!(
        drift.claude_cli_version.is_none(),
        "CLI version should not drift"
    );
    assert!(drift.schema_version.is_none(), "Schema should not drift");

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

    println!("✓ Model drift detection test passed");
    Ok(())
}

/// Test FR-LOCK-007: Drift detection for CLI version field
#[test]
fn test_drift_detection_cli_version_field() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-drift-cli";

    // Create lockfile with specific CLI version
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    // Load and check drift with different CLI version
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    let context = RunContext {
        model_full_name: "haiku".to_string(),
        claude_cli_version: "0.9.0".to_string(),
        schema_version: "1".to_string(),
    };

    let drift = loaded.detect_drift(&context);
    assert!(drift.is_some(), "Should detect CLI version drift");

    let drift = drift.unwrap();
    assert!(drift.model_full_name.is_none(), "Model should not drift");
    assert!(
        drift.claude_cli_version.is_some(),
        "CLI version drift should be detected"
    );
    assert!(drift.schema_version.is_none(), "Schema should not drift");

    let cli_drift = drift.claude_cli_version.unwrap();
    assert_eq!(cli_drift.locked, "0.8.1");
    assert_eq!(cli_drift.current, "0.9.0");

    println!("✓ CLI version drift detection test passed");
    Ok(())
}

/// Test FR-LOCK-007: Drift detection for schema version field
#[test]
fn test_drift_detection_schema_version_field() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-drift-schema";

    // Create lockfile with schema version 1
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    // Load and check drift with different schema version
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    let context = RunContext {
        model_full_name: "haiku".to_string(),
        claude_cli_version: "0.8.1".to_string(),
        schema_version: "2".to_string(),
    };

    let drift = loaded.detect_drift(&context);
    assert!(drift.is_some(), "Should detect schema version drift");

    let drift = drift.unwrap();
    assert!(drift.model_full_name.is_none(), "Model should not drift");
    assert!(
        drift.claude_cli_version.is_none(),
        "CLI version should not drift"
    );
    assert!(
        drift.schema_version.is_some(),
        "Schema drift should be detected"
    );

    let schema_drift = drift.schema_version.unwrap();
    assert_eq!(schema_drift.locked, "1");
    assert_eq!(schema_drift.current, "2");

    println!("✓ Schema version drift detection test passed");
    Ok(())
}

/// Test FR-LOCK-007: No drift when all values match
#[test]
fn test_no_drift_when_values_match() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-no-drift";

    // Create lockfile
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    // Load and check drift with same values
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    let context = RunContext {
        model_full_name: "haiku".to_string(),
        claude_cli_version: "0.8.1".to_string(),
        schema_version: "1".to_string(),
    };

    let drift = loaded.detect_drift(&context);
    assert!(
        drift.is_none(),
        "Should not detect drift when all values match"
    );

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

/// Test FR-LOCK-007: Multiple fields drifting simultaneously
#[test]
fn test_drift_detection_multiple_fields() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-drift-multiple";

    // Create lockfile
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    // Load and check drift with all fields different
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    let context = RunContext {
        model_full_name: "sonnet-20250101".to_string(),
        claude_cli_version: "0.9.0".to_string(),
        schema_version: "2".to_string(),
    };

    let drift = loaded.detect_drift(&context);
    assert!(drift.is_some(), "Should detect drift in multiple fields");

    let drift = drift.unwrap();
    assert!(
        drift.model_full_name.is_some(),
        "Model drift should be detected"
    );
    assert!(
        drift.claude_cli_version.is_some(),
        "CLI version drift should be detected"
    );
    assert!(
        drift.schema_version.is_some(),
        "Schema drift should be detected"
    );

    println!("✓ Multiple field drift detection test passed");
    Ok(())
}

/// Test FR-LOCK-008: Lockfile loading and validation
#[test]
fn test_lockfile_loading_and_validation() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-lockfile-validation";

    // Test 1: Load nonexistent lockfile
    let result = XCheckerLock::load(spec_id)?;
    assert!(
        result.is_none(),
        "Should return None for nonexistent lockfile"
    );

    // Test 2: Create and load valid lockfile
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    let loaded = XCheckerLock::load(spec_id)?;
    assert!(loaded.is_some(), "Should load existing lockfile");

    let loaded = loaded.unwrap();
    assert_eq!(loaded.schema_version, "1");
    assert_eq!(loaded.model_full_name, "haiku");
    assert_eq!(loaded.claude_cli_version, "0.8.1");

    // Test 3: Verify timestamp is valid
    let timestamp_str = loaded.created_at.to_rfc3339();
    assert!(
        !timestamp_str.is_empty(),
        "Timestamp should be valid RFC3339"
    );

    println!("✓ Lockfile loading and validation test passed");
    Ok(())
}

/// Test FR-LOCK-008: Corrupted lockfile handling
#[test]
fn test_corrupted_lockfile_handling() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-corrupted-lockfile";

    // Create spec directory
    let spec_root = xchecker::paths::spec_root(spec_id);
    xchecker::paths::ensure_dir_all(&spec_root)?;

    // Write corrupted JSON
    let lock_path = spec_root.as_std_path().join("lock.json");
    fs::write(&lock_path, "{ invalid json }")?;

    // Should return error for corrupted file
    let result = XCheckerLock::load(spec_id);
    assert!(result.is_err(), "Should fail to load corrupted lockfile");

    println!("✓ Corrupted lockfile handling test passed");
    Ok(())
}

/// Test FR-LOCK-008: Empty lockfile handling
#[test]
fn test_empty_lockfile_handling() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-empty-lockfile";

    // Create spec directory
    let spec_root = xchecker::paths::spec_root(spec_id);
    xchecker::paths::ensure_dir_all(&spec_root)?;

    // Write empty file
    let lock_path = spec_root.as_std_path().join("lock.json");
    fs::write(&lock_path, "")?;

    // Should return error for empty file
    let result = XCheckerLock::load(spec_id);
    assert!(result.is_err(), "Should fail to load empty lockfile");

    println!("✓ Empty lockfile handling test passed");
    Ok(())
}

/// Test FR-LOCK-008: Lockfile with missing required fields
#[test]
fn test_lockfile_missing_fields() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-missing-fields";

    // Create spec directory
    let spec_root = xchecker::paths::spec_root(spec_id);
    xchecker::paths::ensure_dir_all(&spec_root)?;

    // Write JSON with missing required fields
    let lock_path = spec_root.as_std_path().join("lock.json");
    fs::write(&lock_path, r#"{"schema_version": "1"}"#)?;

    // Should return error for missing fields
    let result = XCheckerLock::load(spec_id);
    assert!(
        result.is_err(),
        "Should fail to load lockfile with missing fields"
    );

    println!("✓ Missing fields handling test passed");
    Ok(())
}

/// Test FR-LOCK-006: Lockfile overwrite behavior
#[test]
fn test_lockfile_overwrite() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-lockfile-overwrite";

    // Create first lockfile
    let lock1 = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock1.save(spec_id)?;

    // Create second lockfile with different values
    let lock2 = XCheckerLock::new("sonnet-20250101".to_string(), "0.9.0".to_string());
    lock2.save(spec_id)?;

    // Load and verify it has the second lockfile's values
    let loaded = XCheckerLock::load(spec_id)?.expect("Lockfile should exist");
    assert_eq!(loaded.model_full_name, "sonnet-20250101");
    assert_eq!(loaded.claude_cli_version, "0.9.0");

    println!("✓ Lockfile overwrite test passed");
    Ok(())
}

/// Test FR-LOCK-006: Lockfile directory creation
#[test]
fn test_lockfile_directory_creation() -> Result<()> {
    let _temp_dir = setup_test_env();

    let spec_id = "test-new-directory";

    // Verify directory doesn't exist
    let spec_root = xchecker::paths::spec_root(spec_id);
    let lock_path = spec_root.as_std_path().join("lock.json");
    assert!(!lock_path.exists(), "Lock path should not exist initially");

    // Create lockfile (should create directory)
    let lock = XCheckerLock::new("haiku".to_string(), "0.8.1".to_string());
    lock.save(spec_id)?;

    // Verify directory and file were created
    assert!(lock_path.exists(), "Lock file should be created");
    assert!(
        lock_path.parent().unwrap().exists(),
        "Parent directory should be created"
    );

    println!("✓ Directory creation test passed");
    Ok(())
}

#[cfg(test)]
mod integration_tests {
    use super::*;

    /// Run all lockfile integration tests
    #[test]
    fn run_all_lockfile_tests() {
        println!("\n=== Running Lockfile Integration Tests ===\n");

        test_lockfile_creation_with_init().unwrap();
        test_drift_detection_model_field().unwrap();
        test_drift_detection_cli_version_field().unwrap();
        test_drift_detection_schema_version_field().unwrap();
        test_no_drift_when_values_match().unwrap();
        test_drift_detection_multiple_fields().unwrap();
        test_lockfile_loading_and_validation().unwrap();
        test_corrupted_lockfile_handling().unwrap();
        test_empty_lockfile_handling().unwrap();
        test_lockfile_missing_fields().unwrap();
        test_lockfile_overwrite().unwrap();
        test_lockfile_directory_creation().unwrap();

        println!("\n=== All Lockfile Integration Tests Passed ===\n");
    }
}