bc903a273e
- scripts/extract_config.py: AST 解析两老项目源码取常量(不 import 源模块),
生成 seed_profiles/<slug>/{wall.json, brand_values.json, prompts/};幂等可重跑
- app/engine/wall_schema.py: compile_wall(json)→CompiledWall,坏结构/坏 regex
编译期显性失败(WallConfigError 含字段路径)
- boundary_filter/conversation: 墙词表/话术数据源切换为 DEFAULT_WALL
(jiancai 出厂 seed),函数逻辑不动、常量名保留为 wall 视图(既有测试零修改);
wall 数据化新增 pivots/pivots_contact/zero_number/reask_patterns 字段
- 脆锚点迁移: test_inline_prohibitions 断言对象从 conversation.py 源码改为
wall.json 数据文件,语义等价(唯一允许的测试修改)
- 回归: 322 既有全绿 + 10 新增 wall_schema 测试 = 332;提取脚本两遍无 diff
Co-Authored-By: Claude Code <noreply@anthropic.com>
124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
"""wall.json → CompiledWall 编译器契约(Task 4 词表数据化)。
|
|
|
|
seed_profiles/<slug>/wall.json 是租户墙词表/话术的唯一数据源;
|
|
compile_wall 把 JSON 编译为运行时对象,坏配置必须显性失败(信息含字段名)。
|
|
"""
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from app.engine.wall_schema import CompiledWall, WallConfigError, compile_wall
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def _load(slug: str) -> dict:
|
|
return json.loads(
|
|
(REPO_ROOT / "seed_profiles" / slug / "wall.json").read_text(encoding="utf-8"))
|
|
|
|
|
|
def test_compile_produces_regex_objects():
|
|
wall = compile_wall(_load("jiancai"))
|
|
assert isinstance(wall, CompiledWall)
|
|
assert wall.price_patterns
|
|
assert all(isinstance(p, re.Pattern) for p in wall.price_patterns)
|
|
assert wall.deflections["es"]
|
|
assert all(isinstance(p, re.Pattern) for p in wall.reask_patterns)
|
|
assert all(isinstance(p, re.Pattern) for p in wall.triggers.values())
|
|
assert isinstance(wall.zero_number["long_number"], re.Pattern)
|
|
|
|
|
|
def test_compile_rejects_bad_regex():
|
|
cfg = _load("jiancai")
|
|
cfg["price_patterns"] = ["([unclosed"]
|
|
with pytest.raises(WallConfigError) as ei:
|
|
compile_wall(cfg)
|
|
assert "price_patterns" in str(ei.value)
|
|
assert "regex" in str(ei.value)
|
|
|
|
|
|
def test_compile_rejects_bad_regex_in_nested_field():
|
|
cfg = _load("jiancai")
|
|
cfg["triggers"]["boundary"] = {"pattern": "(?P<n>budget", "flags": "i"}
|
|
with pytest.raises(WallConfigError) as ei:
|
|
compile_wall(cfg)
|
|
assert "triggers.boundary" in str(ei.value)
|
|
|
|
|
|
def test_compile_rejects_unknown_flag():
|
|
cfg = _load("jiancai")
|
|
cfg["reask_patterns"] = [{"pattern": "ok", "flags": "x"}]
|
|
with pytest.raises(WallConfigError) as ei:
|
|
compile_wall(cfg)
|
|
assert "reask_patterns" in str(ei.value)
|
|
|
|
|
|
def test_missing_and_unknown_top_level_fields():
|
|
cfg = _load("jiancai")
|
|
del cfg["no_contact"]
|
|
with pytest.raises(WallConfigError) as ei:
|
|
compile_wall(cfg)
|
|
assert "no_contact" in str(ei.value)
|
|
|
|
cfg = _load("jiancai")
|
|
cfg["deflectionz"] = [] # 拼写错误必须在编译期显性失败
|
|
with pytest.raises(WallConfigError) as ei:
|
|
compile_wall(cfg)
|
|
assert "deflectionz" in str(ei.value)
|
|
|
|
|
|
def test_meta_key_is_ignored():
|
|
cfg = _load("jiancai")
|
|
cfg["_meta"] = {"doc": "任何 _ 前缀键为数据来源标注,不参与编译"}
|
|
assert compile_wall(cfg).no_contact["web"]
|
|
|
|
|
|
def test_flags_entry_restores_ignorecase():
|
|
wall = compile_wall({"price_patterns": [{"pattern": "BUDGET|预算", "flags": "i"}],
|
|
**{k: v for k, v in _load("jiancai").items()
|
|
if k != "price_patterns"}})
|
|
assert any(p.search("my BUDGET plan") for p in wall.price_patterns)
|
|
|
|
|
|
def test_both_seed_walls_compile():
|
|
for slug in ("jiancai", "auto"):
|
|
wall = compile_wall(_load(slug))
|
|
assert wall.triggers["boundary"].search("how much is it?")
|
|
assert wall.no_contact["web"]
|
|
assert wall.reminders["boundary"]["normal"].startswith("⚠️ 职能边界提醒")
|
|
# 租户差异钉死: jiancai 多语言墙/auto 中英+$ 且无 es 池
|
|
if slug == "jiancai":
|
|
assert "es" in wall.deflections
|
|
assert len(wall.price_patterns) > 10
|
|
else:
|
|
assert "es" not in wall.deflections
|
|
assert "_meta" in _load(slug) # 引擎特性字段沿用 jiancai(显性标注)
|
|
|
|
|
|
def test_default_wall_matches_jiancai_seed():
|
|
"""boundary_filter 的出厂默认墙 = jiancai seed 逐字段等价(等价回归的锚)。"""
|
|
from app.engine.services import boundary_filter as bf
|
|
wall = compile_wall(_load("jiancai"))
|
|
assert [p.pattern for p in bf.DEFAULT_WALL.price_patterns] == \
|
|
[p.pattern for p in wall.price_patterns]
|
|
assert [p.flags for p in bf.DEFAULT_WALL.price_patterns] == \
|
|
[p.flags for p in wall.price_patterns]
|
|
assert bf.DEFLECTIONS_ZH == list(wall.deflections["zh"])
|
|
assert bf.NO_CONTACT_WEB == wall.no_contact["web"]
|
|
assert bf.LONG_NUMBER_RE.pattern == wall.zero_number["long_number"].pattern
|
|
assert [p.pattern for p in bf.REASK_PATTERNS] == \
|
|
[p.pattern for p in wall.reask_patterns]
|
|
|
|
|
|
def test_conversation_triggers_match_seed():
|
|
"""conversation 三组触发正则 = wall.triggers 编译产物(pattern+flags 双等价)。"""
|
|
from app.engine.services import conversation as conv
|
|
wall = compile_wall(_load("jiancai"))
|
|
for name, module_re in (("boundary", conv.BOUNDARY_TRIGGER_RE),
|
|
("budget", conv.BUDGET_TRIGGER_RE),
|
|
("contact_ask", conv.CONTACT_ASK_TRIGGER_RE)):
|
|
assert module_re.pattern == wall.triggers[name].pattern, name
|
|
assert module_re.flags == wall.triggers[name].flags, name
|