7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
157 lines
5.9 KiB
Python
157 lines
5.9 KiB
Python
"""Unit tests for HTMLDownloader._extract_content and _format_extracted_content.
|
|
|
|
Production code: src/local_deep_research/research_library/downloaders/html.py
|
|
"""
|
|
|
|
from local_deep_research.research_library.downloaders.html import HTMLDownloader
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _extract_content
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _long_text(n=120):
|
|
"""Generate text longer than the 100-char fallback threshold."""
|
|
return "A" * n
|
|
|
|
|
|
class TestExtractContent:
|
|
"""Tests for HTMLDownloader._extract_content HTML parsing."""
|
|
|
|
def setup_method(self):
|
|
self.downloader = HTMLDownloader()
|
|
|
|
def test_minimal_html_with_body(self):
|
|
long = _long_text()
|
|
html = f"<html><body><p>{long}</p></body></html>"
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert long[:50] in result["content"]
|
|
|
|
def test_article_tag_preferred(self):
|
|
long = _long_text()
|
|
html = f"""<html><body>
|
|
<div>sidebar stuff that is long enough to pass {_long_text()}</div>
|
|
<article><p>{long}</p></article>
|
|
</body></html>"""
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert long[:50] in result["content"]
|
|
|
|
def test_main_tag_fallback(self):
|
|
long = _long_text()
|
|
html = f"<html><body><main><p>{long}</p></main></body></html>"
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert long[:50] in result["content"]
|
|
|
|
def test_content_div_fallback(self):
|
|
long = _long_text()
|
|
html = f'<html><body><div class="content"><p>{long}</p></div></body></html>'
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert long[:50] in result["content"]
|
|
|
|
def test_script_style_nav_removed(self):
|
|
long = _long_text()
|
|
html = f"""<html><body>
|
|
<script>var x = 1;</script>
|
|
<style>.foo {{ color: red; }}</style>
|
|
<nav>navigation links</nav>
|
|
<p>{long}</p>
|
|
</body></html>"""
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert "var x" not in result["content"]
|
|
assert "color: red" not in result["content"]
|
|
|
|
def test_sidebar_pattern_removed(self):
|
|
long = _long_text()
|
|
html = f"""<html><body>
|
|
<nav><a href="/">Home</a><a href="/about">About</a></nav>
|
|
<div class="sidebar">sidebar content with some navigation links and additional information</div>
|
|
<article><p>{long}</p></article>
|
|
<footer><p>Copyright 2024 All rights reserved terms privacy contact</p></footer>
|
|
</body></html>"""
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert long[:50] in result["content"]
|
|
|
|
def test_short_content_extracts_via_justext(self):
|
|
"""Short but meaningful content is extracted by justext."""
|
|
html = "<html><body><p>Short text here but enough to be above fifty characters for the final check.</p></body></html>"
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert "Short text" in result["content"]
|
|
|
|
def test_no_body_returns_none(self):
|
|
# No meaningful content in head-only document
|
|
html = "<html><head><title>Title</title></head></html>"
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is None
|
|
|
|
def test_title_extraction(self):
|
|
long = _long_text()
|
|
html = f"<html><head><title>My Title</title></head><body><p>{long}</p></body></html>"
|
|
result = self.downloader._extract_content(html, "http://example.com")
|
|
assert result is not None
|
|
assert result["title"] == "My Title"
|
|
|
|
def test_url_passed_through(self):
|
|
long = _long_text()
|
|
html = f"<html><body><p>{long}</p></body></html>"
|
|
result = self.downloader._extract_content(
|
|
html, "http://example.com/page"
|
|
)
|
|
assert result["url"] == "http://example.com/page"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _format_extracted_content
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestFormatExtractedContent:
|
|
"""Tests for HTMLDownloader._format_extracted_content."""
|
|
|
|
def setup_method(self):
|
|
self.downloader = HTMLDownloader()
|
|
|
|
def test_full_dict(self):
|
|
extracted = {
|
|
"title": "My Title",
|
|
"description": "A description",
|
|
"url": "http://example.com",
|
|
"content": "Main content here",
|
|
}
|
|
result = self.downloader._format_extracted_content(extracted)
|
|
assert "# My Title" in result
|
|
assert "*A description*" in result
|
|
assert "Source: http://example.com" in result
|
|
assert "Main content here" in result
|
|
|
|
def test_missing_optional_fields(self):
|
|
extracted = {
|
|
"title": None,
|
|
"description": None,
|
|
"url": "http://example.com",
|
|
"content": "Main content",
|
|
}
|
|
result = self.downloader._format_extracted_content(extracted)
|
|
assert "#" not in result # no title heading
|
|
assert "*" not in result # no description italic
|
|
assert "Source: http://example.com" in result
|
|
assert "Main content" in result
|
|
|
|
def test_empty_content(self):
|
|
extracted = {
|
|
"title": None,
|
|
"description": None,
|
|
"url": None,
|
|
"content": None,
|
|
}
|
|
result = self.downloader._format_extracted_content(extracted)
|
|
# Should not crash, just return minimal/empty output
|
|
assert isinstance(result, str)
|