Fix MCP cancellation, mirror run state, and battle boundaries
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""不需要 Windows/AALC 的测试: python -m unittest discover -s tests"""
|
||||
import json
|
||||
import io
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
@@ -84,6 +85,55 @@ class ProtocolTest(unittest.TestCase):
|
||||
self.assertIn('bad arguments', r['error'])
|
||||
self.assertFalse(call(self.s, 'click')['ok'])
|
||||
|
||||
def test_invalid_request_shapes_do_not_escape_or_send_input(self):
|
||||
for params in (['invalid'], 'invalid', 1, True, None):
|
||||
response = self.s.handle({'jsonrpc': '2.0', 'id': 7, 'method': 'tools/call', 'params': params})
|
||||
self.assertEqual(response['error']['code'], -32602)
|
||||
for arguments in ([], 'invalid', 1, True, None):
|
||||
response = self.s.handle({'jsonrpc': '2.0', 'id': 8, 'method': 'tools/call',
|
||||
'params': {'name': 'click', 'arguments': arguments}})
|
||||
self.assertEqual(response['error']['code'], -32602)
|
||||
self.assertEqual(self.engine.actions, [])
|
||||
|
||||
def test_notifications_never_reply_or_perform_request_only_actions(self):
|
||||
message = {'jsonrpc': '2.0', 'method': 'tools/call',
|
||||
'params': {'name': 'click', 'arguments': {'x': 1, 'y': 2}}}
|
||||
self.assertIsNone(self.s.handle(message))
|
||||
self.assertEqual(self.engine.actions, [])
|
||||
self.assertIsNotNone(self.s.handle(dict(message, id=None)))
|
||||
|
||||
def test_malformed_messages_allow_following_valid_request(self):
|
||||
messages = ['null', '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":[1]}',
|
||||
'{"jsonrpc":"2.0","id":2,"method":"ping"}']
|
||||
stdin, stdout = io.StringIO('\n'.join(messages) + '\n'), io.StringIO()
|
||||
srv_mod.serve_lines(self.s, stdin.readline, stdout.write)
|
||||
responses = [json.loads(line) for line in stdout.getvalue().splitlines()]
|
||||
self.assertEqual([r.get('error', {}).get('code') for r in responses[:2]], [-32600, -32602])
|
||||
self.assertEqual(responses[2], {'jsonrpc': '2.0', 'id': 2, 'result': {}})
|
||||
|
||||
def test_invalid_envelope_returns_request_error(self):
|
||||
for updates in ({'jsonrpc': '1.0'}, {'method': []}, {'id': True}, {'id': []}):
|
||||
message = dict({'jsonrpc': '2.0', 'id': 9, 'method': 'ping'}, **updates)
|
||||
self.assertEqual(self.s.handle(message)['error']['code'], -32600)
|
||||
|
||||
def test_task_start_cannot_race_an_input_already_in_progress(self):
|
||||
entered, release = threading.Event(), threading.Event()
|
||||
self.engine.click = lambda *args: (entered.set(), release.wait(3))
|
||||
worker = threading.Thread(target=lambda: call(self.s, 'click', x=1, y=2))
|
||||
worker.start()
|
||||
try:
|
||||
self.assertTrue(entered.wait(2))
|
||||
started = time.monotonic()
|
||||
response = call(self.s, 'run_task', task='mirror')
|
||||
self.assertFalse(response['ok'])
|
||||
self.assertIsNone(self.tasks.proc)
|
||||
self.assertLess(time.monotonic() - started, 1)
|
||||
self.assertEqual(call(self.s, 'task_status')['state'], 'idle')
|
||||
self.assertTrue(call(self.s, 'stop_task')['ok'])
|
||||
finally:
|
||||
release.set()
|
||||
worker.join(3)
|
||||
|
||||
def test_drag_duration_and_rejected_arguments(self):
|
||||
r = call(self.s, 'drag', from_x=200, from_y=300, to_x=200, to_y=600, duration=1.5)
|
||||
self.assertTrue(r['ok'])
|
||||
|
||||
Reference in New Issue
Block a user