"""Tests for scripts/fetch.py — MarkdownConverter and text extraction.
Covers: headings, links (incl. nested bold inside ), fenced code blocks,
inline code, blockquotes, unordered lists, GFM tables (incl. pipe escaping),
emphasis, image tags, script/style stripping, and the stdlib text extractor.
"""
from fetch import extract_with_stdlib, html_to_markdown
# ----- Basic structure -----
def test_markdown_basic_paragraph():
md = html_to_markdown(" Hello worldTitle
")
assert "# Title" in md
def test_markdown_h3():
md = html_to_markdown("Subtitle
")
assert "### Subtitle" in md
def test_markdown_empty_input():
assert html_to_markdown("").strip() == ""
# ----- Links -----
def test_markdown_simple_link():
md = html_to_markdown('click')
assert "[click](https://example.com)" in md
def test_markdown_link_with_nested_bold():
"""Bold text inside a link must survive — the tree-based parser handles
this where a regex approach would break."""
md = html_to_markdown('bold link')
assert "https://x.com" in md
assert "bold link" in md
# ----- Code -----
def test_markdown_fenced_code_block():
md = html_to_markdown("
print('hi')")
assert "```" in md
assert "print('hi')" in md
def test_markdown_inline_code():
md = html_to_markdown("use pip to install
... should render as one fence, not backticks-in-fence."""
md = html_to_markdown("x = 1")
assert "```\nx = 1\n```" in md
assert "`x = 1`" not in md # no inline backticks around the content
# ----- Blockquote / lists -----
def test_markdown_blockquote():
md = html_to_markdown("quoted text") assert "> quoted text" in md def test_markdown_unordered_list(): md = html_to_markdown("
| A | B |
|---|---|
| 1 | 2 |
| Col |
|---|
| a|b |
bold
") assert "**bold**" in md def test_markdown_em_italic(): md = html_to_markdown("italic
") assert "*italic*" in md def test_markdown_image(): md = html_to_markdown('
')
assert "" in md
# ---- Stripping non-content -----
def test_markdown_strips_script():
md = html_to_markdown("visible
") assert "alert" not in md assert "visible" in md def test_markdown_strips_style(): md = html_to_markdown("visible
") assert "color" not in md assert "visible" in md # ----- Stdlib text extractor ----- def test_extract_with_stdlib_basic(): html = "Hello
" text = extract_with_stdlib(html) assert "Hello" in text assert "x" not in text # script content excluded def test_extract_with_stdlib_skips_nav(): html = "