feat(schedule):cron支持图形,RoHS检核增加进度显示

This commit is contained in:
hugo committed 2026-06-15 13:42:15 +08:00
1 parent 6d0e78be81
commit f5aac9c893
46 files changed
+1987 -221

No files matched your search

+300 -2
View File
@@ -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):
+33
View File
@@ -0,0 +1,33 @@
"""排程 Cron 业务时区计算测试。"""
from datetime import datetime
from unittest.mock import patch
import pytest
from app.services.schedule.timezone_util import compute_next_run_at
def test_compute_next_run_at_uses_shanghai_timezone(app):
"""0 12,17 * * * 在 01:00 北京时间后,下次应为当日 12:00(非 UTC 12:00)。"""
with app.app_context():
# 2026-06-12 01:00:15 CST = 2026-06-11 17:00:15 UTC
base_utc = datetime(2026, 6, 11, 17, 0, 15)
nxt = compute_next_run_at('0 12,17 * * *', base_utc_naive=base_utc)
assert nxt == datetime(2026, 6, 12, 4, 0, 0)
def test_compute_next_run_at_after_noon(app):
with app.app_context():
# 2026-06-12 12:30 CST = 2026-06-12 04:30 UTC
base_utc = datetime(2026, 6, 12, 4, 30, 0)
nxt = compute_next_run_at('0 12,17 * * *', base_utc_naive=base_utc)
assert nxt == datetime(2026, 6, 12, 9, 0, 0)
def test_compute_next_run_at_without_tzdata(app):
"""缺少 tzdata 时仍应通过固定 UTC+8 回退计算下次执行。"""
with app.app_context():
with patch('app.services.schedule.timezone_util.ZoneInfo', side_effect=Exception('no tzdata')):
base_utc = datetime(2026, 6, 11, 17, 0, 15)
nxt = compute_next_run_at('0 12,17 * * *', base_utc_naive=base_utc)
assert nxt == datetime(2026, 6, 12, 4, 0, 0)