124 lines
6.1 KiB
Python
124 lines
6.1 KiB
Python
"""MCP server: protocol round trip over stdio and every tool."""
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from helpers import FIXTURE, HAVE_PIL, ROOT
|
|
|
|
from nxhmi import mcp_server as M
|
|
|
|
|
|
class Protocol(unittest.TestCase):
|
|
def test_stdio_session(self):
|
|
env = dict(os.environ, PYTHONPATH=os.path.join(ROOT, "src"))
|
|
msgs = [{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}},
|
|
{"jsonrpc": "2.0", "method": "notifications/initialized"},
|
|
{"jsonrpc": "2.0", "id": 2, "method": "tools/list"},
|
|
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "list_kinds", "arguments": {}}},
|
|
{"jsonrpc": "2.0", "id": 4, "method": "nope"}]
|
|
p = subprocess.run([sys.executable, "-m", "nxhmi.mcp_server"], input="\n".join(json.dumps(m) for m in msgs) + "\n",
|
|
capture_output=True, text=True, env=env, timeout=60)
|
|
replies = [json.loads(l) for l in p.stdout.splitlines()]
|
|
self.assertEqual([r["id"] for r in replies], [1, 2, 3, 4])
|
|
self.assertEqual(replies[0]["result"]["serverInfo"]["name"], "openhmi-nx")
|
|
self.assertIn("render_page", [t["name"] for t in replies[1]["result"]["tools"]])
|
|
self.assertIn("button", json.loads(replies[2]["result"]["content"][0]["text"])["kinds"])
|
|
self.assertEqual(replies[3]["error"]["code"], -32601)
|
|
|
|
|
|
class Tools(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.mkdtemp()
|
|
self.hmi = os.path.join(self.tmp, "p.HMI")
|
|
shutil.copy(FIXTURE, self.hmi)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.tmp)
|
|
|
|
def call(self, tool, **args):
|
|
r = M.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": tool, "arguments": args}})
|
|
res = r["result"]
|
|
if res.get("isError"):
|
|
raise AssertionError(res["content"][0]["text"])
|
|
return res["content"]
|
|
|
|
def j(self, tool, **args):
|
|
return json.loads(self.call(tool, **args)[0]["text"])
|
|
|
|
def test_info_and_page(self):
|
|
info = self.j("info", path=self.hmi)
|
|
self.assertEqual([p["page"] for p in info["pages"]], ["home", "controls", "gallery"])
|
|
page = self.j("get_page", path=self.hmi, page="home")
|
|
title = next(c for c in page["components"] if c["name"] == "title")
|
|
self.assertEqual(title["kind"], "text")
|
|
self.assertEqual(title["attributes"]["xcen"], "left")
|
|
|
|
def test_edit_cycle(self):
|
|
r = self.j("set_attributes", path=self.hmi, page="home", component="title",
|
|
attributes={"txt": "edited", "xcen": "center", "pco": "#ff0000", "x": 20})
|
|
self.assertEqual((r["attributes"]["txt"], r["attributes"]["xcen"], r["attributes"]["pco"]), ("edited", "center", "#ff0000"))
|
|
self.assertEqual(r["attributes"]["endx"], 20 + r["attributes"]["w"] - 1)
|
|
self.j("set_event", path=self.hmi, page="home", component="title", event="codesup", lines=["page 1"])
|
|
c = self.j("add_component", path=self.hmi, page="home", kind="button", name="bNew",
|
|
attributes={"x": 1, "y": 2, "w": 40, "h": 20, "txt": "go", "style": "3D_Auto"},
|
|
events={"codesup": ["page 0"]})
|
|
self.assertEqual(c["events"], {"codesup": ["page 0"]})
|
|
self.j("remove_component", path=self.hmi, page="home", component="bNew")
|
|
self.j("add_page", path=self.hmi, name="extra", attributes={"bco": "#000000"})
|
|
self.assertTrue(self.j("verify", path=self.hmi)["ok"])
|
|
|
|
def test_errors_are_tool_results(self):
|
|
r = M.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
|
|
"params": {"name": "get_page", "arguments": {"path": self.hmi, "page": "missing"}}})
|
|
self.assertTrue(r["result"]["isError"])
|
|
self.assertIn("pages: home", r["result"]["content"][0]["text"])
|
|
|
|
def test_build_tft(self):
|
|
from nxhmi import project, tft, tft_build
|
|
for i, n in enumerate(tft_build.FIRMWARE_FILES): # stand-ins for the Editor's files
|
|
with open(os.path.join(self.tmp, n), "wb") as f:
|
|
f.write(bytes([i]) * 50)
|
|
p, out = os.path.join(self.tmp, "min.HMI"), os.path.join(self.tmp, "min.tft")
|
|
project.new_project("NX4832T035_011").save(p)
|
|
r = self.j("build_tft", path=p, tft_path=out, editor_dir=self.tmp)
|
|
with open(out, "rb") as f:
|
|
data = f.read()
|
|
self.assertEqual(r["bytes"], len(data))
|
|
self.assertTrue(tft.verify_header(data) and tft.verify_trailer(data))
|
|
# the demo uses component types that cannot be compiled yet: a tool error, not a crash
|
|
r = M.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
|
|
"params": {"name": "build_tft", "arguments": {"path": self.hmi, "tft_path": out,
|
|
"editor_dir": self.tmp}}})
|
|
self.assertTrue(r["result"]["isError"])
|
|
|
|
def test_schema(self):
|
|
s = self.j("schema", kind="text")
|
|
xcen = next(a for a in s["attributes"] if a["name"] == "xcen")
|
|
self.assertEqual(xcen["values"], ["left", "center", "right"])
|
|
|
|
def test_new_project_refuses_overwrite(self):
|
|
p = os.path.join(self.tmp, "new.HMI")
|
|
self.assertEqual(self.j("new_project", path=p)["pages"][0]["page"], "page0")
|
|
with self.assertRaises(AssertionError):
|
|
self.j("new_project", path=p)
|
|
|
|
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
|
def test_render_and_media(self):
|
|
content = self.call("render_page", path=self.hmi, page="home")
|
|
self.assertEqual(content[0]["type"], "image")
|
|
self.assertEqual(content[0]["mimeType"], "image/png")
|
|
from PIL import Image
|
|
png = os.path.join(self.tmp, "i.png")
|
|
Image.new("RGB", (4, 4), (9, 9, 9)).save(png)
|
|
self.assertEqual(self.j("add_picture", path=self.hmi, image_path=png)["picture_id"], 6)
|
|
self.assertEqual(self.j("add_font", path=self.hmi, height=12)["font_id"], 2)
|
|
self.assertTrue(self.j("verify", path=self.hmi)["ok"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|