from pathlib import Path import json import shutil import subprocess import tempfile import unittest from pypdf import PdfReader ROOT = Path(__file__).resolve().parents[1] class ReportPluginTests(unittest.TestCase): def render_text(self, source, work, root=ROOT): output = work / 'report.pdf' result = subprocess.run([str(root / 'build.sh'), 'render', 'report', str(source), '--output', str(output), '--force'], text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) reader = PdfReader(output) return '\n'.join(page.extract_text(extraction_mode='layout') for page in reader.pages) def test_localised_report_headings_and_authored_content(self): report = json.loads((ROOT / 'examples/example-report.json').read_text()) report.pop('actions_title') report['diagnostics'] = { 'confidence_scale': {'interpretation': 'Authored confidence explanation'}, 'hypotheses': [{'scope': 'system', 'evidence_for': ['For'], 'evidence_against': ['Against'], 'interpretation': 'Authored interpretation', 'tests_to_confirm': ['Test']}], 'current_leading_assessment': {'statement': 'Authored assessment'}, 'not_yet_supported': ['Unsupported'], } report['sections'].append({'content': [{'type': 'subsection', 'content': [{'type': 'unknown'}]}]}) report['findings'].append({'evidence': 'Authored evidence'}) report['actions'].append({'text': 'Authored action'}) catalog = json.loads((ROOT / 'plugins/report/locales.json').read_text()) with tempfile.TemporaryDirectory() as directory: work = Path(directory) source = work / 'input.json' source.write_text(json.dumps(report)) default = self.render_text(source, work) report['language'] = 'en' source.write_text(json.dumps(report)) self.assertEqual(default, self.render_text(source, work)) report['language'] = 'fr' source.write_text(json.dumps(report)) french = self.render_text(source, work) for label, translation in catalog['fr'].items(): if label == 'Stakeholder briefing' or label[0].islower(): continue with self.subTest(label=label): self.assertIn(translation, french) self.assertIn(report['title'], french) self.assertIn('Authored confidence explanation', french) self.assertIn('Operational precautions', french) self.assertNotIn('Executive summary', french) report['actions_title'] = 'Authored action heading' source.write_text(json.dumps(report)) self.assertIn('Authored action heading', self.render_text(source, work)) def test_localised_status_and_severity_badges(self): catalog = json.loads((ROOT / 'plugins/report/locales.json').read_text()) labels = [key for key in catalog['fr'] if key[0].islower()] report = { 'language': 'fr', 'metadata': {'Author': 'Tester', 'Date': '2026-09-25'}, 'status': 'paused', 'severity': 'medium', 'actions': [{'status': key} for key in labels], 'findings': [{'status': 'confirmed', 'severity': 'high'}], 'diagnostics': {'hypotheses': [{'status': 'recommended'}]}, } with tempfile.TemporaryDirectory() as directory: work = Path(directory) source = work / 'input.json' source.write_text(json.dumps(report)) rendered = self.render_text(source, work) for key in labels: self.assertIn(catalog['fr'][key].upper(), rendered) for key in ('PENDING', 'RECOMMENDED', 'CONFIRMED', 'HIGH', 'MEDIUM', 'PAUSED'): self.assertNotIn(key, rendered) report['actions'] = [{'status': 'statut personnalisé'}] source.write_text(json.dumps(report)) self.assertIn('STATUT PERSONNALISÉ', self.render_text(source, work)) def test_yaml_language_and_invalid_language(self): source_text = (ROOT / 'examples/example-report.yaml').read_text() with tempfile.TemporaryDirectory() as directory: work = Path(directory) source = work / 'input.yaml' source.write_text('language: fr\n' + source_text) self.assertIn('Synthèse', self.render_text(source, work)) for value, message in [('unknown', 'Unknown report language'), ('[fr]', 'must be a string')]: source.write_text(f'language: {value}\n' + source_text) result = subprocess.run([str(ROOT / 'build.sh'), 'validate', 'report', str(source)], text=True, capture_output=True) self.assertNotEqual(result.returncode, 0) self.assertIn(message, result.stderr) def test_additional_catalog_language_and_english_fallback(self): with tempfile.TemporaryDirectory() as directory: work = Path(directory) root = work / 'reportist' root.mkdir() for name in ('build.sh', 'render.sh'): shutil.copy2(ROOT / name, root / name) for name in ('tools', 'visual', 'plugins/report'): shutil.copytree(ROOT / name, root / name) catalog_path = root / 'plugins/report/locales.json' catalog = json.loads(catalog_path.read_text()) catalog['es'] = {'Executive summary': 'Resumen ejecutivo'} catalog_path.write_text(json.dumps(catalog)) source = work / 'input.yaml' source.write_text('language: es\n' + (ROOT / 'examples/example-report.yaml').read_text()) rendered = self.render_text(source, work, root) self.assertIn('Resumen ejecutivo', rendered) self.assertIn('Findings', rendered) self.assertNotIn('Executive summary', rendered) 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()