| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import copy
- import shutil
- import sys
- import tempfile
- import unittest
- from pathlib import Path
- from openpyxl import load_workbook
- ROOT = Path(__file__).resolve().parents[1]
- sys.path.insert(0, str(ROOT / "scripts"))
- import audit
- import mcp_audit
- import rule_check
- import test_mcp_audit as mcp_examples
- from test_rule_sync import FakeKit, record
- WORKBOOK = Path(audit.pick_workbook())
- RULE = """应付清关编码判定
- 命中“应付编码合并清单”的,优先按清单给出应付编码;未命中清单时,参考历史相似商品的应付编码。
- 未命中清单,且本次没有可用历史应付参考时(包括本次未取得历史数据),如果应付编码为空,AI 使用对应的应收清关编码作为兜底建议。已有应付编码时,不得仅因缺少参考就建议覆盖;依据不足时保留待核实。
- 应收编码经原有规则核实可用时,应付字段不再仅因缺少历史参考而判待核实。应收编码存疑时,可以给出兜底建议,但应付字段仍须待核实;应收编码为空或明确错误时,不得直接复制。
- 存在多行清关信息时,按材质、占比能唯一对应的行分别提出建议;无法确定对应关系时转待核实,不得直接取第一行编码套用,也不得擅自增删清关行。
- 兜底仅产生 AI 建议,不自动修改页面原值。员工采纳并完成正式审核后才生效;该字段可用不代表整单审核通过。
- 非美国、目的国未知及手表品类,仍按原有前置规则转人工,不执行上述兜底。
- """
- def current_document():
- text = (ROOT / "references" / "doc-snapshot.txt").read_text(encoding="utf-8")
- if "应付清关编码判定" not in text:
- text = text.replace("七、单价审核规则(固定指令)", RULE + "\n七、单价审核规则(固定指令)")
- return text
- def issues(document, path):
- result = rule_check.cmd_verify(document, str(path), quiet=True)
- return {item for _, item, status, _ in result["results"] if status == "❌"}
- class PayableFallbackSyncTests(unittest.TestCase):
- def changed_workbook(self, tmp, change):
- path = Path(tmp) / "changed.xlsx"
- shutil.copyfile(WORKBOOK, path)
- wb = load_workbook(path)
- try:
- change(wb)
- wb.save(path)
- finally:
- wb.close()
- return path
- def test_old_manual_only_rule_cannot_be_reported_as_synced(self):
- def stale(wb):
- for row in wb["02-审核判定表"].iter_rows(min_row=2):
- if str(row[0].value) == "40":
- row[2].value = "未命中清单 → 按历史 SKU 应付编码填写"
- row[3].value = "查历史/待核实"
- row[4].value = "无历史则人工"
- with tempfile.TemporaryDirectory() as tmp:
- bad = issues(current_document(), self.changed_workbook(tmp, stale))
- self.assertIn("应付空值兜底建议", bad)
- self.assertIn("应付无历史一律转人工", bad)
- def test_document_eligibility_changes_require_resynchronization(self):
- variants = [
- ("如果应付编码为空", "不论应付编码是否为空", "应付空值兜底建议"),
- ("应收编码为空或明确错误时,不得直接复制", "应收编码为空或明确错误时,也可以复制", "应收空错不复制"),
- ("不得擅自增删清关行", "允许增删清关行", "应付兜底保留清关行"),
- ]
- for old, new, label in variants:
- with self.subTest(label=label):
- self.assertIn(label, issues(current_document().replace(old, new), WORKBOOK))
- def test_existing_value_protection_cannot_disappear_from_workbook(self):
- def weaken(wb):
- for row in wb["02-审核判定表"].iter_rows(min_row=2):
- if str(row[0].value) == "40":
- row[2].value = str(row[2].value).replace("已有应付值不因缺参考覆盖", "覆盖已有应付值")
- with tempfile.TemporaryDirectory() as tmp:
- bad = issues(current_document(), self.changed_workbook(tmp, weaken))
- self.assertIn("已有应付编码不覆盖", bad)
- def test_mapping_footer_cannot_keep_obsolete_manual_only_instruction(self):
- def stale(wb):
- wb["05-应付编码映射表"]["F7"] = "无历史则人工待核实"
- with tempfile.TemporaryDirectory() as tmp:
- bad = issues(current_document(), self.changed_workbook(tmp, stale))
- self.assertIn("应付无历史一律转人工", bad)
- def test_excel_engine_does_not_automatically_apply_ai_fallback(self):
- result = audit.Auditor(FakeKit(), skip_images=True).run(audit.Doc(record()))
- self.assertEqual(result["outputs"]["应付清关编码"], "")
- item = next(item for item in result["items"] if item["seq"] == 40)
- self.assertFalse(item["auto"])
- def test_mcp_contract_preserves_blank_original_when_agent_proposes_code(self):
- source = mcp_examples.snapshot()
- source["page_values"]["yf_clearance_data"][0]["clearance_hscode"] = ""
- before = copy.deepcopy(source)
- # This is an agent-reviewed fixture, not automatic semantic review by validate().
- for status in ("pass", "hold"):
- payload = mcp_examples.ValidationTests.reviewed(source)
- code = source["page_values"]["ys_clearance_data"][1]["clearance_hscode"]
- leaf = payload["fields"]["yf_clearance_data"][0]["clearance_hscode"]
- leaf.update(result=status, suggested_value=code, reason="无应付参考,按对应应收编码给出兜底建议", reason_brief="应收编码兜底建议")
- mcp_audit.validate(source, payload)
- self.assertEqual(leaf["value"], "")
- self.assertEqual(leaf["suggested_value"], code)
- self.assertEqual(source, before)
- leaf["value"] = code
- with self.assertRaises(mcp_audit.ContractError):
- mcp_audit.validate(source, payload)
- if __name__ == "__main__":
- unittest.main()
|