//! Compiler-session regression tests. //! //! These tests drive the same public `CompilerSession` API used by the HTTP //! server, so they cover parsing, layout, SVG serialization, and PDF painting //! without network or process setup. use std::path::PathBuf; use serde_json::Value; use texrra_engine::rust_typeset::CompilerSession; fn preview(session: &mut CompilerSession, source: &str, file_name: &str) -> Value { let json = session .preview_json(source, file_name) .unwrap_or_else(|error| panic!("preview failed for {file_name}: {error}")); serde_json::from_str(&json).unwrap_or_else(|error| panic!("invalid preview JSON: {error}")) } fn fresh_preview(source: &str, file_name: &str) -> Value { preview(&mut CompilerSession::default(), source, file_name) } fn pages(value: &Value) -> &Vec { value["pages"] .as_array() .unwrap_or_else(|| panic!("preview has no pages array: {value}")) } fn number(value: &Value, key: &str) -> f64 { value[key] .as_f64() .unwrap_or_else(|| panic!("preview has no numeric {key}: {value}")) } fn page_svg(value: &Value, index: usize) -> String { pages(value)[index]["svg"] .as_str() .unwrap_or_else(|| panic!("page {index} has no SVG")) .to_string() } #[test] fn examples_compile_to_one_a4_page() { let examples = [ ("hello.tex", include_str!("../examples/hello.tex")), ("boxes.tex", include_str!("../examples/boxes.tex")), ("compat.tex", include_str!("../examples/compat.tex")), ]; for (name, source) in examples { let value = fresh_preview(source, name); assert_eq!(value["status"], "ok", "{name}: {value}"); assert_eq!(pages(&value).len(), 1, "{name} page count"); assert!( (number(&value, "pageWidthMm") - 210.0).abs() < 0.1, "{name} width" ); assert!( (number(&value, "pageHeightMm") - 297.0).abs() < 0.1, "{name} height" ); } } #[test] fn hello_compiles_to_pdf() { let source = include_str!("../examples/hello.tex"); let pdf = CompilerSession::default() .compile(source, "hello.tex") .expect("hello PDF"); assert!(pdf.starts_with(b"%PDF-"), "missing PDF header"); assert!( pdf.len() > 10_000, "suspiciously small PDF: {} bytes", pdf.len() ); } #[test] fn arxiv_fixture_is_three_letter_pages() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("fixtures") .join("arxiv-template") .join("template.tex"); let source = std::fs::read_to_string(&path).expect("read arXiv fixture"); let file_name = path.to_string_lossy().to_string(); let value = fresh_preview(&source, &file_name); assert_eq!(value["status"], "ok", "{}", value["message"]); assert_eq!(pages(&value).len(), 3, "arXiv page count"); assert!( (number(&value, "pageWidthMm") - 215.9).abs() < 0.1, "Letter width" ); assert!( (number(&value, "pageHeightMm") - 279.4).abs() < 0.1, "Letter height" ); } #[test] fn starred_headings_are_unnumbered() { let source = r#"\documentclass{article} \begin{document} \section*{Starred Heading} \section{Numbered Heading} \end{document} "#; let value = fresh_preview(source, "headings.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); assert!(!svg.contains("1 *"), "starred heading was numbered: {svg}"); assert!(svg.contains("Star"), "starred heading text missing"); assert!(svg.contains("Number"), "numbered heading text missing"); } #[test] fn box_and_rule_material_paints_rectangles() { let source = r#"\documentclass{article} \begin{document} \fbox{\rule{10mm}{5mm}} \hrule width 20mm height 2pt \end{document} "#; let value = fresh_preview(source, "rectangles.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); // Page background, fbox frame, the 10mm black rule, and the 20mm hrule. assert!( svg.matches("= 4, "expected four rectangles: {svg}" ); assert!( svg.contains("stroke=\"#000000\""), "fbox frame is not stroked" ); assert!(svg.contains("width=\"10."), "10mm rule missing"); assert!(svg.contains("width=\"20."), "20mm hrule missing"); } #[test] fn undefined_commands_report_an_error() { let source = r#"\documentclass{article} \begin{document} \notacommand \end{document} "#; let error = CompilerSession::default() .preview_json(source, "bad.tex") .expect_err("undefined command should fail"); assert!( error.contains("undefined control sequence"), "unexpected error: {error}" ); } #[test] fn repeated_previews_reuse_stable_page_hashes() { let mut session = CompilerSession::default(); let first_source = r#"\documentclass{article} \begin{document} First document. \end{document} "#; let second_source = r#"\documentclass{article} \begin{document} Second document. \end{document} "#; let first = preview(&mut session, first_source, "cache.tex"); let second = preview(&mut session, second_source, "cache.tex"); let again = preview(&mut session, first_source, "cache.tex"); assert_eq!(first["status"], "ok"); assert_eq!(second["status"], "ok"); assert_eq!(again["status"], "ok"); assert_eq!(first["pages"], again["pages"], "cache returned stale pages"); assert_ne!( first["pages"][0]["hash"], second["pages"][0]["hash"], "different sources produced the same page hash" ); } fn text_x(svg: &str, needle: &str) -> f64 { let closing = format!("{needle}"); let index = svg .find(&closing) .unwrap_or_else(|| panic!("missing text element containing `{needle}`")); let start = svg[..index] .rfind("().ok()) .unwrap_or_else(|| panic!("text element for `{needle}` has no x coordinate")) } #[test] fn paragraph_indent_tracks_setlength_groups_and_noindent() { let source = r#"\documentclass{article} \begin{document} \parindent=0pt First paragraph. \setlength{\parindent}{10mm} Second paragraph. {\setlength{\parindent}{0pt} Third paragraph. } Fourth paragraph. \addtolength{\parindent}{5mm} Fifth paragraph. \noindent Noindent paragraph. \end{document} "#; let value = fresh_preview(source, "parindent.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); let first = text_x(&svg, "First"); let second = text_x(&svg, "Second"); let third = text_x(&svg, "Third"); let fourth = text_x(&svg, "Fourth"); let fifth = text_x(&svg, "Fifth"); let noindent = text_x(&svg, "Noindent"); assert!( first.abs() < 0.1, "first paragraph should not be indented: {first}" ); assert!((second - 10.0).abs() < 0.25, "second indent: {second}"); assert!(third.abs() < 0.1, "grouped zero indent leaked: {third}"); assert!( (fourth - 10.0).abs() < 0.25, "group restore failed: {fourth}" ); assert!((fifth - 15.0).abs() < 0.25, "addtolength failed: {fifth}"); assert!(noindent.abs() < 0.1, "noindent ignored: {noindent}"); } #[test] fn default_parindent_is_seventeen_points() { let source = r#"\documentclass{article} \begin{document} Default indent. \end{document} "#; let value = fresh_preview(source, "default-parindent.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); let x = text_x(&svg, "Default"); // 17pt is 5.98mm. assert!((x - 5.98).abs() < 0.15, "default parindent: {x}"); } #[test] fn hfill_fills_the_remaining_line() { let source = r#"\documentclass{article} \begin{document} \setlength{\parindent}{0pt} A\hfill B C\hfill D\hfill E \end{document} "#; let value = fresh_preview(source, "hfill.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); let a = text_x(&svg, "A"); let b = text_x(&svg, "B"); let c = text_x(&svg, "C"); let d = text_x(&svg, "D"); let e = text_x(&svg, "E"); assert!(a.abs() < 0.1, "A should stay at the left margin: {a}"); assert!(b > 150.0, "single hfill did not reach the right edge: {b}"); assert!(c.abs() < 0.1, "C should stay at the left margin: {c}"); assert!(d > 70.0 && d < 100.0, "middle hfill is not centered: {d}"); assert!(e > 150.0, "last hfill did not reach the right edge: {e}"); } #[test] fn sans_family_selects_the_sans_face() { let source = r#"\documentclass{article} \begin{document} \setlength{\parindent}{0pt} Serif text. {\sffamily Sans family text.} \textsf{Textsf sans} and {\sffamily\bfseries bold sans}. \end{document} "#; let value = fresh_preview(source, "sans.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); assert!( svg.contains("font-family=\"TexrraSans\""), "sans face missing" ); assert!( svg.contains("font-family=\"TexrraSerif\""), "serif face missing" ); assert!(svg.contains(">Sans"), "sans text missing"); assert!(svg.contains("extsf"), "textsf text missing"); assert!( svg.contains("font-family=\"TexrraSans\" font-size=\"3.8806\" font-weight=\"700\""), "bold sans face missing" ); } #[test] fn small_caps_uses_reduced_uppercase_glyphs() { let source = r#"\documentclass{article} \begin{document} \setlength{\parindent}{0pt} \textsc{Small Caps} and {\scshape mixed Case}. \end{document} "#; let value = fresh_preview(source, "smallcaps.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); assert!( svg.contains(">MALL"), "lowercase letters were not uppercased" ); assert!(!svg.contains("mall"), "small caps leaked lowercase glyphs"); assert!( svg.contains("font-size=\"3.1750\""), "small caps were not reduced to 80%" ); assert!( svg.contains("font-size=\"3.8806\""), "full-size capitals should remain full-size" ); } fn text_y(svg: &str, needle: &str) -> f64 { let closing = format!("{needle}"); let index = svg .find(&closing) .unwrap_or_else(|| panic!("missing text element containing `{needle}`")); let start = svg[..index] .rfind("().ok()) .unwrap_or_else(|| panic!("text element for `{needle}` has no y coordinate")) } #[test] fn raisebox_lifts_content_inline() { let source = r#"\documentclass{article} \begin{document} \setlength{\parindent}{0pt} A\raisebox{4pt}{raised}B \end{document} "#; let value = fresh_preview(source, "raisebox.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); let baseline = text_y(&svg, "A"); let raised = text_y(&svg, "raised"); let lift = baseline - raised; // 4pt = 1.411mm. assert!((lift - 1.411).abs() < 0.15, "raisebox lift: {lift}"); } #[test] fn makebox_honours_width_and_alignment() { let source = r#"\documentclass{article} \begin{document} \setlength{\parindent}{0pt} X\makebox[20mm][c]{mid}Y \makebox[40mm][l]{L}\makebox[40mm][r]{R} \end{document} "#; let value = fresh_preview(source, "makebox.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); let x = text_x(&svg, "X"); let mid = text_x(&svg, "mid"); let y = text_x(&svg, "Y"); assert!( mid > x + 7.0, "centered box content did not move right: {mid}" ); assert!(mid < y - 4.0, "centered box content overflowed: {mid}"); assert!( (y - x - 22.8).abs() < 0.5, "fixed box width not honoured: {}", y - x ); let left = text_x(&svg, "L"); let right = text_x(&svg, "R"); assert!( right > left + 70.0, "right-aligned box did not fill width: {right}" ); } #[test] fn inline_fbox_draws_a_frame_around_content() { let source = r#"\documentclass{article} \begin{document} \setlength{\parindent}{0pt} A\fbox{B}C \end{document} "#; let value = fresh_preview(source, "inline-fbox.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); assert!( svg.contains("fill=\"none\" stroke=\"#000000\""), "inline fbox frame is missing" ); assert!(svg.contains(">B"), "inline fbox content is missing"); assert!( svg.contains(">A") && svg.contains(">C"), "text around fbox missing" ); } #[test] fn minipage_wraps_to_requested_width_and_vbox_wraps() { let source = r#"\documentclass{article} \begin{document} \begin{minipage}[t]{50mm} This is a minipage with enough text to wrap across several lines and demonstrate that it is block-wide. \end{minipage} \vbox{A vbox wraps at text width with \textbf{bold} content.} \end{document} "#; let value = fresh_preview(source, "minipage.tex"); assert_eq!(value["status"], "ok", "{}", value["message"]); let svg = page_svg(&value, 0); assert!(svg.contains(">minipage"), "minipage content missing"); assert!( svg.contains(">block-wide."), "wrapped minipage tail missing" ); let first = text_y(&svg, "This"); let wrapped = text_y(&svg, "text"); assert!( wrapped > first + 3.0, "minipage did not wrap: {first} -> {wrapped}" ); for word in ["This", "text", "block-wide."] { assert!( text_x(&svg, word) < 50.0, "minipage content escaped its 50mm width: {word}" ); } assert!(svg.contains(">vbox"), "vbox content missing"); assert!(svg.contains(">bold"), "vbox styled content missing"); }