Files
quantux_mcp/tests/build_control_test_app.py
T

65 lines
3.9 KiB
Python

import asyncio
import json
import httpx2
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
KEY = open("/home/ubuntu/quantux-mcp/.env").read().split("MCP_API_KEY=")[1].splitlines()[0].strip()
URL = "http://127.0.0.1:8091/mcp"
async def main():
hc = httpx2.AsyncClient(headers={"Authorization": f"Bearer {KEY}"})
async with streamable_http_client(URL, http_client=hc) as (r, w):
async with ClientSession(r, w) as s:
await s.initialize()
def call(name, args):
import asyncio as aio
return aio.get_event_loop() # placeholder
# simpler: use sequential awaits
res = await s.call_tool("quantux_create_app", {"name": "交互一致性测试", "width": 400, "height": 800})
app = json.loads(res.content[0].text)
app_id = app["app_id"]
print("app:", app_id)
res = await s.call_tool("quantux_add_screen", {"app_id": app_id, "name": "控件页"})
sid = json.loads(res.content[0].text)["screen_id"]
print("screen:", sid)
# 各类控件
controls = [
("TextBox", "100", "100", "250", "44", {"label": "请输入文字", "placeholder": True}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#999", "borderRightColor": "#999", "borderTopColor": "#999", "borderBottomColor": "#999", "fontSize": 14}),
("Password", "100", "160", "250", "44", {"label": "请输入密码", "placeholder": True}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#999", "borderRightColor": "#999", "borderTopColor": "#999", "borderBottomColor": "#999", "fontSize": 14}),
("CheckBox", "100", "220", "200", "30", {"label": "记住我"}, {"fontSize": 15, "color": "#333"}),
("RadioBox", "100", "260", "200", "30", {"label": "男"}, {"fontSize": 15, "color": "#333"}),
("Switch", "100", "300", "200", "30", {"label": "开启通知"}, {"fontSize": 15, "color": "#333"}),
("DropDown", "100", "340", "250", "40", {"label": "请选择", "options": ["选项一", "选项二", "选项三"]}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#999", "borderRightColor": "#999", "borderTopColor": "#999", "borderBottomColor": "#999", "fontSize": 14}),
("Button", "100", "400", "250", "50", {"label": "登录(跳转)"}, {"background": "#4F46E5", "color": "#fff", "fontSize": 16}),
]
btn_id = None
for i, (typ, x, y, w, h, props, style) in enumerate(controls):
res = await s.call_tool("quantux_add_widget", {
"app_id": app_id, "screen_id": sid, "widget_type": typ,
"x": int(x), "y": int(y), "w": int(w), "h": int(h),
"name": typ,
"props_json": json.dumps(props, ensure_ascii=False),
"style_json": json.dumps(style, ensure_ascii=False),
})
wid = json.loads(res.content[0].text)["widget_id"]
if typ == "Button":
btn_id = wid
print(" added", typ, wid)
# 第二个屏幕 + 连线
res = await s.call_tool("quantux_add_screen", {"app_id": app_id, "name": "目标页"})
sid2 = json.loads(res.content[0].text)["screen_id"]
await s.call_tool("quantux_connect_flow", {"app_id": app_id, "from_widget_id": btn_id, "to_screen_id": sid2})
print("flow:", btn_id, "->", sid2)
# 导出
res = await s.call_tool("quantux_export_html", {"app_id": app_id})
print("export:", res.content[0].text)
asyncio.run(main())