2026-03-31 23:55:26 +08:00
|
|
|
"""聊天输入归一:与 memoir 规则共用,配置独立。"""
|
|
|
|
|
|
2026-04-03 10:12:59 +08:00
|
|
|
from unittest.mock import MagicMock, patch
|
2026-03-31 23:55:26 +08:00
|
|
|
|
2026-05-22 13:44:50 +08:00
|
|
|
from app.features.conversation.constants import chat
|
2026-03-31 23:55:26 +08:00
|
|
|
from app.features.conversation.input_normalize import (
|
|
|
|
|
apply_conversation_input_rules,
|
|
|
|
|
normalize_chat_input_for_agent,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_apply_conversation_rules_matches_memoir_mei_kanshang() -> None:
|
|
|
|
|
raw = "我去试镜了 美看上我 张伟"
|
|
|
|
|
assert "没看上我" in apply_conversation_input_rules(raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_chat_rules_mode() -> None:
|
|
|
|
|
raw = "美看上我"
|
2026-05-22 13:44:50 +08:00
|
|
|
with patch("app.features.conversation.input_normalize.chat") as m:
|
|
|
|
|
m.input_normalize_enabled = True
|
|
|
|
|
m.input_normalize_mode = "rules"
|
|
|
|
|
m.input_normalize_llm_max_tokens = 512
|
|
|
|
|
m.input_normalize_llm_max_input_chars = 8000
|
2026-03-31 23:55:26 +08:00
|
|
|
assert normalize_chat_input_for_agent(raw, llm=None) == "没看上我"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_chat_disabled_returns_raw() -> None:
|
|
|
|
|
raw = "美看上我"
|
2026-05-22 13:44:50 +08:00
|
|
|
with patch("app.features.conversation.input_normalize.chat") as m:
|
|
|
|
|
m.input_normalize_enabled = False
|
|
|
|
|
m.input_normalize_mode = "rules"
|
2026-03-31 23:55:26 +08:00
|
|
|
assert normalize_chat_input_for_agent(raw, llm=None) == raw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_chat_off_mode() -> None:
|
|
|
|
|
raw = "美看上我"
|
2026-05-22 13:44:50 +08:00
|
|
|
with patch("app.features.conversation.input_normalize.chat") as m:
|
|
|
|
|
m.input_normalize_enabled = True
|
|
|
|
|
m.input_normalize_mode = "off"
|
2026-03-31 23:55:26 +08:00
|
|
|
assert normalize_chat_input_for_agent(raw, llm=None) == raw
|
2026-04-03 10:12:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_llm_mode_voice_only_passes_no_llm_for_typing() -> None:
|
|
|
|
|
raw = "美看上我"
|
|
|
|
|
fake = MagicMock()
|
2026-05-22 13:44:50 +08:00
|
|
|
with patch("app.features.conversation.input_normalize.chat") as m:
|
|
|
|
|
m.input_normalize_enabled = True
|
|
|
|
|
m.input_normalize_mode = "llm"
|
|
|
|
|
m.input_normalize_llm_voice_only = True
|
|
|
|
|
m.input_normalize_llm_max_tokens = 512
|
|
|
|
|
m.input_normalize_llm_max_input_chars = 8000
|
2026-04-03 10:12:59 +08:00
|
|
|
with patch(
|
|
|
|
|
"app.features.conversation.input_normalize._llm_normalize_chat_input"
|
|
|
|
|
) as llm_norm:
|
|
|
|
|
llm_norm.return_value = None
|
|
|
|
|
out = normalize_chat_input_for_agent(raw, llm=fake, is_from_voice=False)
|
|
|
|
|
llm_norm.assert_called_once()
|
|
|
|
|
assert llm_norm.call_args[0][1] is None
|
|
|
|
|
assert out == "没看上我"
|