//! External acceptance driver for the public full-job API, not a runtime frontend. //! Uses a real native LaTeX image and isolated copies of the frozen A4 project. use std::{ fs, path::{Path, PathBuf}, sync::Arc, }; use texrra_engine::native::{CompileRequest, CompiledDocument, Entry, NativeCompiler}; use texrra_tex_core::{ artifacts::file_version, execution::JobTime, render::DisplayItem, resources::WorldConfig, }; fn request(cwd: &Path, image: &Path, roots: &[PathBuf]) -> Result { let world = WorldConfig::from_environment(cwd, roots.to_vec())?; let mut r = CompileRequest::new( world, "main.tex", "main.pdf", JobTime::new(2026, 11, 16, 123) .unwrap() .with_pdf_clock(0, 480) .unwrap(), ); r.entry = Entry::Format(image.to_owned()); r.etex = true; r.limits.steps = 600_000_000; r.resource_manifest = Some("resources.json".into()); r.layout_manifest = Some("layout.json".into()); Ok(r) } fn compile( compiler: &NativeCompiler, cwd: &Path, image: &Path, roots: &[PathBuf], ) -> Result, String> { compiler .compile(request(cwd, image, roots)?) .map_err(|e| e.to_string()) } fn snapshot(path: &Path) -> Result, String> { file_version(path) } fn main() { if let Err(e) = run() { eprintln!("{e}"); std::process::exit(1); } } fn run() -> Result<(), String> { let mut args = std::env::args_os().skip(1); let image = PathBuf::from( args.next() .ok_or("native_project_session IMAGE NEW_OUTPUT_DIR TEXMF_ROOT...")?, ) .canonicalize() .map_err(|e| e.to_string())?; let out = PathBuf::from(args.next().ok_or("missing new output directory")?); let roots: Vec = args.map(PathBuf::from).collect(); if roots.is_empty() { return Err("explicit TEXMF roots required".into()); } fs::create_dir(&out).map_err(|e| format!("new session directory: {e}"))?; let out = out.canonicalize().map_err(|e| e.to_string())?; let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/native-format-stage/a4-project"); let original_main = fs::read_to_string(fixture.join("main.tex")).map_err(|e| e.to_string())?; let original_child = fs::read_to_string(fixture.join("chapter.tex")).map_err(|e| e.to_string())?; let original_versions = [ snapshot(&fixture.join("main.tex"))?, snapshot(&fixture.join("chapter.tex"))?, ]; let shared = out.join("shared"); fs::create_dir(&shared).map_err(|e| e.to_string())?; let compiler = NativeCompiler::default(); let cases = [ ( "unmodified", original_main.clone(), "chapter.tex", original_child.clone(), ), ( "body", original_main.clone(), "chapter.tex", format!("{original_child}\\par A session body edit.\n"), ), ( "input", original_main.replace(r"\input{chapter}", r"\input{chapter-edit}"), "chapter-edit.tex", format!("{original_child}\\par A changed input file.\n"), ), ( "macro", original_main.replace("Native project", "A changed project macro"), "chapter.tex", original_child.clone(), ), ( "font", original_main.clone(), "chapter.tex", format!( "{original_child}\\par {{\\fontsize{{12}}{{14}}\\selectfont Explicit font edit.}}\n" ), ), ( "aux", original_main.clone(), "chapter.tex", format!( "{original_child}\\par \\ifdefined\\SessionAux\\SessionAux\\else No aux override.\\fi\n" ), ), ( "math", original_main.clone(), "chapter.tex", format!("{original_child}\\par Math baseline. $x_i^2+\\frac{{a}}{{b}}+\\sqrt{{y}}$.\n"), ), ( "math-macro", original_main.replace( r"\begin{document}", r"\newcommand{\ProjectFormula}[1]{#1_j^3+\frac{c}{d}+\sqrt{z}}\begin{document}", ), "chapter.tex", format!("{original_child}\\par Math macro edit. $\\ProjectFormula{{u}}$.\n"), ), ( "math-font", original_main.clone(), "chapter.tex", format!( "{original_child}\\par {{\\fontsize{{12}}{{14}}\\selectfont Math font edit. $v_k^4+\\frac{{e}}{{f}}+\\sqrt{{w}}$}}.\n" ), ), ]; let mut rows = Vec::new(); for (name, main, child_name, child) in cases { let case = out.join(name); fs::create_dir(&case).map_err(|e| e.to_string())?; let clean = case.join("clean"); fs::create_dir(&clean).map_err(|e| e.to_string())?; for dir in [&shared, &clean, &case] { fs::write(dir.join("main.tex"), &main).map_err(|e| e.to_string())?; fs::write(dir.join(child_name), &child).map_err(|e| e.to_string())?; } if name == "aux" { use std::io::Write; let mut file = fs::OpenOptions::new() .append(true) .open(shared.join("main.aux")) .map_err(|e| e.to_string())?; writeln!(file, "\\gdef\\SessionAux{{Auxiliary edit visible}}") .map_err(|e| e.to_string())?; } if let Ok(before) = fs::read(shared.join("main.aux")) { // Give the independent job genuine ownership, then supply exactly // the same observed aux bytes. Never adopt an unknown user input. compile(&NativeCompiler::default(), &clean, &image, &roots)?; fs::write(clean.join("main.aux"), before).map_err(|e| e.to_string())?; } for pass in 1..=3 { let before = snapshot(&shared.join("main.aux"))?; let a = compile(&compiler, &shared, &image, &roots)?; let b = compile(&NativeCompiler::default(), &clean, &image, &roots)?; if a.pages != b.pages || a.pdf != b.pdf || a.source_map.pages != b.source_map.pages || a.source_map .sources .iter() .map(|s| (&s.version, s.kind)) .collect::>() != b.source_map .sources .iter() .map(|s| (&s.version, s.kind)) .collect::>() || fs::read(shared.join("main.aux")).map_err(|e| e.to_string())? != fs::read(clean.join("main.aux")).map_err(|e| e.to_string())? { return Err(format!("session/clean output differs: {name} pass {pass}")); } for file in ["main.tex", child_name] { let path = shared.join(file); if !a.resources.trace.reads.iter().any(|v| v.path == path) { return Err(format!("actual source not read: {}", path.display())); } } if name == "font" && !a.pages.iter().flat_map(|p| p.items.iter()).any( |item| matches!(item,DisplayItem::NativeGlyph {font,..} if font.name=="cmr12"), ) { return Err("font edit did not use actual cmr12 glyphs".into()); } if name.starts_with("math") { use texrra_tex_core::provenance::Derivation; if !a.source_map.is_complete() || ![Derivation::Fraction, Derivation::Radical].iter().all(|d| { a.source_map .pages .iter() .flat_map(|p| &p.entries) .any(|e| e.source.derivations.contains(d)) }) { return Err(format!( "real mathematical paint or sources missing: {name} pass {pass}" )); } } if name == "math-font" && !a .pages .iter() .flat_map(|p| &p.items) .any(|i| matches!(i,DisplayItem::NativeGlyph{font,..} if font.name=="cmmi12")) { return Err("real NFSS math size did not use cmmi12".into()); } let pdf = case.join(format!("pass-{pass}.pdf")); fs::write(&pdf, a.pdf.as_deref().ok_or("no actual PDF")?).map_err(|e| e.to_string())?; let aux = case.join(format!("pass-{pass}.aux")); fs::copy(shared.join("main.aux"), &aux).map_err(|e| e.to_string())?; let receipt = &a.resources.generated_artifacts; let layout = case.join(format!("pass-{pass}-layout.json")); fs::copy(shared.join("layout.json"), &layout).map_err(|e| e.to_string())?; let map = case.join(format!("pass-{pass}-source-map.json")); fs::write( &map, serde_json::to_vec_pretty(&a.source_map).map_err(|e| e.to_string())?, ) .map_err(|e| e.to_string())?; let mapped: usize = a.source_map.pages.iter().map(|p| p.entries.len()).sum(); let unmapped: usize = a.source_map.pages.iter().map(|p| p.unmapped.len()).sum(); rows.push(serde_json::json!({"case":name,"pass":pass,"revision":a.revision(),"pages":a.pages.len(), "pdf":snapshot(&pdf)?,"aux_before":before,"aux_after":snapshot(&aux)?, "source_map":snapshot(&map)?,"mapped_paint":mapped,"unmapped_paint":unmapped,"shared_clean_source_map_equal":true, "layout":snapshot(&layout)?, "source_versions":a.execution.source_versions,"resource_reads":a.resources.trace.reads, "generated_artifacts":receipt,"shared_clean_pages_pdf_aux_equal":true})); fs::write( out.join("progress.json"), serde_json::to_vec_pretty(&serde_json::json!({"passed":false,"rows":rows})) .map_err(|e| e.to_string())?, ) .map_err(|e| e.to_string())?; } } if [ snapshot(&fixture.join("main.tex"))?, snapshot(&fixture.join("chapter.tex"))?, ] != original_versions { return Err("original frozen project changed".into()); } let report = serde_json::json!({"schema":1,"passed":true,"scope":"full native API session and retained paint source maps vs independent clean jobs; uncollected paths explicitly unmapped, no incremental/performance claim", "format":snapshot(&image)?,"original_sources":original_versions,"rows":rows}); fs::write( out.join("report.json"), serde_json::to_vec_pretty(&report).map_err(|e| e.to_string())?, ) .map_err(|e| e.to_string())?; println!("27 shared revisions match independent clean jobs; original sources unchanged"); Ok(()) }