Files
chatgpt-share-exporter/tests/test_pdf_converter.py
T
Meghdad aac8ac2df3
Tests / test (3.10) (push) Successful in 1m55s
Tests / test (3.11) (push) Successful in 16s
Tests / test (3.12) (push) Successful in 14s
Tests / test (3.13) (push) Successful in 20s
Tests / test (3.9) (push) Failing after 13s
fix: stabilize RTL PDF wrapping and list markers
2026-06-30 17:37:36 +03:30

170 lines
5.1 KiB
Python

import tempfile
import unittest
from pathlib import Path
from chatgpt_pdf_converter import (
FontConfig,
PdfOptions,
build_styles,
content_aware_column_widths,
convert_markdown_file,
detect_direction,
inline_markup,
list_flowable,
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('<font name="Courier">python3 main.py</font>', 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("<b>", markup)
self.assertLess(markup.index("L=2"), markup.index("ﺎﺑ"))
def test_rtl_wrapped_markup_keeps_lines_in_reading_order(self):
styles = build_styles(
FontConfig("Helvetica", "Helvetica-Bold", "Courier", None),
14,
"rtl",
)
text = (
"به نظر می‌رسد این یک پروفایل **۱۶ عامل شخصیت کتل (16PF)** "
"به زبان فارسی باشد. می‌توانم نمره‌های عامل‌های اصلی را از برگه بخوانم:"
)
markup = inline_markup(text, "rtl", styles["Body"], 270)
lines = markup.split("<br/>")
self.assertGreater(len(lines), 1)
self.assertIn("ﻞﯾﺎﻓﻭﺮﭘ", lines[0])
self.assertIn("ﻢﻧﺍﻮﺨﺑ", lines[-1])
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_rtl_list_markers_use_fixed_right_column(self):
styles = build_styles(
FontConfig("Helvetica", "Helvetica-Bold", "Courier", None),
14,
"rtl",
)
table = list_flowable(
[
("-", "احتمالا در موقعیت‌های پراسترس آرام می‌مانید."),
("-", "L=2 به آدم‌ها حسن‌نیت می‌دهید."),
],
styles,
"rtl",
480,
)
self.assertEqual(table._cellvalues[0][-1].getPlainText(), "-")
self.assertEqual(table._cellvalues[1][-1].getPlainText(), "-")
self.assertAlmostEqual(sum(table._argW), 480, delta=0.1)
def test_ltr_list_markers_use_fixed_left_column(self):
styles = build_styles(
FontConfig("Helvetica", "Helvetica-Bold", "Courier", None),
14,
"ltr",
)
table = list_flowable([("-", "First item")], styles, "ltr", 480)
self.assertEqual(table._cellvalues[0][0].getPlainText(), "-")
self.assertAlmostEqual(sum(table._argW), 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()