#!/usr/bin/env python3 """守轨迹图上那份抄送:配置与实况分开给,两条都不许说谎。 PYTHONPATH=. python3 scripts/check_flowmap_cc.py 来由是用户的原话——「流程中没有展示出抄送信息,每一个环节配置的抄送信息也需要在流程 步骤体现出来」。图上原先只有「这一环谁签了字」,谁被抄送了、要不要他处理、他看没看, 一格都不落;而催办和事后翻旧账要当场答的恰恰是这三问。落点是 ``flowmap`` 给每个环节 带的 ``cc`` 那一格,这里逐条钉住它的口径: 1. **配置对每个环节都露**,包括没走到的那一支:看图的人问的是「这一环到底会不会抄给 出纳」,那一问不能只有走通了才答得出; 2. **实况按人并成一行**:同一个人被同一环节抄到两条是常事(退回重走时老那条已阅留在 档、新一趟又建一条),不并就会数出「抄送 2 人」而实际是同一个人; 3. **只认 ``read_type='cc'``**:分享与批注 @ 我 不是环节配出来的,挂到「这一环的抄送」 上会说假话; 4. **未阅 / 未处理两本账算对**:需处理的那条点开看了不算办完; 5. **归档件两张表都要**:办结搬档时没打开过的那几条留在 ``oa_read``,只问已阅会漏掉 「抄了没人看」——正是催办最想知道那一档; 6. **试跑预测同口径**:没走到的环节也露配置,但「会抄给谁」只在真走到时才预测, 未阅/未处理这些运行时才有的账一律不编。 全程在内存里算,不落库、不动别人的流程。 """ from __future__ import annotations import pathlib import sys import types from datetime import datetime sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent)) from app import models # noqa: E402 from app.engine import definition as definition_module # noqa: E402 from app.service import flowmap, simulator # noqa: E402 FAILS: list[str] = [] PASSED = 0 PROCESS = "cc-traj-check" def check(label: str, condition, detail: str = "") -> None: global PASSED if condition: PASSED += 1 print(f"[通过] {label}") else: FAILS.append(label) print(f"[FAIL] {label}" + (f" -> {detail}" if detail else "")) def rule(**over) -> dict: """一条环节上的抄送规则,默认「到达时、无需处理、无条件抄给发起人」。""" base = { "id": "cc1", "name": "抄给出纳", "when": "arrive", "requiresAction": False, "taskValues": [{"type": "role", "data": ["出纳"]}], } base.update(over) return base def definition() -> definition_module.ProcessDefinition: """发起人 → 部门主管(配两条抄送)→ 归档;部门主管那条是「办结后 + 有条件 + 需处理」。""" process = models.Process(id=PROCESS, name="轨迹图抄送校验流程") rows = [ models.Activity(id="begin", process=PROCESS, type="begin", name="发起人", alias="发起人", config={}, x=0.0, y=0.0), models.Activity(id="dept", process=PROCESS, type="manual", name="部门主管", alias="部门主管", config={"ccRules": [ rule(), rule(id="cc2", name="大额抄总经理", when="complete", requiresAction=True, expression="float(data.get('money') or 0) >= 50000", taskValues=[{"type": "identity", "data": ["zongjingli"]}], taskScript=""), ]}, x=240.0, y=0.0), models.Activity(id="end", process=PROCESS, type="end", name="归档", alias="归档", config={}, x=480.0, y=0.0), ] routes = [ models.Route(id="r1", process=PROCESS, name="提交", alias="提交", from_activity="begin", to_activity="dept", decision="", order_number=0), models.Route(id="r2", process=PROCESS, name="同意", alias="同意", from_activity="dept", to_activity="end", decision="agree", order_number=0), ] return definition_module.build(process, rows, routes) def attachment(attachment_id: str, name: str, *, is_image: bool = True) -> dict: """一条引用快照:形状见 ``engine.attachment_ref``,库里的 JSON 列存的就是这个。""" return { "id": attachment_id, "name": name, "extension": name.rsplit(".", 1)[-1], "length": 20480, "is_image": is_image, "preview": {"mode": "inline", "renderer": "image" if is_image else "pdf"}, } def read(identity: str, name: str, *, activity: str = "dept", kind: str = "cc", start: str, view: str | None = None, handle: str | None = None, requires_action: bool = False, sender: str = "张三", opinion: str = "", unit: str = "财务部", forwarded: bool = False, attachments: list[dict] | None = None) -> types.SimpleNamespace: """一条待阅/已阅的最小够用形状:``attach_cc`` 只读这几格。""" return types.SimpleNamespace( identity=identity, identity_name=name, unit=unit, activity=activity, read_type=kind, requires_action=requires_action, sender=sender, opinion=opinion, forwarded=forwarded, start_time=datetime.fromisoformat(start) if start else None, view_time=datetime.fromisoformat(view) if view else None, handle_time=datetime.fromisoformat(handle) if handle else None, attachments=list(attachments or []), ) def build(reads, *, activity: str = "dept", status: str = "running"): row = types.SimpleNamespace(id="w1", status=status, activity=activity, start_time=datetime.fromisoformat("2026-11-04 09:00:00")) return flowmap.build(definition(), row, tasks=[], logs=[], completed=(), reads=reads) # ------------------------------------------------------------------ 1. 配置 def check_rules_always_exposed() -> None: trail = build([]) dept = trail["nodes"]["dept"]["cc"] check("环节配的抄送规则原样露到图上,一条都不少", len(dept["rules"]) == 2, str(dept["rules"])) arrive, complete = dept["rules"] check("时机缺省按引擎那条默认补成 arrive,设了 complete 的照实", arrive["when"] == "arrive" and complete["when"] == "complete", f"{arrive['when']} / {complete['when']}") check("要不要处理跟着规则走", arrive["requires_action"] is False and complete["requires_action"] is True) check("只报「有没有条件」,不把表达式摊在卡片上", arrive["conditional"] is False and complete["conditional"] is True) check("抄送对象给来源原文(type + data),中文说法由前端与设计器共用一份", arrive["targets"] == [{"type": "role", "data": ["出纳"]}] and complete["targets"] == [{"type": "identity", "data": ["zongjingli"]}], str(arrive["targets"])) check("脚本对象单独标出来,不混进 targets", arrive["script"] is False and complete["script"] is False) check("没配抄送的环节也给一格空的,前端不必到处判空", trail["nodes"]["begin"]["cc"] == {"rules": [], "people": [], "sent": 0, "unread": 0, "unhandled": 0}, str(trail["nodes"]["begin"]["cc"])) # 没走到的那一支也得看得到配置:问的是「会不会抄」,不是「抄了没」 pending = build([]) check("还没走到的环节也露出配置", len(pending["nodes"]["dept"]["cc"]["rules"]) == 2) # ------------------------------------------------------------------ 2. 实况 def check_people_dedupe_and_counts() -> None: people = [ read("chenni", "陈妮", start="2026-11-04 09:12:00", view="2026-11-04 09:20:00"), read("liqiang", "李强", start="2026-11-04 09:12:00", requires_action=True, opinion="请留意发票合规"), # 退回重走:同一个人第二次被抄,且这一次没打开 read("chenni", "陈妮", start="2026-11-05 10:00:00"), # 别的环节的抄送不许挂到这一环上 read("wangmin", "王敏", activity="begin", start="2026-11-04 09:00:00", view="2026-11-04 09:05:00"), ] cc = build(people)["nodes"]["dept"]["cc"] check("同一个人被同一环节抄到两条并成一行", [item["name"] for item in cc["people"]] == ["陈妮", "李强"], str(cc["people"])) check("并行之后的送达时间取最早那次", cc["people"][0]["time"] == "2026-11-04 09:12:00", cc["people"][0]["time"]) first, second = cc["people"] check("看过/办过取「有一次就算」", first["viewed"] is True and first["handled"] is False, str(first)) check("需处理跟着人走(两条里有一条要办就算要办)", second["requires_action"] is True and second["handled"] is False) check("来意那句留最早那次的", second["opinion"] == "请留意发票合规", second["opinion"]) check("未阅按并完之后的那一本账算,不多数", cc["sent"] == 2 and cc["unread"] == 1, str(cc)) check("需处理没办完的另算一本(点开看了不算办完)", cc["unhandled"] == 1, str(cc)) check("别的环节的抄送不挂到这一环", all(item["name"] != "王敏" for item in cc["people"])) # 跨环节不并:并成一个环节名下会把另一个环节的送达抹掉,那一环的账就数少了 across = build([read("chenni", "陈妮", activity="dept", start="2026-11-04 09:12:00"), read("chenni", "陈妮", activity="end", start="2026-11-04 09:40:00")]) check("同一个人在两个环节各有一条时各归各的环节(去重只在环节内做)", [p["name"] for p in across["nodes"]["dept"]["cc"]["people"]] == ["陈妮"] and [p["name"] for p in across["nodes"]["end"]["cc"]["people"]] == ["陈妮"], str(across["nodes"]["end"]["cc"])) check("去重只在环节内做,不是按人全局去重(否则另一环的账会被抹掉)", across["nodes"]["dept"]["cc"]["sent"] == 1 and across["nodes"]["end"]["cc"]["sent"] == 1, str(across["nodes"]["dept"]["cc"]["sent"])) both = build([read("zhaoliu", "赵六", start="2026-11-04 09:12:00", view="2026-11-04 09:30:00", handle="2026-11-04 09:31:00", requires_action=True)])["nodes"]["dept"]["cc"] check("需处理那条登记过处理意见之后才算办完", both["people"][0]["handled"] is True and both["unhandled"] == 0, str(both)) # ------------------------------------------------------------------ 3. 只认 cc def check_only_cc_kind() -> None: mixed = [ read("chenni", "陈妮", kind="cc", start="2026-11-04 09:12:00"), read("liqiang", "李强", kind="share", start="2026-11-04 09:13:00"), read("wangmin", "王敏", kind="comment", start="2026-11-04 09:14:00"), read("zhaoliu", "赵六", kind="", start="2026-11-04 09:15:00"), ] cc = build(mixed)["nodes"]["dept"]["cc"] check("只有 read_type=cc 进这一格:分享与批注 @ 我是别人手工点的,与环节配置无关", [item["name"] for item in cc["people"]] == ["陈妮"], str(cc["people"])) check("空的 read_type 也不算抄送", cc["sent"] == 1, str(cc)) # ------------------------------------------------------------------ 4. 归档件 def check_archived_reads_both_tables() -> None: """归档件上「抄了没人看」的那几条留在 oa_read,两张表并一份传才数得全。""" live = [read("liqiang", "李强", start="2026-11-04 09:12:00")] # 没打开过,归档时留在待阅 archived = [read("chenni", "陈妮", start="2026-11-04 09:12:00", view="2026-11-04 09:20:00")] cc = build(live + archived, activity="", status="terminated")["nodes"]["dept"]["cc"] check("归档件也两张表并起来:只问已阅会漏掉「抄了没人看」", cc["sent"] == 2 and cc["unread"] == 1, str(cc)) # ------------------------------------------------------------------ 5. 不传就是没有 def check_default_is_empty() -> None: row = types.SimpleNamespace(id="w1", status="running", activity="dept", start_time=datetime.fromisoformat("2026-11-04 09:00:00")) trail = flowmap.build(definition(), row, tasks=[], logs=[], completed=()) check("不传 reads 时整张图照常画得出,抄送那一格是空的而不是缺的", trail["nodes"]["dept"]["cc"]["rules"] and not trail["nodes"]["dept"]["cc"]["people"]) # ------------------------------------------------------------------ 6. 试跑预测 def check_simulator_predict() -> None: node = definition().activity("dept") briefs = flowmap.cc_rule_briefs(node) check("试跑那份配置与真单据同一份口径(同一个函数,不另写一遍)", len(briefs) == 2 and briefs[1]["requires_action"] is True, str(briefs)) probe = types.SimpleNamespace( _cc=lambda act, context: [{"identity": "zongjingli", "name": "总经理", "unit": "总经办", "requires_action": True}], _context=lambda act: {}, ) merged = simulator._Simulator._predicted_cc( probe, node, [types.SimpleNamespace(watchers=[ {"identity": "chenni", "name": "陈妮", "unit": "财务部", "requires_action": False}, {"identity": "chenni", "name": "陈妮", "unit": "财务部", "requires_action": True}, ])], ) check("走访记下来的抄送对象按人并一份,要不要处理取并集", [item["identity"] for item in merged] == ["chenni"] and merged[0]["requires_action"] is True, str(merged)) refilled = simulator._Simulator._predicted_cc(probe, node, [types.SimpleNamespace(watchers=[])]) check("走过却没记下抄送对象的环节(脚本/外部调用那一类)当场补算一份", [item["identity"] for item in refilled] == ["zongjingli"], str(refilled)) check("没走到的环节不预测人:摊一堆人会让看图的人以为已经抄出去了", simulator._Simulator._predicted_cc(probe, node, []) == [], str(simulator._Simulator._predicted_cc(probe, node, []))) quiet = types.SimpleNamespace(_cc=lambda act, context: [], _context=lambda act: {}) check("没配抄送的环节补算也不出人", simulator._Simulator._predicted_cc(quiet, definition().activity("begin"), [types.SimpleNamespace(watchers=[])]) == []) # ------------------------------------------------------------------ 7. 转发 def check_forwards() -> None: """办结之后的转发:它不属某一环,另开一本账,但也不能从抄送那一行里凭空消失。""" rows = [ read("chenni", "陈妮", activity="dept", start="2026-11-04 09:12:00", view="2026-11-04 09:20:00"), # 归档件上的转发:没有环节可挂(oa_work_completed 不落 activity) read("liqiang", "李强", activity="", kind="cc", sender="张锦杰", start="2026-11-06 10:00:00", forwarded=True, opinion="结果请看一下"), read("wangmin", "王敏", activity="", kind="cc", sender="张锦杰", start="2026-11-06 10:00:00", forwarded=True, requires_action=True), # 分享与批注 @ 我 不是转发,也不算抄送 read("zhaoliu", "赵六", activity="", kind="share", start="2026-11-06 11:00:00", forwarded=True), ] trail = build(rows, activity="", status="terminated") block = trail["forwards"] check("转发逐条露出来(谁转的、转给谁、附了什么话都在)", [item["name"] for item in block["items"]] == ["李强", "王敏"], str(block["items"])) check("转发数的是被转发过的人,分享那条不算进来", block["count"] == 2 and block["unread"] == 2, str(block)) check("转发里需处理没办的另算一本(点开看了不算办完)", block["unhandled"] == 1, str(block)) check("转发的送达人就是那条 cc 待阅上的 sender", block["items"][0]["sender"] == "张锦杰" and block["items"][0]["opinion"] == "结果请看一下", str(block["items"][0])) check("一条转发都没有时给的是空格子,不是缺字段", build([read("chenni", "陈妮", start="2026-11-04 09:12:00")], activity="")["forwards"] == {"items": [], "count": 0, "unread": 0, "unhandled": 0}) # 归档件上没有环节可落:转发不许混进任何一环的抄送名单(上面那条陈妮是这一环真抄过的) names = {name for node in trail["nodes"].values() for name in (item["name"] for item in node["cc"]["people"])} check("转发不进任何一环的抄送名单(否则图上念出「这一环抄给过一个办结后才被转来看的人」)", names == {"陈妮"}, names) check("环节那三笔账里也不数转发的人", all(node["cc"]["sent"] <= 1 for node in trail["nodes"].values()), {key: node["cc"]["sent"] for key, node in trail["nodes"].items()}) # 一个人既被抄过又被转发过(引擎一张单一个人只留一条 cc 待阅):两本账都记他 both = build([read("chenni", "陈妮", activity="dept", start="2026-11-04 09:12:00", view="2026-11-04 09:20:00", forwarded=True)], activity="dept") dept = both["nodes"]["dept"]["cc"] check("人被抄过又被转发过时,抄送那一行带着 forwarded 记号(不假装没转过)", dept["sent"] == 1 and dept["people"][0]["forwarded"] is True, str(dept["people"])) check("同一行两种来源时两本账各记各的,谁也不许多数", both["forwards"]["count"] == 1 and dept["sent"] == 1, str(both["forwards"])) # 流程定义没了(老单)也要回一个能渲染的图 + 空格子 empty = flowmap._empty(definition()) check("图纸没了也给转发那一格,前端不必判空", empty["forwards"] == {"items": [], "count": 0, "unread": 0, "unhandled": 0}, str(empty.get("forwards"))) # ------------------------------------------------------------------ 8. 逐人处理结果与办结抄送 def check_person_results_and_finish_cc() -> None: """每个人的处理结果与「办结时的抄送」都得有落点。 两个坑都是审批记录那条时间线上撞到的:只给已阅/已处理两个布尔值,界面说不出 「几点看的、几点回的」;而办结抄送记在结束环节身上,结束环节压根不派生待办。 """ rows = [ read("chenni", "陈妮", activity="end", start="2026-11-13 19:32:02", view="2026-11-13 20:01:00", handle="2026-11-13 20:05:00", requires_action=True, sender="系统管理员", opinion="已登记台账并安排付款"), read("zhangsan", "张三", activity="end", start="2026-11-13 19:32:02", sender="系统管理员", opinion="请登记台账并安排付款"), ] trail = build(rows, activity="", status="terminated") cc = trail["nodes"]["end"]["cc"] check("办结抄送落在结束环节那一格上(引擎用结束环节调 apply_activity_cc)", cc["sent"] == 2, str(cc["people"])) handled, unread = cc["people"] check("处理过的露出他点开与写意见那两个时刻,界面才说得出「已处理于几点」", handled["view_time"] == "2026-11-13 20:01:00" and handled["handle_time"] == "2026-11-13 20:05:00" and handled["handled"] is True, str(handled)) check("没打开过的那一条两个时刻都是空的,界面上不许编一个时间出来", unread["view_time"] is None and unread["handle_time"] is None and unread["viewed"] is False, str(unread)) check("来意那句话跟着那一条走(本人没回过意见时那是抄送人写的)", unread["opinion"] == "请登记台账并安排付款", unread["opinion"]) check("要他办与不要他办分开算两本账", cc["unhandled"] == 0 and cc["unread"] == 1, str(cc)) # 同一个人两条:时刻也取最早那一次,不能因为后一条没看过就把已有的时刻顶成空 merged = build([ read("chenni", "陈妮", activity="dept", start="2026-11-04 09:12:00", view="2026-11-04 09:20:00"), read("chenni", "陈妮", activity="dept", start="2026-11-05 10:00:00"), ])["nodes"]["dept"]["cc"] check("并成一行后已阅时刻留着(后一次没看过不许把它顶成空)", merged["people"][0]["view_time"] == "2026-11-04 09:20:00", str(merged["people"])) forward_rows = [ read("liqiang", "李强", activity="", sender="张锦杰", start="2026-11-06 10:00:00", view="2026-11-06 10:30:00", forwarded=True), read("wangmin", "王敏", activity="", sender="张锦杰", start="2026-11-06 10:00:00", handle="2026-11-06 11:00:00", requires_action=True, forwarded=True), ] block = build(forward_rows, activity="", status="terminated")["forwards"] check("转发那一本账同样带着每个人的两个时刻(逐人说处理结果读的是它)", block["items"][0]["view_time"] == "2026-11-06 10:30:00" and block["items"][1]["handle_time"] == "2026-11-06 11:00:00", str(block["items"])) # ------------------------------------------------------------------ 9. 处理时附的图与文件 def check_cc_handler_attachments() -> None: """收受人处理抄送时上传的图片与文件,得跟着他那句话一起到审批记录。 引擎把那几个附件认在他那条待阅/已阅的 ``attachments`` JSON 列上 (``engine.mark_read`` -> ``claim_attachments``,返回引用快照),库里一直留着, 只是 ``cc_of_read`` 从前没往下带——于是记录里只看得见「已处理于 10:05 · 已登记台账」那句话,他附上的发票截图一个字都不提。 """ rows = [ read("chenni", "陈妮", activity="end", start="2026-11-13 19:32:02", view="2026-11-13 20:01:00", handle="2026-11-13 20:05:00", requires_action=True, opinion="已登记台账并安排付款", attachments=[attachment("a1", "发票.jpg"), attachment("a2", "台账.pdf", is_image=False)]), read("zhangsan", "张三", activity="end", start="2026-11-13 19:32:02", sender="系统管理员", opinion="请登记台账并安排付款"), ] people = build(rows, activity="", status="terminated")["nodes"]["end"]["cc"]["people"] handled, unread = people check("处理时上传的图片与文件跟着那一条一起露出来(含预览元数据)", [item["id"] for item in handled["attachments"]] == ["a1", "a2"] and handled["attachments"][0]["name"] == "发票.jpg" and handled["attachments"][0]["is_image"] is True and handled["attachments"][1]["is_image"] is False, str(handled["attachments"])) check("没附东西时给空表而不是缺字段,前端不必到处判空", unread["attachments"] == [], str(unread)) legacy = build([ read("wangmin", "王敏", activity="end", start="2026-11-13 19:32:02", handle="2026-11-13 20:20:00", requires_action=True, opinion="已安排", # 加 preview 那一格之前落库的快照只有这四列,前端按扩展名兜底判断 attachments=[{"id": "c1", "name": "老单据.jpg", "extension": "jpg", "length": 1024}]), ], activity="", status="terminated")["nodes"]["end"]["cc"] check("引用快照原样带出,不在这层回附件表补字段(老快照缺 preview 也照给)", legacy["people"][0]["attachments"] == [ {"id": "c1", "name": "老单据.jpg", "extension": "jpg", "length": 1024} ], str(legacy["people"][0]["attachments"])) # 退回重走留下两条,两条各附过东西:并成一行时两趟的都要在,且不重复 merged = build([ read("chenni", "陈妮", activity="dept", start="2026-11-04 09:12:00", view="2026-11-04 09:20:00", handle="2026-11-04 09:25:00", requires_action=True, attachments=[attachment("a1", "第一趟的截图.jpg"), attachment("a3", "说明.docx", is_image=False)]), read("chenni", "陈妮", activity="dept", start="2026-11-05 10:00:00", handle="2026-11-05 10:20:00", requires_action=True, attachments=[attachment("a1", "第一趟的截图.jpg"), attachment("a2", "第二趟的发票.jpg")]), ])["nodes"]["dept"]["cc"] check("同一个人两趟抄送并成一行时,两趟上传的都在(按附件 id 去重,先来的排前面)", [item["id"] for item in merged["people"][0]["attachments"]] == ["a1", "a3", "a2"], str(merged["people"][0]["attachments"])) # 转发用的也是同一份 cc 待阅,附件同样得跟着走 block = build([ read("liqiang", "李强", activity="", sender="张锦杰", start="2026-11-06 10:00:00", view="2026-11-06 10:30:00", handle="2026-11-06 10:40:00", requires_action=True, opinion="已阅,同意", forwarded=True, attachments=[attachment("b1", "批复扫描件.pdf", is_image=False)]), read("liqiang", "李强", activity="", sender="张锦杰", start="2026-11-07 09:00:00", forwarded=True, attachments=[attachment("b2", "补充材料.jpg")]), ], activity="", status="terminated")["forwards"] check("转发那本账同样带着随附的图片与文件,两条并一行时也不漏", [item["id"] for item in block["items"][0]["attachments"]] == ["b1", "b2"], str(block["items"][0]["attachments"])) # 试跑预测里还没有"处理"这件事,那一档压根不该有附件 trail = build([], activity="dept") check("没抄送过人时那一格是空表(试跑预测那份由前端按可选字段处理)", trail["nodes"]["dept"]["cc"]["people"] == [], str(trail["nodes"]["dept"]["cc"])) def main() -> int: check_rules_always_exposed() check_people_dedupe_and_counts() check_only_cc_kind() check_archived_reads_both_tables() check_default_is_empty() check_simulator_predict() check_forwards() check_person_results_and_finish_cc() check_cc_handler_attachments() print() if FAILS: print(f"[未通过] {len(FAILS)} 项:\n - " + "\n - ".join(FAILS)) return 1 print(f"[全部通过] {PASSED} 项") return 0 if __name__ == "__main__": raise SystemExit(main())