Skip to content

Commit 54004ff

Browse files
committed
unittest: 给kook适配器添加ws消息数据模型单测,覆盖常见kook ws事件
1 parent a928689 commit 54004ff

9 files changed

Lines changed: 322 additions & 37 deletions
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"s": 0,
3+
"d": {
4+
"channel_type": "GROUP",
5+
"type": 9,
6+
"target_id": "2732467349811313213",
7+
"author_id": "7324688132731983",
8+
"content": "done!",
9+
"extra": {
10+
"quote": {
11+
"id": "69a788adb0cfb9ece50eae1c",
12+
"rong_id": "7baef72c-0cd7-49ad-9592-1615236136cb",
13+
"type": 9,
14+
"content": "/am 1",
15+
"interact_res": null,
16+
"create_at": 1772587180973,
17+
"author": {
18+
"id": "2701973210937821093781",
19+
"username": "some_username",
20+
"identify_num": "4198",
21+
"online": true,
22+
"os": "Websocket",
23+
"status": 1,
24+
"avatar": "https://example.com",
25+
"vip_avatar": "https://example.com",
26+
"banner": "",
27+
"nickname": "some_username",
28+
"roles": [
29+
63724577
30+
],
31+
"is_vip": false,
32+
"vip_amp": false,
33+
"bot": false,
34+
"nameplate": [],
35+
"kpm_vip": null,
36+
"wealth_level": 0,
37+
"decorations_id_map": null,
38+
"mobile_verified": true,
39+
"is_sys": false,
40+
"joined_at": 1772259607000,
41+
"active_time": 1772587181304
42+
},
43+
"can_jump": true,
44+
"preview_content": null,
45+
"kmarkdown": {
46+
"mention_part": [],
47+
"mention_role_part": [],
48+
"channel_part": [],
49+
"item_part": []
50+
}
51+
},
52+
"type": 9,
53+
"code": "",
54+
"guild_id": "273902183210983210983",
55+
"guild_type": 0,
56+
"channel_name": "聊天大厅",
57+
"author": {
58+
"id": "7324688132731983",
59+
"username": "Bot_Test",
60+
"identify_num": "9561",
61+
"online": true,
62+
"os": "Websocket",
63+
"status": 0,
64+
"avatar": "https://example.com",
65+
"vip_avatar": "https://example.com",
66+
"banner": "",
67+
"nickname": "Bot_Test",
68+
"roles": [
69+
63725384
70+
],
71+
"is_vip": false,
72+
"vip_amp": false,
73+
"bot": true,
74+
"nameplate": [],
75+
"kpm_vip": null,
76+
"wealth_level": 0,
77+
"bot_status": 0,
78+
"tag_info": {
79+
"color": "#0096FF",
80+
"bg_color": "#0096FF33",
81+
"text": "机器人"
82+
},
83+
"is_sys": false,
84+
"client_id": "sAdiIHoGhdSFUOA",
85+
"verified": false
86+
},
87+
"visible_only": "",
88+
"mention": [],
89+
"mention_no_at": [],
90+
"mention_all": false,
91+
"mention_roles": [],
92+
"mention_here": false,
93+
"nav_channels": [],
94+
"kmarkdown": {
95+
"raw_content": "done!",
96+
"mention_part": [],
97+
"mention_role_part": [],
98+
"channel_part": [],
99+
"spl": []
100+
},
101+
"emoji": [],
102+
"preview_content": "",
103+
"channel_type": 1,
104+
"last_msg_content": "Bot_Test:done!",
105+
"send_msg_device": 0
106+
},
107+
"msg_id": "c51a8761-63bv-5l2a-5681-0ac16e140a1b",
108+
"msg_timestamp": 1772587182234,
109+
"nonce": "",
110+
"from_type": 1
111+
},
112+
"extra": {
113+
"verifyToken": "kW4FH_ASHio1hosd",
114+
"encryptKey": "",
115+
"callbackUrl": "",
116+
"intent": 255
117+
},
118+
"sn": 3
119+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"s": 1,
3+
"d": {
4+
"sessionId": "67d7d497-2b10-4849-9c2c-dda2fe58ed60",
5+
"session_id": "67d7d497-2b10-4849-9c2c-dda2fe58ed60",
6+
"code": 0
7+
}
8+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"s": 0,
3+
"d": {
4+
"channel_type": "PERSON",
5+
"type": 10,
6+
"target_id": "2732467349811313213",
7+
"author_id": "7324688132731983",
8+
"content": "[{\"theme\":\"primary\",\"color\":\"\",\"size\":\"lg\",\"expand\":false,\"modules\":[{\"type\":\"audio\",\"cover\":\"\",\"duration\":0,\"title\":\"dancing_shot5.wav\",\"src\":\"https:\\/\\/img.kookapp.cn\\/attachments\\/2026-03\\/03\\/69a6841c3125d.wav\",\"external\":false,\"size\":443414,\"canDownload\":true,\"elements\":[]}],\"type\":\"card\"}]",
9+
"extra": {
10+
"type": 10,
11+
"code": "1738914789hd8fd91098he809h19y491",
12+
"author": {
13+
"id": "7324688132731983",
14+
"username": "Bot_Test",
15+
"identify_num": "9561",
16+
"online": true,
17+
"os": "Websocket",
18+
"status": 0,
19+
"avatar": "https://example.com",
20+
"vip_avatar": "https://example.com",
21+
"banner": "",
22+
"nickname": "Bot_Test",
23+
"roles": [],
24+
"is_vip": false,
25+
"vip_amp": false,
26+
"bot": true,
27+
"nameplate": [],
28+
"kpm_vip": null,
29+
"wealth_level": 0,
30+
"bot_status": 0,
31+
"tag_info": {
32+
"color": "#0096FF",
33+
"bg_color": "#0096FF33",
34+
"text": "机器人"
35+
},
36+
"is_sys": false,
37+
"client_id": "u109u3108h8ds0qsdaHUIOS",
38+
"verified": false
39+
},
40+
"visible_only": "",
41+
"mention": [],
42+
"mention_no_at": [],
43+
"mention_all": false,
44+
"mention_roles": [],
45+
"mention_here": false,
46+
"nav_channels": [],
47+
"emoji": [],
48+
"kmarkdown": {
49+
"raw_content": "[音频]dancing_shot5.wav",
50+
"mention_part": [],
51+
"mention_role_part": [],
52+
"channel_part": []
53+
},
54+
"editable": false,
55+
"preview_content": "[音频]dancing_shot5.wav",
56+
"preview_content_search": "[音频]dancing_shot5.wav",
57+
"last_msg_content": "[音频]dancing_shot5.wav",
58+
"send_msg_device": 0
59+
},
60+
"msg_id": "82c0b042-79b4-4066-a0f4-6c7a95c74e67",
61+
"msg_timestamp": 1772587223043,
62+
"nonce": "",
63+
"from_type": 1
64+
},
65+
"extra": {
66+
"verifyToken": "kW4FH_ASHio1hosd",
67+
"encryptKey": "",
68+
"callbackUrl": "",
69+
"intent": 255
70+
},
71+
"sn": 5
72+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"s": 3
3+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"s": 0,
3+
"d": {
4+
"channel_type": "PERSON",
5+
"type": 9,
6+
"target_id": "7324688132731983",
7+
"author_id": "2732467349811313213",
8+
"content": "/help",
9+
"extra": {
10+
"type": 9,
11+
"code": "1738914789hd8fd91098he809h19y491",
12+
"author": {
13+
"id": "2732467349811313213",
14+
"username": "shuiping233",
15+
"identify_num": "4198",
16+
"online": true,
17+
"os": "Websocket",
18+
"status": 1,
19+
"avatar": "https://example.com",
20+
"vip_avatar": "https://example.com",
21+
"banner": "",
22+
"nickname": "shuiping233",
23+
"roles": [],
24+
"is_vip": false,
25+
"vip_amp": false,
26+
"bot": false,
27+
"nameplate": [],
28+
"kpm_vip": null,
29+
"wealth_level": 0,
30+
"decorations_id_map": null,
31+
"is_sys": false
32+
},
33+
"visible_only": "",
34+
"mention": [],
35+
"mention_no_at": [],
36+
"mention_all": false,
37+
"mention_roles": [],
38+
"mention_here": false,
39+
"nav_channels": [],
40+
"kmarkdown": {
41+
"raw_content": "/help",
42+
"mention_part": [],
43+
"mention_role_part": [],
44+
"channel_part": [],
45+
"spl": []
46+
},
47+
"emoji": [],
48+
"preview_content": "",
49+
"last_msg_content": "/help",
50+
"send_msg_device": 0
51+
},
52+
"msg_id": "b0f57b9e-2cd4-4e07-8f0e-9c1ecfeaa837",
53+
"msg_timestamp": 1772587358662,
54+
"nonce": "6AwzUe5YjgyC8pAfxcLGjewL",
55+
"from_type": 1
56+
},
57+
"extra": {
58+
"verifyToken": "kW4FH_ASHio1hosd",
59+
"encryptKey": "",
60+
"callbackUrl": "",
61+
"intent": 255
62+
},
63+
"sn": 19
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"s": 0,
3+
"d": {
4+
"channel_type": "PERSON",
5+
"type": 255,
6+
"target_id": "7324688132731983",
7+
"author_id": "1",
8+
"content": "[系统消息]",
9+
"extra": {
10+
"type": "guild_member_offline",
11+
"body": {
12+
"user_id": "2732467349811313213",
13+
"event_time": 1772589748914,
14+
"guilds": [
15+
"78941897317309873120973"
16+
]
17+
}
18+
},
19+
"msg_id": "e91b4451-75ce-47bd-bda6-e4498ed8d30d",
20+
"msg_timestamp": 1772589748933,
21+
"nonce": "",
22+
"from_type": 1
23+
},
24+
"extra": {
25+
"verifyToken": "kW4FH_ASHio1hosd",
26+
"encryptKey": "",
27+
"callbackUrl": "",
28+
"intent": 255
29+
},
30+
"sn": 1
31+
}

tests/test_kook/shared.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
22

33

4-
TEST_DATA_DIR = Path(__file__).parent / "data"
4+
CURRENT_DIR = Path(__file__).parent
5+
TEST_DATA_DIR = CURRENT_DIR / "data"

tests/test_kook/test_kook_event.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -185,39 +185,4 @@ async def test_kook_event_warp_message(
185185

186186
result = await event._wrap_message(1, input_message)
187187
assert result == expected_output
188-
189-
190-
# @pytest.mark.asyncio
191-
# @pytest.mark.parametrize(
192-
# "message_chain,send_text_expected_output,expected_error",
193-
# [
194-
# (
195-
# MessageChain(
196-
# chain=[
197-
# Image(file="test image"),
198-
# Plain(text="test plain"),
199-
# ],
200-
# ),
201-
# ""
202-
# ),
203-
# ],
204-
# )
205-
# async def test_kook_event_send():
206-
# client = await mock_kook_client(
207-
# "",
208-
# "",
209-
# )
210-
211-
# event = KookEvent(
212-
# "",
213-
# mock_astrbot_message(),
214-
# PlatformMetadata(
215-
# name="test",
216-
# id="test",
217-
# description="test",
218-
# ),
219-
# "",
220-
# client,
221-
# )
222-
223-
# await event.send(message=mock_astrbot_message())
188+

tests/test_kook/test_kook_types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
KmarkdownElement,
1818
KookCardMessage,
1919
KookModuleType,
20+
KookWebsocketEvent,
2021
ParagraphStructure,
2122
PlainTextElement,
2223
SectionModule,
@@ -106,3 +107,24 @@ def test_all_kook_card_type():
106107
],
107108
).to_json(indent=4, ensure_ascii=False)
108109
assert json_output == expect_json_data
110+
111+
@pytest.mark.parametrize(
112+
"expected_json_data_path",
113+
[
114+
(TEST_DATA_DIR/"kook_ws_event_group_message.json"),
115+
(TEST_DATA_DIR/"kook_ws_event_hello.json"),
116+
(TEST_DATA_DIR/"kook_ws_event_message_with_card.json"),
117+
(TEST_DATA_DIR/"kook_ws_event_pong.json"),
118+
(TEST_DATA_DIR/"kook_ws_event_privite_message.json"),
119+
(TEST_DATA_DIR/"kook_ws_event_privite_system_message.json"),
120+
],
121+
)
122+
def test_websocket_event_type_parse(expected_json_data_path:Path):
123+
expected_json_data_str =expected_json_data_path.read_text(encoding="utf-8")
124+
event = KookWebsocketEvent.from_json(
125+
expected_json_data_str,
126+
)
127+
event_dict = event.to_dict(mode="json",exclude_unset=True,exclude_none=False)
128+
assert event_dict == json.loads(expected_json_data_str)
129+
130+

0 commit comments

Comments
 (0)