补齐遗漏日志: 服务日志/看门狗日志, 并归档上一轮遗留的测试产物
用户问"日志都上传了吗", 一查发现确有遗漏: - server.log (427KB, 写至 10-13) 与 keep_display.log (看门狗, 12.5KB), lc_mcp.log 此前只上传了 runs/ 下的 20 份任务日志, 这三个漏了。 - 10-02/10-03 那轮会话遗留在仓库根目录的测试产物从未提交, 一直显示为未跟踪。 本次补上: - logs/full/ 增加 server.log.gz / keep_display.log.gz / lc_mcp.log.gz - logs/keep_display-watchdog.txt 为看门狗日志全文(177 行), 完整记录了 "RDP 一断开就提权 tscon 切回物理 console" 的每次触发, 即被要求关闭的那支脚本的行为证据。 - logs/server-excerpt.txt 为服务日志摘录(52 行)。 - 10-02 的测试产物归档到 docs/feedback/2026-10-02/validation/ 并附 README 说明, PHYSICAL_INPUT_CHANGE.md 归到 2026-10-03/。 注: tests/*.py 归档后不在 tests/ 目录, 不会被 unittest discover 收集; 此为保留证据链的有意选择, 已在 README 中注明。
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
"""Use only generated images; never send screenshots or desktop input."""
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
sys.path.insert(0, str(ROOT))
|
||||||
|
import lc_mcp as app
|
||||||
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
|
|
||||||
|
out = ROOT / 'validation-artifacts'
|
||||||
|
out.mkdir(exist_ok=True)
|
||||||
|
results = {}
|
||||||
|
image = Image.new('RGB', (900, 300), 'white')
|
||||||
|
draw = ImageDraw.Draw(image)
|
||||||
|
font = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', 52)
|
||||||
|
draw.text((40, 30), '开始 测试', font=font, fill='black')
|
||||||
|
draw.text((40, 130), 'TEST 123', font=font, fill='black')
|
||||||
|
path = out / 'ocr-synthetic.png'
|
||||||
|
image.save(str(path))
|
||||||
|
start = time.monotonic()
|
||||||
|
items, error = app.ocr_image(str(path))
|
||||||
|
results['remote_ocr'] = {'url': app.OCR_URL, 'seconds': round(time.monotonic() - start, 3),
|
||||||
|
'items': items, 'error': error, 'input': str(path)}
|
||||||
|
print(json.dumps({'remote_ocr': results['remote_ocr']}, ensure_ascii=True), flush=True)
|
||||||
|
|
||||||
|
# Exact script-generation expression from platform/win.py, with isolated paths.
|
||||||
|
job = str(out / 'nonexistent-test-job.ps1')
|
||||||
|
vbs = out / 'run-original.vbs'
|
||||||
|
vbs.write_text('CreateObject("WScript.Shell").Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%s"", 0, False\n' % job, encoding='utf-8')
|
||||||
|
r = subprocess.run(['cscript.exe', '//nologo', str(vbs)], capture_output=True, timeout=10)
|
||||||
|
results['windows_vbs'] = {'returncode': r.returncode,
|
||||||
|
'stdout': r.stdout.decode('mbcs', errors='replace'), 'stderr': r.stderr.decode('mbcs', errors='replace'),
|
||||||
|
'script': vbs.read_text(encoding='utf-8')}
|
||||||
|
print(json.dumps({'windows_vbs': results['windows_vbs']}, ensure_ascii=True), flush=True)
|
||||||
|
for name in ('sendinput', 'interception'):
|
||||||
|
try:
|
||||||
|
backend = app.get_input_backend(name, 'win')
|
||||||
|
results[name + '_initialization'] = {'ok': True}
|
||||||
|
except Exception as e:
|
||||||
|
results[name + '_initialization'] = {'ok': False, 'error': str(e)}
|
||||||
|
(ROOT / 'live-service-results.json').write_text(json.dumps(results, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||||
|
print(json.dumps(results, ensure_ascii=True), flush=True)
|
||||||
@@ -0,0 +1,251 @@
|
|||||||
|
"""Independent validation; no real desktop inputs are sent.
|
||||||
|
|
||||||
|
Run: python tests/test_validation.py
|
||||||
|
Failures assert the intended behavior and reproduce defects in the tested revision.
|
||||||
|
"""
|
||||||
|
import contextlib
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import queue
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import threading
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
sys.path.insert(0, str(ROOT))
|
||||||
|
import lc_mcp as app
|
||||||
|
from backends.mock import MockBackend
|
||||||
|
|
||||||
|
ITEMS = [
|
||||||
|
{'text': '开始', 'x': 100, 'y': 200, 'w': 80, 'h': 40, 'score': .99},
|
||||||
|
{'text': '开始战斗', 'x': 300, 'y': 400, 'w': 100, 'h': 50, 'score': .95},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class Provider:
|
||||||
|
def grab(self):
|
||||||
|
return {'ok': True, 'path': 'synthetic.png'}
|
||||||
|
|
||||||
|
|
||||||
|
class Peer:
|
||||||
|
def __init__(self, extra_env=None):
|
||||||
|
env = dict(os.environ, INPUT_BACKEND='mock', PYTHONIOENCODING='utf-8')
|
||||||
|
env.update(extra_env or {})
|
||||||
|
self.p = subprocess.Popen([sys.executable, str(ROOT / 'lc_mcp.py')],
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
|
text=True, encoding='utf-8', env=env)
|
||||||
|
self.q = queue.Queue()
|
||||||
|
def pump():
|
||||||
|
for line in self.p.stdout:
|
||||||
|
self.q.put(line)
|
||||||
|
self.q.put(None)
|
||||||
|
threading.Thread(target=pump, daemon=True).start()
|
||||||
|
|
||||||
|
def send(self, obj):
|
||||||
|
self.p.stdin.write(json.dumps(obj, ensure_ascii=True) + '\n')
|
||||||
|
self.p.stdin.flush()
|
||||||
|
line = self.q.get(timeout=5)
|
||||||
|
return json.loads(line) if line else None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.p.poll() is None:
|
||||||
|
self.p.kill()
|
||||||
|
self.p.communicate(timeout=5)
|
||||||
|
|
||||||
|
|
||||||
|
class HealthyPaths(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.b = MockBackend()
|
||||||
|
self.s = app.MCPServer('win', Provider(), self.b)
|
||||||
|
|
||||||
|
def test_text_coordinates(self):
|
||||||
|
self.assertEqual(app.calc_click('开始', ITEMS)[0], {'text': '开始', 'x': 140, 'y': 220})
|
||||||
|
|
||||||
|
def test_substring_case_insensitive(self):
|
||||||
|
items = [dict(ITEMS[0], text='Start Battle')]
|
||||||
|
self.assertEqual(len(app.calc_click('START', items)), 1)
|
||||||
|
|
||||||
|
def test_get_state(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
self.assertEqual(self.s.call('get_state', {})['items'], ITEMS)
|
||||||
|
|
||||||
|
def test_click_text_second_hit(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
self.s.call('click_text', {'text': '开始', 'index': 1})
|
||||||
|
self.assertEqual(self.b.log[-1]['x'], 350)
|
||||||
|
self.assertEqual(self.b.log[-1]['y'], 425)
|
||||||
|
|
||||||
|
def test_missing_text_does_not_click(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
self.assertFalse(self.s.call('click_text', {'text': '不存在'})['ok'])
|
||||||
|
self.assertEqual(self.b.log, [])
|
||||||
|
|
||||||
|
def test_ocr_error_does_not_click(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(None, 'service unavailable')):
|
||||||
|
self.assertFalse(self.s.call('click_text', {'text': '开始'})['ok'])
|
||||||
|
self.assertEqual(self.b.log, [])
|
||||||
|
|
||||||
|
def test_screenshot_error_propagates(self):
|
||||||
|
with patch.object(self.s.provider, 'grab', return_value={'ok': False, 'error': 'capture fail'}):
|
||||||
|
self.assertEqual(self.s.call('get_state', {})['error'], 'capture fail')
|
||||||
|
|
||||||
|
def test_wait_present(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
self.assertTrue(self.s.call('wait_for_text', {'text': '开始', 'timeout': 1})['present'])
|
||||||
|
|
||||||
|
def test_wait_absent(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=([], None)):
|
||||||
|
result = self.s.call('wait_for_text_gone', {'text': '开始', 'timeout': 1})
|
||||||
|
self.assertTrue(result['ok'])
|
||||||
|
self.assertFalse(result['present'])
|
||||||
|
|
||||||
|
def test_ocr_confidence_filter(self):
|
||||||
|
payload = {'ok': True, 'items': ITEMS + [dict(ITEMS[0], score=.1)]}
|
||||||
|
response = contextlib.closing(io.BytesIO(json.dumps(payload).encode('utf-8')))
|
||||||
|
with tempfile.TemporaryDirectory() as tmp, patch.object(app.urllib.request, 'urlopen', return_value=response):
|
||||||
|
path = Path(tmp) / 'image.png'
|
||||||
|
path.write_bytes(b'fixture')
|
||||||
|
items, error = app.ocr_image(str(path))
|
||||||
|
self.assertIsNone(error)
|
||||||
|
self.assertEqual(len(items), 2)
|
||||||
|
|
||||||
|
def test_protocol_discovery_and_all_input_tools(self):
|
||||||
|
p = Peer()
|
||||||
|
try:
|
||||||
|
response = p.send({'jsonrpc': '2.0', 'id': 1, 'method': 'initialize', 'params': {}})
|
||||||
|
self.assertEqual(response['result']['serverInfo']['name'], 'lc-mcp')
|
||||||
|
response = p.send({'jsonrpc': '2.0', 'id': 2, 'method': 'tools/list'})
|
||||||
|
self.assertEqual(len(response['result']['tools']), 9)
|
||||||
|
for name, args in [('click', {'x': 10, 'y': 20}), ('move', {'x': 5, 'y': 6}),
|
||||||
|
('keypress', {'key': 'p'}), ('type_text', {'text': '开始'})]:
|
||||||
|
r = p.send({'jsonrpc': '2.0', 'id': 3, 'method': 'tools/call', 'params': {'name': name, 'arguments': args}})
|
||||||
|
self.assertTrue(json.loads(r['result']['content'][0]['text'])['ok'])
|
||||||
|
finally:
|
||||||
|
p.close()
|
||||||
|
|
||||||
|
|
||||||
|
class RequiredBehavior(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.b = MockBackend()
|
||||||
|
self.s = app.MCPServer('win', Provider(), self.b)
|
||||||
|
|
||||||
|
def test_missing_click_coordinates_rejected(self):
|
||||||
|
result = self.s.call('click', {})
|
||||||
|
self.assertFalse(result['ok'], result)
|
||||||
|
self.assertEqual(self.b.log, [])
|
||||||
|
|
||||||
|
def test_missing_click_text_rejected(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
result = self.s.call('click_text', {})
|
||||||
|
self.assertFalse(result['ok'], result)
|
||||||
|
self.assertEqual(self.b.log, [])
|
||||||
|
|
||||||
|
def test_out_of_range_index_rejected(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
result = self.s.call('click_text', {'text': '开始', 'index': 99})
|
||||||
|
self.assertFalse(result['ok'], result)
|
||||||
|
|
||||||
|
def test_negative_index_rejected(self):
|
||||||
|
with patch.object(app, 'ocr_image', return_value=(ITEMS, None)):
|
||||||
|
result = self.s.call('click_text', {'text': '开始', 'index': -1})
|
||||||
|
self.assertFalse(result['ok'], result)
|
||||||
|
|
||||||
|
def test_wait_timeout_honored(self):
|
||||||
|
clock = [0.0]
|
||||||
|
def slow_ocr(_):
|
||||||
|
clock[0] += 30
|
||||||
|
return None, 'network timeout'
|
||||||
|
def sleep(seconds):
|
||||||
|
clock[0] += seconds
|
||||||
|
with patch.object(app.time, 'time', side_effect=lambda: clock[0]), \
|
||||||
|
patch.object(app.time, 'sleep', side_effect=sleep), \
|
||||||
|
patch.object(app, 'ocr_image', side_effect=slow_ocr):
|
||||||
|
self.s.call('wait_for_text', {'text': '开始', 'timeout': 1})
|
||||||
|
self.assertLessEqual(clock[0], 1.1, 'one-second deadline took %.1f seconds' % clock[0])
|
||||||
|
|
||||||
|
def test_tool_exception_sets_result_iserror(self):
|
||||||
|
p = Peer()
|
||||||
|
try:
|
||||||
|
r = p.send({'jsonrpc': '2.0', 'id': 1, 'method': 'tools/call',
|
||||||
|
'params': {'name': 'move', 'arguments': {'x': 'invalid', 'y': 1}}})
|
||||||
|
self.assertTrue(r['result'].get('isError', False), r)
|
||||||
|
finally:
|
||||||
|
p.close()
|
||||||
|
|
||||||
|
def test_unknown_tool_returns_protocol_error(self):
|
||||||
|
p = Peer()
|
||||||
|
try:
|
||||||
|
r = p.send({'jsonrpc': '2.0', 'id': 1, 'method': 'tools/call',
|
||||||
|
'params': {'name': 'unknown', 'arguments': {}}})
|
||||||
|
self.assertIn('error', r, r)
|
||||||
|
finally:
|
||||||
|
p.close()
|
||||||
|
|
||||||
|
def test_invalid_json_keeps_server_alive(self):
|
||||||
|
p = Peer()
|
||||||
|
try:
|
||||||
|
p.p.stdin.write('{ invalid json }\n')
|
||||||
|
p.p.stdin.flush()
|
||||||
|
line = p.q.get(timeout=5)
|
||||||
|
self.assertIsNotNone(line, 'server closes stdout and exits without JSON-RPC parse error')
|
||||||
|
self.assertEqual(json.loads(line)['error']['code'], -32700)
|
||||||
|
finally:
|
||||||
|
p.close()
|
||||||
|
|
||||||
|
def test_sendinput_failed_injection_is_reported(self):
|
||||||
|
from backends.sendinput_win import SendInputBackend
|
||||||
|
b = SendInputBackend.__new__(SendInputBackend)
|
||||||
|
with patch.object(b, '_send_input', return_value=0):
|
||||||
|
r = b.keypress('p')
|
||||||
|
self.assertFalse(r['ok'], r)
|
||||||
|
|
||||||
|
def test_uinput_key_constants_in_scope(self):
|
||||||
|
from backends.uinput_linux import UinputBackend
|
||||||
|
b = UinputBackend.__new__(UinputBackend)
|
||||||
|
with patch.object(b, '_key'):
|
||||||
|
r = b.keypress('enter')
|
||||||
|
self.assertTrue(r['ok'])
|
||||||
|
|
||||||
|
def test_uinput_device_events_are_valid(self):
|
||||||
|
import types
|
||||||
|
from backends.uinput_linux import UinputBackend
|
||||||
|
fake = types.SimpleNamespace(Device=lambda events: object())
|
||||||
|
names = ['KEY_A', 'KEY_Z', 'KEY_ENTER', 'KEY_ESC', 'KEY_SPACE', 'KEY_TAB', 'KEY_F4',
|
||||||
|
'KEY_LEFTCTRL', 'KEY_LEFTSHIFT', 'KEY_LEFTALT', 'KEY_BACKSPACE',
|
||||||
|
'REL_X', 'REL_Y', 'BTN_LEFT', 'BTN_RIGHT']
|
||||||
|
for i, name in enumerate(names):
|
||||||
|
setattr(fake, name, (1, i + 30))
|
||||||
|
b = UinputBackend.__new__(UinputBackend)
|
||||||
|
b._ctx = None
|
||||||
|
with patch.dict(sys.modules, {'uinput': fake}):
|
||||||
|
self.assertIsNotNone(b._ensure())
|
||||||
|
|
||||||
|
|
||||||
|
class Result(unittest.TextTestResult):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.records = []
|
||||||
|
|
||||||
|
def addSuccess(self, test):
|
||||||
|
super().addSuccess(test)
|
||||||
|
self.records.append({'test': test.id(), 'status': 'PASS'})
|
||||||
|
|
||||||
|
def addFailure(self, test, err):
|
||||||
|
super().addFailure(test, err)
|
||||||
|
self.records.append({'test': test.id(), 'status': 'FAIL', 'detail': self._exc_info_to_string(err, test)})
|
||||||
|
|
||||||
|
def addError(self, test, err):
|
||||||
|
super().addError(test, err)
|
||||||
|
self.records.append({'test': test.id(), 'status': 'ERROR', 'detail': self._exc_info_to_string(err, test)})
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
suite = unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
|
||||||
|
result = unittest.TextTestRunner(verbosity=2, resultclass=Result).run(suite)
|
||||||
|
(ROOT / 'validation-results.json').write_text(json.dumps(result.records, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||||
|
sys.exit(0 if result.wasSuccessful() else 1)
|
||||||
Reference in New Issue
Block a user