2024-12-10 19:49:11 +08:00
|
|
|
from collections.abc import Callable
|
2024-09-01 14:53:41 +08:00
|
|
|
import json
|
2024-12-10 19:49:11 +08:00
|
|
|
import os
|
2024-08-26 23:54:07 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2024-09-01 14:53:41 +08:00
|
|
|
import nonebot
|
|
|
|
|
from nonebug import NONEBOT_INIT_KWARGS
|
2024-12-10 19:49:11 +08:00
|
|
|
from nonebug.app import App
|
2024-09-01 14:53:41 +08:00
|
|
|
from nonebug.mixin.process import MatcherContext
|
2024-12-10 19:49:11 +08:00
|
|
|
import pytest
|
2024-12-23 10:09:06 +08:00
|
|
|
from pytest_asyncio import is_async_test
|
2024-12-10 19:49:11 +08:00
|
|
|
from pytest_mock import MockerFixture
|
|
|
|
|
from respx import MockRouter
|
2024-09-01 14:53:41 +08:00
|
|
|
|
2025-02-03 21:23:14 +08:00
|
|
|
from tests.config import BotId, GroupId, UserId
|
2024-09-01 14:53:41 +08:00
|
|
|
|
|
|
|
|
nonebot.load_plugin("nonebot_plugin_session")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_response_json(path: str) -> dict:
|
|
|
|
|
return json.loads(
|
|
|
|
|
(Path(__file__).parent / "response" / path).read_text(encoding="utf8")
|
|
|
|
|
)
|
2024-08-26 23:54:07 +08:00
|
|
|
|
|
|
|
|
|
2024-12-23 10:09:06 +08:00
|
|
|
def pytest_collection_modifyitems(items: list[pytest.Item]):
|
|
|
|
|
pytest_asyncio_tests = (item for item in items if is_async_test(item))
|
|
|
|
|
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
|
|
|
|
|
for async_test in pytest_asyncio_tests:
|
|
|
|
|
async_test.add_marker(session_scope_marker, append=False)
|
|
|
|
|
|
|
|
|
|
|
2024-08-26 23:54:07 +08:00
|
|
|
def pytest_configure(config: pytest.Config) -> None:
|
|
|
|
|
config.stash[NONEBOT_INIT_KWARGS] = {
|
|
|
|
|
"driver": "~fastapi+~httpx+~websockets",
|
2024-09-01 14:53:41 +08:00
|
|
|
"superusers": [UserId.SUPERUSER.__str__()],
|
|
|
|
|
"command_start": [""],
|
2024-08-26 23:54:07 +08:00
|
|
|
"session_running_expression": "别急呀,小真寻要宕机了!QAQ",
|
|
|
|
|
"image_to_bytes": False,
|
|
|
|
|
"nickname": ["真寻", "小真寻", "绪山真寻", "小寻子"],
|
|
|
|
|
"session_expire_timeout": 30,
|
|
|
|
|
"self_nickname": "小真寻",
|
|
|
|
|
"db_url": "sqlite://:memory:",
|
2024-09-01 14:53:41 +08:00
|
|
|
"platform_superusers": {
|
|
|
|
|
"qq": [UserId.SUPERUSER_QQ.__str__()],
|
|
|
|
|
"dodo": [UserId.SUPERUSER_DODO.__str__()],
|
|
|
|
|
},
|
2024-08-26 23:54:07 +08:00
|
|
|
"host": "127.0.0.1",
|
|
|
|
|
"port": 8080,
|
2025-01-09 10:29:49 +08:00
|
|
|
"log_level": "INFO",
|
2024-08-26 23:54:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2024-09-01 14:53:41 +08:00
|
|
|
def _init_bot(nonebug_init: None):
|
|
|
|
|
from nonebot.adapters.onebot.v11 import Adapter as OneBotV11Adapter
|
|
|
|
|
|
|
|
|
|
driver = nonebot.get_driver()
|
|
|
|
|
driver.register_adapter(OneBotV11Adapter)
|
|
|
|
|
|
|
|
|
|
nonebot.load_plugin("nonebot_plugin_alconna")
|
|
|
|
|
nonebot.load_plugin("nonebot_plugin_apscheduler")
|
|
|
|
|
nonebot.load_plugin("nonebot_plugin_htmlrender")
|
|
|
|
|
nonebot.load_plugins("zhenxun/builtin_plugins")
|
|
|
|
|
nonebot.load_plugins("zhenxun/plugins")
|
2024-08-26 23:54:07 +08:00
|
|
|
|
2025-02-03 21:23:14 +08:00
|
|
|
# 手动缓存 uninfo 所需信息
|
|
|
|
|
from nonebot_plugin_uninfo import (
|
|
|
|
|
Scene,
|
|
|
|
|
SceneType,
|
|
|
|
|
Session,
|
|
|
|
|
SupportAdapter,
|
|
|
|
|
SupportScope,
|
|
|
|
|
User,
|
|
|
|
|
)
|
|
|
|
|
from nonebot_plugin_uninfo.adapters.onebot11.main import fetcher as onebot11_fetcher
|
|
|
|
|
from nonebot_plugin_uninfo.adapters.onebot12.main import fetcher as onebot12_fetcher
|
|
|
|
|
|
|
|
|
|
onebot11_fetcher.session_cache = {
|
|
|
|
|
f"group_{GroupId.GROUP_ID_LEVEL_5}_{UserId.SUPERUSER}": Session(
|
|
|
|
|
self_id="test",
|
|
|
|
|
adapter=SupportAdapter.onebot11,
|
|
|
|
|
scope=SupportScope.qq_client,
|
|
|
|
|
scene=Scene(str(GroupId.GROUP_ID_LEVEL_0), SceneType.GROUP),
|
|
|
|
|
user=User(str(UserId.SUPERUSER)),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
onebot12_fetcher.session_cache = {
|
|
|
|
|
f"group_{GroupId.GROUP_ID_LEVEL_5}_{UserId.SUPERUSER}": Session(
|
|
|
|
|
self_id="test",
|
|
|
|
|
adapter=SupportAdapter.onebot12,
|
|
|
|
|
scope=SupportScope.qq_client,
|
|
|
|
|
scene=Scene(str(GroupId.GROUP_ID_LEVEL_0), SceneType.GROUP),
|
|
|
|
|
user=User(str(UserId.SUPERUSER)),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 23:54:07 +08:00
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
async def app(app: App, tmp_path: Path, mocker: MockerFixture):
|
2024-12-10 19:49:11 +08:00
|
|
|
from zhenxun.services.db_context import disconnect, init
|
2024-09-01 14:53:41 +08:00
|
|
|
|
|
|
|
|
driver = nonebot.get_driver()
|
|
|
|
|
# 清除连接钩子,现在 NoneBug 会自动触发 on_bot_connect
|
|
|
|
|
driver._bot_connection_hook.clear()
|
|
|
|
|
mock_config_path = mocker.MagicMock()
|
|
|
|
|
mock_config_path.LOG_PATH = tmp_path / "log"
|
|
|
|
|
# mock_config_path.LOG_PATH.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
mock_config_path.DATA_PATH = tmp_path / "data"
|
|
|
|
|
# mock_config_path.DATA_PATH.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
mock_config_path.TEMP_PATH = tmp_path / "resources" / "temp"
|
|
|
|
|
# mock_config_path.TEMP_PATH.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
2024-09-02 01:50:39 +08:00
|
|
|
mocker.patch("zhenxun.configs.path_config", new=mock_config_path)
|
2024-09-01 14:53:41 +08:00
|
|
|
|
|
|
|
|
await init()
|
2024-09-02 01:50:39 +08:00
|
|
|
# await driver._lifespan.startup()
|
2024-09-03 13:04:49 +08:00
|
|
|
os.environ["AIOCACHE_DISABLE"] = "1"
|
2025-07-03 17:39:13 +08:00
|
|
|
os.environ["PYTEST_CURRENT_TEST"] = "1"
|
2024-09-02 01:50:39 +08:00
|
|
|
|
2024-09-01 14:53:41 +08:00
|
|
|
yield app
|
2024-09-03 13:04:49 +08:00
|
|
|
|
|
|
|
|
del os.environ["AIOCACHE_DISABLE"]
|
2024-09-02 01:50:39 +08:00
|
|
|
# await driver._lifespan.shutdown()
|
2024-09-01 14:53:41 +08:00
|
|
|
await disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def create_bot() -> Callable:
|
2024-12-10 19:49:11 +08:00
|
|
|
from nonebot.adapters.onebot.v11 import Adapter, Bot
|
2024-09-01 14:53:41 +08:00
|
|
|
|
2024-09-02 15:00:37 +08:00
|
|
|
def _create_bot(context: MatcherContext) -> Bot:
|
2024-09-01 14:53:41 +08:00
|
|
|
return context.create_bot(
|
|
|
|
|
base=Bot,
|
|
|
|
|
adapter=nonebot.get_adapter(Adapter),
|
|
|
|
|
self_id=BotId.QQ_BOT.__str__(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return _create_bot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mocked_api(respx_mock: MockRouter):
|
|
|
|
|
return respx_mock
|