test_trial_goods_tax_tool.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import unittest
  2. from unittest.mock import Mock
  3. from app import GatewayApp
  4. from public_gateway import PublicGatewayApp
  5. from services.output_presenter import OutputPresenter
  6. from tools.trial_goods_tax import TrialGoodsTaxTool
  7. class TrialGoodsTaxToolTest(unittest.TestCase):
  8. def test_call_forwards_required_fields(self):
  9. client = Mock()
  10. client.call_tool.return_value = {'code': 'MCP_0000'}
  11. TrialGoodsTaxTool(client).call(
  12. sku=' SKU-1 ',
  13. departure_country=' CN ',
  14. destination_country='us',
  15. price=10,
  16. qty=2,
  17. gross_weight=1.5,
  18. box=1,
  19. )
  20. client.call_tool.assert_called_once_with(
  21. 'trial_goods_tax',
  22. '/mcp/tools/trialGoodsTax',
  23. {
  24. 'sku': 'SKU-1',
  25. 'departure_country': 'CN',
  26. 'destination_country': 'US',
  27. 'price': 10,
  28. 'qty': 2,
  29. 'gross_weight': 1.5,
  30. 'box': 1,
  31. },
  32. 'rq_trial_goods_tax',
  33. )
  34. def test_call_rejects_country_name(self):
  35. tool = TrialGoodsTaxTool(api_client=Mock())
  36. with self.assertRaises(ValueError):
  37. tool.call(
  38. sku='SKU-1', departure_country='中国', destination_country='US',
  39. price=10, qty=1, gross_weight=1, box=1,
  40. )
  41. def test_call_rejects_zero_price(self):
  42. tool = TrialGoodsTaxTool(api_client=Mock())
  43. with self.assertRaises(ValueError):
  44. tool.call(
  45. sku='SKU-1', departure_country='CN', destination_country='US',
  46. price=0, qty=1, gross_weight=1, box=1,
  47. )
  48. def test_registries_match(self):
  49. local = GatewayApp().registered_tool_names()
  50. public = PublicGatewayApp(None, None).registered_tool_names()
  51. self.assertEqual(local, public)
  52. self.assertIn('trial_goods_tax', local)
  53. self.assertEqual(39, len(local))
  54. self.assertIn('trial_goods_tax', OutputPresenter.SAFE_TOOLS)
  55. def test_presenter_renders_amount(self):
  56. result = OutputPresenter().present('trial_goods_tax', {
  57. 'code': 'MCP_0000',
  58. 'data': {
  59. 'estimated_tax': 12.5,
  60. 'sku': 'SKU-1',
  61. 'departure_country': 'CN',
  62. 'destination_country': 'US',
  63. },
  64. 'meta': {'request_id': 'rq1'},
  65. })
  66. self.assertFalse(result['is_error'])
  67. self.assertEqual(12.5, result['structured_content']['estimated_tax'])
  68. self.assertIn('预计关税金额', result['text'])
  69. def test_presenter_keeps_safe_failure_message(self):
  70. result = OutputPresenter().present('trial_goods_tax', {
  71. 'code': 'MCP_1501',
  72. 'msg': '未找到已审核商品',
  73. 'data': [],
  74. 'meta': {'request_id': 'rq1'},
  75. })
  76. self.assertTrue(result['is_error'])
  77. self.assertEqual('未找到已审核商品', result['structured_content']['message'])