from pathlib import Path import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] class ReportPluginTests(unittest.TestCase): def test_new_palettes_render_in_both_plugins(self): palettes = ("warm", "steel", "bw", "high-contrast", "yon-sen", "steingrund", "draan") capture = ROOT / "plugins/diagnostics/tests/fixtures/legacy/windiagnostics-v1.json" source = (ROOT / "examples/example-report.yaml").read_text() with tempfile.TemporaryDirectory() as directory: work = Path(directory) for theme in palettes: with self.subTest(theme=theme): report = work / f"{theme}.yaml" report.write_text(f"theme: {theme}\n" + source) for kind, input_path in (("report", report), ("diagnostic", capture)): output = work / f"{theme}-{kind}.pdf" arguments = [str(ROOT / "build.sh"), "render", kind, str(input_path), "--output", str(output)] if kind == "diagnostic": arguments.extend(("--theme", theme)) result = subprocess.run(arguments, text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) self.assertTrue(output.read_bytes().startswith(b"%PDF-")) def test_public_cli_renders_diagnostic_from_standalone_checkout(self): fixture = ROOT / "plugins/diagnostics/tests/fixtures/legacy/windiagnostics-v1.json" with tempfile.TemporaryDirectory() as directory: output = Path(directory) / "diagnostic.pdf" result = subprocess.run([str(ROOT / "build.sh"), "render", "diagnostic", str(fixture), "--output", str(output)], text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) self.assertTrue(output.read_bytes().startswith(b"%PDF-")) def test_public_cli_batch_preserves_relative_paths(self): with tempfile.TemporaryDirectory() as directory: work = Path(directory) source = work / "source/nested" source.mkdir(parents=True) (source / "item.yaml").write_bytes((ROOT / "examples/example-report.yaml").read_bytes()) output = work / "output" result = subprocess.run([str(ROOT / "build.sh"), "render-all", "report", str(work / "source"), "--output-dir", str(output)], text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) self.assertTrue((output / "nested/item.pdf").read_bytes().startswith(b"%PDF-")) def test_report_renders_with_its_own_typst_model(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory) / "report.pdf" result = subprocess.run([str(ROOT / "render.sh"), str(ROOT / "examples/example-report.yaml"), "--output", str(output)], text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) pdf = output.read_bytes() self.assertTrue(pdf.startswith(b"%PDF-")) self.assertGreater(len(pdf), 1024) def test_existing_output_requires_force(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory) / "report.pdf" output.write_text("existing") result = subprocess.run([str(ROOT / "render.sh"), str(ROOT / "examples/example-report.yaml"), "--output", str(output)], text=True, capture_output=True) self.assertNotEqual(result.returncode, 0) self.assertEqual(output.read_text(), "existing") if __name__ == "__main__": unittest.main()