Skip to content

Commit

Permalink
🐛 fix version 0.13.0
Browse files Browse the repository at this point in the history
fix test
  • Loading branch information
RF-Tar-Railt committed Dec 11, 2024
1 parent 2e3bdae commit a3b5f60
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
test:
strategy:
matrix:
py_ver: ['3.8', '3.9', '3.10', '3.11']
py_ver: ['3.9', '3.10', '3.11', '3.12']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions nonebot/adapters/satori/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ async def _authenticate(self, info: ClientInfo, ws: WebSocket) -> Optional[Liter
for login in resp.body.logins:

if login.sn not in self.bots:
bot = Bot(self, login, info, resp.body.proxy_urls)
bot = Bot(self, login.sn,login, info, resp.body.proxy_urls)
self._bots[info.identity].add(bot.self_id)
self.bot_connect(bot)
log(
"INFO",
f"<y>Bot {escape_tag(bot.identity)}</y> connected",
f"<y>Bot {login.user.id if login.user else login.sn}</y> connected",
)
else:
self._bots[info.identity].add(login.sn)
Expand Down Expand Up @@ -241,7 +241,7 @@ async def _loop(self, info: ClientInfo, ws: WebSocket):
)
else:
if isinstance(event, LoginAddedEvent):
bot = Bot(self, event.login, info, self.proxys[info.identity])
bot = Bot(self, event.login.sn, event.login, info, self.proxys[info.identity])
self._bots[info.identity].add(bot.self_id)
self.bot_connect(bot)
log(
Expand Down
4 changes: 2 additions & 2 deletions nonebot/adapters/satori/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Bot(BaseBot):
adapter: "Adapter"

@override
def __init__(self, adapter: "Adapter", login: Login, info: ClientInfo, proxy_urls: list[str]):
def __init__(self, adapter: "Adapter", self_id: str, login: Login, info: ClientInfo, proxy_urls: list[str]):
# Bot 配置信息
self.info: ClientInfo = info
# Bot 自身所属平台
Expand All @@ -156,7 +156,7 @@ def __init__(self, adapter: "Adapter", login: Login, info: ClientInfo, proxy_url
self._self_info: Login = login
self.proxy_urls = proxy_urls

super().__init__(adapter, login.sn)
super().__init__(adapter, self_id)

def __getattr__(self, item):
raise AttributeError(f"'Bot' object has no attribute '{item}'")
Expand Down
34 changes: 18 additions & 16 deletions nonebot/adapters/satori/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import IO, Any, Union, Generic, Literal, TypeVar, Optional

from pydantic import Field, BaseModel
from nonebot.compat import PYDANTIC_V2, ConfigDict
from nonebot.compat import PYDANTIC_V2, ConfigDict, model_dump

from .utils import log
from .compat import field_validator, model_validator
Expand Down Expand Up @@ -301,21 +301,23 @@ def parse_timestamp(cls, v):

@model_validator(mode="before")
def ensure_login(cls, values):
if "self_id" not in values and "platform" not in values:
log(
"WARNING",
"received event without `self_id` and `platform`, "
"this may be caused by Satori Server used protocol version 1.2.",
)
if "login" in values:
values["self_id"] = values["login"]["user"]["id"]
values["platform"] = values["login"]["platform"]
return values
log(
"WARNING",
"received event without login, " "this may be caused by a bug of Satori Server.",
)
return values
if isinstance(values, dict):
if "self_id" in values and "platform" in values:
log(
"WARNING",
"received event with `self_id` and `platform`, "
"this may be caused by Satori Server used protocol under version 1.2.",
)
if "login" not in values:
values["login"] = model_dump(LoginOnline(
sn=values["self_id"],
status=LoginStatus.ONLINE,
adapter="satori",
platform=values["platform"],
user=User(id=values["self_id"]),
))
if "id" in values and "sn" not in values:
values["sn"] = values["id"]
return values

if PYDANTIC_V2:
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ typeCheckingMode = "basic"
reportShadowedImports = false
disableBytesTypePromotions = true
defineConstant = { PYDANTIC_V2 = true }

[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "session"
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest
from nonebot.drivers import URL
from pytest_asyncio import is_async_test

from fake_server import request_handler
from nonebug import NONEBOT_INIT_KWARGS
from werkzeug.serving import BaseWSGIServer, make_server
Expand All @@ -24,8 +26,15 @@ def pytest_configure(config: pytest.Config) -> None:
}


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)


@pytest.fixture(scope="session", autouse=True)
def _init_adapter(nonebug_init: None):
async def after_nonebot_init(after_nonebot_init: None):
driver = nonebot.get_driver()
driver.register_adapter(SatoriAdapter)

Expand Down
23 changes: 18 additions & 5 deletions tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import nonebot
from nonebot.adapters.satori import Bot, Adapter
from nonebot.adapters.satori.models import Login, LoginStatus
from nonebot.adapters.satori.models import Login, LoginStatus, User
from nonebot.adapters.satori.event import PublicMessageCreatedEvent


Expand All @@ -22,19 +22,32 @@ async def handle(bot: Bot):
async with app.test_matcher(cmd) as ctx:
adapter: Adapter = nonebot.get_adapter(Adapter)
bot: Bot = ctx.create_bot(
base=Bot, adapter=adapter, self_id="0", login=Login(status=LoginStatus.CONNECT, platform="test"), info=None
base=Bot,
adapter=adapter,
self_id="0",
login=Login(sn="0", adapter="test", status=LoginStatus.ONLINE, platform="test", user=User(id="12345", name="test")),
info=None,
proxy_urls=[],
)

ctx.receive_event(
bot,
type_validate_python(
PublicMessageCreatedEvent,
{
"id": 1,
"sn": 1,
"type": "message-created",
"platform": "test",
"self_id": "0",
"timestamp": 1000 * int(datetime.now().timestamp()),
"login": {
"sn": "0",
"adapter": "test",
"platform": "test",
"status": 1,
"user": {
"id": "12345",
"nick": "test",
},
},
"channel": {
"id": "67890",
"type": 0,
Expand Down
50 changes: 0 additions & 50 deletions tests/test_connection.py

This file was deleted.

0 comments on commit a3b5f60

Please sign in to comment.