53 行
2.5 KiB
Python
53 行
2.5 KiB
Python
|
|
from adapters.astrbot_command_adapter import ChatCommandContext
|
||
|
|
from backend.dto.chat_command_dto import SingleImageCommand
|
||
|
|
from backend.services.chat_workflow_service import ChatWorkflowService, _quick_questionnaire
|
||
|
|
|
||
|
|
|
||
|
|
def test_single_image_orchestration_reuses_existing_routes():
|
||
|
|
calls = []
|
||
|
|
|
||
|
|
def dispatch(ctx, method, path, query=None, payload=None):
|
||
|
|
calls.append((method, path, query, payload))
|
||
|
|
if method == "GET" and path == "jobs":
|
||
|
|
return {"ok": True, "data": {"items": []}}
|
||
|
|
if method == "GET" and path == "profiles":
|
||
|
|
return {"ok": True, "data": {"items": []}}
|
||
|
|
if method == "POST" and path == "profiles":
|
||
|
|
return {"ok": True, "data": {"profile_id": "profile-chat", "name": "chat"}}
|
||
|
|
if path.endswith("/workflows"):
|
||
|
|
return {"ok": True, "data": {"workflow": {"workflow_id": "wf-1"}, "nodes": [{"node_id": "node-1"}]}}
|
||
|
|
if path.endswith("/start"):
|
||
|
|
return {"ok": True, "data": {"status": "running"}}
|
||
|
|
if path.endswith("/generate"):
|
||
|
|
return {"ok": True, "data": {"task_id": "task-1", "images": [{"image_id": "img-1"}]}}
|
||
|
|
if path == "images/img-1":
|
||
|
|
return {"ok": True, "data": {"image_id": "img-1", "image_url": "/media/img-1"}}
|
||
|
|
raise AssertionError((method, path))
|
||
|
|
|
||
|
|
class Guard:
|
||
|
|
def check_generation(self, *args, **kwargs): pass
|
||
|
|
def record(self, *args, **kwargs): pass
|
||
|
|
|
||
|
|
service = ChatWorkflowService(
|
||
|
|
dispatch,
|
||
|
|
lambda: {"chat_access": {"group_allowlist": ["group-1"], "user_allowlist": ["user-1"]}},
|
||
|
|
guard=Guard(),
|
||
|
|
)
|
||
|
|
context = ChatCommandContext("user-1", "group-1", "qq:group:1", "aiocqhttp", "msg-1")
|
||
|
|
result = service.submit_single_image(SingleImageCommand("portrait"), context)
|
||
|
|
assert result["generation"]["task_id"] == "task-1"
|
||
|
|
assert result["images"][0]["image_url"] == "/media/img-1"
|
||
|
|
generate = next(item for item in calls if item[1] == "nodes/node-1/generate")
|
||
|
|
assert generate[3]["size"] == "1920x1080"
|
||
|
|
assert generate[3]["idempotency_key"].startswith("chat_")
|
||
|
|
first_key = generate[3]["idempotency_key"]
|
||
|
|
assert first_key == "chat_" + __import__("hashlib").sha256(b"msg-1").hexdigest()[:32]
|
||
|
|
|
||
|
|
|
||
|
|
def test_quick_questionnaire_extracts_character_fields():
|
||
|
|
result = _quick_questionnaire("米卡 银发蓝眼的冷静少女,穿学院制服,留着长发")
|
||
|
|
assert result["name"] == "米卡"
|
||
|
|
assert "冷静" in result["personality"]
|
||
|
|
assert "制服" in result["clothing_keywords"]
|
||
|
|
assert result["hairstyle"] == "长发"
|