feat(schedule):cron支持图形,RoHS检核增加进度显示
This commit is contained in:
1 parent
6d0e78be81
commit
f5aac9c893
46 files changed
+1987
-221
No files matched your search
@@ -4,7 +4,7 @@ import types
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, User
|
||||
from app.models import ChatLog, ChatSession, ScheduleRun, ScheduleTask, User
|
||||
from app.services.rohs_check import RoHSCheckService
|
||||
|
||||
|
||||
@@ -277,6 +277,86 @@ def test_rohs_tool_running_conflict(app, client):
|
||||
assert data['error_code'] == 'check_running'
|
||||
|
||||
|
||||
def test_rohs_tool_check_running_records_chat_log_bind_context(app, client):
|
||||
with app.app_context():
|
||||
app.config['INTEGRATION_API_KEY'] = 'secret'
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-ROHS-BIND-CONFLICT',
|
||||
schedule_task_id=task.id,
|
||||
status='running',
|
||||
trigger_type='manual',
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
task_id = task.id
|
||||
running_run_id = run.id
|
||||
|
||||
res = client.post(
|
||||
'/api/rohs-check/tools/tasks/run',
|
||||
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
|
||||
json={
|
||||
'task_id': task_id,
|
||||
'hxops_session_id': 'bind-on-conflict',
|
||||
'user_message': '立即执行检核',
|
||||
},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
assert res.get_json()['error_code'] == 'check_running'
|
||||
|
||||
with app.app_context():
|
||||
bound = db.session.get(ScheduleRun, running_run_id)
|
||||
req = bound.api_request_json or {}
|
||||
assert req.get('bind_session_id') == 'bind-on-conflict'
|
||||
assert req.get('bind_user_message') == '立即执行检核'
|
||||
|
||||
|
||||
def test_rohs_tool_start_records_chat_log_bind_context(app, client, monkeypatch):
|
||||
class _Thread:
|
||||
def __init__(self, *, target, daemon, name):
|
||||
self.target = target
|
||||
self.daemon = daemon
|
||||
self.name = name
|
||||
|
||||
def start(self):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr('app.api.rohs_check_tools.threading.Thread', _Thread)
|
||||
|
||||
with app.app_context():
|
||||
app.config['INTEGRATION_API_KEY'] = 'secret'
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
task_id = task.id
|
||||
|
||||
res = client.post(
|
||||
'/api/rohs-check/tools/tasks/run',
|
||||
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
|
||||
json={
|
||||
'task_id': task_id,
|
||||
'hxops_session_id': 'bind-on-start',
|
||||
'hxops_turn_id': 'turn-abc-001',
|
||||
'user_message': '执行 RoHS 检核',
|
||||
},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
run_id = res.get_json()['run']['run_id']
|
||||
|
||||
with app.app_context():
|
||||
run = db.session.get(ScheduleRun, run_id)
|
||||
req = run.api_request_json or {}
|
||||
assert req.get('bind_session_id') == 'bind-on-start'
|
||||
assert req.get('bind_turn_id') == 'turn-abc-001'
|
||||
assert req.get('bind_user_message') == '执行 RoHS 检核'
|
||||
from app.services.rohs_check_tools.chat_log_run_binding import resolve_run_id_for_chat_log
|
||||
|
||||
assert resolve_run_id_for_chat_log(turn_id='turn-abc-001') == run_id
|
||||
assert resolve_run_id_for_chat_log(session_id='bind-on-start', query='执行 RoHS 检核') == run_id
|
||||
|
||||
|
||||
def test_rohs_tool_requires_task_id(app, client):
|
||||
with app.app_context():
|
||||
app.config['INTEGRATION_API_KEY'] = 'secret'
|
||||
@@ -402,6 +482,85 @@ def test_workspace_rohs_active_run_returns_progress(app, client):
|
||||
assert data['run']['current_step']['row_id'] == 'ROW-16'
|
||||
|
||||
|
||||
def test_workspace_rohs_active_run_filters_by_task_id(app, client):
|
||||
with app.app_context():
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task_a = _mk_check_task(admin.id)
|
||||
task_b = ScheduleTask(
|
||||
schedule_no='CHK-ROHS-002',
|
||||
name='RoHS 文件检核 B',
|
||||
schedule_type='platform_app',
|
||||
owner_user_id=admin.id,
|
||||
cron_expr='0 * * * *',
|
||||
wait_seconds=5,
|
||||
platform_api_path='/2',
|
||||
enabled=True,
|
||||
)
|
||||
db.session.add(task_b)
|
||||
db.session.commit()
|
||||
run_a = ScheduleRun(
|
||||
run_no='RUN-ROHS-TASK-A',
|
||||
schedule_task_id=task_a.id,
|
||||
status='running',
|
||||
trigger_type='manual',
|
||||
total_count=10,
|
||||
success_count=1,
|
||||
)
|
||||
run_b = ScheduleRun(
|
||||
run_no='RUN-ROHS-TASK-B',
|
||||
schedule_task_id=task_b.id,
|
||||
status='running',
|
||||
trigger_type='manual',
|
||||
total_count=8,
|
||||
success_count=2,
|
||||
)
|
||||
db.session.add_all([run_a, run_b])
|
||||
db.session.commit()
|
||||
task_a_id = task_a.id
|
||||
task_b_id = task_b.id
|
||||
|
||||
hit = client.get(
|
||||
f'/api/workspace/rohs-check/runs/active?task_id={task_a_id}',
|
||||
headers={'X-Username': 'admin'},
|
||||
)
|
||||
assert hit.status_code == 200
|
||||
assert hit.get_json()['run']['run_no'] == 'RUN-ROHS-TASK-A'
|
||||
|
||||
miss = client.get(
|
||||
f'/api/workspace/rohs-check/runs/active?task_id={task_b_id}',
|
||||
headers={'X-Username': 'admin'},
|
||||
)
|
||||
assert miss.status_code == 200
|
||||
assert miss.get_json()['run']['run_no'] == 'RUN-ROHS-TASK-B'
|
||||
|
||||
|
||||
def test_workspace_rohs_active_run_ignores_conversation_when_task_id_set(app, client):
|
||||
with app.app_context():
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-ROHS-TASK-CONV-MISMATCH',
|
||||
schedule_task_id=task.id,
|
||||
status='running',
|
||||
trigger_type='dify_chatflow',
|
||||
total_count=10,
|
||||
success_count=1,
|
||||
api_request_json={'conversation_id': 'dify-conv-uuid'},
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
task_id = task.id
|
||||
|
||||
res = client.get(
|
||||
f'/api/workspace/rohs-check/runs/active?task_id={task_id}&conversation_id=hxops-session-id',
|
||||
headers={'X-Username': 'admin'},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
assert res.get_json()['run']['run_no'] == 'RUN-ROHS-TASK-CONV-MISMATCH'
|
||||
|
||||
|
||||
def test_workspace_rohs_active_run_filters_by_conversation_id(app, client):
|
||||
with app.app_context():
|
||||
admin = _mk_admin()
|
||||
@@ -428,6 +587,144 @@ def test_workspace_rohs_active_run_filters_by_conversation_id(app, client):
|
||||
assert hit.get_json()['run']['run_no'] == 'RUN-ROHS-ACTIVE-CONV'
|
||||
|
||||
|
||||
def test_workspace_rohs_run_for_log_returns_null_when_unbound(app, client):
|
||||
with app.app_context():
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
log = ChatLog(
|
||||
user_id=admin.id,
|
||||
agent_id=1,
|
||||
query='查询最新结果',
|
||||
answer='查询结果',
|
||||
session_id='sess-query-only',
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
task_id = task.id
|
||||
chat_log_id = log.id
|
||||
|
||||
res = client.get(
|
||||
f'/api/workspace/rohs-check/runs/for-log?task_id={task_id}&chat_log_id={chat_log_id}',
|
||||
headers={'X-Username': 'admin'},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
payload = res.get_json()
|
||||
assert payload['run'] is None
|
||||
assert payload.get('match_source') is None
|
||||
|
||||
|
||||
def test_workspace_rohs_run_for_log_reads_bound_run_id(app, client):
|
||||
with app.app_context():
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-ROHS-BOUND',
|
||||
schedule_task_id=task.id,
|
||||
status='running',
|
||||
trigger_type='dify_chatflow',
|
||||
total_count=5,
|
||||
success_count=1,
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
log = ChatLog(
|
||||
user_id=admin.id,
|
||||
agent_id=1,
|
||||
query='立即执行检核',
|
||||
answer='检核已启动',
|
||||
session_id='hxops-sess-002',
|
||||
run_id=run.id,
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
task_id = task.id
|
||||
chat_log_id = log.id
|
||||
|
||||
res = client.get(
|
||||
f'/api/workspace/rohs-check/runs/for-log?task_id={task_id}&chat_log_id={chat_log_id}',
|
||||
headers={'X-Username': 'admin'},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
payload = res.get_json()
|
||||
assert payload['run']['run_no'] == 'RUN-ROHS-BOUND'
|
||||
assert payload['match_source'] == 'chat_log'
|
||||
|
||||
|
||||
def test_workspace_rohs_run_for_log_allows_different_user(app, client):
|
||||
with app.app_context():
|
||||
owner = User(username='rohs-owner', email='owner-rohs@example.com', role='admin', status='active')
|
||||
owner.set_password('x')
|
||||
viewer = User(username='rohs-viewer', email='viewer-rohs@example.com', role='user', status='active')
|
||||
viewer.set_password('x')
|
||||
db.session.add_all([owner, viewer])
|
||||
db.session.commit()
|
||||
task = _mk_check_task(owner.id)
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-ROHS-CROSS-USER',
|
||||
schedule_task_id=task.id,
|
||||
status='running',
|
||||
trigger_type='dify_chatflow',
|
||||
total_count=3,
|
||||
success_count=1,
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
log = ChatLog(
|
||||
user_id=owner.id,
|
||||
agent_id=1,
|
||||
query='立即执行检核',
|
||||
answer='检核已启动',
|
||||
session_id='hxops-sess-cross-user',
|
||||
run_id=run.id,
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
task_id = task.id
|
||||
chat_log_id = log.id
|
||||
|
||||
res = client.get(
|
||||
f'/api/workspace/rohs-check/runs/for-log?task_id={task_id}&chat_log_id={chat_log_id}',
|
||||
headers={'X-Username': 'rohs-viewer'},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
assert res.get_json()['run']['run_no'] == 'RUN-ROHS-CROSS-USER'
|
||||
|
||||
|
||||
def test_workspace_rohs_run_for_log_returns_null_when_task_mismatch(app, client):
|
||||
with app.app_context():
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-ROHS-OTHER-TASK',
|
||||
schedule_task_id=task.id,
|
||||
status='running',
|
||||
trigger_type='dify_chatflow',
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
log = ChatLog(
|
||||
user_id=admin.id,
|
||||
agent_id=1,
|
||||
query='立即执行检核',
|
||||
answer='检核已启动',
|
||||
session_id='hxops-sess-empty',
|
||||
run_id=run.id,
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
chat_log_id = log.id
|
||||
|
||||
res = client.get(
|
||||
f'/api/workspace/rohs-check/runs/for-log?task_id=99999&chat_log_id={chat_log_id}',
|
||||
headers={'X-Username': 'admin'},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
assert res.get_json()['run'] is None
|
||||
|
||||
|
||||
def test_workspace_rohs_active_run_returns_null_when_idle(app, client):
|
||||
with app.app_context():
|
||||
_mk_admin()
|
||||
@@ -529,9 +826,10 @@ def test_rohs_dify_chat_inputs(app):
|
||||
with app.app_context():
|
||||
app.config['HXOPS_TOOLS_API_BASE'] = 'https://rohs-tool.example.com'
|
||||
app.config['ROHS_TOOLS_API_KEY'] = 'rohs-secret'
|
||||
inputs = build_dify_chat_inputs()
|
||||
inputs = build_dify_chat_inputs(hxops_session_id='sess-abc')
|
||||
assert inputs[HXOPS_TOOLS_API_BASE_INPUT] == 'https://rohs-tool.example.com'
|
||||
assert inputs[HXOPS_TOOL_API_TOKEN_INPUT] == 'rohs-secret'
|
||||
assert inputs['hxops_session_id'] == 'sess-abc'
|
||||
|
||||
|
||||
def test_rohs_dify_chat_inputs_falls_back_to_legacy_env_key(app):
|
||||
|
||||
Reference in new issue
Block a user