141 lines
4.7 KiB
Python
141 lines
4.7 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 (
|
|
ShippingCreateBatchFromIntakesAction,
|
|
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 "")
|
|
|
|
|
|
def test_create_batch_from_intakes_handler_forwards_candidate_nos(app, monkeypatch):
|
|
"""Chat Turn Candidate 建批必须把选择集合透传后端,且不得引用未定义局部变量。"""
|
|
user = User(username="candidate_handler", email="candidate-handler@example.com", role="user", status="active")
|
|
user.set_password("password")
|
|
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_all([user, agent])
|
|
db.session.commit()
|
|
captured: dict = {}
|
|
|
|
def _fake_create(**kwargs):
|
|
captured.update(kwargs)
|
|
return {
|
|
"status": "ok",
|
|
"batch_no": None,
|
|
"message": "已打开已有任务,未创建新批次",
|
|
"opened_existing": [{"candidate_no": "SNC-1", "task_no": "SN-EXISTING"}],
|
|
"data": {"focus_task": {"task_no": "SN-EXISTING"}},
|
|
}
|
|
|
|
monkeypatch.setattr(
|
|
"app.services.shipping_notice.batch_from_intakes.create_shipping_batch_from_intakes",
|
|
_fake_create,
|
|
)
|
|
ctx = TurnContext(
|
|
turn_id="turn-candidate",
|
|
run_id="turn-candidate",
|
|
session_id="sess-candidate",
|
|
request_id="req-candidate",
|
|
user=user,
|
|
agent=agent,
|
|
input=TurnInput(
|
|
type="action",
|
|
display_text="确认两个订单",
|
|
action_type="shipping.create_batch_from_intakes",
|
|
payload={
|
|
"intake_nos": ["SNI-1"],
|
|
"candidate_nos": ["SNC-1", "SNC-2"],
|
|
"idempotency_key": "idem-candidate-handler",
|
|
},
|
|
),
|
|
refs={},
|
|
)
|
|
|
|
events = list(ShippingCreateBatchFromIntakesAction().stream(ctx))
|
|
|
|
assert captured["intake_nos"] == ["SNI-1"]
|
|
assert captured["candidate_nos"] == ["SNC-1", "SNC-2"]
|
|
assert captured["conversation_id"] == "sess-candidate"
|
|
assert any(event.event == "message.completed" for event in events)
|