#[cfg(test)]
mod tests {
use std::fs;
use std::path::Path;
fn test_file_contains_tests(test_file: &str, test_names: &[&str]) -> bool {
let path = Path::new("tests").join(test_file);
if !path.exists() {
return false;
}
let content = match fs::read_to_string(&path) {
Ok(c) => c,
Err(_) => return false,
};
test_names
.iter()
.all(|test_name| content.contains(&format!("fn {test_name}")))
}
fn readme_documents_feature(feature_keywords: &[&str]) -> bool {
let readme_path = Path::new("README.md");
if !readme_path.exists() {
return false;
}
let content = match fs::read_to_string(readme_path) {
Ok(c) => c,
Err(_) => return false,
};
feature_keywords
.iter()
.all(|keyword| content.contains(keyword))
}
#[test]
fn test_timeout_feature_documented() {
assert!(
readme_documents_feature(&["phase_timeout", "Phase execution exceeded timeout"]),
"Timeout feature should be documented in README.md"
);
let timeout_tests = [
"test_phase_timeout_constants",
"test_phase_timeout_minimum_enforcement",
"test_phase_timeout_from_config",
"test_timeout_warning_format",
"test_timeout_exit_code",
];
assert!(
test_file_contains_tests("test_phase_timeout.rs", &timeout_tests),
"Timeout feature should have smoke tests in test_phase_timeout.rs"
);
let exit_tests = ["test_receipt_exit_code_alignment", "test_exit_code_mapping"];
assert!(
test_file_contains_tests("test_exit_alignment.rs", &exit_tests),
"Exit alignment tests should exist in test_exit_alignment.rs"
);
let exit_codes_path = Path::new("crates/xchecker-utils/src/exit_codes.rs");
assert!(exit_codes_path.exists(), "Exit codes module should exist");
let exit_codes_content = fs::read_to_string(exit_codes_path).unwrap();
assert!(
exit_codes_content.contains("PHASE_TIMEOUT") && exit_codes_content.contains("10"),
"PHASE_TIMEOUT exit code should be defined as 10"
);
}
#[test]
fn test_lockfile_feature_documented() {
assert!(
readme_documents_feature(&["Lockfile System", "lock_drift", "--strict-lock"]),
"Lockfile drift feature should be documented in README.md"
);
let lock_path = Path::new("crates/xchecker-utils/src/lock.rs");
assert!(lock_path.exists(), "Lock module should exist");
let lock_content = fs::read_to_string(lock_path).unwrap();
assert!(
lock_content.contains("Lock") || lock_content.contains("lock"),
"Lock module should handle lock files"
);
let status_path = Path::new("crates/xchecker-status/src/status.rs");
assert!(status_path.exists(), "Status module should exist");
let status_content = fs::read_to_string(status_path).unwrap();
assert!(
status_content.contains("drift") || status_content.contains("lock"),
"Status module should handle drift detection"
);
}
#[test]
fn test_fixup_feature_documented() {
assert!(
readme_documents_feature(&["Fixup System", "--apply-fixups", "Preview Mode"]),
"Fixup validation feature should be documented in README.md"
);
let fixup_tests = [
"test_apply_fixups_config_handling",
"test_fixup_parser_mode_behavior",
"test_fixup_mode_enum",
];
assert!(
test_file_contains_tests("test_apply_fixups_flag.rs", &fixup_tests),
"Fixup feature should have smoke tests in test_apply_fixups_flag.rs"
);
let m4_gate_path = Path::new("tests/m4_gate_validation.rs");
assert!(
m4_gate_path.exists(),
"M4 gate validation tests should exist"
);
let m4_content = fs::read_to_string(m4_gate_path).unwrap();
assert!(
m4_content.contains("fixup") || m4_content.contains("Fixup"),
"M4 gate tests should cover fixup functionality"
);
let fixup_path = Path::new("crates/xchecker-engine/src/fixup/mod.rs");
assert!(fixup_path.exists(), "Fixup module should exist");
let fixup_content = fs::read_to_string(fixup_path).unwrap();
assert!(
fixup_content.contains("Preview") || fixup_content.contains("Apply"),
"Fixup module should handle preview and apply modes"
);
assert!(
fixup_content.contains("path") || fixup_content.contains("validate"),
"Fixup module should validate paths"
);
}
#[test]
fn test_exit_alignment_documented() {
assert!(
readme_documents_feature(&["Exit Codes", "exit_code", "Standardized Exit Codes"]),
"Exit code alignment feature should be documented in README.md"
);
let exit_tests = [
"test_error_kind_serialization",
"test_exit_code_mapping",
"test_receipt_exit_code_alignment",
];
assert!(
test_file_contains_tests("test_exit_alignment.rs", &exit_tests),
"Exit alignment feature should have smoke tests in test_exit_alignment.rs"
);
let exit_codes_path = Path::new("crates/xchecker-utils/src/exit_codes.rs");
assert!(exit_codes_path.exists(), "Exit codes module should exist");
let exit_codes_content = fs::read_to_string(exit_codes_path).unwrap();
let expected_codes = [
("CLI_ARGS", "2"),
("PACKET_OVERFLOW", "7"),
("SECRET_DETECTED", "8"),
("LOCK_HELD", "9"),
("PHASE_TIMEOUT", "10"),
("CLAUDE_FAILURE", "70"),
];
for (name, code) in &expected_codes {
assert!(
exit_codes_content.contains(name) && exit_codes_content.contains(code),
"Exit code {name} ({code}) should be defined"
);
}
let error_path = Path::new("crates/xchecker-utils/src/error.rs");
assert!(error_path.exists(), "Error module should exist");
let types_path = Path::new("crates/xchecker-utils/src/types.rs");
assert!(types_path.exists(), "Types module should exist");
let types_content = fs::read_to_string(types_path).unwrap();
assert!(
types_content.contains("ErrorKind"),
"Types module should define ErrorKind enum"
);
let receipt_path = Path::new("crates/xchecker-receipt/src/errors.rs");
assert!(receipt_path.exists(), "Receipt module should exist");
let receipt_content = fs::read_to_string(receipt_path).unwrap();
assert!(
receipt_content.contains("exit_code") || receipt_content.contains("error"),
"Receipt module should handle error receipts with exit codes"
);
}
#[test]
fn test_all_documented_features_have_tests() {
let features = [
("Multi-Phase Orchestration", "test_phase_timeout.rs"),
("Lockfile System", "m3_gate_validation.rs"),
("Fixup System", "test_apply_fixups_flag.rs"),
("Exit Codes", "test_exit_alignment.rs"),
];
for (feature_name, test_file) in &features {
assert!(
readme_documents_feature(&[feature_name]),
"Feature '{feature_name}' should be documented in README.md"
);
let test_path = Path::new("tests").join(test_file);
assert!(
test_path.exists(),
"Feature '{feature_name}' should have tests in {test_file}"
);
}
}
#[test]
fn test_feature_side_effects_documented() {
let timeout_side_effects = [
".partial.md", "phase_timeout:", "exit_code", "error_kind", ];
let test_timeout_path = Path::new("tests/test_phase_timeout.rs");
let timeout_content = fs::read_to_string(test_timeout_path).unwrap();
for side_effect in &timeout_side_effects {
assert!(
timeout_content.contains(side_effect),
"Timeout tests should verify side effect: {side_effect}"
);
}
let lockfile_side_effects = [
"lock_drift", "strict-lock", ];
let readme_content = fs::read_to_string("README.md").unwrap();
for side_effect in &lockfile_side_effects {
assert!(
readme_content.contains(side_effect),
"README should document lockfile side effect: {side_effect}"
);
}
let fixup_side_effects = [
"Preview", "Apply", "path", ];
let test_fixup_path = Path::new("tests/test_apply_fixups_flag.rs");
let fixup_content = fs::read_to_string(test_fixup_path).unwrap();
for side_effect in &fixup_side_effects {
assert!(
fixup_content.contains(side_effect),
"Fixup tests should verify side effect: {side_effect}"
);
}
let exit_side_effects = [
"exit_code", "error_kind", "receipt", ];
let test_exit_path = Path::new("tests/test_exit_alignment.rs");
let exit_content = fs::read_to_string(test_exit_path).unwrap();
for side_effect in &exit_side_effects {
assert!(
exit_content.contains(side_effect),
"Exit alignment tests should verify side effect: {side_effect}"
);
}
}
}