# -*- coding: utf-8 -*- """测试 fullpage_archiver.py — 纯函数部分(不依赖浏览器)。""" import json import os import sys import tempfile import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts")) sys.dont_write_bytecode = True from fullpage_archiver import ( FullPageArchiver, ArchiveResult, _IMG_SRC_RE, _TABLE_RE, ) class TestArchiveResult(unittest.TestCase): """ArchiveResult 数据类测试。""" def test_to_dict(self): r = ArchiveResult( url="https://example.com", title="Test Article", page_count=3, image_count=10, canvas_count=2, table_count=5, ) d = r.to_dict() self.assertEqual(d["url"], "https://example.com") self.assertEqual(d["title"], "Test Article") self.assertEqual(d["page_count"], 3) def test_summary_contains_key_info(self): r = ArchiveResult( url="https://example.com", title="测试文章", page_count=2, image_count=5, canvas_count=1, table_count=3, inline_html_path="/tmp/test_inline.html", ) s = r.summary self.assertIn("测试文章", s) self.assertIn("2", s) # page count self.assertIn("5", s) # image count self.assertIn("test_inline.html", s) def test_summary_handles_errors(self): r = ArchiveResult( url="https://example.com", errors=["连接超时", "图片下载失败"], ) self.assertIn("2 个错误", r.summary) class TestImageExtraction(unittest.TestCase): """图片 URL 提取正则测试。""" def test_img_src_simple(self): html = '' matches = _IMG_SRC_RE.findall(html) self.assertEqual(len(matches), 1) self.assertEqual(matches[0], "https://example.com/img.jpg") def test_img_src_single_quotes(self): html = "" matches = _IMG_SRC_RE.findall(html) self.assertEqual(len(matches), 1) def test_img_src_multiple(self): html = """ A B C """ matches = _IMG_SRC_RE.findall(html) self.assertEqual(len(matches), 3) def test_img_data_uri_ignored(self): """data: URI 不应该被提取(由 _download_single_image 处理)。""" html = '' matches = _IMG_SRC_RE.findall(html) self.assertEqual(len(matches), 1) # 提取出来,但在下载时跳过 self.assertIn("data:image/png", matches[0]) class TestTableExtraction(unittest.TestCase): """表格提取测试。""" def test_simple_table(self): html = """
NameValue
营收1500亿
净利润300亿
""" archiver = FullPageArchiver() tables = archiver._extract_page_tables(html, 1, tempfile.mkdtemp()) self.assertEqual(len(tables), 1) self.assertEqual(tables[0]["rows"], 3) self.assertEqual(tables[0]["cols"], 2) def test_empty_table_no_rows(self): html = "
" archiver = FullPageArchiver() tables = archiver._extract_page_tables(html, 1, tempfile.mkdtemp()) self.assertEqual(len(tables), 0) def test_table_with_colspan(self): html = """
Header
Value 1
""" archiver = FullPageArchiver() tables = archiver._extract_page_tables(html, 1, tempfile.mkdtemp()) self.assertEqual(len(tables), 1) def test_multiple_tables(self): html = "
A
B
" archiver = FullPageArchiver() tables = archiver._extract_page_tables(html, 1, tempfile.mkdtemp()) self.assertEqual(len(tables), 2) class TestHTMLProcessing(unittest.TestCase): """HTML 处理函数测试。""" def test_extract_title(self): html = "测试标题" title = FullPageArchiver._extract_title(html) self.assertEqual(title, "测试标题") def test_extract_title_none(self): title = FullPageArchiver._extract_title("") self.assertEqual(title, "") def test_extract_domain(self): domain = FullPageArchiver._extract_domain("https://www.example.com/path?a=1") self.assertEqual(domain, "example.com") def test_guess_image_ext(self): self.assertEqual(FullPageArchiver._guess_image_ext("https://x.com/img.png"), ".png") self.assertEqual(FullPageArchiver._guess_image_ext("https://x.com/img.jpg"), ".jpg") self.assertEqual(FullPageArchiver._guess_image_ext("https://x.com/img.jpeg"), ".jpg") self.assertEqual(FullPageArchiver._guess_image_ext("https://x.com/img.webp"), ".webp") self.assertEqual(FullPageArchiver._guess_image_ext("https://x.com/img"), ".jpg") # default def test_escape_html(self): self.assertEqual( FullPageArchiver._escape_html(''), '<script>alert("XSS")</script>', ) def test_file_to_base64(self): with tempfile.NamedTemporaryFile(suffix=".txt", delete=False, mode="w") as f: f.write("hello") path = f.name try: b64 = FullPageArchiver._file_to_base64(path) self.assertTrue(len(b64) > 0) finally: os.unlink(path) def test_file_to_base64_missing(self): b64 = FullPageArchiver._file_to_base64("/nonexistent/file.jpg") self.assertEqual(b64, "") class TestFallbackFetch(unittest.TestCase): """HTTP 回退抓取测试(不依赖浏览器)。""" def test_http_fallback_returns_list(self): archiver = FullPageArchiver() # 用不存在的 URL 测试回退逻辑 result = archiver._fetch_pages_http("https://invalid.example.com/nonexistent") self.assertIsInstance(result, list) def test_http_fallback_empty_on_failure(self): archiver = FullPageArchiver() result = archiver._fetch_pages_http("https://192.0.2.1/nonexistent") self.assertEqual(len(result), 0) class TestInlineImageReplacement(unittest.TestCase): """图片内嵌替换测试。""" def setUp(self): self.archiver = FullPageArchiver() def test_inline_with_no_images(self): html = "

No images here

" result = self.archiver._inline_images_in_html(html, {}, "https://example.com") self.assertIn("No images here", result) def test_relink_with_no_images(self): html = "

No images here

" result = self.archiver._relink_images_in_html(html, {}) self.assertIn("No images here", result) def test_extract_body_content(self): html = "

Hello

" content = self.archiver._extract_body_content(html) self.assertIn("Hello", content) def test_extract_body_no_body_tag(self): html = "

No body tag

" content = self.archiver._extract_body_content(html) self.assertIn("No body tag", content) if __name__ == "__main__": unittest.main()