feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认
This commit is contained in:
1 parent
b595e157e9
commit
4fbaed2b4c
176 files changed
+14011
-994
No files matched your search
@@ -2,6 +2,8 @@
|
||||
tests/test_dify_workflow_reliability.py
|
||||
|
||||
用途:覆盖 Dify Workflow streaming 断连补偿、重试与可靠性元信息。
|
||||
依赖:requests、DifyService、FileCheckService 与 pytest Flask fixture。
|
||||
逻辑:模拟 Dify SSE、HTTP 错误及超时,验证 Workflow 轮询补偿和 Chatflow 终态事件不会丢失。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -22,6 +24,7 @@ class _Agent:
|
||||
|
||||
|
||||
def _sse_lines(*events: dict) -> list[bytes]:
|
||||
"""把事件字典编码为 Dify requests.iter_lines 返回的字节行。"""
|
||||
return [f"data: {json.dumps(evt, ensure_ascii=False)}".encode("utf-8") for evt in events]
|
||||
|
||||
|
||||
@@ -308,3 +311,73 @@ def test_dedupe_exact_doubled_text_fixes_historical_records():
|
||||
doubled = "检核完成,共处理 3 个文件。" * 2
|
||||
assert DifyService._dedupe_exact_doubled_text(doubled) == "检核完成,共处理 3 个文件。"
|
||||
assert DifyService._dedupe_exact_doubled_text("unique answer") == "unique answer"
|
||||
|
||||
|
||||
def test_chat_stream_error_event_keeps_persisted_chat_log_id(app, monkeypatch):
|
||||
"""连接超时的 error SSE 必须携带持久化主键,供领域 Handler 回写失败状态。"""
|
||||
monkeypatch.setattr(
|
||||
"app.services.dify.requests.post",
|
||||
lambda *args, **kwargs: (_ for _ in ()).throw(requests.exceptions.ConnectTimeout("connect timed out")),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
DifyService,
|
||||
"_persist_stream_chat_record",
|
||||
staticmethod(lambda **kwargs: {"chat_log_id": 42, "run_id": None}),
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
lines = list(DifyService.stream_chat(
|
||||
"extract orders",
|
||||
1,
|
||||
_Agent(),
|
||||
request_id="req-connect-timeout",
|
||||
session_id="session-connect-timeout",
|
||||
))
|
||||
|
||||
events = [json.loads(line.removeprefix("data: ").strip()) for line in lines]
|
||||
error_event = next(event for event in events if event.get("event") == "error")
|
||||
# 关键断言:持久化返回的主键必须沿原 error 事件继续向 relay 传播。
|
||||
assert error_event["chat_log_id"] == 42
|
||||
assert error_event["message"]
|
||||
|
||||
|
||||
def test_chat_stream_read_timeout_after_answer_emits_terminal_event(app, monkeypatch):
|
||||
"""已有正文后 ReadTimeout 应合成 message_end,避免领域状态永久停留在 extracting。"""
|
||||
class _ChatResponse:
|
||||
ok = True
|
||||
status_code = 200
|
||||
reason = "OK"
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
return False
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def iter_lines(self):
|
||||
yield _sse_lines({"event": "message", "answer": "partial answer"})[0]
|
||||
raise requests.exceptions.ReadTimeout("read timed out")
|
||||
|
||||
monkeypatch.setattr("app.services.dify.requests.post", lambda *args, **kwargs: _ChatResponse())
|
||||
monkeypatch.setattr(
|
||||
DifyService,
|
||||
"_persist_stream_chat_record",
|
||||
staticmethod(lambda **kwargs: {"chat_log_id": 43, "run_id": None}),
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
lines = list(DifyService.stream_chat(
|
||||
"extract orders",
|
||||
1,
|
||||
_Agent(),
|
||||
request_id="req-read-timeout",
|
||||
session_id="session-read-timeout",
|
||||
))
|
||||
|
||||
events = [json.loads(line.removeprefix("data: ").strip()) for line in lines]
|
||||
terminal = next(event for event in events if event.get("event") == "message_end")
|
||||
assert terminal["answer"] == "partial answer"
|
||||
assert terminal["chat_log_id"] == 43
|
||||
Reference in new issue
Block a user