use std::collections::BTreeSet;
use std::path::PathBuf;
use std::process::Command;
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn manifest_path() -> PathBuf {
repo_root().join("tools/scripts/build-embedded-ts.ts")
}
fn declared_assets() -> BTreeSet<String> {
let text =
std::fs::read_to_string(manifest_path()).expect("read tools/scripts/build-embedded-ts.ts");
let start = text
.find("export const ASSETS")
.expect("build-embedded-ts.ts must export const ASSETS");
let eq = start
+ text[start..]
.find('=')
.expect("an `=` after the declaration");
let open = eq + text[eq..].find('[').expect("an `[` after the declaration");
let end = open
+ text[open..]
.find("];")
.expect("the ASSETS array must terminate with `];`");
let mut paths = BTreeSet::new();
for line in text[start..end].lines() {
for field in ["source", "artifact"] {
let marker = format!("{field}: \"");
let Some(at) = line.find(&marker) else {
continue;
};
let rest = &line[at + marker.len()..];
let close = rest.find('"').expect("a closing quote for the path");
paths.insert(rest[..close].to_string());
}
}
paths
}
fn embedded_in_rust() -> BTreeSet<String> {
let src = repo_root().join("src");
let mut found = BTreeSet::new();
let mut stack = vec![src];
while let Some(dir) = stack.pop() {
let entries = std::fs::read_dir(&dir).expect("read a src/ directory");
for entry in entries {
let path = entry.expect("dir entry").path();
if path.is_dir() {
stack.push(path);
continue;
}
if path.extension().and_then(|e| e.to_str()) != Some("rs") {
continue;
}
let text = std::fs::read_to_string(&path).expect("read a source file");
let parent = path.parent().expect("a source file has a parent");
for (idx, _) in text.match_indices("include_str!(\"") {
let rest = &text[idx + "include_str!(\"".len()..];
let close = rest.find('"').expect("a closing quote");
let target = parent.join(&rest[..close]);
let rel = target
.strip_prefix(repo_root())
.expect("include_str! inside the repo");
found.insert(rel.to_string_lossy().replace('\\', "/"));
}
}
}
found
}
fn node_available() -> bool {
Command::new("node")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
fn compile_script() -> PathBuf {
repo_root().join("tools/scripts/build-embedded-ts.ts")
}
fn check_pipeline() -> (bool, String) {
let output = Command::new("node")
.arg(compile_script())
.arg("--check")
.current_dir(repo_root())
.output()
.expect("spawn node tools/scripts/build-embedded-ts.ts --check");
let mut text = String::from_utf8_lossy(&output.stdout).into_owned();
text.push_str(&String::from_utf8_lossy(&output.stderr));
(output.status.success(), text.trim().to_string())
}
#[test]
fn drift_check_reports_nothing() {
if !node_available() {
if std::env::var("LAGRANGE_ALLOW_NO_NODE").as_deref() == Ok("1") {
use std::io::Write as _;
let _ = writeln!(
std::io::stderr(),
"WARN embedded_ts: SKIPPED — node is not on PATH and LAGRANGE_ALLOW_NO_NODE=1: \
the committed embedded artifacts are NOT verified in this run"
);
return;
}
panic!(
"embedded_ts: node is not on PATH, so the committed embedded artifacts cannot be \
verified. Install Node (the compile needs it), or set LAGRANGE_ALLOW_NO_NODE=1 to \
record that this run deliberately went unverified."
);
}
let (ok, text) = check_pipeline();
assert!(
ok,
"the committed embedded artifacts must equal what \
`node tools/scripts/build-embedded-ts.ts` emits — rebuild with \
`node tools/scripts/build-embedded-ts.ts` (or `just embedded`) and commit the .js.\n{text}"
);
let assets = declared_assets().len() / 2;
assert!(
text.contains(&format!("{assets} artifacts match")),
"the compile must report on every declared asset; declared={assets} output:\n{text}"
);
}
#[test]
fn the_embedded_manifest_matches_the_rust_include_sites() {
let declared = declared_assets();
assert!(
!declared.is_empty(),
"ASSETS must declare the embedded sources"
);
let embedded = embedded_in_rust();
let declared_artifacts: BTreeSet<String> = declared
.iter()
.filter(|p| p.ends_with(".js"))
.cloned()
.collect();
let declared_sources: BTreeSet<String> = declared
.iter()
.filter(|p| p.ends_with(".ts"))
.cloned()
.collect();
assert_eq!(
declared_artifacts.len(),
declared_sources.len(),
"every asset needs one source and one artifact; declared={declared:?}"
);
let missing_from_rust: Vec<_> = declared_artifacts.difference(&embedded).cloned().collect();
let rust_js: BTreeSet<String> = embedded
.iter()
.filter(|p| p.ends_with(".js"))
.cloned()
.collect();
let missing_from_manifest: Vec<_> = rust_js.difference(&declared_artifacts).cloned().collect();
assert!(
missing_from_rust.is_empty(),
"these compiled artifacts are declared in tools/scripts/build-embedded-ts.ts but no \
include_str! reads them (compiled for nothing, or the Rust site moved): {missing_from_rust:?}"
);
assert!(
missing_from_manifest.is_empty(),
"these files are include_str!-embedded but not produced by \
tools/scripts/build-embedded-ts.ts (edit the .ts and declare it, never the .js): \
{missing_from_manifest:?}"
);
for source in &declared_sources {
let path = repo_root().join(source);
assert!(path.is_file(), "declared source {source} does not exist");
let sibling: PathBuf = path.with_extension("js");
assert!(
sibling.is_file(),
"declared source {source} has no emitted artifact beside it ({}) — \
run node tools/scripts/build-embedded-ts.ts",
sibling.display()
);
}
}