modify: 统一插件风格

This commit is contained in:
2026-08-14 16:03:49 +08:00
parent fe95a67e93
commit f2a3a37389
14 changed files with 306 additions and 53 deletions
+26 -11
View File
@@ -99,20 +99,20 @@ async def test_custom_routing():
)
# 1. 默认 subject = <worker>.<最后一段>;响应 = <实际主题>/response
await bridge._handle(make_msg("devices/dev-01/telemetry", {"v": 1}))
await bridge._handle(make_msg("devices/dev-01/telemetry", {"meta": {}, "data": {"v": 1}}))
assert wm.calls[-1]["worker_type"] == "device"
assert wm.calls[-1]["subject"] == "device.telemetry"
assert published[-1][0] == "devices/dev-01/telemetry/response"
# 2. 带占位符 + 自定义响应主题
await bridge._handle(make_msg("foo/a/b", {"v": 2}))
await bridge._handle(make_msg("foo/a/b", {"meta": {}, "data": {"v": 2}}))
assert wm.calls[-1]["worker_type"] == "foo"
assert wm.calls[-1]["subject"] == "foo.handle.a/b"
assert published[-1][0] == "out/a/b/resp"
# 3. 内置 chongming 兜底
await bridge._handle(
make_msg(f"{MQTT_TOPIC_PREFIX}/example/add", {"a": 1, "b": 2})
make_msg(f"{MQTT_TOPIC_PREFIX}/example/add", {"meta": {}, "data": {"a": 1, "b": 2}})
)
assert wm.calls[-1]["worker_type"] == "example"
assert wm.calls[-1]["subject"] == "example.add"
@@ -120,7 +120,7 @@ async def test_custom_routing():
print("✅ 自定义主题路由 + 内置兜底通过")
async def test_custom_routing_control_fields():
async def test_envelope_meta_fields():
wm = FakeWM()
bridge, published = make_bridge(
wm, [{"topic": "devices/+/telemetry", "worker": "device"}]
@@ -128,14 +128,27 @@ async def test_custom_routing_control_fields():
await bridge._handle(
make_msg(
"devices/d1/telemetry",
{"_timeout": 5, "_cacheable": True, "_method": "POST"},
{"meta": {"timeout": 5, "cacheable": True, "method": "POST"}, "data": {}},
)
)
assert wm.calls[-1]["timeout"] == 5.0
assert wm.calls[-1]["cacheable"] is True
assert "_timeout" not in wm.calls[-1]["payload"]
assert wm.calls[-1]["payload"].get("_method") == "POST"
print("✅ 控制字段 (_timeout/_cacheable/_method) 透传通过")
payload = wm.calls[-1]["payload"]
assert payload["meta"]["method"] == "POST"
assert payload["data"] == {}
print("✅ 信封 meta 字段 (method/timeout/cacheable) 透传通过")
async def test_flat_payload_dropped():
"""严格信封协议:扁平载荷(无 meta/data)应被丢弃且不调用 worker"""
wm = FakeWM()
bridge, published = make_bridge(
wm, [{"topic": "devices/+/telemetry", "worker": "device"}]
)
await bridge._handle(make_msg("devices/d1/telemetry", {"_method": "POST", "v": 1}))
assert wm.calls == []
assert published == []
print("✅ 非信封(扁平)载荷被丢弃通过")
async def test_non_json_dropped():
@@ -165,7 +178,7 @@ async def test_board_ping_topic():
}
],
)
await bridge._handle(make_msg("/board_ping", {"BoardId": "B001", "Status": 1}))
await bridge._handle(make_msg("/board_ping", {"meta": {}, "data": {"BoardId": "B001", "Status": 1}}))
assert wm.calls[-1]["worker_type"] == "aibox_sophon_adapter"
assert wm.calls[-1]["subject"] == "aibox_sophon_adapter.board_ping"
assert published[-1][0] == "/board_ping/response"
@@ -182,7 +195,8 @@ def test_plugin_config_driven_collection():
assert "aibox_sophon_adapter" in by_type, "未发现 aibox_sophon_adapter 插件"
topics = by_type["aibox_sophon_adapter"]["mqtt_topics"]
assert any(
t["topic"] == "/board_ping" and t["worker"] == "aibox_sophon_adapter"
t["topic"] == "box/aibox_sophon/board_ping"
and t["worker"] == "aibox_sophon_adapter"
and t["subject"] == "aibox_sophon_adapter.board_ping"
for t in topics
), topics
@@ -194,7 +208,8 @@ def test_plugin_config_driven_collection():
async def main():
test_compile_topic()
await test_custom_routing()
await test_custom_routing_control_fields()
await test_envelope_meta_fields()
await test_flat_payload_dropped()
await test_non_json_dropped()
await test_board_ping_topic()
test_plugin_config_driven_collection()