import importlib.util import json from pathlib import Path import shutil import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] spec = importlib.util.spec_from_file_location("reportist_render", ROOT / "tools" / "render.py") renderer = importlib.util.module_from_spec(spec) spec.loader.exec_module(renderer) TYPST = shutil.which("typst") @unittest.skipUnless(TYPST, "Typst is not installed") class RenderTests(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory() self.work = Path(self.temporary.name) def tearDown(self): self.temporary.cleanup() def assert_pdf(self, path): self.assertTrue(path.read_bytes().startswith(b"%PDF-")) self.assertGreater(path.stat().st_size, 1000) def test_yaml_and_json_examples_render(self): for example in ("example-report.yaml", "example-report.json"): with self.subTest(example=example): output = self.work / f"{example}.pdf" renderer.render(ROOT / "examples" / example, output, TYPST) self.assert_pdf(output) def test_minimal_report_renders_without_optional_fields(self): output = self.work / "minimal.pdf" renderer.render(ROOT / "examples" / "minimal-report.json", output, TYPST) self.assert_pdf(output) def test_invalid_input_leaves_no_pdf(self): report = self.work / "invalid.json" report.write_text(json.dumps({"schema_version": 2}), encoding="utf-8") output = self.work / "invalid.pdf" with self.assertRaisesRegex(ValueError, "invalid report"): renderer.render(report, output, TYPST) self.assertFalse(output.exists()) def test_replaceable_branding_logo_renders(self): (self.work / "assets").mkdir() (self.work / "assets" / "logo.svg").write_text( '' '', encoding="utf-8") report = {"schema_version": 1, "title": "Alternate Brand", "metadata": { "Date": "2026-09-15", "Author": "Synthetic Author"}, "branding": { "organization": "Unrelated Example", "logo": "assets/logo.svg", "theme": {"accent": "#663399"}}} input_path = self.work / "brand.json" input_path.write_text(json.dumps(report), encoding="utf-8") output = self.work / "brand.pdf" renderer.render(input_path, output, TYPST) self.assert_pdf(output) def test_existing_output_requires_force_and_input_is_never_output(self): input_path = ROOT / "examples" / "minimal-report.json" output = self.work / "report.pdf" output.write_bytes(b"existing") with self.assertRaisesRegex(ValueError, "output exists"): renderer.render(input_path, output, TYPST) self.assertEqual(output.read_bytes(), b"existing") renderer.render(input_path, output, TYPST, force=True) self.assert_pdf(output) with self.assertRaisesRegex(ValueError, "must differ"): renderer.render(input_path, input_path, TYPST, force=True) def test_logo_symlink_is_rejected(self): outside = self.work / "outside.svg" outside.write_text('', encoding="utf-8") report_dir = self.work / "report" report_dir.mkdir() (report_dir / "logo.svg").symlink_to(outside) report = {"schema_version": 1, "title": "Unsafe asset", "metadata": { "Date": "2026-09-15", "Author": "Synthetic Author"}, "branding": {"logo": "logo.svg"}} input_path = report_dir / "report.json" input_path.write_text(json.dumps(report), encoding="utf-8") with self.assertRaisesRegex(ValueError, "symlinks"): renderer.render(input_path, self.work / "unsafe.pdf", TYPST) def test_cli_runs_outside_repository(self): output = self.work / "outside-cwd.pdf" result = subprocess.run([ sys.executable, str(ROOT / "tools" / "render.py"), str(ROOT / "examples" / "minimal-report.json"), "--output", str(output), ], cwd=self.work, text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) self.assert_pdf(output) if __name__ == "__main__": unittest.main()