|
@@ -0,0 +1,414 @@
|
|
|
|
|
+import copy
|
|
|
|
|
+import json
|
|
|
|
|
+import subprocess
|
|
|
|
|
+import sys
|
|
|
|
|
+import tempfile
|
|
|
|
|
+import unittest
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+
|
|
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
+sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
|
+import mcp_audit as mcp
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def snapshot():
|
|
|
|
|
+ row = {
|
|
|
|
|
+ "clearance_hscode": "0691.10.5200", "material": "陶瓷",
|
|
|
|
|
+ "material_ratio": "100.00", "kit_hscode": "",
|
|
|
|
|
+ "adjust_hscode_data": ["9903.88.15", "9903.01.25"],
|
|
|
|
|
+ "tax_rate": "0.00", "extra_tax": "25.00", "is_new_hscode": False,
|
|
|
|
|
+ "kit_tax_rate": None, "is_new_kit": 0,
|
|
|
|
|
+ "adjust_rate_data": [{"code": "9903.88.15", "rate": "25.00"}],
|
|
|
|
|
+ "tax_tag": ["附录II"], "box_value": "20",
|
|
|
|
|
+ }
|
|
|
|
|
+ second = dict(row, clearance_hscode="3924.10.4000", material="塑料", adjust_hscode_data=[])
|
|
|
|
|
+ return {
|
|
|
|
|
+ "goods_id": 123, "claim_ref": "a" * 64, "source_ref": "B" * 64,
|
|
|
|
|
+ "context": {"country_code": "US", "departure_country": "CN",
|
|
|
|
|
+ "selected_labels": {"goods_type": "非普货", "goods_category_id": "杯具"}},
|
|
|
|
|
+ "page_values": {
|
|
|
|
|
+ "sku": "DEMO-001", "cn_name": "陶瓷杯", "en_name": "Ceramic cup",
|
|
|
|
|
+ "material_cn": "陶瓷", "material_en": "Ceramic", "material_ratio_info": "",
|
|
|
|
|
+ "purpose_cn": "盛饮料", "purpose_en": "Holding drinks", "image_url": "",
|
|
|
|
|
+ "shipping_method": "1", "goods_type": 2, "goods_category_id": None,
|
|
|
|
|
+ "category_ids": [0, 4], "is_inspection_fee": 0, "is_fda_declaration": "0",
|
|
|
|
|
+ "is_epa_declaration": "0", "is_act_declaration": "0", "is_dot_declaration": "0",
|
|
|
|
|
+ "is_cpsc_declaration": "0", "product_surcharge": "0", "tax_surcharge": "2",
|
|
|
|
|
+ "yf_cn_goodsname": "陶瓷杯", "yf_en_goodsname": "Ceramic cup",
|
|
|
|
|
+ "ys_clearance_price": "2.00", "yf_clearance_price": "1.00", "currency": "USD",
|
|
|
|
|
+ "recommended_grade": None, "recommended_keyword": "", "box_value": "0.00",
|
|
|
|
|
+ "is_battery": False, "is_magnetic": False, "is_wood": False,
|
|
|
|
|
+ "is_toy": False, "is_sensitive": False, "is_food": False,
|
|
|
|
|
+ "ys_clearance_data": [row, second], "yf_clearance_data": [copy.deepcopy(second)],
|
|
|
|
|
+ "sync_orders": [{"id": 321, "can_sync": False}],
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class PreparationTests(unittest.TestCase):
|
|
|
|
|
+ def test_exact_full_fields_and_no_automatic_passes_or_control_fields(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ before = copy.deepcopy(source)
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ self.assertEqual(set(payload), {"goods_id", "claim_ref", "source_ref", "result", "summary",
|
|
|
|
|
+ "summary_brief", "fields"})
|
|
|
|
|
+ self.assertLessEqual(len(payload["summary_brief"]), mcp.SUMMARY_BRIEF_MAX)
|
|
|
|
|
+ self.assertEqual(payload["result"], "hold")
|
|
|
|
|
+ self.assertEqual(set(payload["fields"]), set(source["page_values"]))
|
|
|
|
|
+ for name, value in source["page_values"].items():
|
|
|
|
|
+ if name in ("ys_clearance_data", "yf_clearance_data"):
|
|
|
|
|
+ self.assertEqual(len(payload["fields"][name]), len(value))
|
|
|
|
|
+ leaves = [(payload["fields"][name][i][key], original)
|
|
|
|
|
+ for i, row in enumerate(value) for key, original in row.items()]
|
|
|
|
|
+ else:
|
|
|
|
|
+ leaves = [(payload["fields"][name], value)]
|
|
|
|
|
+ for leaf, original in leaves:
|
|
|
|
|
+ self.assertEqual(leaf["value"], original)
|
|
|
|
|
+ self.assertEqual(leaf["result"], "not_checked")
|
|
|
|
|
+ self.assertIsNone(leaf["suggested_value"])
|
|
|
|
|
+ self.assertTrue(leaf["reason"].strip())
|
|
|
|
|
+ self.assertTrue(leaf["reason_brief"].strip())
|
|
|
|
|
+ self.assertLessEqual(len(leaf["reason_brief"]), mcp.REASON_BRIEF_MAX)
|
|
|
|
|
+ payload["fields"]["category_ids"]["value"].append(99)
|
|
|
|
|
+ self.assertEqual(source, before)
|
|
|
|
|
+
|
|
|
|
|
+ def test_recursive_objects_ordinary_arrays_and_empty_containers(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ source["page_values"]["future_info"] = {"flag": False, "empty": {}, "zero": 0}
|
|
|
|
|
+ source["page_values"]["images"] = [{"url": "local-only", "width": 0}]
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ self.assertIs(payload["fields"]["future_info"]["flag"]["value"], False)
|
|
|
|
|
+ self.assertEqual(payload["fields"]["future_info"]["empty"]["value"], {})
|
|
|
|
|
+ self.assertEqual(payload["fields"]["images"]["value"], source["page_values"]["images"])
|
|
|
|
|
+ mcp.validate(source, ValidationTests.reviewed(source))
|
|
|
|
|
+
|
|
|
|
|
+ def test_hidden_fields_and_optional_hints_are_not_invented(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ source["page_values"]["goods_type"] = 1
|
|
|
|
|
+ for key in ("is_battery", "is_magnetic", "is_wood", "is_toy", "is_sensitive", "is_food", "sync_orders"):
|
|
|
|
|
+ del source["page_values"][key]
|
|
|
|
|
+ source["page_values"]["ys_clearance_data"] = []
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ self.assertEqual(set(payload["fields"]), set(source["page_values"]))
|
|
|
|
|
+ self.assertEqual(payload["fields"]["ys_clearance_data"], [])
|
|
|
|
|
+ self.assertNotIn("goods_attr", payload["fields"])
|
|
|
|
|
+
|
|
|
|
|
+ def test_non_us_and_unknown_country_stop_before_p2_and_keep_all_fields(self):
|
|
|
|
|
+ for country in ("CA", "CN", "", None):
|
|
|
|
|
+ with self.subTest(country=country):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ source["context"]["country_code"] = country
|
|
|
|
|
+ source["context"]["departure_country"] = "US"
|
|
|
|
|
+ source["page_values"]["cn_name"] = "手表"
|
|
|
|
|
+ if country == "CA":
|
|
|
|
|
+ for key in list(source["page_values"]):
|
|
|
|
|
+ if key.endswith("_declaration"):
|
|
|
|
|
+ del source["page_values"][key]
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ self.assertEqual(payload["result"], "manual")
|
|
|
|
|
+ self.assertIn("P1", payload["summary"])
|
|
|
|
|
+ self.assertIn("P1", payload["fields"]["cn_name"]["reason"])
|
|
|
|
|
+ self.assertEqual(set(payload["fields"]), set(source["page_values"]))
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ del source["context"]["country_code"]
|
|
|
|
|
+ self.assertEqual(mcp.prepare(source)["result"], "manual")
|
|
|
|
|
+
|
|
|
|
|
+ def test_p2_uses_names_or_actual_selected_category_label(self):
|
|
|
|
|
+ for field in ("cn_name", "en_name", "selected_category"):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ if field == "selected_category":
|
|
|
|
|
+ source["context"]["selected_labels"]["goods_category_id"] = ["SMART WATCH"]
|
|
|
|
|
+ else:
|
|
|
|
|
+ source["page_values"][field] = "WRISTWATCH"
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ self.assertEqual(payload["result"], "manual")
|
|
|
|
|
+ self.assertIn("P2", payload["summary"])
|
|
|
|
|
+ self.assertEqual(payload["fields"]["yf_clearance_data"][0]["clearance_hscode"]["result"], "not_checked")
|
|
|
|
|
+ self.assertIsNone(payload["fields"]["yf_clearance_data"][0]["clearance_hscode"]["suggested_value"])
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_explicit_agent_identified_p2_preserves_fields(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ payload = mcp.prepare(source, stop_rule="P2", stop_reason="已目视确认图片为手表,名称未写明")
|
|
|
|
|
+ self.assertEqual(payload["result"], "manual")
|
|
|
|
|
+ self.assertIn("已目视", payload["summary"])
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.prepare(source, stop_rule="P2", stop_reason="")
|
|
|
|
|
+
|
|
|
|
|
+ def test_agent_identified_p2_can_be_enforced_again_at_validation(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ reason = "已按当前可见品类核实为手表"
|
|
|
|
|
+ payload = mcp.prepare(source, stop_rule="P2", stop_reason=reason)
|
|
|
|
|
+ mcp.validate(source, payload, stop_rule="P2", stop_reason=reason)
|
|
|
|
|
+ payload["fields"]["cn_name"].update(result="pass")
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload, stop_rule="P2", stop_reason=reason)
|
|
|
|
|
+
|
|
|
|
|
+ def test_success_envelope_is_accepted_but_tool_error_is_not(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ self.assertEqual(mcp.prepare({"code": 0, "data": source}), mcp.prepare(source))
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.prepare({"code": 403, "data": source})
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class EvidenceTests(unittest.TestCase):
|
|
|
|
|
+ def test_not_supplied_is_distinct_from_known_empty_false_and_zero(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/certificates"), {"status": "not_supplied"})
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/image_url"), {"status": "empty", "value": ""})
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/goods_category_id"), {"status": "empty", "value": None})
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/is_battery"), {"status": "supplied", "value": False})
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/is_inspection_fee"), {"status": "supplied", "value": 0})
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/ys_clearance_data/1/adjust_hscode_data"), {"status": "empty", "value": []})
|
|
|
|
|
+
|
|
|
|
|
+ def test_certification_context_is_distinct_from_absent_and_empty_evidence(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/certification_files", scope="context"), {"status": "not_supplied"})
|
|
|
|
|
+ files = {key: [] for key in ("battery_file", "fda_file", "toy_file", "other_file")}
|
|
|
|
|
+ files["toy_file"] = [{"name": "CPC.pdf", "url": "https://files.example.test/cpc.pdf"}]
|
|
|
|
|
+ source["context"]["certification_files"] = files
|
|
|
|
|
+ before = copy.deepcopy(source)
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/certification_files/fda_file", scope="context"), {"status": "empty", "value": []})
|
|
|
|
|
+ evidence = mcp.evidence(source, "/certification_files/toy_file/0", scope="context")
|
|
|
|
|
+ self.assertEqual(evidence, {"status": "supplied", "value": files["toy_file"][0]})
|
|
|
|
|
+ evidence["value"]["name"] = "changed"
|
|
|
|
|
+ self.assertEqual(source, before)
|
|
|
|
|
+ self.assertNotIn("certification_files", mcp.prepare(source)["fields"])
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.evidence(source, "/", scope="credentials")
|
|
|
|
|
+
|
|
|
|
|
+ def test_json_pointer_escaping_and_strict_array_indices(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ source["page_values"]["a/b~c"] = "exact"
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/a~1b~0c")["value"], "exact")
|
|
|
|
|
+ self.assertEqual(mcp.evidence(source, "/ys_clearance_data/99/material")["status"], "not_supplied")
|
|
|
|
|
+ for pointer in ("image_url", "/bad~2escape", "/ys_clearance_data/-1", "/ys_clearance_data/01"):
|
|
|
|
|
+ with self.subTest(pointer=pointer), self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.evidence(source, pointer)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class ValidationTests(unittest.TestCase):
|
|
|
|
|
+ @staticmethod
|
|
|
|
|
+ def reviewed(source, result="hold"):
|
|
|
|
|
+ """A prepared payload whose agent replaced every draft placeholder with an actual conclusion."""
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ payload["result"] = result
|
|
|
|
|
+ payload["summary"] = "代理按适用规则完成审查;其余只读提示不参与判断"
|
|
|
|
|
+ payload["summary_brief"] = "1 项待核实"
|
|
|
|
|
+
|
|
|
|
|
+ def finish(node):
|
|
|
|
|
+ for value in node.values() if isinstance(node, dict) else node:
|
|
|
|
|
+ if isinstance(value, dict) and "reason_brief" in value:
|
|
|
|
|
+ value.update(reason="本项规则不适用,未检查", reason_brief="规则不适用")
|
|
|
|
|
+ elif isinstance(value, (dict, list)):
|
|
|
|
|
+ finish(value)
|
|
|
|
|
+ finish(payload["fields"])
|
|
|
|
|
+ payload["fields"]["cn_name"].update(result="hold", reason="品名需核实", reason_brief="品名需核实")
|
|
|
|
|
+ return payload
|
|
|
|
|
+
|
|
|
|
|
+ def test_all_four_overall_results_and_field_results_with_explicit_reasons(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ for result in ("hold", "manual", "reject", "pass"):
|
|
|
|
|
+ payload = self.reviewed(source, result)
|
|
|
|
|
+ payload["fields"]["cn_name"].update(result="pass", reason="已核对品名具体且与实际产品相符")
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ payload["summary_brief"] = "汇" * mcp.SUMMARY_BRIEF_MAX
|
|
|
|
|
+ payload["fields"]["cn_name"]["reason_brief"] = "依" * mcp.REASON_BRIEF_MAX
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ for result in ("pass", "reject", "hold", "not_checked"):
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ payload["fields"]["image_url"].update(result=result, reason="本用例由代理显式填写的结论依据")
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_null_false_zero_and_empty_suggestions_remain_distinct(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ for suggestion in (None, False, 0, "", [], {"future": [False, 0]}):
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ leaf = payload["fields"]["cn_name"]
|
|
|
|
|
+ leaf.update(result="hold", suggested_value=suggestion, reason="建议须由 PHP 核实合法性")
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ self.assertIs(type(leaf["suggested_value"]), type(suggestion))
|
|
|
|
|
+
|
|
|
|
|
+ def test_draft_placeholders_cannot_be_saved(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, mcp.prepare(source))
|
|
|
|
|
+ for mutate in (
|
|
|
|
|
+ lambda p: p.update(summary=mcp.DRAFT_SUMMARY),
|
|
|
|
|
+ lambda p: p.update(summary_brief=mcp.DRAFT_SUMMARY_BRIEF),
|
|
|
|
|
+ lambda p: p["fields"]["sku"].update(reason_brief=mcp.PENDING_BRIEF),
|
|
|
|
|
+ lambda p: p["fields"]["ys_clearance_data"][0]["material"].update(reason=mcp.PENDING),
|
|
|
|
|
+ ):
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ mutate(payload)
|
|
|
|
|
+ with self.subTest(mutate=mutate), self.assertRaisesRegex(mcp.ContractError, "草稿"):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_hint_findings_belong_on_the_related_audit_field(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ for result in ("hold", "reject"):
|
|
|
|
|
+ for at in (("recommended_grade",), ("ys_clearance_data", 0, "tax_rate"), ("yf_clearance_data", 0, "box_value")):
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ leaf = payload["fields"]
|
|
|
|
|
+ for key in at:
|
|
|
|
|
+ leaf = leaf[key]
|
|
|
|
|
+ leaf["result"] = result
|
|
|
|
|
+ with self.subTest(at=at, result=result), self.assertRaisesRegex(mcp.ContractError, "系统提示"):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ payload["fields"]["ys_clearance_data"][0]["tax_rate"]["result"] = "pass"
|
|
|
|
|
+ payload["fields"]["ys_clearance_data"][0]["clearance_hscode"].update(result="hold", reason="税率提示显示新编码,编码需核实")
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_all_unchecked_reviewable_fields_must_be_manual(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ payload = self.reviewed(source)
|
|
|
|
|
+ payload["fields"]["cn_name"]["result"] = "not_checked"
|
|
|
|
|
+ payload["fields"]["recommended_grade"]["result"] = "pass"
|
|
|
|
|
+ with self.assertRaisesRegex(mcp.ContractError, "manual"):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ payload["result"] = "manual"
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_missing_extra_or_changed_original_values_are_rejected(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ mutations = [
|
|
|
|
|
+ lambda p: p["fields"].pop("image_url"),
|
|
|
|
|
+ lambda p: p["fields"].update(certificates=p["fields"]["image_url"]),
|
|
|
|
|
+ lambda p: p["fields"]["is_battery"].update(value=0),
|
|
|
|
|
+ lambda p: p["fields"]["is_inspection_fee"].update(value=0.0),
|
|
|
|
|
+ lambda p: p["fields"]["ys_clearance_price"].update(value=2),
|
|
|
|
|
+ lambda p: p["fields"]["category_ids"]["value"].reverse(),
|
|
|
|
|
+ lambda p: p["fields"]["ys_clearance_data"].reverse(),
|
|
|
|
|
+ lambda p: p["fields"]["yf_clearance_data"].append(copy.deepcopy(p["fields"]["yf_clearance_data"][0])),
|
|
|
|
|
+ lambda p: p["fields"]["ys_clearance_data"][0].pop("material"),
|
|
|
|
|
+ lambda p: p["fields"]["ys_clearance_data"][0]["adjust_hscode_data"].update(value="9903.88.15"),
|
|
|
|
|
+ ]
|
|
|
|
|
+ for mutate in mutations:
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ mutate(payload)
|
|
|
|
|
+ with self.subTest(mutate=mutate), self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_malformed_leaves_metadata_reasons_and_enums_are_rejected(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ mutations = [
|
|
|
|
|
+ lambda p: p.update(result="approved"), lambda p: p.update(summary=" "),
|
|
|
|
|
+ lambda p: p.update(claim_ref="c" * 64), lambda p: p.update(source_ref="c" * 64),
|
|
|
|
|
+ lambda p: p.update(goods_id=True), lambda p: p.update(context={}),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].update(result="manual"),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].update(reason=""),
|
|
|
|
|
+ lambda p: p.pop("summary_brief"), lambda p: p.update(summary_brief=" "),
|
|
|
|
|
+ lambda p: p.update(summary_brief="汇" * 41), lambda p: p.update(summary_brief="两行\n汇总"),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].pop("reason_brief"),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].update(reason_brief=""),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].update(reason_brief="依" * 21),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].update(reason_brief="换\r行"),
|
|
|
|
|
+ lambda p: p["fields"]["ys_clearance_data"][0]["material"].update(reason_brief=None),
|
|
|
|
|
+ lambda p: p["fields"]["cn_name"].pop("suggested_value"),
|
|
|
|
|
+ lambda p: p["fields"].update(category_ids=[]),
|
|
|
|
|
+ ]
|
|
|
|
|
+ for mutate in mutations:
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ mutate(payload)
|
|
|
|
|
+ with self.subTest(mutate=mutate), self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_early_stop_cannot_claim_checked_fields_or_suggestions(self):
|
|
|
|
|
+ for country, name in (("CA", "陶瓷杯"), ("US", "手表")):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ source["context"]["country_code"] = country
|
|
|
|
|
+ source["page_values"]["cn_name"] = name
|
|
|
|
|
+ for change in ({"result": "pass"}, {"suggested_value": "new"}):
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ payload["fields"]["cn_name"].update(change)
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ payload["result"] = "hold"
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_unreviewed_draft_and_pending_fields_cannot_be_promoted_to_pass(self):
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ payload = mcp.prepare(source)
|
|
|
|
|
+ payload["result"] = "pass"
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+ payload["fields"]["cn_name"].update(result="pass")
|
|
|
|
|
+ payload["fields"]["en_name"].update(result="hold")
|
|
|
|
|
+ with self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.validate(source, payload)
|
|
|
|
|
+
|
|
|
|
|
+ def test_bad_source_shapes_and_opaque_refs_are_rejected(self):
|
|
|
|
|
+ mutations = [
|
|
|
|
|
+ lambda s: s.update(goods_id=0), lambda s: s.update(goods_id=True),
|
|
|
|
|
+ lambda s: s.update(claim_ref="short"), lambda s: s.update(source_ref="g" * 64),
|
|
|
|
|
+ lambda s: s.update(context=[]), lambda s: s.update(page_values=[]),
|
|
|
|
|
+ lambda s: s["page_values"].update(ys_clearance_data=None),
|
|
|
|
|
+ lambda s: s["page_values"].update(yf_clearance_data=["not a row"]),
|
|
|
|
|
+ ]
|
|
|
|
|
+ for mutate in mutations:
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ mutate(source)
|
|
|
|
|
+ with self.subTest(mutate=mutate), self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.prepare(source)
|
|
|
|
|
+
|
|
|
|
|
+ def test_strict_json_rejects_duplicate_keys_and_non_json_numbers(self):
|
|
|
|
|
+ for text in ('{"cn_name":1,"cn_name":2}', '{"x":NaN}', '{"x":Infinity}'):
|
|
|
|
|
+ with self.subTest(text=text), self.assertRaises(mcp.ContractError):
|
|
|
|
|
+ mcp.loads(text)
|
|
|
|
|
+ self.assertEqual(mcp.loads('{"value":false}'), {"value": False})
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CommandLineTests(unittest.TestCase):
|
|
|
|
|
+ def test_context_evidence_command_returns_only_readonly_attachment_data(self):
|
|
|
|
|
+ with tempfile.TemporaryDirectory() as directory:
|
|
|
|
|
+ source = snapshot()
|
|
|
|
|
+ source["context"]["certification_files"] = {
|
|
|
|
|
+ "battery_file": [], "fda_file": [],
|
|
|
|
|
+ "toy_file": [{"name": "CPC.pdf", "url": "https://files.example.test/cpc.pdf"}],
|
|
|
|
|
+ "other_file": []}
|
|
|
|
|
+ path = Path(directory) / "query.json"
|
|
|
|
|
+ path.write_text(json.dumps(source), encoding="utf-8")
|
|
|
|
|
+ result = subprocess.run([sys.executable, str(ROOT / "scripts/mcp_audit.py"), "evidence", str(path),
|
|
|
|
|
+ "--scope", "context", "/certification_files/toy_file", "/certification_files/fda_file"],
|
|
|
|
|
+ capture_output=True, text=True, encoding="utf-8")
|
|
|
|
|
+ self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
|
+ values = json.loads(result.stdout)
|
|
|
|
|
+ self.assertEqual(values["/certification_files/toy_file"]["status"], "supplied")
|
|
|
|
|
+ self.assertEqual(values["/certification_files/fda_file"], {"status": "empty", "value": []})
|
|
|
|
|
+
|
|
|
|
|
+ def test_offline_prepare_validate_and_evidence_commands(self):
|
|
|
|
|
+ with tempfile.TemporaryDirectory(dir=ROOT / "validation") as directory:
|
|
|
|
|
+ directory = Path(directory)
|
|
|
|
|
+ source_path = directory / "query.json"
|
|
|
|
|
+ payload_path = directory / "result.json"
|
|
|
|
|
+ source_path.write_text(json.dumps(snapshot(), ensure_ascii=False), encoding="utf-8")
|
|
|
|
|
+ def run(*args):
|
|
|
|
|
+ return subprocess.run([sys.executable, str(ROOT / "scripts/mcp_audit.py"), *map(str, args)],
|
|
|
|
|
+ capture_output=True, text=True, encoding="utf-8", cwd=directory)
|
|
|
|
|
+ prepared = run("prepare", source_path, "--out", payload_path)
|
|
|
|
|
+ self.assertEqual(prepared.returncode, 0, prepared.stderr)
|
|
|
|
|
+ self.assertEqual(json.loads(payload_path.read_text(encoding="utf-8"))["result"], "hold")
|
|
|
|
|
+ draft = run("validate", source_path, payload_path)
|
|
|
|
|
+ self.assertEqual(draft.returncode, 2, "an unedited draft must not pass local validation")
|
|
|
|
|
+ self.assertIn("草稿", draft.stderr)
|
|
|
|
|
+ payload_path.write_text(json.dumps(ValidationTests.reviewed(snapshot()), ensure_ascii=False), encoding="utf-8")
|
|
|
|
|
+ checked = run("validate", source_path, payload_path)
|
|
|
|
|
+ self.assertEqual(checked.returncode, 0, checked.stderr)
|
|
|
|
|
+ self.assertIn("结构", checked.stdout)
|
|
|
|
|
+ report = run("evidence", source_path, "/certificates", "/image_url")
|
|
|
|
|
+ self.assertEqual(report.returncode, 0, report.stderr)
|
|
|
|
|
+ self.assertEqual(json.loads(report.stdout)["/certificates"]["status"], "not_supplied")
|
|
|
|
|
+ payload_path.write_text('{"result": "pass"}', encoding="utf-8")
|
|
|
|
|
+ invalid = run("validate", source_path, payload_path)
|
|
|
|
|
+ self.assertEqual(invalid.returncode, 2)
|
|
|
|
|
+ self.assertNotIn("Traceback", invalid.stderr)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ unittest.main()
|