test_payable_fallback_sync.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import copy
  2. import shutil
  3. import sys
  4. import tempfile
  5. import unittest
  6. from pathlib import Path
  7. from openpyxl import load_workbook
  8. ROOT = Path(__file__).resolve().parents[1]
  9. sys.path.insert(0, str(ROOT / "scripts"))
  10. import audit
  11. import mcp_audit
  12. import rule_check
  13. import test_mcp_audit as mcp_examples
  14. from test_rule_sync import FakeKit, record
  15. WORKBOOK = Path(audit.pick_workbook())
  16. RULE = """应付清关编码判定
  17. 命中“应付编码合并清单”的,优先按清单给出应付编码;未命中清单时,参考历史相似商品的应付编码。
  18. 未命中清单,且本次没有可用历史应付参考时(包括本次未取得历史数据),如果应付编码为空,AI 使用对应的应收清关编码作为兜底建议。已有应付编码时,不得仅因缺少参考就建议覆盖;依据不足时保留待核实。
  19. 应收编码经原有规则核实可用时,应付字段不再仅因缺少历史参考而判待核实。应收编码存疑时,可以给出兜底建议,但应付字段仍须待核实;应收编码为空或明确错误时,不得直接复制。
  20. 存在多行清关信息时,按材质、占比能唯一对应的行分别提出建议;无法确定对应关系时转待核实,不得直接取第一行编码套用,也不得擅自增删清关行。
  21. 兜底仅产生 AI 建议,不自动修改页面原值。员工采纳并完成正式审核后才生效;该字段可用不代表整单审核通过。
  22. 非美国、目的国未知及手表品类,仍按原有前置规则转人工,不执行上述兜底。
  23. """
  24. def current_document():
  25. text = (ROOT / "references" / "doc-snapshot.txt").read_text(encoding="utf-8")
  26. if "应付清关编码判定" not in text:
  27. text = text.replace("七、单价审核规则(固定指令)", RULE + "\n七、单价审核规则(固定指令)")
  28. return text
  29. def issues(document, path):
  30. result = rule_check.cmd_verify(document, str(path), quiet=True)
  31. return {item for _, item, status, _ in result["results"] if status == "❌"}
  32. class PayableFallbackSyncTests(unittest.TestCase):
  33. def changed_workbook(self, tmp, change):
  34. path = Path(tmp) / "changed.xlsx"
  35. shutil.copyfile(WORKBOOK, path)
  36. wb = load_workbook(path)
  37. try:
  38. change(wb)
  39. wb.save(path)
  40. finally:
  41. wb.close()
  42. return path
  43. def test_old_manual_only_rule_cannot_be_reported_as_synced(self):
  44. def stale(wb):
  45. for row in wb["02-审核判定表"].iter_rows(min_row=2):
  46. if str(row[0].value) == "40":
  47. row[2].value = "未命中清单 → 按历史 SKU 应付编码填写"
  48. row[3].value = "查历史/待核实"
  49. row[4].value = "无历史则人工"
  50. with tempfile.TemporaryDirectory() as tmp:
  51. bad = issues(current_document(), self.changed_workbook(tmp, stale))
  52. self.assertIn("应付空值兜底建议", bad)
  53. self.assertIn("应付无历史一律转人工", bad)
  54. def test_document_eligibility_changes_require_resynchronization(self):
  55. variants = [
  56. ("如果应付编码为空", "不论应付编码是否为空", "应付空值兜底建议"),
  57. ("应收编码为空或明确错误时,不得直接复制", "应收编码为空或明确错误时,也可以复制", "应收空错不复制"),
  58. ("不得擅自增删清关行", "允许增删清关行", "应付兜底保留清关行"),
  59. ]
  60. for old, new, label in variants:
  61. with self.subTest(label=label):
  62. self.assertIn(label, issues(current_document().replace(old, new), WORKBOOK))
  63. def test_existing_value_protection_cannot_disappear_from_workbook(self):
  64. def weaken(wb):
  65. for row in wb["02-审核判定表"].iter_rows(min_row=2):
  66. if str(row[0].value) == "40":
  67. row[2].value = str(row[2].value).replace("已有应付值不因缺参考覆盖", "覆盖已有应付值")
  68. with tempfile.TemporaryDirectory() as tmp:
  69. bad = issues(current_document(), self.changed_workbook(tmp, weaken))
  70. self.assertIn("已有应付编码不覆盖", bad)
  71. def test_mapping_footer_cannot_keep_obsolete_manual_only_instruction(self):
  72. def stale(wb):
  73. wb["05-应付编码映射表"]["F7"] = "无历史则人工待核实"
  74. with tempfile.TemporaryDirectory() as tmp:
  75. bad = issues(current_document(), self.changed_workbook(tmp, stale))
  76. self.assertIn("应付无历史一律转人工", bad)
  77. def test_excel_engine_does_not_automatically_apply_ai_fallback(self):
  78. result = audit.Auditor(FakeKit(), skip_images=True).run(audit.Doc(record()))
  79. self.assertEqual(result["outputs"]["应付清关编码"], "")
  80. item = next(item for item in result["items"] if item["seq"] == 40)
  81. self.assertFalse(item["auto"])
  82. def test_mcp_contract_preserves_blank_original_when_agent_proposes_code(self):
  83. source = mcp_examples.snapshot()
  84. source["page_values"]["yf_clearance_data"][0]["clearance_hscode"] = ""
  85. before = copy.deepcopy(source)
  86. # This is an agent-reviewed fixture, not automatic semantic review by validate().
  87. for status in ("pass", "hold"):
  88. payload = mcp_examples.ValidationTests.reviewed(source)
  89. code = source["page_values"]["ys_clearance_data"][1]["clearance_hscode"]
  90. leaf = payload["fields"]["yf_clearance_data"][0]["clearance_hscode"]
  91. leaf.update(result=status, suggested_value=code, reason="无应付参考,按对应应收编码给出兜底建议", reason_brief="应收编码兜底建议")
  92. mcp_audit.validate(source, payload)
  93. self.assertEqual(leaf["value"], "")
  94. self.assertEqual(leaf["suggested_value"], code)
  95. self.assertEqual(source, before)
  96. leaf["value"] = code
  97. with self.assertRaises(mcp_audit.ContractError):
  98. mcp_audit.validate(source, payload)
  99. if __name__ == "__main__":
  100. unittest.main()