nonebot-bison/tests/theme/test_themes.py
2024-06-03 18:36:07 +08:00

298 lines
8.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from time import time
from typing import Any
from inspect import cleandoc
import pytest
from flaky import flaky
from nonebug import App
now = time()
passed = now - 3 * 60 * 60
raw_post_list_1 = [{"id": 1, "text": "p1", "date": now, "tags": ["tag1"], "category": 1}]
raw_post_list_2 = raw_post_list_1 + [
{"id": 2, "text": "p2", "date": now, "tags": ["tag1"], "category": 1},
{"id": 3, "text": "p3", "date": now, "tags": ["tag2"], "category": 2},
{"id": 4, "text": "p4", "date": now, "tags": ["tag2"], "category": 3},
]
@pytest.fixture()
def mock_platform(app: App):
from nonebot_bison.post import Post
from nonebot_bison.types import Target, RawPost
from nonebot_bison.platform.platform import NewMessage
class MockPlatform(NewMessage):
platform_name = "mock_platform"
name = "Mock Platform"
enabled = True
is_common = True
schedule_interval = 10
enable_tag = False
categories = {}
has_target = True
sub_index = 0
@classmethod
async def get_target_name(cls, client, _: "Target"):
return "MockPlatform"
def get_id(self, post: "RawPost") -> Any:
return post["id"]
def get_date(self, raw_post: "RawPost") -> float:
return raw_post["date"]
async def parse(self, raw_post: "RawPost") -> "Post":
return Post(
self,
raw_post["text"],
url="http://t.tt/" + str(self.get_id(raw_post)),
nickname="Mock",
)
@classmethod
async def get_sub_list(cls, _: "Target"):
if cls.sub_index == 0:
cls.sub_index += 1
return raw_post_list_1
else:
return raw_post_list_2
return MockPlatform
@pytest.fixture()
def mock_post(app: App, mock_platform):
from nonebot_bison.post import Post
from nonebot_bison.utils import ProcessContext
from nonebot_bison.utils.scheduler_config import DefaultClientManager
return Post(
m := mock_platform(ProcessContext(DefaultClientManager())),
"text",
title="title",
images=["http://t.tt/1.jpg"],
timestamp=1234567890,
url="http://t.tt/1",
avatar="http://t.tt/avatar.jpg",
nickname="Mock",
description="description",
repost=Post(
m,
"repost",
title="repost-title",
images=["http://t.tt/2.jpg"],
timestamp=1234567891,
url="http://t.tt/2",
avatar="http://t.tt/avatar-re.jpg",
nickname="re-Mock",
description="re-description",
),
)
@pytest.mark.asyncio
async def test_theme_need_browser(app: App, mock_post):
from nonebot_bison.theme import Theme, theme_manager
class MockTheme(Theme):
name: str = "mock_theme"
need_browser: bool = False
async def render(self, post):
return []
theme = MockTheme()
theme_manager.register(theme)
mock_post.platform.default_theme = theme.name
await theme.do_render(mock_post)
assert not theme._browser_checked
theme.need_browser = True
await theme.do_render(mock_post)
assert theme._browser_checked
theme_manager.unregister(theme.name)
@pytest.mark.asyncio
async def test_theme_no_enable_use_browser(app: App, mock_post):
from nonebot_bison.plugin_config import plugin_config
plugin_config.bison_use_browser = False
from nonebot_bison.theme import Theme, ThemeRenderUnsupportError, theme_manager
class MockTheme(Theme):
name: str = "mock_theme"
need_browser: bool = True
async def render(self, post):
return []
theme = MockTheme()
theme_manager.register(theme)
mock_post.platform.default_theme = theme.name
with pytest.raises(ThemeRenderUnsupportError, match="not support render"):
await theme.do_render(mock_post)
theme_manager.unregister(theme.name)
plugin_config.bison_use_browser = True
@pytest.mark.asyncio
@flaky(max_runs=3, min_passes=1)
async def test_arknights_theme(app: App, mock_post):
from nonebot_plugin_saa import Image
from nonebot_bison.theme import theme_manager
from nonebot_bison.theme.themes.arknights import ArknightsTheme
arknights_theme = theme_manager["arknights"]
assert isinstance(arknights_theme, ArknightsTheme)
assert arknights_theme.name == "arknights"
res = await arknights_theme.render(mock_post)
assert len(res) == 1
assert isinstance(res[0], Image)
@pytest.mark.asyncio
async def test_basic_theme(app: App, mock_post):
from nonebot_plugin_saa import Text, Image
from nonebot_bison.theme import theme_manager
from nonebot_bison.theme.themes.basic import BasicTheme
basic_theme = theme_manager["basic"]
assert isinstance(basic_theme, BasicTheme)
assert basic_theme.name == "basic"
res = await basic_theme.render(mock_post)
assert len(res) == 2
assert res[0] == Text(
cleandoc(
"""title
text
--------------
转发自 re-Mock:
repost-title
repost
--------------
来源: Mock Platform Mock
转发详情http://t.tt/2
详情: http://t.tt/1"""
)
)
assert isinstance(res[1], Image)
mock_post2 = mock_post
mock_post2.repost = None
mock_post2.images = []
res2 = await basic_theme.render(mock_post2)
assert len(res2) == 1
assert res2[0] == Text(
cleandoc(
"""title
text
--------------
来源: Mock Platform Mock
详情: http://t.tt/1"""
)
)
@pytest.mark.asyncio
async def test_brief_theme(app: App, mock_post):
from nonebot_plugin_saa import Text, Image
from nonebot_bison.theme import theme_manager
from nonebot_bison.theme.themes.brief import BriefTheme
brief_theme = theme_manager["brief"]
assert isinstance(brief_theme, BriefTheme)
assert brief_theme.name == "brief"
res = await brief_theme.render(mock_post)
assert len(res) == 2
assert res[0] == Text(
cleandoc(
"""title
来源: Mock Platform Mock 的转发
转发详情: http://t.tt/2
详情: http://t.tt/1
"""
)
)
assert isinstance(res[1], Image)
mock_post2 = mock_post
mock_post2.repost = None
mock_post2.images = []
res2 = await brief_theme.render(mock_post2)
assert len(res2) == 1
assert res2[0] == Text(
cleandoc(
"""title
来源: Mock Platform Mock
详情: http://t.tt/1"""
)
)
@pytest.mark.render
@pytest.mark.asyncio
@flaky(max_runs=3, min_passes=1)
async def test_ceobecanteen_theme(app: App, mock_post):
from nonebot_plugin_saa import Text, Image
from nonebot_bison.theme import theme_manager
from nonebot_bison.theme.themes.ceobe_canteen import CeobeCanteenTheme
ceobecanteen_theme = theme_manager["ceobecanteen"]
assert isinstance(ceobecanteen_theme, CeobeCanteenTheme)
assert ceobecanteen_theme.name == "ceobecanteen"
res = await ceobecanteen_theme.render(mock_post)
assert len(res) == 3
assert isinstance(res[0], Image)
assert isinstance(res[2], Image)
assert res[1] == Text("来源: Mock Platform Mock\n详情: http://t.tt/1")
@pytest.mark.render
@pytest.mark.asyncio
@flaky(max_runs=3, min_passes=1)
async def test_ht2i_theme(app: App, mock_post):
from nonebot_plugin_saa import Text, Image
from nonebot_bison.theme import theme_manager
from nonebot_bison.theme.themes.ht2i import Ht2iTheme
ht2i_theme = theme_manager["ht2i"]
assert isinstance(ht2i_theme, Ht2iTheme)
assert ht2i_theme.name == "ht2i"
res = await ht2i_theme.render(mock_post)
assert len(res) == 3
assert isinstance(res[0], Image)
assert isinstance(res[2], Image)
assert res[1] == Text("转发详情: http://t.tt/2\n详情: http://t.tt/1")
mock_post2 = mock_post
mock_post2.repost = None
mock_post2.images = []
res2 = await ht2i_theme.render(mock_post2)
assert len(res2) == 2
assert res2[1] == Text("详情: http://t.tt/1")