refactor: config_views 单入口 + 0语义统一 + FakeAI 收敛(清理单#1#2#3)
- #3 六个消费视图 resolver 收拢 app/platform/config_views(单一定义点): resolve_email/push/ai_settings ← 各服务文件, resolve_contact_flags/ context_seed_map ← conversation, name_to_region_extras ← manager __init__ 内联取值(or None 语义原样)——函数体逐字搬入,服务层 import 之, 旧定义删;Phase 3 编辑器/试金石与该模块对接 - #2 限流 0 语义统一: conn_limiter.try_acquire 的 limit 与 chat_rest 的 rate_limit_per_hour 取值同款——0 或负 = 回退出厂默认(与姊妹键一致), 原 conn_limiter 0=全拒改为回退;chat_rest 取值抽 _resolve_rate_limit 纯函数供锚定 - #1 FakeAI 收敛进 conftest(超集: extract/evaluate/eval_calls),四份拷贝 删(test_lead_email_trigger/test_cc_integration/test_channel_hygiene/ test_lead_push_wiring——计划记三份,grep 实有四份;push_wiring 经 evaluate 参数注入原本地固定返回值,断言零改动) 全量 558 passed(554 + 新增 4: 单入口/内联等价/两处 0 语义) Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -6,26 +6,12 @@ from sqlalchemy import select
|
||||
from app.engine.models import ConversationSource, LeadPushLog
|
||||
from app.engine.services import email_service, lead_push_service
|
||||
from app.engine.services.conversation import ConversationManager
|
||||
from tests.conftest import build_jiancai_runtime
|
||||
from tests.conftest import FakeAI, build_jiancai_runtime
|
||||
|
||||
|
||||
class FakeAI:
|
||||
"""同 test_lead_email_trigger 的确定性桩。"""
|
||||
|
||||
def __init__(self, extract=None):
|
||||
self._extract_seq = extract if isinstance(extract, list) else [extract or {}]
|
||||
|
||||
async def stream_chat(self, messages, context=""):
|
||||
for token in ("ok ", "reply"):
|
||||
yield token
|
||||
|
||||
async def extract_info(self, messages, existing_info):
|
||||
idx = min(max(len(messages) // 2 - 1, 0), len(self._extract_seq) - 1)
|
||||
return self._extract_seq[idx]
|
||||
|
||||
async def evaluate(self, profile, recent_messages):
|
||||
return {"intent_level": "interested", "reasoning": "留号",
|
||||
"contact_info": {}, "info_sufficient": True}
|
||||
# 本文件场景依赖"interested"触发推送——conftest FakeAI(超集)经 evaluate
|
||||
# 参数注入,与原本地拷贝的固定返回值逐字等价(setdefault 补 contact_info={})
|
||||
_EVAL = {"intent_level": "interested", "reasoning": "留号",
|
||||
"info_sufficient": True}
|
||||
|
||||
|
||||
def _setup(async_db, monkeypatch, ai, lead_succeeds=True):
|
||||
@@ -81,7 +67,7 @@ def _logs(async_db):
|
||||
|
||||
|
||||
def test_lead_email_triggers_pending_push(async_db, monkeypatch):
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}})
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}}, evaluate=_EVAL)
|
||||
(manager, conv), sent, fired = _setup(async_db, monkeypatch, ai)
|
||||
_send(manager, conv.session_id, "my whatsapp is +254722111117")
|
||||
assert [s[0] for s in sent] == ["lead"]
|
||||
@@ -94,7 +80,7 @@ def test_lead_email_triggers_pending_push(async_db, monkeypatch):
|
||||
|
||||
def test_smtp_failure_still_pushes(async_db, monkeypatch):
|
||||
"""设计定案: 推送与 SMTP 成败解耦——邮件失败询盘照推。"""
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}})
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}}, evaluate=_EVAL)
|
||||
(manager, conv), sent, fired = _setup(
|
||||
async_db, monkeypatch, ai, lead_succeeds=False)
|
||||
_send(manager, conv.session_id, "my whatsapp is +254722111117")
|
||||
@@ -104,7 +90,7 @@ def test_smtp_failure_still_pushes(async_db, monkeypatch):
|
||||
|
||||
|
||||
def test_wechat_only_push_marked_skipped(async_db, monkeypatch):
|
||||
ai = FakeAI(extract={"联系方式": {"微信": "li_ming_88"}})
|
||||
ai = FakeAI(extract={"联系方式": {"微信": "li_ming_88"}}, evaluate=_EVAL)
|
||||
(manager, conv), sent, fired = _setup(async_db, monkeypatch, ai)
|
||||
_send(manager, conv.session_id, "add my wechat li_ming_88")
|
||||
assert [s[0] for s in sent] == ["lead"] # 我方邮件照发(wechat 算 confirmed)
|
||||
@@ -117,7 +103,8 @@ def test_wechat_only_push_marked_skipped(async_db, monkeypatch):
|
||||
def test_correction_push_event_update_after_success(async_db, monkeypatch):
|
||||
"""改号 → 更正推送;前次已 success(手动置位模拟 _run_push)→ event=update。"""
|
||||
ai = FakeAI(extract=[{"联系方式": {"手机": "+254722111117"}},
|
||||
{"联系方式": {"手机": "+254799999999"}}])
|
||||
{"联系方式": {"手机": "+254799999999"}}],
|
||||
evaluate=_EVAL)
|
||||
(manager, conv), sent, fired = _setup(async_db, monkeypatch, ai)
|
||||
_send(manager, conv.session_id, "my whatsapp is +254722111117")
|
||||
assert len(fired) == 1 and fired[0].push_type == "lead"
|
||||
@@ -139,13 +126,13 @@ def test_correction_push_event_update_after_success(async_db, monkeypatch):
|
||||
|
||||
def test_returning_push_follows_nudge(async_db, monkeypatch):
|
||||
"""24h 内同值另已发过 → 回访轻提醒 → 推送 push_type=returning,event=new。"""
|
||||
ai1 = FakeAI(extract={"联系方式": {"email": "same@x.com"}})
|
||||
ai1 = FakeAI(extract={"联系方式": {"email": "same@x.com"}}, evaluate=_EVAL)
|
||||
(m1, c1), _s1, f1 = _setup(async_db, monkeypatch, ai1)
|
||||
_send(m1, c1.session_id, "mail same@x.com")
|
||||
assert len(f1) == 1 and f1[0].push_type == "lead"
|
||||
asyncio.run(m1.db.close())
|
||||
|
||||
ai2 = FakeAI(extract={"联系方式": {"email": "same@x.com"}})
|
||||
ai2 = FakeAI(extract={"联系方式": {"email": "same@x.com"}}, evaluate=_EVAL)
|
||||
(m2, c2), sent2, f2 = _setup(async_db, monkeypatch, ai2)
|
||||
_send(m2, c2.session_id, "im back, same@x.com")
|
||||
assert [s[0] for s in sent2] == ["returning"]
|
||||
@@ -157,7 +144,7 @@ def test_returning_push_follows_nudge(async_db, monkeypatch):
|
||||
def test_push_disabled_writes_nothing(async_db, monkeypatch):
|
||||
"""PUSH_ENABLED=false(默认): 零日志零派发,既有行为完全不变。"""
|
||||
monkeypatch.setattr(lead_push_service.settings, "PUSH_ENABLED", False)
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}})
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}}, evaluate=_EVAL)
|
||||
|
||||
sent = []
|
||||
|
||||
@@ -205,7 +192,7 @@ def test_fire_end_to_end_single_loop(async_db, monkeypatch):
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(email_service, "send_lead_email", fake_lead)
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}})
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}}, evaluate=_EVAL)
|
||||
|
||||
async def run():
|
||||
async with async_db() as db:
|
||||
@@ -232,7 +219,7 @@ def test_fire_happens_only_after_commit(async_db, monkeypatch):
|
||||
确定性落在提交前 → NoResultFound 被记录 → 变红;正确序 gate 不触发。
|
||||
"""
|
||||
from sqlalchemy import select as sa_select
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}})
|
||||
ai = FakeAI(extract={"联系方式": {"手机": "+254722111117"}}, evaluate=_EVAL)
|
||||
sent = []
|
||||
read_result = {}
|
||||
commit_gate: asyncio.Event | None = None
|
||||
|
||||
Reference in New Issue
Block a user