feat(rohs-check): 新增 RoHS 检核工具进度查询功能及相关接口

This commit is contained in:
hugo committed 2026-06-11 10:05:29 +08:00
1 parent fee33f69d9
commit a18a07ef1a
19 files changed
+654 -58

No files matched your search

+123 -3
View File
@@ -177,6 +177,126 @@ def test_rohs_tool_running_conflict(app, client):
assert data['error_code'] == 'check_running'
def test_rohs_tool_requires_task_id(app, client):
with app.app_context():
app.config['INTEGRATION_API_KEY'] = 'secret'
res = client.post(
'/api/rohs-check/tools/tasks/run',
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
json={},
)
assert res.status_code == 400
assert res.get_json()['error_code'] == 'task_id_required'
def test_rohs_tool_rejects_invalid_task_id(app, client):
with app.app_context():
app.config['INTEGRATION_API_KEY'] = 'secret'
res = client.post(
'/api/rohs-check/tools/tasks/run',
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
json={'task_id': 'abc'},
)
assert res.status_code == 400
assert res.get_json()['error_code'] == 'task_id_invalid'
def test_rohs_tool_running_conflict_is_global(app, client):
with app.app_context():
app.config['INTEGRATION_API_KEY'] = 'secret'
admin = _mk_admin()
db.session.commit()
running_task = _mk_check_task(admin.id)
second_task = ScheduleTask(
schedule_no='CHK-ROHS-002',
name='RoHS 文件检核备用',
schedule_type='platform_app',
owner_user_id=admin.id,
cron_expr='0 * * * *',
wait_seconds=5,
platform_api_path='/2',
enabled=True,
)
db.session.add(second_task)
db.session.flush()
running = ScheduleRun(run_no='RUN-ROHS-RUNNING', schedule_task_id=running_task.id, status='running', trigger_type='manual')
db.session.add(running)
db.session.commit()
second_task_id = second_task.id
running_id = running.id
res = client.post(
'/api/rohs-check/tools/tasks/run',
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
json={'task_id': second_task_id},
)
assert res.status_code == 409
data = res.get_json()
assert data['error_code'] == 'check_running'
assert data['run']['run_id'] == running_id
def test_rohs_tool_lists_all_rohs_runs(app, client):
with app.app_context():
app.config['INTEGRATION_API_KEY'] = 'secret'
admin = _mk_admin()
db.session.commit()
rohs_task = _mk_check_task(admin.id)
other_task = _mk_non_rohs_task(admin.id)
db.session.add_all([
ScheduleRun(run_no='RUN-ROHS-001', schedule_task_id=rohs_task.id, status='success', trigger_type='manual', total_count=3, success_count=3),
ScheduleRun(run_no='RUN-ROHS-002', schedule_task_id=rohs_task.id, status='partial_failed', trigger_type='manual', total_count=4, success_count=3, failed_count=1),
ScheduleRun(run_no='RUN-OTHER-001', schedule_task_id=other_task.id, status='success', trigger_type='manual', total_count=9, success_count=9),
])
db.session.commit()
res = client.get('/api/rohs-check/tools/runs?limit=10', headers={'X-Integration-Key': 'secret'})
assert res.status_code == 200
data = res.get_json()
assert data['ok'] is True
assert data['count'] == 2
assert {item['run_no'] for item in data['runs']} == {'RUN-ROHS-001', 'RUN-ROHS-002'}
def test_workspace_rohs_active_run_returns_progress(app, client):
with app.app_context():
admin = _mk_admin()
db.session.commit()
task = _mk_check_task(admin.id)
run = ScheduleRun(
run_no='RUN-ROHS-ACTIVE',
schedule_task_id=task.id,
status='running',
trigger_type='dify_chatflow',
total_count=10,
success_count=3,
failed_count=1,
skipped_count=1,
)
db.session.add(run)
db.session.commit()
res = client.get('/api/workspace/rohs-check/runs/active', headers={'X-Username': 'admin'})
assert res.status_code == 200
data = res.get_json()
assert data['ok'] is True
assert data['run']['run_no'] == 'RUN-ROHS-ACTIVE'
assert data['run']['processed_count'] == 5
assert data['run']['progress_percent'] == 50
def test_workspace_rohs_active_run_returns_null_when_idle(app, client):
with app.app_context():
_mk_admin()
db.session.commit()
res = client.get('/api/workspace/rohs-check/runs/active', headers={'X-Username': 'admin'})
assert res.status_code == 200
assert res.get_json()['run'] is None
def test_rohs_tool_excel_download_uses_signed_token(app, client):
with app.app_context():
app.config['INTEGRATION_API_KEY'] = 'secret'
@@ -267,7 +387,7 @@ 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_TOOL_API_KEY'] = 'rohs-secret'
app.config['ROHS_TOOLS_API_KEY'] = 'rohs-secret'
inputs = build_dify_chat_inputs()
assert inputs[HXOPS_TOOLS_API_BASE_INPUT] == 'https://rohs-tool.example.com'
assert inputs[HXOPS_TOOL_API_TOKEN_INPUT] == 'rohs-secret'
@@ -278,8 +398,8 @@ def test_rohs_dify_chat_inputs_falls_back_to_legacy_env_key(app):
with app.app_context():
app.config.pop('HXOPS_TOOLS_API_BASE', None)
app.config['HXOPS_TOOL_API_BASE'] = 'https://legacy-tool.example.com'
app.config.pop('ROHS_TOOL_API_KEY', None)
app.config['HXOPS_TOOLS_API_BASE'] = 'https://legacy-tool.example.com'
app.config.pop('ROHS_TOOLS_API_KEY', None)
app.config['INTEGRATION_API_KEY'] = 'integration-secret'
inputs = build_dify_chat_inputs()
assert inputs['HXOPS_TOOLS_API_BASE'] == 'https://legacy-tool.example.com'