"""测试 HN 首页 HTML 解析""" import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent.parent)) from src.sections.hackernews.frontpage_scraper import parse_frontpage_html def test_parse_frontpage_returns_stories(): fixture = (Path(__file__).parent / "fixtures" / "hn_frontpage.html").read_text( encoding="utf-8" ) stories = parse_frontpage_html(fixture) assert len(stories) >= 25 s = stories[0] print(json.dumps(stories[:5], indent=4, ensure_ascii=False)) assert s["id"] assert s["title"] assert s["url"] assert isinstance(s["points"], int) assert isinstance(s["comments"], int) assert s["comments_url"].startswith("https://news.ycombinator.com/item?id=") def test_parse_frontpage_detects_show_hn_internal_url(): html = """
| Ask HN: what's new? |
| 50 points by alice 2 hours ago | 5 comments |