fix:表情包识别失败问题
This commit is contained in:
74
pytests/image_sys_test/test_image_data_model.py
Normal file
74
pytests/image_sys_test/test_image_data_model.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import io
|
||||
|
||||
from PIL import Image as PILImage
|
||||
import pytest
|
||||
|
||||
from src.common.data_models.image_data_model import MaiEmoji, MaiImage
|
||||
|
||||
|
||||
def _build_test_image_bytes(image_format: str) -> bytes:
|
||||
image = PILImage.new("RGB", (8, 8), color="white")
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, format=image_format)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_calculate_hash_format_updates_runtime_path_metadata(tmp_path: Path) -> None:
|
||||
image_bytes = _build_test_image_bytes("JPEG")
|
||||
tmp_file_path = tmp_path / "emoji.tmp"
|
||||
tmp_file_path.write_bytes(image_bytes)
|
||||
|
||||
emoji = MaiEmoji(full_path=tmp_file_path, image_bytes=image_bytes)
|
||||
|
||||
assert await emoji.calculate_hash_format() is True
|
||||
assert emoji.image_format == "jpeg"
|
||||
assert emoji.full_path.suffix == ".jpeg"
|
||||
assert emoji.file_name == emoji.full_path.name
|
||||
assert emoji.dir_path == tmp_path.resolve()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("model_cls", "extra_fields"),
|
||||
[
|
||||
(
|
||||
MaiEmoji,
|
||||
{
|
||||
"description": "",
|
||||
"last_used_time": None,
|
||||
"query_count": 0,
|
||||
"register_time": None,
|
||||
},
|
||||
),
|
||||
(
|
||||
MaiImage,
|
||||
{
|
||||
"description": "",
|
||||
"vlm_processed": False,
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_from_db_instance_restores_image_format_from_path(
|
||||
tmp_path: Path,
|
||||
model_cls: type[MaiEmoji] | type[MaiImage],
|
||||
extra_fields: dict[str, object],
|
||||
) -> None:
|
||||
image_path = tmp_path / "cached.png"
|
||||
image_path.write_bytes(_build_test_image_bytes("PNG"))
|
||||
|
||||
record = SimpleNamespace(
|
||||
no_file_flag=False,
|
||||
image_hash="hash",
|
||||
full_path=str(image_path),
|
||||
**extra_fields,
|
||||
)
|
||||
|
||||
image = model_cls.from_db_instance(record)
|
||||
|
||||
assert image.full_path == image_path.resolve()
|
||||
assert image.file_name == image_path.name
|
||||
assert image.image_format == "png"
|
||||
@@ -125,7 +125,7 @@ def setup_mocks(monkeypatch):
|
||||
db_model_mod = _stub_module("src.common.database.database_model")
|
||||
db_model_mod.Messages = None # 可以根据需要添加更多的属性或方法
|
||||
|
||||
emoji_manager_mod = _stub_module("src.chat.emoji_system.emoji_manager")
|
||||
emoji_manager_mod = _stub_module("src.emoji_system.emoji_manager")
|
||||
emoji_manager_mod.emoji_manager = None # 可以根据需要添加更多的属性或方法
|
||||
|
||||
image_manager_mod = _stub_module("src.chat.image_system.image_manager")
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Any, Callable
|
||||
|
||||
import pytest
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
|
||||
from src.chat.replyer import maisaka_generator as legacy_replyer_module
|
||||
from src.chat.replyer import maisaka_generator_multi as multimodal_replyer_module
|
||||
@@ -445,3 +446,33 @@ def test_runtime_build_tool_detail_panels_uses_emotion_prompt_access_panel(monke
|
||||
assert captured["content"] == "emotion prompt link"
|
||||
assert captured["kwargs"]["chat_id"] == "session-emotion"
|
||||
assert captured["kwargs"]["request_kind"] == "emotion"
|
||||
|
||||
|
||||
def test_runtime_render_context_usage_panel_merges_timing_and_planner(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
runtime = object.__new__(MaisakaHeartFlowChatting)
|
||||
runtime.session_id = "session-merged"
|
||||
runtime.session_name = "测试聊天流"
|
||||
runtime._max_context_size = 20
|
||||
|
||||
printed: list[Any] = []
|
||||
monkeypatch.setattr("src.maisaka.runtime.console.print", lambda renderable: printed.append(renderable))
|
||||
|
||||
runtime._render_context_usage_panel(
|
||||
cycle_id=12,
|
||||
timing_selected_history_count=3,
|
||||
timing_prompt_tokens=15,
|
||||
timing_action="continue",
|
||||
timing_response="继续执行",
|
||||
planner_selected_history_count=5,
|
||||
planner_prompt_tokens=42,
|
||||
planner_response="先查询再回复",
|
||||
)
|
||||
|
||||
assert len(printed) == 1
|
||||
outer_panel = printed[0]
|
||||
assert isinstance(outer_panel, Panel)
|
||||
renderables = list(outer_panel.renderable.renderables)
|
||||
assert isinstance(renderables[0], Text)
|
||||
assert "聊天流名称:测试聊天流" in renderables[0].plain
|
||||
assert "聊天流ID:session-merged" in renderables[0].plain
|
||||
assert len(renderables) == 3
|
||||
|
||||
@@ -64,7 +64,7 @@ def test_builtin_hook_catalog_includes_new_business_hooks(monkeypatch: pytest.Mo
|
||||
async def test_send_emoji_for_maisaka_can_be_aborted_by_hook(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""表情包系统应允许在选择前被 Hook 中止。"""
|
||||
|
||||
from src.chat.emoji_system import maisaka_tool
|
||||
from src.emoji_system import maisaka_tool
|
||||
|
||||
fake_manager = _FakeHookManager(
|
||||
{
|
||||
|
||||
@@ -149,7 +149,7 @@ def setup_mocks(monkeypatch):
|
||||
db_model_mod = _stub_module("src.common.database.database_model")
|
||||
db_model_mod.Messages = None # 可以根据需要添加更多的属性或方法
|
||||
|
||||
emoji_manager_mod = _stub_module("src.chat.emoji_system.emoji_manager")
|
||||
emoji_manager_mod = _stub_module("src.emoji_system.emoji_manager")
|
||||
emoji_manager_mod.emoji_manager = None # 可以根据需要添加更多的属性或方法
|
||||
|
||||
image_manager_mod = _stub_module("src.chat.image_system.image_manager")
|
||||
|
||||
Reference in New Issue
Block a user