112 行
4.5 KiB
Python
112 行
4.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from adapters.astrbot_command_adapter import ChatCommandContext
|
||
|
|
from backend.services.chat_workflow_service import ChatWorkflowService
|
||
|
|
|
||
|
|
|
||
|
|
class _Dispatcher:
|
||
|
|
def __init__(self) -> None:
|
||
|
|
self.calls: list[tuple[str, str, dict]] = []
|
||
|
|
|
||
|
|
def __call__(self, _ctx, method: str, endpoint: str, **kwargs):
|
||
|
|
self.calls.append((method, endpoint, kwargs))
|
||
|
|
data = self._data(method, endpoint, kwargs)
|
||
|
|
return {"ok": True, "data": data}
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _data(method: str, endpoint: str, kwargs: dict):
|
||
|
|
if endpoint == "nodes/node-head":
|
||
|
|
return {
|
||
|
|
"node_id": "node-head",
|
||
|
|
"workflow_id": "workflow-1",
|
||
|
|
"node_key": "headshot",
|
||
|
|
"title": "Headshot",
|
||
|
|
"branch_name": "main",
|
||
|
|
"sort_order": 40,
|
||
|
|
}
|
||
|
|
if endpoint == "nodes/node-body":
|
||
|
|
return {
|
||
|
|
"node_id": "node-body",
|
||
|
|
"workflow_id": "workflow-1",
|
||
|
|
"node_key": "full_body",
|
||
|
|
"title": "Full body",
|
||
|
|
"branch_name": "main",
|
||
|
|
"sort_order": 60,
|
||
|
|
}
|
||
|
|
if endpoint == "workflows/workflow-1":
|
||
|
|
return {
|
||
|
|
"workflow": {
|
||
|
|
"workflow_id": "workflow-1",
|
||
|
|
"profile_id": "profile-1",
|
||
|
|
"current_branch": "main",
|
||
|
|
},
|
||
|
|
"nodes": [
|
||
|
|
{"node_id": "node-head", "node_key": "headshot", "title": "Headshot", "branch_name": "main", "sort_order": 40},
|
||
|
|
{"node_id": "node-next", "node_key": "half_body", "title": "Half body", "branch_name": "main", "sort_order": 50},
|
||
|
|
{"node_id": "node-body", "node_key": "full_body", "title": "Full body", "branch_name": "main", "sort_order": 60},
|
||
|
|
],
|
||
|
|
}
|
||
|
|
if endpoint == "images/image-1":
|
||
|
|
return {
|
||
|
|
"image_id": "image-1",
|
||
|
|
"profile_id": "profile-1",
|
||
|
|
"workflow_id": "workflow-1",
|
||
|
|
"image_uri": "storage://images/image-1.png",
|
||
|
|
}
|
||
|
|
if endpoint == "profiles/profile-1" and method == "GET":
|
||
|
|
return {"profile_id": "profile-1", "owner_user_id": "user-a"}
|
||
|
|
if endpoint.endswith("/select-image"):
|
||
|
|
return {"image_id": kwargs["payload"]["image_id"]}
|
||
|
|
if endpoint.endswith("/adopt-image"):
|
||
|
|
return {"history_id": "history-1", "image_id": kwargs["payload"]["image_id"]}
|
||
|
|
if endpoint == "profiles/profile-1" and method == "PUT":
|
||
|
|
return {"profile_id": "profile-1", **kwargs["payload"]}
|
||
|
|
raise AssertionError(f"unexpected request: {method} {endpoint}")
|
||
|
|
|
||
|
|
|
||
|
|
def _chat() -> ChatCommandContext:
|
||
|
|
return ChatCommandContext(
|
||
|
|
platform="qq",
|
||
|
|
user_id="user-a",
|
||
|
|
group_id="group-a",
|
||
|
|
message_origin="origin-a",
|
||
|
|
message_id="message-a",
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _service(dispatcher: _Dispatcher) -> ChatWorkflowService:
|
||
|
|
return ChatWorkflowService(
|
||
|
|
dispatcher,
|
||
|
|
config_loader=lambda: {
|
||
|
|
"chat_access": {
|
||
|
|
"group_allowlist": ["group-a"],
|
||
|
|
"user_allowlist": ["user-a"],
|
||
|
|
}
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_select_is_temporary_and_does_not_update_avatar_or_adopt() -> None:
|
||
|
|
dispatcher = _Dispatcher()
|
||
|
|
|
||
|
|
result = _service(dispatcher).select_node_image("node-head", "image-1", _chat())
|
||
|
|
|
||
|
|
assert result["confirmation_required"] is True
|
||
|
|
assert any(endpoint.endswith("/select-image") for _, endpoint, _ in dispatcher.calls)
|
||
|
|
assert not any(endpoint.endswith("/adopt-image") for _, endpoint, _ in dispatcher.calls)
|
||
|
|
assert not any(method == "PUT" and endpoint.startswith("profiles/") for method, endpoint, _ in dispatcher.calls)
|
||
|
|
|
||
|
|
|
||
|
|
def test_confirm_adopts_and_only_headshot_updates_avatar() -> None:
|
||
|
|
head_dispatcher = _Dispatcher()
|
||
|
|
head = _service(head_dispatcher).confirm_node_image("node-head", "image-1", _chat())
|
||
|
|
|
||
|
|
assert head["next_node"]["node_id"] == "node-next"
|
||
|
|
adopt_call = next(call for call in head_dispatcher.calls if call[1].endswith("/adopt-image"))
|
||
|
|
assert adopt_call[2]["payload"]["metadata"]["next_backend_node_id"] == "node-next"
|
||
|
|
assert any(method == "PUT" and endpoint == "profiles/profile-1" for method, endpoint, _ in head_dispatcher.calls)
|
||
|
|
|
||
|
|
body_dispatcher = _Dispatcher()
|
||
|
|
_service(body_dispatcher).confirm_node_image("node-body", "image-1", _chat())
|
||
|
|
assert not any(method == "PUT" and endpoint.startswith("profiles/") for method, endpoint, _ in body_dispatcher.calls)
|