79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""
|
|
backend/tests/test_shipping_chat_action_handlers.py
|
|
|
|
覆盖货代 Chat Action Handler 与业务 workflow 的集成(不依赖 Dify)。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.models import Agent, User
|
|
from app.services.chat.action_schema import TurnInput
|
|
from app.services.chat.turn_context import TurnContext
|
|
from app.services.shipping_notice.chat_action_handlers import ShippingGenerateExcelAction
|
|
|
|
|
|
def _create_task(user_id: int) -> str:
|
|
from app.models import ShippingBatch, ShippingTask
|
|
|
|
batch = ShippingBatch(batch_no="BATCH-HANDLER-001", user_id=user_id, file_count=1)
|
|
task = ShippingTask(
|
|
task_no="SN-20260706-EFGH5678",
|
|
batch=batch,
|
|
user_id=user_id,
|
|
source_type="manual",
|
|
process_status="matched",
|
|
match_status="ok",
|
|
)
|
|
db.session.add_all([batch, task])
|
|
db.session.commit()
|
|
return task.task_no
|
|
|
|
|
|
def test_generate_excel_handler_yields_ui_state_and_message(app, monkeypatch):
|
|
user = User(username="handler_user", email="handler_user@example.com", role="admin", status="active")
|
|
user.set_password("password")
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
task_no = _create_task(user.id)
|
|
agent = Agent(
|
|
name="货代发货通知 Agent",
|
|
description="t",
|
|
dify_api_key="k",
|
|
api_base_url="http://example.com",
|
|
agent_code="shipping_notice",
|
|
runtime_provider="hybrid",
|
|
conversation_mode="task_chat",
|
|
)
|
|
db.session.add(agent)
|
|
db.session.commit()
|
|
|
|
monkeypatch.setattr(
|
|
"app.services.shipping_notice.chat_action_handlers.shipping_tools.generate_excel",
|
|
lambda payload: {"status": "ok", "message": "Excel 已生成。", "task_id": task_no, "data": {}},
|
|
)
|
|
|
|
ctx = TurnContext(
|
|
turn_id="turn-001",
|
|
run_id="turn-001",
|
|
session_id="sess-001",
|
|
request_id="req-001",
|
|
user=user,
|
|
agent=agent,
|
|
input=TurnInput(
|
|
type="action",
|
|
display_text="生成发货 Excel",
|
|
action_type="shipping.generate_excel",
|
|
payload={"task_no": task_no},
|
|
),
|
|
refs={"task_no": task_no},
|
|
)
|
|
handler = ShippingGenerateExcelAction()
|
|
events = list(handler.stream(ctx))
|
|
event_names = [event.event for event in events]
|
|
assert "step.started" in event_names
|
|
assert "ui_state.updated" in event_names
|
|
assert "message.completed" in event_names
|
|
completed = next(item for item in events if item.event == "message.completed")
|
|
assert "Excel 已生成" in str(completed.data.get("content") or "")
|