65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""Unit tests for storage-format HTML -> Markdown conversion."""
|
|
from confluence_crawler.markdown import (
|
|
page_filename,
|
|
render_markdown,
|
|
slugify,
|
|
storage_to_markdown,
|
|
)
|
|
|
|
|
|
def test_slugify_sanitizes_illegal_chars():
|
|
assert slugify('a/b\\c:d*e?f"g<h>i|j') == "a-b-c-d-e-f-g-h-i-j"
|
|
assert slugify(" Hello World ") == "Hello-World"
|
|
assert slugify("///") == "untitled"
|
|
|
|
|
|
def test_storage_to_markdown_basic():
|
|
html = "<h1>Hello</h1><p>Some <strong>bold</strong> and <em>italic</em> text.</p>"
|
|
md = storage_to_markdown(html)
|
|
assert "Hello" in md
|
|
assert "**bold**" in md
|
|
assert "*italic*" in md
|
|
|
|
|
|
def test_storage_to_markdown_table():
|
|
html = "<table><tr><td>a</td><td>b</td></tr><tr><td>1</td><td>2</td></tr></table>"
|
|
md = storage_to_markdown(html)
|
|
assert "a" in md and "b" in md and "1" in md and "2" in md
|
|
|
|
|
|
def test_storage_to_markdown_empty():
|
|
assert storage_to_markdown("") == ""
|
|
assert storage_to_markdown(" ") == ""
|
|
|
|
|
|
def test_storage_to_markdown_deeply_nested_no_crash():
|
|
"""Regression: deeply nested tables must not raise RecursionError."""
|
|
nested = "<table><tr><td>" * 7000 + "core" + "</td></tr></table>" * 7000
|
|
md = storage_to_markdown(nested)
|
|
assert "core" in md # content preserved via fallback
|
|
|
|
|
|
def test_render_markdown_has_front_matter():
|
|
page = {
|
|
"id": "123",
|
|
"title": "My Page",
|
|
"space": {"key": "DEV"},
|
|
"version": {"number": 3, "when": "2025-01-01T00:00:00.000Z"},
|
|
"body": {"storage": {"value": "<p>Hello</p>"}},
|
|
}
|
|
md = render_markdown(page, "https://wiki.example.com")
|
|
assert "title: My Page" in md
|
|
assert "page_id: 123" in md
|
|
assert "space: DEV" in md
|
|
assert "https://wiki.example.com/pages/viewpage.action?pageId=123" in md
|
|
assert "# My Page" in md
|
|
assert "Hello" in md
|
|
|
|
|
|
def test_page_filename_unique_and_safe():
|
|
p1 = page_filename({"id": "1", "title": "A/B: C"})
|
|
p2 = page_filename({"id": "2", "title": "A/B: C"})
|
|
assert p1 != p2
|
|
assert p1 == "1_A-B-C.md"
|
|
assert p2 == "2_A-B-C.md"
|