"""Pure-parse tests for HtmlPageSource. No network.""" from __future__ import annotations import pytest # Skip if optional extras are not installed. selectolax = pytest.importorskip("selectolax") from arborist.sources.html_page import parse_html SAMPLE_HTML = """ Eight Forms of Capital

Site nav we want stripped

Eight Forms of Capital

Living, Material, Financial, Intellectual.

Experiential, Social, Cultural, Spiritual.

See also this page and an external link.

js link mail link
""" def test_parse_extracts_title_and_body(): doc = parse_html("https://example.com/page", SAMPLE_HTML) assert doc is not None assert doc.title == "Eight Forms of Capital" assert "Living" in doc.content assert "Spiritual" in doc.content def test_parse_strips_noise(): doc = parse_html("https://example.com/page", SAMPLE_HTML) assert doc is not None assert "console.log" not in doc.content assert "color: red" not in doc.content assert "Site nav we want stripped" not in doc.content assert "strip me too" not in doc.content assert "nav links also stripped" not in doc.content def test_parse_extracts_edges_and_resolves_relative(): doc = parse_html("https://example.com/page", SAMPLE_HTML) assert doc is not None uris = {e.dst_uri for e in doc.edges} # Relative href resolved against the page URL. assert "https://example.com/eight-forms-of-capital/" in uris # External link kept; fragment moved to anchor. assert "https://example.org/external" in uris # javascript: / mailto: / tel: / hash-only anchors must be skipped. assert all("javascript" not in u for u in uris) assert all("mailto" not in u for u in uris) def test_parse_anchor_split(): doc = parse_html("https://example.com/page", SAMPLE_HTML) assert doc is not None external = next(e for e in doc.edges if e.dst_uri == "https://example.org/external") assert external.anchor == "section" def test_parse_empty_body_returns_none(): doc = parse_html("https://example.com/empty", "") assert doc is None def test_parse_no_html_returns_none_or_empty(): # Selectolax tolerates non-HTML; we want no Document for empty content. doc = parse_html("https://example.com/x", "") assert doc is None