瀏覽代碼

7124556399增加商品编码查询MCP接口 测试脚本

zhongjr 2 周之前
父節點
當前提交
36b2e3135a
共有 1 個文件被更改,包括 85 次插入 和 0 次删除
  1. 85 0
      tests/test_trial_goods_tax_tool.py

+ 85 - 0
tests/test_trial_goods_tax_tool.py

@@ -0,0 +1,85 @@
+import unittest
+from unittest.mock import Mock
+
+from app import GatewayApp
+from public_gateway import PublicGatewayApp
+from services.output_presenter import OutputPresenter
+from tools.trial_goods_tax import TrialGoodsTaxTool
+
+
+class TrialGoodsTaxToolTest(unittest.TestCase):
+    def test_call_forwards_required_fields(self):
+        client = Mock()
+        client.call_tool.return_value = {'code': 'MCP_0000'}
+        TrialGoodsTaxTool(client).call(
+            sku=' SKU-1 ',
+            departure_country=' CN ',
+            destination_country='us',
+            price=10,
+            qty=2,
+            gross_weight=1.5,
+            box=1,
+        )
+        client.call_tool.assert_called_once_with(
+            'trial_goods_tax',
+            '/mcp/tools/trialGoodsTax',
+            {
+                'sku': 'SKU-1',
+                'departure_country': 'CN',
+                'destination_country': 'US',
+                'price': 10,
+                'qty': 2,
+                'gross_weight': 1.5,
+                'box': 1,
+            },
+            'rq_trial_goods_tax',
+        )
+
+    def test_call_rejects_country_name(self):
+        tool = TrialGoodsTaxTool(api_client=Mock())
+        with self.assertRaises(ValueError):
+            tool.call(
+                sku='SKU-1', departure_country='中国', destination_country='US',
+                price=10, qty=1, gross_weight=1, box=1,
+            )
+
+    def test_call_rejects_zero_price(self):
+        tool = TrialGoodsTaxTool(api_client=Mock())
+        with self.assertRaises(ValueError):
+            tool.call(
+                sku='SKU-1', departure_country='CN', destination_country='US',
+                price=0, qty=1, gross_weight=1, box=1,
+            )
+
+    def test_registries_match(self):
+        local = GatewayApp().registered_tool_names()
+        public = PublicGatewayApp(None, None).registered_tool_names()
+        self.assertEqual(local, public)
+        self.assertIn('trial_goods_tax', local)
+        self.assertEqual(36, len(local))
+        self.assertIn('trial_goods_tax', OutputPresenter.SAFE_TOOLS)
+
+    def test_presenter_renders_amount(self):
+        result = OutputPresenter().present('trial_goods_tax', {
+            'code': 'MCP_0000',
+            'data': {
+                'estimated_tax': 12.5,
+                'sku': 'SKU-1',
+                'departure_country': 'CN',
+                'destination_country': 'US',
+            },
+            'meta': {'request_id': 'rq1'},
+        })
+        self.assertFalse(result['is_error'])
+        self.assertEqual(12.5, result['structured_content']['estimated_tax'])
+        self.assertIn('预计关税金额', result['text'])
+
+    def test_presenter_keeps_safe_failure_message(self):
+        result = OutputPresenter().present('trial_goods_tax', {
+            'code': 'MCP_1501',
+            'msg': '未找到已审核商品',
+            'data': [],
+            'meta': {'request_id': 'rq1'},
+        })
+        self.assertTrue(result['is_error'])
+        self.assertEqual('未找到已审核商品', result['structured_content']['message'])