88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
from __future__ import annotations
|
|||
|
|
|
||
|
|
import sys
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
from unittest.mock import AsyncMock, patch
|
||
|
|
|
||
|
|
from pixiv_app.core.plugin.base import Resource
|
||
|
|
from pixiv_app.core.plugin.manager import PluginManager
|
||
|
|
from plugins.pixiv.plugin import PixivPlugin
|
||
|
|
from plugins.x.plugin import XPlugin
|
||
|
|
|
||
|
|
|
||
|
|
class BundledPluginRegressionTests(unittest.TestCase):
|
||
|
|
@classmethod
|
||
|
|
def setUpClass(cls) -> None:
|
||
|
|
cls.root = Path(__file__).resolve().parents[1]
|
||
|
|
if str(cls.root) not in sys.path:
|
||
|
|
sys.path.insert(0, str(cls.root))
|
||
|
|
|
||
|
|
def test_all_bundled_plugins_load_without_errors(self) -> None:
|
||
|
|
manager = PluginManager([self.root / "plugins"])
|
||
|
|
plugins = manager.load_all()
|
||
|
|
|
||
|
|
self.assertTrue({"Pixiv", "X", "Douyin", "AO3"}.issubset(plugins))
|
||
|
|
self.assertEqual(manager.load_errors, {})
|
||
|
|
for plugin in plugins.values():
|
||
|
|
self.assertIsInstance(plugin.validate(), bool)
|
||
|
|
|
||
|
|
def test_manager_routes_x_and_pixiv_urls(self) -> None:
|
||
|
|
manager = PluginManager([self.root / "plugins"])
|
||
|
|
manager.load_all()
|
||
|
|
|
||
|
|
self.assertEqual(
|
||
|
|
manager.get_plugin_for_url("https://x.com/example/status/123456789").name,
|
||
|
|
"X",
|
||
|
|
)
|
||
|
|
self.assertEqual(
|
||
|
|
manager.get_plugin_for_url("https://www.pixiv.net/artworks/123456789").name,
|
||
|
|
"Pixiv",
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_pixiv_novel_url_creates_lazy_novel_resource(self) -> None:
|
||
|
|
plugin = PixivPlugin()
|
||
|
|
|
||
|
|
resources = plugin.parse("https://www.pixiv.net/novel/show.php?id=123456")
|
||
|
|
|
||
|
|
self.assertEqual(len(resources), 1)
|
||
|
|
self.assertEqual(resources[0].id, "123456")
|
||
|
|
self.assertEqual(resources[0].metadata["kind"], "novel")
|
||
|
|
|
||
|
|
def test_x_parse_wrapper_returns_resources_from_parser(self) -> None:
|
||
|
|
plugin = XPlugin()
|
||
|
|
plugin.config.safety_mode = False
|
||
|
|
expected = [
|
||
|
|
Resource(
|
||
|
|
id="123_0",
|
||
|
|
url="https://x.com/example/status/123",
|
||
|
|
title="example",
|
||
|
|
author="Example",
|
||
|
|
author_id="example",
|
||
|
|
files=[{"url": "https://video.twimg.com/example.mp4", "type": "video"}],
|
||
|
|
)
|
||
|
|
]
|
||
|
|
|
||
|
|
with patch.object(plugin, "_parse_async", AsyncMock(return_value=expected)):
|
||
|
|
resources = plugin.parse("https://x.com/example/status/123")
|
||
|
|
|
||
|
|
self.assertEqual(resources, expected)
|
||
|
|
self.assertEqual(resources[0].files[0]["type"], "video")
|
||
|
|
|
||
|
|
def test_x_url_recognition_rejects_unrelated_hosts(self) -> None:
|
||
|
|
plugin = XPlugin()
|
||
|
|
|
||
|
|
self.assertTrue(plugin.can_handle("https://x.com/example/media"))
|
||
|
|
self.assertTrue(plugin.can_handle("https://twitter.com/example/status/123"))
|
||
|
|
self.assertFalse(plugin.can_handle("https://example.com/x.com/example/status/123"))
|
||
|
|
|
||
|
|
def test_pixiv_url_recognition_rejects_unrelated_hosts(self) -> None:
|
||
|
|
plugin = PixivPlugin()
|
||
|
|
|
||
|
|
self.assertTrue(plugin.can_handle("https://www.pixiv.net/artworks/123"))
|
||
|
|
self.assertTrue(plugin.can_handle("https://i.pximg.net/img-original/example.jpg"))
|
||
|
|
self.assertFalse(plugin.can_handle("https://example.com/pixiv.net/artworks/123"))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|