first commit

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kunkunxiaomaoandClaude committed 2026-11-16 20:45:22 +08:00
commit 9c5fef1a4d
412 files changed
+94582

No files matched your search

+296
View File
@@ -0,0 +1,296 @@
from __future__ import annotations
import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import Mock, patch
import requests
from pixiv_app.core.plugin.base import PluginAuthError, PluginDownloadError, PluginError, PluginParseError, Resource
from pixiv_app.core.plugin.manager import PluginManager
from plugins.ao3.plugin import (
AO3Plugin,
AO3_BASE,
AO3WorkMeta,
_ao3_request,
_extract_html_work_text,
build_ao3_sync_metadata,
extract_ao3_ids,
parse_work_meta,
)
PUBLIC_WORK_HTML = """
<html>
<head><meta property="og:title" content="Fallback Title"></head>
<body>
<h2 class="title heading">The Sakura Work</h2>
<a rel="author" href="/users/sakura_user/pseuds/Sakura">Sakura</a>
<blockquote class="userstuff"><p>A gentle summary.</p></blockquote>
<dl class="work meta group">
<dt>Rating:</dt><dd><a class="tag">Teen And Up Audiences</a></dd>
<dt>Archive Warnings*:</dt><dd><a class="tag">No Archive Warnings Apply</a></dd>
<dt>Fandoms:</dt><dd><a class="tag">Original Work</a></dd>
<dt>Relationships:</dt><dd><a class="tag">A/B</a></dd>
<dt>Characters:</dt><dd><a class="tag">A</a><a class="tag">B</a></dd>
<dt>Additional Tags:</dt><dd><a class="tag">Fluff</a><a class="tag">Tea</a></dd>
<dt>Language:</dt><dd>English</dd>
<dt>Chapters:</dt><dd>3/5</dd>
<dt>Words:</dt><dd>12,345</dd>
<dt>Published:</dt><dd>2024-01-02</dd>
<dt>Updated:</dt><dd>2024-02-03</dd>
</dl>
<div class="userstuff module" role="article">
<h3 class="landmark heading" id="work">Chapter One</h3>
<p>Hello AO3.</p>
<p>Second paragraph.</p>
</div>
</body>
</html>
"""
AO3_CHAPTER_PAGE_WITH_SUMMARY_HTML = """
<html>
<body>
<div id="workskin">
<div class="preface group">
<div class="summary module">
<h3 class="heading">Summary:</h3>
<blockquote class="userstuff">
<p>Summary should not be treated as body.</p>
</blockquote>
</div>
</div>
<div id="chapters">
<div class="chapter" id="chapter-1">
<div class="chapter preface group">
<h3 class="title">
<a href="/works/86702241/chapters/229471946">Chapter 1</a>
</h3>
</div>
<!--main content-->
<div class="userstuff module" role="article">
<h3 class="landmark heading" id="work">Chapter Text</h3>
<p>The phone rang in the deep night.</p>
<p>Only this person had that ringtone.</p>
</div>
<!--/main-->
</div>
</div>
</div>
<!-- BEGIN comment section -->
<div id="feedback"><p>Comment UI should not be body.</p></div>
</body>
</html>
"""
class FakeResponse:
def __init__(
self,
*,
status_code: int = 200,
text: str = PUBLIC_WORK_HTML,
url: str = f"{AO3_BASE}/works/12345",
content: bytes = b"",
headers: dict[str, str] | None = None,
) -> None:
self.status_code = status_code
self.text = text
self.url = url
self.content = content
self.headers = headers or {}
self.ok = 200 <= status_code < 400
def raise_for_status(self) -> None:
if not self.ok:
raise requests.HTTPError(f"HTTP {self.status_code}")
def iter_content(self, chunk_size: int = 1024):
yield self.content or b"x" * 2048
class AO3PluginTests(unittest.TestCase):
def test_url_recognition_is_strict(self) -> None:
plugin = AO3Plugin()
self.assertTrue(plugin.can_handle("https://archiveofourown.org/works/12345"))
self.assertTrue(plugin.can_handle("https://archiveofourown.org/works/12345/chapters/67890"))
self.assertTrue(plugin.can_handle("https://ao3.org/works/12345"))
self.assertFalse(plugin.can_handle("https://evilarchiveofourown.org/works/12345"))
self.assertFalse(plugin.can_handle("https://archiveofourown.org/users/sakura"))
self.assertFalse(plugin.can_handle(""))
def test_extract_work_and_chapter_ids(self) -> None:
self.assertEqual(extract_ao3_ids("https://archiveofourown.org/works/12345"), ("12345", None))
self.assertEqual(
extract_ao3_ids("https://archiveofourown.org/works/12345/chapters/67890"),
("12345", "67890"),
)
with self.assertRaises(PluginParseError):
extract_ao3_ids("https://archiveofourown.org/users/sakura")
def test_public_work_parse_without_cookie(self) -> None:
plugin = AO3Plugin()
with patch("plugins.ao3.plugin.requests.get", return_value=FakeResponse()) as mocked:
resources = plugin.parse("https://archiveofourown.org/works/12345")
self.assertEqual(mocked.call_args.kwargs["headers"].get("Cookie"), None)
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].id, "12345")
self.assertEqual(resources[0].title, "The Sakura Work")
self.assertEqual(resources[0].author, "Sakura")
self.assertEqual(resources[0].metadata["source_platform"], "ao3")
def test_cookie_injected_into_headers_and_requests(self) -> None:
plugin = AO3Plugin(cookie="session=abc")
self.assertEqual(plugin.get_headers()["Cookie"], "session=abc")
with patch("plugins.ao3.plugin.requests.get", return_value=FakeResponse()) as mocked:
_ao3_request("https://archiveofourown.org/works/12345", cookie="session=abc")
self.assertEqual(mocked.call_args.kwargs["headers"]["Cookie"], "session=abc")
def test_metadata_parse(self) -> None:
meta = parse_work_meta(PUBLIC_WORK_HTML, "https://archiveofourown.org/works/12345")
self.assertEqual(meta.work_id, "12345")
self.assertEqual(meta.title, "The Sakura Work")
self.assertEqual(meta.author, "Sakura")
self.assertEqual(meta.author_id, "sakura_user")
self.assertEqual(meta.chapter_count, 3)
self.assertEqual(meta.status, "In Progress")
self.assertEqual(meta.word_count, 12345)
self.assertIn("Fluff", meta.tags)
self.assertEqual(meta.updated_at, "2024-02-03T00:00:00+00:00")
def test_html_primary_download_with_epub_secondary(self) -> None:
"""HTML full-work page is the primary download; EPUB is a secondary bonus."""
plugin = AO3Plugin(cookie="session=abc")
resource = _resource()
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
epub_url = f"{AO3_BASE}/downloads/12345/Title.epub"
def fake_download(_url: str, save_path: Path, **_kwargs) -> Path:
save_path.write_bytes(b"PK" + b"x" * 2048)
return save_path
with (
patch(
"plugins.ao3.plugin._ao3_request",
return_value=FakeResponse(text=PUBLIC_WORK_HTML),
) as mocked_html_request,
patch(
"plugins.ao3.plugin._resolve_ao3_epub_url",
return_value=epub_url,
),
patch(
"plugins.ao3.plugin._ao3_download_file",
side_effect=fake_download,
) as mocked_download,
):
paths = plugin.download(resource, root)
# Primary: HTML → .txt
self.assertTrue(any(path.suffix == ".txt" for path in paths),
f"Expected .txt file, got: {[p.suffix for p in paths]}")
html_url = mocked_html_request.call_args.args[0]
self.assertIn("view_full_work=true", html_url)
# Secondary: EPUB → .epub
self.assertTrue(any(path.suffix == ".epub" for path in paths),
f"Expected .epub file, got: {[p.suffix for p in paths]}")
self.assertIn(epub_url, mocked_download.call_args.args[0])
def test_html_only_download_when_epub_not_available(self) -> None:
"""When EPUB resolution returns None, the .txt alone still succeeds."""
plugin = AO3Plugin()
resource = _resource()
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
with (
patch(
"plugins.ao3.plugin._ao3_request",
return_value=FakeResponse(text=PUBLIC_WORK_HTML),
) as mocked_request,
patch(
"plugins.ao3.plugin._resolve_ao3_epub_url",
return_value=None,
),
):
paths = plugin.download(resource, root)
self.assertTrue(any(path.suffix == ".txt" for path in paths),
f"Expected .txt file, got: {[p.suffix for p in paths]}")
self.assertFalse(any(path.suffix == ".epub" for path in paths))
self.assertIn("view_full_work=true", mocked_request.call_args.args[0])
def test_chapter_page_body_extraction_ignores_summary_and_feedback(self) -> None:
text = _extract_html_work_text(AO3_CHAPTER_PAGE_WITH_SUMMARY_HTML)
self.assertIn("The phone rang in the deep night.", text)
self.assertIn("Only this person had that ringtone.", text)
self.assertNotIn("Summary should not be treated as body.", text)
self.assertNotIn("Comment UI should not be body.", text)
self.assertNotIn("Chapter Text", text)
def test_update_check_returns_metadata(self) -> None:
plugin = AO3Plugin()
with patch("plugins.ao3.plugin._ao3_request", return_value=FakeResponse(text=PUBLIC_WORK_HTML)):
update = plugin.check_update("12345")
assert update is not None
self.assertEqual(update["work_id"], "12345")
self.assertEqual(update["chapter_count"], 3)
self.assertTrue(update["has_update"])
def test_cloud_sync_payload_generation(self) -> None:
meta = parse_work_meta(PUBLIC_WORK_HTML, "https://archiveofourown.org/works/12345")
payload = build_ao3_sync_metadata(meta, Path("work.txt"))
self.assertEqual(payload.resource_type, "novel")
self.assertEqual(payload.source_platform, "ao3")
self.assertEqual(payload.source_id, "12345")
self.assertEqual(payload.series_id, "12345")
self.assertEqual(payload.word_count, 12345)
self.assertIn("Fluff", payload.tags)
def test_plugin_registration_and_loading(self) -> None:
root = Path(__file__).resolve().parents[1]
if str(root) not in sys.path:
sys.path.insert(0, str(root))
manager = PluginManager([root / "plugins"])
plugin = manager.load_plugin(root / "plugins" / "ao3" / "plugin.py")
self.assertIsNotNone(plugin)
assert plugin is not None
self.assertEqual(plugin.name, "AO3")
self.assertIs(manager.get_plugin_for_url("https://archiveofourown.org/works/12345"), plugin)
def test_error_handling_invalid_link_login_wall_and_network_failure(self) -> None:
plugin = AO3Plugin()
with self.assertRaises(PluginParseError):
plugin.parse("https://archiveofourown.org/users/sakura")
with (
patch("plugins.ao3.plugin.requests.get", return_value=FakeResponse(text="Please log in")),
self.assertRaises(PluginAuthError),
):
_ao3_request("https://archiveofourown.org/works/12345", retries=1)
with (
patch("plugins.ao3.plugin.requests.get", side_effect=requests.RequestException("offline")),
self.assertRaises(PluginError),
):
_ao3_request("https://archiveofourown.org/works/12345", retries=1)
def _resource() -> Resource:
return Resource(
id="12345",
url="https://archiveofourown.org/works/12345",
title="The Sakura Work",
author="Sakura",
author_id="sakura_user",
files=[],
metadata={"work_id": "12345", "summary": "A gentle summary."},
)
if __name__ == "__main__":
unittest.main()