2026-07-05 22:07:36 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-07-06 09:37:00 +08:00
|
|
|
import asyncio
|
2026-07-05 22:07:36 +08:00
|
|
|
import unittest
|
2026-07-06 09:37:00 +08:00
|
|
|
from pathlib import Path
|
2026-07-05 22:07:36 +08:00
|
|
|
|
2026-07-06 04:00:59 +08:00
|
|
|
from backend.route_registry import ROUTES
|
2026-07-05 22:07:36 +08:00
|
|
|
from core.identity import RequestContext
|
2026-07-06 10:00:18 +08:00
|
|
|
from main import (
|
|
|
|
|
Main,
|
|
|
|
|
PLUGIN_NAME,
|
|
|
|
|
dispatch_bridge_request,
|
|
|
|
|
dispatch_web_request,
|
|
|
|
|
get_plugin_manifest,
|
|
|
|
|
get_runtime_health,
|
|
|
|
|
register_bridge_api,
|
|
|
|
|
register_plugin,
|
|
|
|
|
)
|
2026-07-05 22:07:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class PluginEntrypointTests(unittest.TestCase):
|
|
|
|
|
def test_plugin_manifest_exposes_astrbot_mounts(self):
|
|
|
|
|
manifest = get_plugin_manifest()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(manifest["plugin_name"], PLUGIN_NAME)
|
|
|
|
|
self.assertTrue(manifest["api_base"].endswith("astrbot_plugin_imagestudio"))
|
2026-07-06 04:21:50 +08:00
|
|
|
self.assertTrue(manifest["contract_url"].endswith("/contract"))
|
2026-07-05 22:07:36 +08:00
|
|
|
self.assertIn("web", manifest)
|
|
|
|
|
self.assertTrue(any(route["endpoint"] == "dashboard/summary" for route in manifest["routes"]))
|
2026-07-06 04:00:59 +08:00
|
|
|
self.assertTrue(manifest["runtime"]["health"]["ok"])
|
|
|
|
|
self.assertEqual(manifest["health"]["route_count"], len(ROUTES))
|
2026-07-05 22:07:36 +08:00
|
|
|
|
|
|
|
|
def test_dispatch_web_request_delegates_to_adapter(self):
|
|
|
|
|
ctx = RequestContext.from_astrbot(username="admin", user_id="user_001")
|
|
|
|
|
|
|
|
|
|
response = dispatch_web_request(
|
|
|
|
|
ctx,
|
|
|
|
|
"GET",
|
|
|
|
|
"/api/v1/plugins/extensions/astrbot_plugin_imagestudio/ui/bootstrap",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertTrue(response["ok"])
|
|
|
|
|
self.assertEqual(response["data"]["plugin_name"], PLUGIN_NAME)
|
|
|
|
|
|
2026-07-06 04:00:59 +08:00
|
|
|
def test_register_plugin_delegates_to_runtime_registration(self):
|
|
|
|
|
class FakeRuntime:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.routes = []
|
|
|
|
|
self.mounts = []
|
|
|
|
|
|
|
|
|
|
def register_extension_route(self, **kwargs):
|
|
|
|
|
self.routes.append(kwargs)
|
|
|
|
|
|
|
|
|
|
def register_web_mount(self, **kwargs):
|
|
|
|
|
self.mounts.append(kwargs)
|
|
|
|
|
|
|
|
|
|
runtime = FakeRuntime()
|
|
|
|
|
result = register_plugin(runtime)
|
|
|
|
|
|
|
|
|
|
self.assertTrue(result["registered"])
|
|
|
|
|
self.assertEqual(len(runtime.routes), len(ROUTES))
|
|
|
|
|
self.assertEqual(len(runtime.mounts), 1)
|
2026-07-06 10:00:18 +08:00
|
|
|
self.assertFalse(result["bridge_api"]["registered"])
|
2026-07-06 04:00:59 +08:00
|
|
|
self.assertTrue(get_runtime_health()["ok"])
|
|
|
|
|
|
2026-07-06 10:00:18 +08:00
|
|
|
def test_register_bridge_api_registers_astrbot_web_routes(self):
|
|
|
|
|
class FakeRuntime:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.web_apis = []
|
|
|
|
|
|
|
|
|
|
def register_web_api(self, path, handler, methods=None):
|
|
|
|
|
self.web_apis.append({"path": path, "handler": handler, "methods": methods or []})
|
|
|
|
|
|
|
|
|
|
runtime = FakeRuntime()
|
|
|
|
|
result = register_bridge_api(runtime)
|
|
|
|
|
|
|
|
|
|
self.assertTrue(result["registered"])
|
|
|
|
|
self.assertGreater(len(runtime.web_apis), len(ROUTES))
|
|
|
|
|
self.assertTrue(any(api["path"] == "/ui/bootstrap" for api in runtime.web_apis))
|
|
|
|
|
|
|
|
|
|
bootstrap_api = next(
|
|
|
|
|
api for api in runtime.web_apis if api["path"] == "/ui/bootstrap" and api["methods"] == ["GET"]
|
|
|
|
|
)
|
|
|
|
|
response = asyncio.run(bootstrap_api["handler"]())
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response["status"], "ok")
|
|
|
|
|
self.assertEqual(response["data"]["plugin_name"], PLUGIN_NAME)
|
|
|
|
|
|
|
|
|
|
def test_dispatch_bridge_request_accepts_astrbot_plugin_path(self):
|
|
|
|
|
class FakeUrl:
|
|
|
|
|
path = "/api/plug/astrbot_plugin_imagestudio/ui/bootstrap"
|
|
|
|
|
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
method = "GET"
|
|
|
|
|
url = FakeUrl()
|
|
|
|
|
query_params = {}
|
|
|
|
|
|
|
|
|
|
response = asyncio.run(dispatch_bridge_request("", request=FakeRequest()))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response["status"], "ok")
|
|
|
|
|
self.assertEqual(response["data"]["plugin_name"], PLUGIN_NAME)
|
|
|
|
|
|
2026-07-06 09:26:29 +08:00
|
|
|
def test_astrbot_main_class_exposes_manifest(self):
|
|
|
|
|
plugin = Main(context=object())
|
|
|
|
|
|
|
|
|
|
manifest = plugin.get_plugin_manifest()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(manifest["plugin_name"], PLUGIN_NAME)
|
|
|
|
|
self.assertTrue(manifest["runtime"]["health"]["ok"])
|
|
|
|
|
|
2026-07-06 09:37:00 +08:00
|
|
|
def test_astrbot_main_initialize_registers_routes_when_supported(self):
|
|
|
|
|
class FakeRuntime:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.routes = []
|
|
|
|
|
self.mounts = []
|
|
|
|
|
|
|
|
|
|
def register_extension_route(self, **kwargs):
|
|
|
|
|
self.routes.append(kwargs)
|
|
|
|
|
|
|
|
|
|
def register_web_mount(self, **kwargs):
|
|
|
|
|
self.mounts.append(kwargs)
|
|
|
|
|
|
|
|
|
|
runtime = FakeRuntime()
|
|
|
|
|
plugin = Main(context=runtime)
|
|
|
|
|
|
|
|
|
|
asyncio.run(plugin.initialize())
|
|
|
|
|
|
|
|
|
|
self.assertTrue(plugin.registration_result["registered"])
|
|
|
|
|
self.assertEqual(len(runtime.routes), len(ROUTES))
|
|
|
|
|
self.assertEqual(len(runtime.mounts), 1)
|
2026-07-06 10:00:18 +08:00
|
|
|
self.assertFalse(plugin.registration_result["bridge_api"]["registered"])
|
2026-07-06 09:37:00 +08:00
|
|
|
|
|
|
|
|
def test_plugin_page_uses_relative_assets(self):
|
|
|
|
|
page = Path(__file__).resolve().parents[1] / "pages" / "imagestudio" / "index.html"
|
|
|
|
|
html = page.read_text(encoding="utf-8")
|
|
|
|
|
|
2026-07-06 09:45:13 +08:00
|
|
|
self.assertRegex(html, r'src="\.?/assets/')
|
|
|
|
|
self.assertRegex(html, r'href="\.?/assets/')
|
2026-07-06 09:37:00 +08:00
|
|
|
self.assertNotIn('src="/assets/', html)
|
|
|
|
|
self.assertNotIn('href="/assets/', html)
|
|
|
|
|
|
2026-07-05 22:07:36 +08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|