use std::path::PathBuf;
const ADR: &str = "docs/architecture/ADR-045-aee-substrate-signed-run-end-seal.md";
const CITING_SOURCES: &[&str] = &[
"crates/assay-cli/src/aee_seal.rs",
"crates/assay-cli/src/aee_seal_envelope.rs",
"scripts/experiments/aee_landlock_seal_fixture.py",
];
const KNOWN_CITATIONS: usize = 12;
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..")
}
fn read(rel: &str) -> String {
let path = repo_root().join(rel);
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()))
}
fn flatten(s: &str) -> String {
s.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn trim_terminal(s: &str) -> &str {
s.trim_end_matches([',', '.', ';', ':'])
}
fn comment_body(line: &str) -> Option<&str> {
let t = line.trim_start();
for marker in ["///", "//!", "//", "#"] {
if let Some(rest) = t.strip_prefix(marker) {
return Some(rest);
}
}
None
}
fn comment_blocks(src: &str) -> Vec<String> {
let mut blocks = Vec::new();
let mut current: Vec<&str> = Vec::new();
for line in src.lines() {
match comment_body(line) {
Some(body) => current.push(body),
None => {
if !current.is_empty() {
blocks.push(flatten(¤t.join(" ")));
current.clear();
}
}
}
}
if !current.is_empty() {
blocks.push(flatten(¤t.join(" ")));
}
blocks
}
fn quoted_spans(block: &str) -> Vec<String> {
let mut spans = Vec::new();
let mut rest = block;
while let Some(open) = rest.find('"') {
rest = &rest[open + 1..];
let Some(close) = rest.find('"') else { break };
let span = rest[..close].trim().to_string();
if !span.is_empty() {
spans.push(span);
}
rest = &rest[close + 1..];
}
spans
}
fn citations() -> Vec<(&'static str, String)> {
let mut found = Vec::new();
for src in CITING_SOURCES {
for block in comment_blocks(&read(src)) {
if !block.contains("ADR-045") {
continue;
}
for span in quoted_spans(&block) {
found.push((*src, span));
}
}
}
found
}
#[test]
fn every_quotation_from_the_adr_is_still_in_the_adr() {
let adr = flatten(&read(ADR));
let citations = citations();
assert!(
citations.len() >= KNOWN_CITATIONS,
"found {} citations, expected at least {KNOWN_CITATIONS}. Either they were deleted or the \
extractor stopped seeing them; both are worth looking at before lowering this number.",
citations.len()
);
let missing: Vec<String> = citations
.iter()
.filter(|(_, q)| !adr.contains(trim_terminal(&flatten(q))))
.map(|(f, q)| format!("{f}: \"{q}\""))
.collect();
assert!(
missing.is_empty(),
"these are quoted as ADR-045 and are not in it. Either the ADR moved and the comment needs \
updating, or the quotation was never exact:\n {}",
missing.join("\n ")
);
}
#[test]
fn a_quotation_absent_from_the_adr_is_rejected() {
let adr = flatten(&read(ADR));
let fabricated = "the ADR does not contain this sentence anywhere at all";
assert!(
!adr.contains(fabricated),
"the containment check accepted a fabricated quotation, so it cannot fail"
);
}
#[test]
fn no_citation_points_at_a_line_number() {
let pattern = regex_lite_line_citation();
let mut offenders = Vec::new();
for src in CITING_SOURCES {
for (n, line) in read(src).lines().enumerate() {
let Some(body) = comment_body(line) else {
continue;
};
if pattern(body) {
offenders.push(format!("{src}:{}: {}", n + 1, body.trim()));
}
}
}
assert!(
offenders.is_empty(),
"cite the sentence, not its position -- a line number decays on someone else's edit and \
nothing reads it:\n {}",
offenders.join("\n ")
);
}
fn regex_lite_line_citation() -> impl Fn(&str) -> bool {
|body: &str| {
let flat = flatten(body);
for marker in [" line ", " lines "] {
let mut rest = flat.as_str();
while let Some(i) = rest.find(marker) {
let after = &rest[i + marker.len()..];
if after.chars().next().is_some_and(|c| c.is_ascii_digit()) {
if flat.contains("ADR-045") {
return true;
}
}
rest = &rest[i + marker.len()..];
}
}
false
}
}