"""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 world

") assert "Hello world" in md def test_markdown_h1(): md = html_to_markdown("

Title

") 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

") assert "`pip`" in md def test_markdown_pre_code_not_double_fenced(): """
...
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("") assert "- one" in md assert "- two" in md # ----- Tables (GFM) ----- def test_markdown_table_basic(): html = "
AB
12
" md = html_to_markdown(html) assert "| A | B |" in md assert "| --- | --- |" in md assert "| 1 | 2 |" in md def test_markdown_table_pipe_escaped(): """Pipe chars inside cells must be escaped so they don't break the row.""" html = "
Col
a|b
" md = html_to_markdown(html) assert "a\\|b" in md # ----- Emphasis / images ----- def test_markdown_strong_emphasis(): md = html_to_markdown("

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('pic') assert "![pic](http://x.com/a.png)" 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 = "
content
" text = extract_with_stdlib(html) assert "content" in text assert "menu" not in text # ----- Ordered / nested lists ----- def test_markdown_ordered_list_numbered(): md = html_to_markdown("
  1. first
  2. second
  3. third
") assert "1. first" in md assert "2. second" in md assert "3. third" in md def test_markdown_nested_ordered_list(): html = "
  1. outer1
    1. inner1
    2. inner2
  2. outer2
" md = html_to_markdown(html) assert "1. outer1" in md assert " 1. inner1" in md assert " 2. inner2" in md assert "2. outer2" in md def test_markdown_nested_unordered_list(): html = "" md = html_to_markdown(html) assert "- outer" in md assert " - inner" in md def test_markdown_mixed_nested_list(): """