import tempfile import unittest from pathlib import Path from chatgpt_pdf_converter import ( PdfOptions, content_aware_column_widths, convert_markdown_file, detect_direction, inline_markup, parse_markdown, reshape_plain_text, ) class PdfConverterTests(unittest.TestCase): def test_detects_rtl_text(self): self.assertEqual(detect_direction("سلام این یک متن فارسی است"), "rtl") self.assertEqual(detect_direction("This is an English export."), "ltr") def test_parses_markdown_blocks(self): blocks = parse_markdown( """--- title: "Demo" --- # Demo | Field | Value | |---|---| | Author | User | - first - second ```python print("hello") ``` """ ) self.assertEqual(blocks[0]["type"], "heading") self.assertEqual(blocks[1]["type"], "table") self.assertEqual(blocks[2]["type"], "list") self.assertEqual(blocks[3]["type"], "code") def test_inline_code_uses_reportlab_font_tag(self): markup = inline_markup("Use `python3 main.py` now.", "ltr") self.assertIn('python3 main.py', markup) def test_rtl_shaping_preserves_iso_timestamps(self): shaped = reshape_plain_text("1. کاربر - 2026-06-30T13:18:44+03:30") self.assertIn("2026-06-30T13:18:44+03:30", shaped) self.assertIn(" - 2026-06-30T13:18:44+03:30", shaped) def test_rtl_inline_markup_preserves_mixed_ltr_order(self): markup = inline_markup("با **L=2**، شما به طور کلی:", "rtl") self.assertIn("L=2", markup) self.assertNotIn("", markup) self.assertLess(markup.index("L=2"), markup.index("ﺎﺑ")) def test_content_aware_table_widths_prioritize_long_source_column(self): rows = [ ["نام", "نوع", "اندازه", "ابعاد", "منبع"], [ "1000404866.jpg", "image/jpeg", "164.8 KB", "1884x4080", "sediment://file_00000000741c71f6bd139887dd91b7c3?shared_conversation_id=6a43a1b9", ], ] widths = content_aware_column_widths(rows, 480) self.assertGreater(widths[-1], widths[0]) self.assertGreater(widths[0], 95) self.assertGreater(widths[1], 75) self.assertGreater(widths[2], 45) self.assertAlmostEqual(sum(widths), 480, delta=0.1) def test_generates_pdf(self): with tempfile.TemporaryDirectory() as tmpdir: tmp = Path(tmpdir) markdown = tmp / "demo.md" markdown.write_text( """# Demo Export ## 1. User | Field | Value | |---|---| | Author | User | Hello from the Markdown export. """, encoding="utf-8", ) output = convert_markdown_file( markdown, PdfOptions( output_dir=tmp / "pdf", direction="ltr", font_family="Helvetica", overwrite=True, ), ) self.assertTrue(output.exists()) self.assertEqual(output.read_bytes()[:5], b"%PDF-") if __name__ == "__main__": unittest.main()