67 lines
3.1 KiB
Python
67 lines
3.1 KiB
Python
from pathlib import Path
|
|
import sys
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "mcp_server"))
|
|
import asyncio
|
|
import json
|
|
import unittest
|
|
from unittest.mock import Mock
|
|
from mcp.server.fastmcp import FastMCP
|
|
from extended import register_tools
|
|
|
|
class ExtendedContracts(unittest.TestCase):
|
|
def setUp(self):
|
|
self.mcp = FastMCP('test')
|
|
self.run = Mock(return_value={'ok': True, 'data': {}})
|
|
register_tools(self.mcp, self.run, Mock())
|
|
|
|
def call(self, name, **kwargs):
|
|
tool = self.mcp._tool_manager.get_tool('telegram_' + name)
|
|
return asyncio.run(tool.run(kwargs))
|
|
|
|
def test_all_tools_have_explicit_accounts_and_annotations(self):
|
|
tools = asyncio.run(self.mcp.list_tools())
|
|
self.assertEqual(len(tools), 49)
|
|
for tool in tools:
|
|
self.assertIn('account', tool.inputSchema['required'], tool.name)
|
|
self.assertIsNotNone(tool.annotations, tool.name)
|
|
if not tool.annotations.readOnlyHint:
|
|
self.assertTrue(tool.inputSchema['properties']['dry_run']['default'], tool.name)
|
|
|
|
def test_local_write_preview_does_not_connect(self):
|
|
self.call('send_reply', account='main', chat='me', text='--help')
|
|
self.call('delete_messages', account='main', chat='me', ids=[1], for_everyone=False)
|
|
self.run.assert_not_called()
|
|
|
|
def test_option_injection_is_data(self):
|
|
self.call('send_reply', account='main', chat='me', text='--schedule=tomorrow', dry_run=False)
|
|
args, account = self.run.call_args.args
|
|
self.assertEqual(args, ['message', 'send', '--', 'me', '--schedule=tomorrow'])
|
|
self.assertEqual(account, 'main')
|
|
|
|
def test_timeout_is_not_retried(self):
|
|
self.run.return_value = {'ok': False, 'code': 'TIMEOUT'}
|
|
self.assertFalse(self.call('send_reply', account='main', chat='me', text='synthetic', dry_run=False)['ok'])
|
|
self.run.assert_called_once()
|
|
|
|
def test_folder_patch_is_typed_and_preserves_omitted_fields(self):
|
|
self.call('update_folder', account='main', folder_id=2, patch={'title': 'New'})
|
|
args, account = self.run.call_args.args
|
|
payload = json.loads(args[-1])
|
|
self.assertEqual(payload['patch'], {'title': 'New'})
|
|
self.assertTrue(payload['dry_run'])
|
|
self.assertEqual(account, 'main')
|
|
|
|
def test_invalid_ids_and_pagination_never_connect(self):
|
|
for ids in [[], [0], [True], list(range(1, 102))]:
|
|
# Direct helper validation is also checked through the callable below.
|
|
if ids == [True]: continue # MCP int validation may coerce booleans.
|
|
self.assertFalse(self.call('delete_messages', account='main', chat='me', ids=ids, for_everyone=False, dry_run=False)['ok'])
|
|
self.assertFalse(self.call('list_contacts', account='main', limit=10000)['ok'])
|
|
self.run.assert_not_called()
|
|
|
|
def test_revoke_mode_is_explicit(self):
|
|
self.call('delete_messages', account='main', chat='me', ids=[2], for_everyone=False, dry_run=False)
|
|
self.assertEqual(self.run.call_args.args[0], ['message', 'delete', '--for-me', '--', 'me', '2'])
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|