test_modeling_v13.py 5.74 KB
"""AI 建模流水线 v13:拆环节(规则 / 指标 / 动作 / 流程卡 / 定时与监控 / 对账)、文档叫法优先、
标题留在正文里、可选值解析、决策表布尔列、不许把说明拼进状态、流程卡引用草案不算错、定时任务采纳即建好。

复用 test_platform 的夹具:SQLite 元数据库 + 假业务库(store / order_item)+ 打桩 LLM。
"""
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))

from test_platform import env  # noqa: E402,F401

HANDBOOK = """# 工单手册

## 一、术语
| 术语 | 口径 |
|---|---|
| 超时 | 处理时长超过 48 小时未关闭 |

## 二、审批规则
| 场景 | 审批人 |
|---|---|
| 金额 ≥ 1 万 | 维修经理 |

## 三、每日工单巡检 SOP(维修主管,每天 9:00)
1. 查出所有超时工单。
2. 逐一升级。
"""


# ---------------- 不调模型的部分 ----------------
def test_切块_标题留在正文里_小文档整篇一块():
    from app.ingest.chunk import chunk_text
    from app.kernel import modeling
    text = HANDBOOK + ("填充。" * 900)
    cs = chunk_text(text, 1, chunk_tokens=300, overlap_tokens=0, inline_headings=True)
    assert any("## 二、审批规则" in c["text"] for c in cs)
    assert not any("## 二、审批规则" in c["text"] for c in chunk_text(text, 1, chunk_tokens=300))   # 检索用的切块照旧
    one = modeling._split_doc(HANDBOOK, "u1", "工单手册", 0)
    assert len(one) == 1 and "## 三、每日工单巡检 SOP" in one[0]["text"]


def test_出处定位到原句和章节():
    from app.kernel import propose
    heading, snip = propose.locate({"text": HANDBOOK, "heading": "工单手册"}, "金额 ≥ 1 万")
    assert heading == "二、审批规则" and "维修经理" in snip
    assert propose.locate({"text": "abc", "heading": "h"}, "没有这句")[0] == "h"


def test_列注释里的可选值():
    from app.kernel import propose
    assert propose._column_enum("状态:待审批/已下单/已到货/已取消") == ("状态", ["待审批", "已下单", "已到货", "已取消"])
    assert propose._column_enum("合作状态:正常、延期、暂停")[1] == ["正常", "延期", "暂停"]
    assert propose._column_enum("在途数量(已下单未到货)") == ("在途数量(已下单未到货)", [])


def test_决策表_是否列按布尔比对():
    from app.kernel import decision
    spec = decision.normalize({
        "inputs": [{"name": "amount", "type": "number"}, {"name": "delayed", "type": "number"}],
        "outputs": [{"name": "priority"}],
        "rows": [{"when": {"delayed": "true"}, "then": {"priority": "P0"}},
                 {"when": {"amount": ">= 20000", "delayed": "false"}, "then": {"priority": "P1"}}],
        "default": {"priority": "P3"}})
    assert spec["inputs"][1]["type"] == "boolean" and spec["rows"][0]["when"]["delayed"] is True
    assert decision.evaluate(spec, {"amount": 30000, "delayed": True})["result"]["priority"] == "P0"
    assert decision.evaluate(spec, {"amount": 30000, "delayed": False})["result"]["priority"] == "P1"


def test_有可选值的属性不许拼说明():
    from app.kernel import modeling
    objs = {"补货单": {"name": "补货单", "binding": None, "item": None, "id": "",
                       "properties": [{"name": "status", "enum_values": ["待审批", "已下单", "已到货"]}]}}
    bad = {"target_type": "补货单", "effects": [{"tool": "object.write", "args": {
        "object_type": "补货单", "key": "$po.key", "property": "status", "op": "set", "value": "='已延期:' + $reason"}}]}
    assert "只能取" in modeling._check_enum_writes(bad, objs)
    ok = {"target_type": "补货单", "effects": [{"tool": "object.create", "args": {
        "object_type": "补货单", "values": {"status": "待审批"}}}]}
    assert modeling._check_enum_writes(ok, objs) == ""
    wrong = {"target_type": "补货单", "effects": [{"tool": "object.create", "args": {
        "object_type": "补货单", "values": {"status": "已延期"}}}]}
    assert modeling._check_enum_writes(wrong, objs)


def test_流程卡引用草案里的元素_算待发布不算错(env):
    from app.sop import spec as sop_spec
    spec = sop_spec.normalize({"start": "q", "steps": [
        {"id": "q", "type": "query", "title": "查工单", "object_type": "工单", "mode": "list", "next": "a"},
        {"id": "a", "type": "action", "title": "升级", "action": "升级工单", "next": "end"},
        {"id": "end", "type": "end", "title": "结束"}]})
    plain = sop_spec.validate(spec)
    assert any("工单" in e for e in plain["errors"]) and any("升级工单" in e for e in plain["errors"])
    v = sop_spec.validate(spec, draft_names={"object_type": {"工单"}, "action": {"升级工单"}})
    assert not v["errors"], v["errors"]
    assert {d["name"] for d in v["draft_refs"]} == {"工单", "升级工单"}


# 整条流水线的打桩测试换到了 test_modeling_v14(环节、对账、按对账修改都变了)


def test_老记录_规则变成可算的拆成三步(env):
    from app.kernel import modeling
    rows = modeling._stage_rows([{"key": "route", "status": "done"}, {"key": "convert", "status": "done", "log": ["老日志"]},
                                 {"key": "deepen", "status": "done", "log": [{"t": "", "msg": "写成了"}], "counts": {"deepened": 2}},
                                 {"key": "sop", "status": "done"}, {"key": "draft", "status": "done"}])
    st = {r["key"]: r for r in rows}
    assert [r["key"] for r in rows] == modeling.ORDER
    assert st["rules"]["status"] == st["metrics"]["status"] == st["actions"]["status"] == "done"
    assert st["rules"]["counts"]["deepened"] == 2 and st["schedule"]["status"] == "skipped"
    assert st["convert"]["log"][0]["msg"] == "老日志"