use super::*;
use crate::cli::pipeline::generate::ensure_generated_header;
use crate::core::hash::{
POLY_GENERATED_SCAN_LINES, content_has_alef_marker, deepest_hash_line, extract_hash, inject_hash_line,
};
const STAMP: &str = "0ce4d753fdb4854e44358639dcbaebee3449a4afa142dbc4f0a72aa72c214648";
fn as_written(file: &GeneratedFile) -> String {
let headered = if file.generated_header {
ensure_generated_header(&file.path, &file.content)
} else {
file.content.clone()
};
inject_hash_line(&headered, STAMP)
}
fn stamp_line_number(content: &str) -> Option<usize> {
content
.lines()
.position(|line| line.contains("alef:hash:"))
.map(|index| index + 1)
}
fn assert_content_ends_with_newline(file: &GeneratedFile) {
let tail: String = {
let mut characters: Vec<char> = file.content.chars().rev().take(24).collect();
characters.reverse();
characters.into_iter().collect()
};
assert!(
file.content.ends_with('\n'),
"{}: emitter produced content with no trailing newline. `normalize_content` hides this on \
the write path, but every direct reader of `GeneratedFile::content` (diff, verify-side \
comparisons, tests) sees the raw bytes; got tail {tail:?}",
file.path.display()
);
}
fn assert_claim_is_stamped_inside_polys_window(file: &GeneratedFile) {
let path = file.path.display().to_string();
let written = as_written(file);
assert!(
content_has_alef_marker(&written),
"{path}: emitted with `generated_header: true` but no alef marker survives \
`ensure_generated_header`, so `finalize_hashes` will never stamp it; got:\n{}",
written.lines().take(5).collect::<Vec<_>>().join("\n")
);
assert_eq!(
extract_hash(&written).as_deref(),
Some(STAMP),
"{path}: marker is present but `extract_hash` cannot recover the injected stamp, so \
`alef verify` reads the file as unstamped; got:\n{}",
written.lines().take(5).collect::<Vec<_>>().join("\n")
);
let stamp_line = stamp_line_number(&written)
.unwrap_or_else(|| panic!("{path}: claimed by alef but `inject_hash_line` wrote no stamp at all"));
assert!(
stamp_line <= POLY_GENERATED_SCAN_LINES,
"{path}: stamp landed on line {stamp_line}, past the first {POLY_GENERATED_SCAN_LINES} \
lines poly reads. poly will not skip this file, so it reformats content alef owns and \
alef rewrites the formatting on the next run"
);
}
fn require<'a>(files: &'a [GeneratedFile], relative_path: &str) -> &'a GeneratedFile {
files
.iter()
.find(|file| file.path.to_string_lossy() == relative_path)
.unwrap_or_else(|| {
let emitted: Vec<String> = files.iter().map(|f| f.path.to_string_lossy().into_owned()).collect();
panic!("{relative_path} was not emitted at all, so the marker assertions examine nothing; got {emitted:?}")
})
}
#[test]
fn rustfmt_toml_is_stamped_inside_polys_scan_window() {
let files = scaffold(&test_api(), &test_config(), &[Language::Python, Language::Node]).unwrap();
let rustfmt = require(&files, "rustfmt.toml");
assert_content_ends_with_newline(rustfmt);
assert_claim_is_stamped_inside_polys_window(rustfmt);
}
#[test]
fn poly_toml_is_stamped_inside_polys_scan_window() {
let files = scaffold(&test_api(), &test_config(), &[Language::Python, Language::Node]).unwrap();
let poly = require(&files, "poly.toml");
assert_content_ends_with_newline(poly);
assert_claim_is_stamped_inside_polys_window(poly);
}
#[test]
fn python_pyproject_toml_is_stamped_inside_polys_scan_window() {
let files = scaffold(&test_api(), &test_config(), &[Language::Python]).unwrap();
let pyproject = files
.iter()
.find(|file| file.path.file_name().is_some_and(|name| name == "pyproject.toml"))
.unwrap_or_else(|| panic!("python scaffold emitted no pyproject.toml, so this test examines nothing"));
assert_content_ends_with_newline(pyproject);
assert_claim_is_stamped_inside_polys_window(pyproject);
}
#[test]
fn rust_toolchain_toml_is_stamped_inside_polys_scan_window() {
for languages in [Vec::new(), vec![Language::Wasm]] {
let file = rust_toolchain_file(&languages);
assert_eq!(
file.path,
std::path::PathBuf::from("rust-toolchain.toml"),
"the seed must still be the repo-root toolchain file"
);
assert!(
file.content.contains("[toolchain]"),
"control: the seed must still emit a `[toolchain]` table, else the marker assertions \
cover an empty file; got:\n{}",
file.content
);
assert_content_ends_with_newline(&file);
assert_claim_is_stamped_inside_polys_window(&file);
}
}
#[test]
fn wasm_cargo_config_is_stamped_inside_polys_scan_window() {
let file = wasm_cargo_config_file();
assert!(
file.content.contains("[target.wasm32-unknown-unknown]"),
"control: the wasm seed must still carry its wasm32 target table; got:\n{}",
file.content
);
assert_content_ends_with_newline(&file);
assert_claim_is_stamped_inside_polys_window(&file);
}
#[test]
fn configured_cargo_config_is_stamped_inside_polys_scan_window() {
let configured = GeneratedFile {
path: std::path::PathBuf::from(".cargo/config.toml"),
content: render_cargo_config(&crate::core::config::ScaffoldCargo::default()),
generated_header: true,
};
assert!(
configured.content.contains("[build]"),
"control: the configured branch must still render a `[build]` table; got:\n{}",
configured.content
);
assert_content_ends_with_newline(&configured);
assert_claim_is_stamped_inside_polys_window(&configured);
}
const MINIMUM_MARKER_RAIL_FILES: usize = 10;
#[test]
fn every_marker_rail_scaffold_file_is_stamped_inside_polys_scan_window() {
let api = test_api();
let config = test_config();
let mut checked: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
let mut unmarkable: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for language in Language::ALL {
let Ok(files) = scaffold(&api, &config, &[language]) else {
continue;
};
for file in &files {
if !file.generated_header && !content_has_alef_marker(&file.content) {
continue;
}
let relative_path = file.path.to_string_lossy().into_owned();
if !content_has_alef_marker(&as_written(file)) {
unmarkable.insert(relative_path);
continue;
}
assert_claim_is_stamped_inside_polys_window(file);
checked.insert(relative_path);
}
}
assert!(
checked.len() >= MINIMUM_MARKER_RAIL_FILES,
"the sweep checked only {} marker-rail scaffold file(s), below the {MINIMUM_MARKER_RAIL_FILES} \
floor -- it has gone vacuous and would pass no matter what the emitters do. Checked: \
{checked:?}; unmarkable-by-format: {unmarkable:?}",
checked.len()
);
for expected in ["poly.toml", "rustfmt.toml"] {
assert!(
checked.contains(expected),
"{expected} is a marker-rail scaffold file but the sweep never saw it. Checked: {checked:?}"
);
}
}
#[test]
fn the_deepest_reachable_stamp_line_stays_inside_polys_scan_window() {
assert!(
deepest_hash_line() <= POLY_GENERATED_SCAN_LINES,
"a marker at alef's deepest legal position stamps line {}, past poly's {POLY_GENERATED_SCAN_LINES}-line \
window: alef would claim files poly still reformats",
deepest_hash_line()
);
}