feat: 出站补全渲染——首封e164+标注/主题行/推送派生/canon双保险/面板尾注

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-08 19:51:46 +08:00
parent 9d16235454
commit 7df2e4ae1e
8 changed files with 524 additions and 37 deletions
+101 -2
View File
@@ -68,14 +68,14 @@ def test_map_intent():
assert map_intent(IntentLevel.pending, True) == "medium" # DB 读回枚举成员形态
def _seed_push_ctx(async_db, client_info=None):
def _seed_push_ctx(async_db, client_info=None, sid="s-push"):
"""建 conv/profile/ev 三件套,返回 (conv, profile, ev)。"""
from app.models.database import ConversationSource
async def run():
async with async_db() as db:
conv = Conversation(
session_id="s-push", source=ConversationSource.widget,
session_id=sid, source=ConversationSource.widget,
client_info=client_info or {"ip": "8.8.8.8", "lang": "en"},
)
db.add(conv)
@@ -533,3 +533,102 @@ def test_sweep_stale_pending(async_db, monkeypatch):
statuses = sorted(l.status for l in logs)
assert statuses == ["failed", "pending", "success"]
asyncio.run(run())
# ---- Task 6(spec §5 首封即补全号): 推送 contact 纯 e164 + profile 派生标注 ----
_PUSH_CC_WA = {"e164": "+966501234567", "intl": "+966 50 123 4567",
"confirmed": False, "attempts": 1, "source": "ip_proposed",
"canon": "0501234567"}
_PUSH_CC_PHONE = {"e164": "+96613800138000", "intl": "+966 138 001 3800",
"confirmed": False, "attempts": 0, "source": "naive",
"canon": "13800138000"}
def _dispatch_with_cc(async_db, monkeypatch, raw_info, confirmed, sid="s-cc"):
"""三件套 + 指定 raw_info(_cc) → dispatch,返回 pending log。"""
from app.services import lead_push_service as svc
monkeypatch.setattr(svc.settings, "PUSH_ENABLED", True)
_seed_push_ctx(async_db, sid=sid)
async def run():
async with async_db() as db:
from app.models.database import Conversation, CustomerProfile, Evaluation
conv = (await db.execute(
select(Conversation).where(Conversation.session_id == sid))
).scalar_one()
profile = (await db.execute(
select(CustomerProfile).where(
CustomerProfile.conversation_id == conv.id))).scalar_one()
ev = (await db.execute(
select(Evaluation).where(
Evaluation.conversation_id == conv.id))).scalar_one()
profile.raw_info = raw_info
await db.commit()
log = await svc.dispatch_lead_push(
db, ev, conv, profile, confirmed, "lead",
{"intent_level": "interested"}, True)
await db.commit()
return log
return asyncio.run(run())
def test_push_contact_cc_e164_and_derived_note(async_db, monkeypatch):
"""contact.phone/whatsapp = 纯 e164(confirmed 与否都给——首封即补全号,
契约要求"带国际区号且有效");标注进 profile"号码补全说明"派生键
(出站时由 _cc 派生,不落正常键);_cc 本体不泄漏(先消费后过滤)。"""
raw_info = {"目标国家": "肯尼亚",
"_cc": {"whatsapp": dict(_PUSH_CC_WA), "phone": dict(_PUSH_CC_PHONE)}}
log = _dispatch_with_cc(
async_db, monkeypatch, raw_info,
{"whatsapp": "0501234567", "phone": "13800138000"})
assert log is not None and log.status == "pending"
p = log.request_payload
assert p["contact"]["whatsapp"] == "+966501234567" # 补全号直发
assert p["contact"]["phone"] == "+96613800138000"
note = p["profile"]["号码补全说明"]
assert "0501234567→+966501234567(区号按 IP 推测,未经客户确认)" in note
assert "13800138000→+96613800138000(区号按 IP 推测,未经客户确认)" in note
assert "_cc" not in p["profile"] # 本体不泄漏
assert not [k for k in p["profile"] if str(k).startswith("_")]
assert p["profile"]["目标国家"] == "肯尼亚" # 正常键不受影响
def test_push_confirmed_state_e164_without_note(async_db, monkeypatch):
"""confirmed 态(prefix_normalized): contact 给 e164,但无需披露——
"号码补全说明"键不出现。"""
state = {"e164": "+966501234567", "intl": "+966 50 123 4567",
"confirmed": True, "attempts": 0, "source": "prefix_normalized",
"canon": "966501234567"}
log = _dispatch_with_cc(
async_db, monkeypatch, {"_cc": {"whatsapp": state}},
{"whatsapp": "966501234567"})
p = log.request_payload
assert p["contact"]["whatsapp"] == "+966501234567"
assert "号码补全说明" not in p["profile"]
def test_push_no_e164_state_raw_contact_and_missing_note(async_db, monkeypatch):
"""无 e164 态(no_ip/fallback-无-e164): contact 给裸号原文(拿不出补全号
不硬造),"号码补全说明"披露(缺国家区号)。"""
for source in ("no_ip", "fallback"):
state = {"confirmed": False, "attempts": 1, "source": source,
"canon": "0501234567"}
log = _dispatch_with_cc(
async_db, monkeypatch, {"_cc": {"whatsapp": dict(state)}},
{"whatsapp": "0501234567"}, sid=f"s-cc-{source}")
p = log.request_payload
assert p["contact"]["whatsapp"] == "0501234567" # 裸号原文
assert p["profile"]["号码补全说明"] == "0501234567(缺国家区号)"
def test_push_stale_cc_canon_ignored(async_db, monkeypatch):
"""canon 不匹配的残留态(换号后旧 _cc): contact 用原文、零派生——
旧补全永不跟随新号。"""
log = _dispatch_with_cc(
async_db, monkeypatch, {"_cc": {"whatsapp": dict(_PUSH_CC_WA)}},
{"whatsapp": "0512345678"})
p = log.request_payload
assert p["contact"]["whatsapp"] == "0512345678"
assert "号码补全说明" not in p["profile"]
assert "_cc" not in p["profile"]