mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 18:30:14 +08:00
_remove_element used parent.remove(el), which in lxml also drops el.tail,
so text after a stripped button, icon or aria-hidden span was lost
("The answer is <span aria-hidden>*</span>42." became "The answer is").
Move the tail onto the previous sibling or the parent before removing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""Tests for web snapshot cleanup, URL normalization, and local assets."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.web_source.html_extractor import extract_article_markdown
|
|
from deeptutor.services.web_source.markdown import strip_leading_snapshot_provenance
|
|
from deeptutor.services.web_source.snapshot_assets import (
|
|
SnapshotAsset,
|
|
localize_snapshot_images,
|
|
)
|
|
|
|
|
|
def test_strips_only_leading_source_comments() -> None:
|
|
markdown = (
|
|
"\n<!-- source: https://docs.example.com/page -->\n"
|
|
"<!-- source: https://docs.example.com/canonical -->\n# Documentation\n"
|
|
)
|
|
assert strip_leading_snapshot_provenance(markdown) == "# Documentation\n"
|
|
|
|
body_comment = (
|
|
"# Documentation\n\n<!-- source: https://docs.example.com/body -->\n\n"
|
|
"```html\n<!-- source: https://docs.example.com/code -->\n```\n"
|
|
)
|
|
assert strip_leading_snapshot_provenance(body_comment) == body_comment
|
|
|
|
|
|
def test_html_extractor_resolves_relative_links_and_images() -> None:
|
|
_, markdown = extract_article_markdown(
|
|
"<html><body><article><a href='../next'>Next</a>"
|
|
"<img src='../images/diagram.png' alt='Diagram'></article></body></html>",
|
|
base_url="https://docs.example.com/guide/start/",
|
|
)
|
|
|
|
assert "[Next](https://docs.example.com/guide/next)" in markdown
|
|
assert "" in markdown
|
|
|
|
|
|
def test_html_extractor_preserves_text_after_aria_hidden_span() -> None:
|
|
_, markdown = extract_article_markdown(
|
|
'<article><p>The answer is <span aria-hidden="true">★</span>42.</p></article>'
|
|
)
|
|
|
|
assert markdown == "The answer is 42."
|
|
|
|
|
|
def test_html_extractor_preserves_text_after_stripped_button() -> None:
|
|
_, markdown = extract_article_markdown(
|
|
"<article><p><strong>The answer</strong> is <button>Copy</button>42.</p></article>"
|
|
)
|
|
|
|
assert markdown == "**The answer** is 42."
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_localizes_safe_images_and_never_leaves_failed_hotlinks() -> None:
|
|
png = b"\x89PNG\r\n\x1a\n" + b"safe"
|
|
|
|
async def fetcher(url: str):
|
|
if url.endswith("ok.png"):
|
|
return SnapshotAsset(png, "image/png", "png")
|
|
return None
|
|
|
|
markdown, assets = await localize_snapshot_images(
|
|
"\n",
|
|
"0123456789abcdef",
|
|
fetcher=fetcher,
|
|
)
|
|
|
|
assert "https://cdn.example.com" not in markdown
|
|
assert "/api/reading/materials/0123456789abcdef/assets/" in markdown
|
|
assert "Image unavailable: Tracker" in markdown
|
|
assert list(assets.values()) == [png]
|