Expose the compiler session as a library target, run core as a workspace member, and add direct typeset tests plus end-to-end HTTP tests. Coverage: examples, arXiv fixture, starred headings, rectangles, errors, page-hash cache stability, static frontend assets, project API, preview/PDF, traversal rejection, and the legacy endpoint.
180 lines
5.5 KiB
Rust
180 lines
5.5 KiB
Rust
//! 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> {
|
|
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("<rect").count() >= 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"
|
|
);
|
|
}
|