2026-07-05 22:07:36 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-07-06 09:37:00 +08:00
|
|
|
import asyncio
|
2026-07-11 21:50:34 +08:00
|
|
|
import sys
|
2026-07-06 10:36:39 +08:00
|
|
|
import tempfile
|
2026-07-11 21:50:34 +08:00
|
|
|
import threading
|
|
|
|
|
import types
|
2026-07-05 22:07:36 +08:00
|
|
|
import unittest
|
2026-07-06 09:37:00 +08:00
|
|
|
from pathlib import Path
|
2026-07-11 21:50:34 +08:00
|
|
|
from unittest.mock import patch
|
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-10 01:25:15 +08:00
|
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
2026-07-11 21:50:34 +08:00
|
|
|
from database.db_helper import get_runtime_config_overrides, load_database_settings, set_runtime_config_overrides
|
2026-07-06 10:00:18 +08:00
|
|
|
from main import (
|
2026-07-11 21:50:34 +08:00
|
|
|
BRIDGE_PERMISSIONS,
|
2026-07-06 10:00:18 +08:00
|
|
|
Main,
|
|
|
|
|
PLUGIN_NAME,
|
2026-07-06 10:59:28 +08:00
|
|
|
apply_astrbot_config_overrides,
|
2026-07-06 10:00:18 +08:00
|
|
|
dispatch_bridge_request,
|
|
|
|
|
dispatch_web_request,
|
|
|
|
|
get_plugin_manifest,
|
|
|
|
|
get_runtime_health,
|
2026-07-06 10:36:39 +08:00
|
|
|
initialize_runtime_database,
|
2026-07-06 10:00:18 +08:00
|
|
|
register_bridge_api,
|
|
|
|
|
register_plugin,
|
2026-07-11 21:50:34 +08:00
|
|
|
_current_astrbot_web_request,
|
|
|
|
|
_request_context_from_astrbot_request,
|
|
|
|
|
_request_payload,
|
|
|
|
|
_request_query,
|
2026-07-06 10:00:18 +08:00
|
|
|
)
|
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"])
|
2026-07-11 21:50:34 +08:00
|
|
|
expected_methods = sorted({route.method for route in ROUTES})
|
|
|
|
|
self.assertEqual(len(runtime.web_apis), len(expected_methods))
|
|
|
|
|
self.assertEqual(result["routes_registered"], len(expected_methods))
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
{api["path"] for api in runtime.web_apis},
|
|
|
|
|
{f"/{PLUGIN_NAME}/<path:endpoint>"},
|
|
|
|
|
)
|
2026-07-06 10:00:18 +08:00
|
|
|
|
|
|
|
|
bootstrap_api = next(
|
2026-07-06 10:11:30 +08:00
|
|
|
api
|
|
|
|
|
for api in runtime.web_apis
|
2026-07-11 21:50:34 +08:00
|
|
|
if api["methods"] == ["GET"]
|
2026-07-06 10:00:18 +08:00
|
|
|
)
|
2026-07-11 21:50:34 +08:00
|
|
|
request = types.SimpleNamespace(method="GET", username="admin", client_host="127.0.0.1")
|
|
|
|
|
response = asyncio.run(bootstrap_api["handler"](request=request, endpoint="ui/bootstrap"))
|
2026-07-06 10:00:18 +08:00
|
|
|
|
|
|
|
|
self.assertEqual(response["status"], "ok")
|
|
|
|
|
self.assertEqual(response["data"]["plugin_name"], PLUGIN_NAME)
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
def test_register_bridge_api_supports_official_astrbot_signature_without_duplicate_paths(self):
|
|
|
|
|
class FakeRuntime:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.web_apis = []
|
|
|
|
|
|
|
|
|
|
def register_web_api(self, route, handler, methods, desc):
|
|
|
|
|
self.web_apis.append(
|
|
|
|
|
{
|
|
|
|
|
"route": route,
|
|
|
|
|
"handler": handler,
|
|
|
|
|
"methods": methods,
|
|
|
|
|
"desc": desc,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
runtime = FakeRuntime()
|
|
|
|
|
result = register_bridge_api(runtime)
|
|
|
|
|
|
|
|
|
|
expected_methods = sorted({route.method for route in ROUTES})
|
|
|
|
|
self.assertTrue(result["registered"])
|
|
|
|
|
self.assertEqual(result["errors"], [])
|
|
|
|
|
self.assertEqual(len(runtime.web_apis), len(expected_methods))
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
{api["route"] for api in runtime.web_apis},
|
|
|
|
|
{f"/{PLUGIN_NAME}/<path:endpoint>"},
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sorted(api["methods"][0] for api in runtime.web_apis),
|
|
|
|
|
expected_methods,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-06 10:00:18 +08:00
|
|
|
def test_dispatch_bridge_request_accepts_astrbot_plugin_path(self):
|
|
|
|
|
class FakeUrl:
|
|
|
|
|
path = "/api/plug/astrbot_plugin_imagestudio/ui/bootstrap"
|
|
|
|
|
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
method = "GET"
|
2026-07-11 21:50:34 +08:00
|
|
|
username = "admin"
|
2026-07-06 10:00:18 +08:00
|
|
|
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-11 21:50:34 +08:00
|
|
|
def test_modern_request_uses_authenticated_fields_not_identity_headers(self):
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
username = "real-admin"
|
|
|
|
|
user_id = "real-user-id"
|
|
|
|
|
client_host = "10.0.0.8"
|
|
|
|
|
headers = {
|
|
|
|
|
"x-username": "spoofed-admin",
|
|
|
|
|
"x-user-id": "spoofed-user-id",
|
|
|
|
|
"x-forwarded-for": "203.0.113.9",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx = _request_context_from_astrbot_request(FakeRequest())
|
|
|
|
|
|
|
|
|
|
self.assertEqual(ctx.username, "real-admin")
|
|
|
|
|
self.assertEqual(ctx.user_id, "real-user-id")
|
|
|
|
|
self.assertEqual(ctx.client_ip, "10.0.0.8")
|
|
|
|
|
self.assertEqual(ctx.permissions, BRIDGE_PERMISSIONS)
|
|
|
|
|
self.assertIn("can_retry_tasks", ctx.permissions)
|
|
|
|
|
self.assertNotIn("*", ctx.permissions)
|
|
|
|
|
|
|
|
|
|
class HeaderOnlyRequest:
|
|
|
|
|
headers = {"x-username": "spoofed-admin", "x-user-id": "spoofed-user-id"}
|
|
|
|
|
|
|
|
|
|
anonymous_ctx = _request_context_from_astrbot_request(HeaderOnlyRequest())
|
|
|
|
|
self.assertIsNone(anonymous_ctx.username)
|
|
|
|
|
self.assertIsNone(anonymous_ctx.user_id)
|
|
|
|
|
self.assertFalse(anonymous_ctx.is_authenticated)
|
|
|
|
|
self.assertFalse(anonymous_ctx.permissions)
|
|
|
|
|
|
|
|
|
|
def test_modern_request_reads_query_and_async_json(self):
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
method = "POST"
|
|
|
|
|
query = {"limit": "5", "status": "queued"}
|
|
|
|
|
|
|
|
|
|
async def json(self, default=None):
|
|
|
|
|
return {"name": "runtime payload"}
|
|
|
|
|
|
|
|
|
|
request = FakeRequest()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(_request_query(request), request.query)
|
|
|
|
|
self.assertEqual(asyncio.run(_request_payload(request)), {"name": "runtime payload"})
|
|
|
|
|
|
|
|
|
|
def test_legacy_quart_request_and_g_are_supported(self):
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
method = "GET"
|
|
|
|
|
args = {"limit": "3"}
|
|
|
|
|
remote_addr = "127.0.0.9"
|
|
|
|
|
|
|
|
|
|
quart = types.ModuleType("quart")
|
|
|
|
|
quart.request = FakeRequest()
|
|
|
|
|
quart.g = types.SimpleNamespace(username="legacy-admin")
|
|
|
|
|
|
|
|
|
|
with patch.dict(sys.modules, {"quart": quart}):
|
|
|
|
|
current = _current_astrbot_web_request()
|
|
|
|
|
ctx = _request_context_from_astrbot_request(current)
|
|
|
|
|
|
|
|
|
|
self.assertIs(current, quart.request)
|
|
|
|
|
self.assertEqual(ctx.username, "legacy-admin")
|
|
|
|
|
self.assertEqual(ctx.user_id, "legacy-admin")
|
|
|
|
|
self.assertEqual(ctx.client_ip, "127.0.0.9")
|
|
|
|
|
self.assertEqual(_request_query(current), {"limit": "3"})
|
|
|
|
|
|
|
|
|
|
def test_post_method_override_envelope_dispatches_original_method_and_body(self):
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
method = "POST"
|
|
|
|
|
username = "admin"
|
|
|
|
|
client_host = "127.0.0.1"
|
|
|
|
|
query = {"force": "true"}
|
|
|
|
|
|
|
|
|
|
async def json(self, default=None):
|
|
|
|
|
return {
|
|
|
|
|
"__imagestudio_method": "PUT",
|
|
|
|
|
"__imagestudio_body": {"name": "updated"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routed = {
|
|
|
|
|
"ok": True,
|
|
|
|
|
"data": {"updated": True},
|
|
|
|
|
"warnings": [],
|
|
|
|
|
"request_id": "req_override",
|
|
|
|
|
}
|
|
|
|
|
with patch("main.dispatch_web_request", return_value=routed) as dispatch:
|
|
|
|
|
response = asyncio.run(dispatch_bridge_request("profiles/profile_1", request=FakeRequest()))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response["status"], "ok")
|
|
|
|
|
args, kwargs = dispatch.call_args
|
|
|
|
|
self.assertEqual(args[1], "PUT")
|
|
|
|
|
self.assertEqual(args[2], "profiles/profile_1")
|
|
|
|
|
self.assertEqual(kwargs["query"], {"force": "true"})
|
|
|
|
|
self.assertEqual(kwargs["payload"], {"name": "updated"})
|
|
|
|
|
|
|
|
|
|
def test_dispatch_bridge_request_offloads_sync_business_dispatch(self):
|
|
|
|
|
event_loop_thread = threading.get_ident()
|
|
|
|
|
dispatch_threads = []
|
|
|
|
|
|
|
|
|
|
def routed(*args, **kwargs):
|
|
|
|
|
dispatch_threads.append(threading.get_ident())
|
|
|
|
|
return {
|
|
|
|
|
"ok": True,
|
|
|
|
|
"data": {"threaded": True},
|
|
|
|
|
"warnings": [],
|
|
|
|
|
"request_id": "req_threaded",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with patch("main.dispatch_web_request", side_effect=routed) as dispatch:
|
|
|
|
|
response = asyncio.run(
|
|
|
|
|
dispatch_bridge_request(
|
|
|
|
|
"profiles/profile_1",
|
|
|
|
|
method="GET",
|
|
|
|
|
query={"limit": "1"},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response["status"], "ok")
|
|
|
|
|
self.assertEqual(response["data"], {"threaded": True})
|
|
|
|
|
dispatch.assert_called_once()
|
|
|
|
|
self.assertEqual(len(dispatch_threads), 1)
|
|
|
|
|
self.assertNotEqual(dispatch_threads[0], event_loop_thread)
|
|
|
|
|
|
|
|
|
|
def test_method_override_is_ignored_for_non_post_requests(self):
|
|
|
|
|
envelope = {
|
|
|
|
|
"__imagestudio_method": "DELETE",
|
|
|
|
|
"__imagestudio_body": {},
|
|
|
|
|
}
|
|
|
|
|
routed = {
|
|
|
|
|
"ok": True,
|
|
|
|
|
"data": {},
|
|
|
|
|
"warnings": [],
|
|
|
|
|
"request_id": "req_no_override",
|
|
|
|
|
}
|
|
|
|
|
with patch("main.dispatch_web_request", return_value=routed) as dispatch:
|
|
|
|
|
asyncio.run(dispatch_bridge_request("profiles/profile_1", method="PUT", payload=envelope))
|
|
|
|
|
|
|
|
|
|
args, kwargs = dispatch.call_args
|
|
|
|
|
self.assertEqual(args[1], "PUT")
|
|
|
|
|
self.assertEqual(kwargs["payload"], envelope)
|
|
|
|
|
|
|
|
|
|
def test_delete_method_override_accepts_empty_original_body(self):
|
|
|
|
|
routed = {
|
|
|
|
|
"ok": True,
|
|
|
|
|
"data": {},
|
|
|
|
|
"warnings": [],
|
|
|
|
|
"request_id": "req_delete_override",
|
|
|
|
|
}
|
|
|
|
|
envelope = {
|
|
|
|
|
"__imagestudio_method": "DELETE",
|
|
|
|
|
"__imagestudio_body": {},
|
|
|
|
|
}
|
|
|
|
|
with patch("main.dispatch_web_request", return_value=routed) as dispatch:
|
|
|
|
|
asyncio.run(
|
|
|
|
|
dispatch_bridge_request(
|
|
|
|
|
"profiles/profile_1",
|
|
|
|
|
method="POST",
|
|
|
|
|
payload=envelope,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
args, kwargs = dispatch.call_args
|
|
|
|
|
self.assertEqual(args[1], "DELETE")
|
|
|
|
|
self.assertEqual(kwargs["payload"], {})
|
|
|
|
|
|
|
|
|
|
def test_media_fetch_bridge_returns_binary_modern_response(self):
|
|
|
|
|
class FakeResponse:
|
|
|
|
|
def __init__(self, *, content, media_type, headers):
|
|
|
|
|
self.body = content
|
|
|
|
|
self.media_type = media_type
|
|
|
|
|
self.headers = headers
|
|
|
|
|
|
|
|
|
|
astrbot = types.ModuleType("astrbot")
|
|
|
|
|
astrbot.__path__ = []
|
|
|
|
|
astrbot_api = types.ModuleType("astrbot.api")
|
|
|
|
|
astrbot_api.__path__ = []
|
|
|
|
|
astrbot_web = types.ModuleType("astrbot.api.web")
|
|
|
|
|
astrbot_web.request = object()
|
|
|
|
|
astrbot.api = astrbot_api
|
|
|
|
|
astrbot_api.web = astrbot_web
|
|
|
|
|
starlette = types.ModuleType("starlette")
|
|
|
|
|
starlette.__path__ = []
|
|
|
|
|
starlette_responses = types.ModuleType("starlette.responses")
|
|
|
|
|
starlette_responses.Response = FakeResponse
|
|
|
|
|
routed = {
|
|
|
|
|
"ok": True,
|
|
|
|
|
"data": {
|
|
|
|
|
"body_base64": "MTIzNDU",
|
|
|
|
|
"content_type": "image/png",
|
|
|
|
|
"cache_control": "private, max-age=300",
|
|
|
|
|
},
|
|
|
|
|
"warnings": [],
|
|
|
|
|
"request_id": "req_media",
|
|
|
|
|
}
|
|
|
|
|
modules = {
|
|
|
|
|
"astrbot": astrbot,
|
|
|
|
|
"astrbot.api": astrbot_api,
|
|
|
|
|
"astrbot.api.web": astrbot_web,
|
|
|
|
|
"starlette": starlette,
|
|
|
|
|
"starlette.responses": starlette_responses,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with patch.dict(sys.modules, modules), patch("main.dispatch_web_request", return_value=routed):
|
|
|
|
|
response = asyncio.run(dispatch_bridge_request("media/fetch", method="GET", query={"token": "x"}))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.body, b"12345")
|
|
|
|
|
self.assertEqual(response.media_type, "image/png")
|
|
|
|
|
self.assertEqual(response.headers["Cache-Control"], "private, max-age=300")
|
|
|
|
|
|
|
|
|
|
def test_media_fetch_uses_host_native_file_response_before_base64_dispatch(self):
|
|
|
|
|
class FakeFileResponse:
|
|
|
|
|
def __init__(self, path, *, content_type, headers):
|
|
|
|
|
self.path = Path(path)
|
|
|
|
|
self.content_type = content_type
|
|
|
|
|
self.headers = headers
|
|
|
|
|
|
|
|
|
|
astrbot = types.ModuleType("astrbot")
|
|
|
|
|
astrbot.__path__ = []
|
|
|
|
|
astrbot_api = types.ModuleType("astrbot.api")
|
|
|
|
|
astrbot_api.__path__ = []
|
|
|
|
|
astrbot_web = types.ModuleType("astrbot.api.web")
|
|
|
|
|
astrbot_web.file_response = lambda path, *, content_type, headers: FakeFileResponse(
|
|
|
|
|
path,
|
|
|
|
|
content_type=content_type,
|
|
|
|
|
headers=headers,
|
|
|
|
|
)
|
|
|
|
|
astrbot.api = astrbot_api
|
|
|
|
|
astrbot_api.web = astrbot_web
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
|
|
|
media_path = Path(tmp) / "image.png"
|
|
|
|
|
media_path.write_bytes(b"png")
|
|
|
|
|
modules = {"astrbot": astrbot, "astrbot.api": astrbot_api, "astrbot.api.web": astrbot_web}
|
|
|
|
|
with patch.dict(sys.modules, modules), patch(
|
|
|
|
|
"main.MediaProxy.resolve_file",
|
|
|
|
|
return_value=(media_path, {"content_type": "image/png"}),
|
|
|
|
|
), patch("main.dispatch_web_request") as dispatch:
|
|
|
|
|
response = asyncio.run(dispatch_bridge_request("media/fetch", method="GET", query={"token": "signed"}))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.path, media_path)
|
|
|
|
|
self.assertEqual(response.content_type, "image/png")
|
|
|
|
|
self.assertEqual(response.headers["Cache-Control"], "private, max-age=300")
|
|
|
|
|
dispatch.assert_not_called()
|
|
|
|
|
|
|
|
|
|
def test_media_fetch_bridge_returns_binary_legacy_quart_response(self):
|
|
|
|
|
class FakeQuartResponse:
|
|
|
|
|
def __init__(self, body, *, content_type, headers):
|
|
|
|
|
self.body = body
|
|
|
|
|
self.content_type = content_type
|
|
|
|
|
self.headers = headers
|
|
|
|
|
|
|
|
|
|
quart = types.ModuleType("quart")
|
|
|
|
|
quart.Response = FakeQuartResponse
|
|
|
|
|
quart.g = types.SimpleNamespace(username="legacy-admin")
|
|
|
|
|
routed = {
|
|
|
|
|
"ok": True,
|
|
|
|
|
"data": {
|
|
|
|
|
"body_base64": "MTIzNDU",
|
|
|
|
|
"content_type": "image/webp",
|
|
|
|
|
"cache_control": "private, max-age=300",
|
|
|
|
|
},
|
|
|
|
|
"warnings": [],
|
|
|
|
|
"request_id": "req_media_legacy",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with patch.dict(sys.modules, {"quart": quart, "astrbot.api.web": None}), patch(
|
|
|
|
|
"main.dispatch_web_request", return_value=routed
|
|
|
|
|
):
|
|
|
|
|
response = asyncio.run(dispatch_bridge_request("media/fetch", method="GET", query={"token": "x"}))
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.body, b"12345")
|
|
|
|
|
self.assertEqual(response.content_type, "image/webp")
|
|
|
|
|
self.assertEqual(response.headers["Cache-Control"], "private, max-age=300")
|
|
|
|
|
|
2026-07-06 10:36:39 +08:00
|
|
|
def test_initialize_runtime_database_runs_migrations(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
|
|
|
db_path = Path(tmp) / "imagestudio.db"
|
2026-07-10 01:25:15 +08:00
|
|
|
settings = DatabaseSettings(sqlite=SqliteSettings(path=str(db_path)))
|
|
|
|
|
result = initialize_runtime_database(settings)
|
2026-07-06 10:36:39 +08:00
|
|
|
|
|
|
|
|
self.assertTrue(result["initialized"])
|
|
|
|
|
self.assertEqual(result["backend"], "sqlite")
|
2026-07-10 01:25:15 +08:00
|
|
|
self.assertGreater(result["seed"]["material_categories"]["total"], 0)
|
2026-07-06 10:36:39 +08:00
|
|
|
self.assertTrue(db_path.exists())
|
2026-07-10 01:25:15 +08:00
|
|
|
database = Database(settings)
|
|
|
|
|
conn = database.connect()
|
|
|
|
|
try:
|
|
|
|
|
row = conn.execute(
|
|
|
|
|
"""
|
|
|
|
|
SELECT parent_id, category_type
|
|
|
|
|
FROM material_categories
|
|
|
|
|
WHERE category_id = 'cat_imagestudio_accessory_头部'
|
|
|
|
|
"""
|
|
|
|
|
).fetchone()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
self.assertEqual(row["parent_id"], "cat_imagestudio_配饰")
|
|
|
|
|
self.assertEqual(row["category_type"], "accessory_group")
|
2026-07-06 10:36:39 +08:00
|
|
|
|
2026-07-06 10:59:28 +08:00
|
|
|
def test_astrbot_config_overrides_default_database_settings(self):
|
|
|
|
|
try:
|
|
|
|
|
result = apply_astrbot_config_overrides(
|
|
|
|
|
{
|
|
|
|
|
"database_backend": "postgresql",
|
|
|
|
|
"postgresql_enabled": True,
|
|
|
|
|
"postgresql_host": "127.0.0.1",
|
|
|
|
|
"postgresql_port": "5432",
|
|
|
|
|
"postgresql_database": "imagestudio_runtime",
|
|
|
|
|
"postgresql_user": "imagestudio",
|
|
|
|
|
"enable_private_companion_integration": True,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
settings = load_database_settings()
|
|
|
|
|
|
|
|
|
|
self.assertTrue(result["applied"])
|
|
|
|
|
self.assertIn("database.postgresql.host", result["keys"])
|
|
|
|
|
self.assertEqual(settings.selected_backend, "postgresql")
|
|
|
|
|
self.assertEqual(settings.postgresql.database, "imagestudio_runtime")
|
|
|
|
|
finally:
|
|
|
|
|
set_runtime_config_overrides({})
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
def test_astrbot_flat_config_maps_material_vision_provider(self):
|
|
|
|
|
try:
|
|
|
|
|
result = apply_astrbot_config_overrides(
|
|
|
|
|
{
|
|
|
|
|
"vision_provider_id": "vision_site",
|
|
|
|
|
"vision_provider_base_url": "https://vision.example",
|
|
|
|
|
"vision_provider_endpoint": "/v1/chat/completions",
|
|
|
|
|
"vision_provider_model": "vision-model",
|
|
|
|
|
"vision_provider_route": "material_custom",
|
|
|
|
|
"vision_provider_api_key_env_var": "IMAGESTUDIO_VISION_KEY",
|
|
|
|
|
"vision_provider_timeout_seconds": "75",
|
|
|
|
|
"vision_provider_enabled": True,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
overrides = get_runtime_config_overrides()
|
|
|
|
|
|
|
|
|
|
self.assertIn("vision.provider.model_id", result["keys"])
|
|
|
|
|
self.assertEqual(overrides["vision"]["default_route"], "material_custom")
|
|
|
|
|
self.assertEqual(overrides["vision"]["provider"]["route_name"], "material_custom")
|
|
|
|
|
self.assertEqual(overrides["vision"]["provider"]["vision_endpoint"], "/v1/chat/completions")
|
|
|
|
|
self.assertEqual(overrides["vision"]["provider"]["timeout_seconds"], 75)
|
|
|
|
|
finally:
|
|
|
|
|
set_runtime_config_overrides({})
|
|
|
|
|
|
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()
|