189 lines
6.7 KiB
Python
189 lines
6.7 KiB
Python
import pytest
|
|
from datetime import datetime
|
|
from app import create_app, db
|
|
from app.models import Agent, User, ChatLog
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
app = create_app('testing')
|
|
app.config['TESTING'] = True
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
|
|
|
with app.test_client() as client:
|
|
with app.app_context():
|
|
db.create_all()
|
|
# Create user and agent
|
|
user = User(username='testuser', role='user', email='test@example.com')
|
|
db.session.add(user)
|
|
|
|
agent = Agent(name='Test Agent', type='api')
|
|
db.session.add(agent)
|
|
user.agents.append(agent)
|
|
|
|
db.session.flush()
|
|
|
|
# Add some chat logs
|
|
log1 = ChatLog(
|
|
user_id=user.id, agent_id=agent.id,
|
|
conversation_id='conv1',
|
|
query='Hi', answer='Hello',
|
|
created_at=datetime(2026, 1, 1)
|
|
)
|
|
db.session.add(log1)
|
|
|
|
db.session.commit()
|
|
|
|
yield client
|
|
|
|
db.session.remove()
|
|
db.drop_all()
|
|
|
|
def get_headers(client):
|
|
return {
|
|
'X-User-Id': '1',
|
|
'X-Username': 'testuser',
|
|
'X-Role': 'user',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
def test_chat_history_with_query_param(client):
|
|
"""
|
|
Verify that /api/chat/sessions accepts agent_id as query parameter
|
|
(This tests the fix in decorators.py)
|
|
"""
|
|
headers = get_headers(client)
|
|
|
|
# 1. Test without agent_id (Should fail with 400)
|
|
res = client.get('/api/chat/sessions', headers=headers)
|
|
assert res.status_code == 400
|
|
assert res.get_json()['error']['message'] == 'agent_id is required'
|
|
|
|
# 2. Test with agent_id in query string (Should succeed)
|
|
res = client.get('/api/chat/sessions?agent_id=1', headers=headers)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert len(data) == 1
|
|
assert data[0]['conversation_id'] == 'conv1'
|
|
|
|
# 3. Test with agent_id in JSON body (Should still work if GET allows body, but requests/fetch usually don't)
|
|
# Flask test client allows GET with body
|
|
res = client.get('/api/chat/sessions', headers=headers, json={'agent_id': 1})
|
|
assert res.status_code == 200
|
|
assert len(res.get_json()) == 1
|
|
|
|
def test_chat_messages_history(client):
|
|
"""
|
|
Verify getting messages for a conversation
|
|
"""
|
|
headers = get_headers(client)
|
|
|
|
# Need agent_id now
|
|
res = client.get('/api/chat/messages?conversation_id=conv1&agent_id=1', headers=headers)
|
|
assert res.status_code == 200
|
|
msgs = res.get_json()
|
|
assert len(msgs) == 2 # User + Assistant
|
|
assert msgs[0]['content'] == 'Hi'
|
|
assert msgs[1]['content'] == 'Hello'
|
|
|
|
|
|
def test_local_assistant_message_query_does_not_duplicate_user_bubble(client):
|
|
"""助手本地消息即使误传 query,历史回放也不能多生成一条用户确认。"""
|
|
headers = get_headers(client)
|
|
|
|
user_res = client.post('/api/chat/messages/local?agent_id=1', headers=headers, json={
|
|
'conversation_id': 'conv-local-confirm',
|
|
'role': 'user',
|
|
'type': 'text',
|
|
'content': '确认\n任务编号:SN-001',
|
|
'query': '确认',
|
|
})
|
|
assert user_res.status_code == 200
|
|
|
|
assistant_res = client.post('/api/chat/messages/local?agent_id=1', headers=headers, json={
|
|
'conversation_id': 'conv-local-confirm',
|
|
'role': 'assistant',
|
|
'type': 'text',
|
|
'content': '已确认,进入下一步。',
|
|
'query': '确认',
|
|
})
|
|
assert assistant_res.status_code == 200
|
|
|
|
res = client.get('/api/chat/messages?conversation_id=conv-local-confirm&agent_id=1', headers=headers)
|
|
assert res.status_code == 200
|
|
msgs = res.get_json()
|
|
user_messages = [item for item in msgs if item['role'] == 'user']
|
|
assert len(user_messages) == 1
|
|
assert user_messages[0]['content'] == '确认'
|
|
assert msgs[-1]['role'] == 'assistant'
|
|
assert msgs[-1]['content'] == '已确认,进入下一步。'
|
|
|
|
|
|
def test_local_typed_assistant_message_keeps_query_for_user_bubble_and_title(client):
|
|
"""结构化结果卡须保留 query:刷新后回放用户提问,会话标题用提问而非 headline。"""
|
|
headers = get_headers(client)
|
|
user_question = '请对《邦达激光合同.pdf》进行模板比对'
|
|
headline = '模板比对完成:高 1 / 中 0 / 低 0,共 1 处变更'
|
|
|
|
save_res = client.post('/api/chat/messages/local?agent_id=1', headers=headers, json={
|
|
'conversation_id': 'conv-contract-review',
|
|
'role': 'assistant',
|
|
'type': 'procurement-contract-review',
|
|
'content': headline,
|
|
'query': user_question,
|
|
'payload': {
|
|
'review_no': 'CR-TEST-001',
|
|
'contract_file_name': '邦达激光合同.pdf',
|
|
'high_risk_count': 1,
|
|
'medium_risk_count': 0,
|
|
'low_risk_count': 0,
|
|
},
|
|
})
|
|
assert save_res.status_code == 200
|
|
|
|
# 断言:历史回放含用户气泡 + 结果卡
|
|
msg_res = client.get(
|
|
'/api/chat/messages?conversation_id=conv-contract-review&agent_id=1',
|
|
headers=headers,
|
|
)
|
|
assert msg_res.status_code == 200
|
|
msgs = msg_res.get_json()
|
|
assert len(msgs) >= 2
|
|
assert msgs[0]['role'] == 'user'
|
|
assert msgs[0]['content'] == user_question
|
|
assert msgs[-1]['role'] == 'assistant'
|
|
assert msgs[-1]['type'] == 'procurement-contract-review'
|
|
|
|
# 断言:侧栏标题取用户提问,而非审查 headline
|
|
sessions_res = client.get('/api/chat/sessions?agent_id=1', headers=headers)
|
|
assert sessions_res.status_code == 200
|
|
sessions = sessions_res.get_json()
|
|
target = next((s for s in sessions if s['conversation_id'] == 'conv-contract-review'), None)
|
|
assert target is not None
|
|
assert target['title'].startswith('请对《邦达激光合同.pdf》')
|
|
assert '模板比对完成' not in target['title']
|
|
|
|
|
|
def test_chat_sessions_keep_latest_non_empty_query_as_title(client):
|
|
headers = get_headers(client)
|
|
|
|
with client.application.app_context():
|
|
user = User.query.filter_by(username='testuser').first()
|
|
agent = Agent.query.filter_by(name='Test Agent').first()
|
|
db.session.add(ChatLog(
|
|
user_id=user.id,
|
|
agent_id=agent.id,
|
|
conversation_id='conv1',
|
|
query='',
|
|
answer='请填写请假表单',
|
|
message_type='leave-form',
|
|
created_at=datetime(2026, 1, 2),
|
|
))
|
|
db.session.commit()
|
|
|
|
res = client.get('/api/chat/sessions?agent_id=1', headers=headers)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert len(data) == 1
|
|
assert data[0]['conversation_id'] == 'conv1'
|
|
assert data[0]['title'] == 'Hi'
|