feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理

清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
This commit is contained in:
hugo committed 2026-08-26 02:47:07 +08:00
1 parent 7cfd5d167e
commit 1e5aedb9ed
235 files changed
+11394 -9613

No files matched your search

+59 -1
View File
@@ -1,6 +1,7 @@
import pytest
from flask import g
from app import create_app, db
from app.models import Agent, User
from app.models import Agent, ChatSession, User
@pytest.fixture
def client():
@@ -97,3 +98,60 @@ def test_chat_url_logic(client):
res = client.get('/api/chat/sessions', headers=headers, json={'agent_id': agent_id})
assert res.status_code == 403
assert res.get_json()['error'] == 'Permission denied for this agent'
def test_chat_completions_stream_reads_session_snapshot_after_commit(client, monkeypatch):
"""
提交会话后消费 SSE 时使用标量快照,避免已脱离 Session 的 ORM 实例被延迟刷新。
关键断言:生成器在请求返回后仍能产出 session_created 事件,而不会抛出
DetachedInstanceError。
"""
with client.application.app_context():
user = User(username='stream-user', role='admin', email='stream-user@example.com')
agent = Agent(name='Stream Agent', dify_api_key='test-key', api_base_url='http://dify.invalid')
db.session.add_all([user, agent])
db.session.commit()
user_id = user.id
agent_id = agent.id
monkeypatch.setattr(
'app.services.chat.executors.stream_chat_via_executor',
lambda _context: iter(['data: {"event": "message_end"}\n\n']),
)
from app.api.chat import completions
captured_session = None
original_ensure_chat_session = completions._ensure_chat_session
def capture_ensured_session(*args, **kwargs):
nonlocal captured_session
captured_session = original_ensure_chat_session(*args, **kwargs)
return captured_session
monkeypatch.setattr(completions, '_ensure_chat_session', capture_ensured_session)
def get_current_user():
current_user = db.session.get(User, user_id)
g.current_user = current_user
return current_user
monkeypatch.setattr('app.core.decorators._get_current_user', get_current_user)
with client.application.test_request_context(
'/api/chat/completions',
method='POST',
json={'agent_id': agent_id, 'query': '测试流式会话'},
):
response = client.application.view_functions['chat.chat_completions']()
# 模拟生产环境:流式处理中的事务令 ORM 属性过期,随后请求 scoped Session 被清理。
assert captured_session is not None
db.session.expire(captured_session)
db.session.remove()
body = ''.join(response.response).encode()
assert response.status_code == 200
assert b'"event": "session_created"' in body
assert b'DetachedInstanceError' not in body
with client.application.app_context():
assert ChatSession.query.filter_by(user_id=user_id, agent_id=agent_id).count() == 1