feat(shipping-notice): 添加 RPA 工作者心跳告警开关,更新环境配置以支持样本证据历史记录

This commit is contained in:
hugo committed 2026-07-19 23:15:25 +08:00
1 parent 705971ad03
commit 88408490d7
34 files changed
+1697 -109

No files matched your search

@@ -2,6 +2,7 @@
from __future__ import annotations
from datetime import datetime, timedelta
from pathlib import Path
from unittest.mock import patch
@@ -18,6 +19,7 @@ from app.models import (
ShippingExcelTemplate,
ShippingInventoryFile,
ShippingInventoryField,
ShippingRpaProfile,
ShippingInventorySource,
)
from app.services.admin import shipping_inventory_service
@@ -125,6 +127,57 @@ def test_configuration_test_callback_isolated_from_production_inventory(app, tmp
assert ShippingInventoryConfigurationTestRun.query.count() == 1
def test_latest_configuration_test_recovers_only_current_configuration(app, tmp_path):
"""重开配置抽屉只能恢复当前版本测试;站点改动后可发起独立新测试而不复用旧运行。"""
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
app.config['SHIPPING_NOTICE_RPA_CREDENTIAL_KEY'] = Fernet.generate_key().decode('ascii')
with app.app_context(), patch(
'app.services.shipping_notice.rpa.write_inventory_export_batch_trigger', return_value='trigger.json',
):
source = _source()
first = inventory_configuration_test.start_test_export(source['id'], actor_id=None)
assert inventory_configuration_test.get_latest_test_run(source['id'])['run_no'] == first['run_no']
current = next(item for item in shipping_inventory_service.list_sources() if item['id'] == source['id'])
shipping_inventory_service.save_source({**current, 'display_name': 'Changed before callback'}, actor_id=None, source_id=source['id'])
# 关键断言:旧运行不再属于当前配置,重新测试必须创建新运行而不是误复用。
assert inventory_configuration_test.get_latest_test_run(source['id']) is None
second = inventory_configuration_test.start_test_export(source['id'], actor_id=None)
assert second['run_no'] != first['run_no']
assert inventory_configuration_test.get_latest_test_run(source['id'])['run_no'] == second['run_no']
def test_configuration_test_timeout_marks_run_failed(app, tmp_path):
"""RPA 长时间未回调时,读取最近运行应服务端收口为失败而非让前端无限轮询。"""
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
app.config['SHIPPING_NOTICE_RPA_CREDENTIAL_KEY'] = Fernet.generate_key().decode('ascii')
app.config['SHIPPING_NOTICE_RPA_INVENTORY_TIMEOUT_SECONDS'] = 1
with app.app_context(), patch(
'app.services.shipping_notice.rpa.write_inventory_export_batch_trigger', return_value='trigger.json',
):
source = _source()
started = inventory_configuration_test.start_test_export(source['id'], actor_id=None)
run = ShippingInventoryConfigurationTestRun.query.filter_by(run_no=started['run_no']).one()
run.created_at = datetime.utcnow() - timedelta(seconds=2)
db.session.commit()
recovered = inventory_configuration_test.get_latest_test_run(source['id'])
# 关键断言:超时是明确失败终态,客户端无需自行推断或继续轮询。
assert recovered['status'] == 'failed'
assert recovered['stage'] == 'failed'
assert '超时' in recovered['error_message']
result_file = tmp_path / 'late-rpa-result.xlsx'
_workbook(result_file)
late_callback = inventory_configuration_test.report_test_export({
'run_no': started['run_no'], 'status': 'success', 'file_path': str(result_file),
})
# 关键断言:超时后的迟到回调不能将失败任务重新写成完成,也不能生成结构分析证据。
assert late_callback['status'] == 'failed'
assert ShippingInventoryConfigurationTestRun.query.filter_by(run_no=started['run_no']).one().analysis_id is None
def test_confirming_test_profile_binds_only_compatible_published_profile(app, tmp_path):
"""确认本次测试推荐的已发布方案后,才绑定库存站点并形成有效测试证据。"""
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
@@ -165,6 +218,40 @@ def test_confirming_test_profile_binds_only_compatible_published_profile(app, tm
db.session.commit()
assert shipping_inventory_service.get_source_activation_status(source_model)['ready'] is True
assert shipping_inventory_service.activate_source(source['id'], actor_id=None)['enabled'] is True
assert next(
item for item in shipping_inventory_service.get_source_activation_status(source_model)['checks']
if item['code'] == 'configuration_test'
)['status'] == 'passed'
def test_rpa_profile_change_stales_completed_configuration_test(app, tmp_path):
"""连接凭据或登录配置变更后,旧测试不得继续作为当前库存站点的启用证据。"""
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
app.config['SHIPPING_NOTICE_RPA_CREDENTIAL_KEY'] = Fernet.generate_key().decode('ascii')
with app.app_context(), patch(
'app.services.shipping_notice.rpa.write_inventory_export_batch_trigger', return_value='trigger.json',
):
source = _source()
completed = _complete_test_run(source, tmp_path)
profile = _publish_profile_from_analysis(completed['analysis_id'], _published_order_field(), code='PROFILE_STALE')
inventory_configuration_test.confirm_test_profile(source['id'], completed['run_no'], profile['id'], actor_id=None)
rpa_profile = db.session.get(ShippingRpaProfile, source['rpa_profile_id'])
shipping_inventory_service.save_profile({
'profile_code': rpa_profile.profile_code, 'display_name': rpa_profile.display_name,
'connector_type': rpa_profile.connector_type, 'login_url': rpa_profile.login_url,
'login_config_json': rpa_profile.login_config_json, 'enabled': True,
'username': 'updated-user', 'password': 'updated-password',
}, actor_id=None, profile_id=rpa_profile.id)
source_model = db.session.get(ShippingInventorySource, source['id'])
test_check = next(
item for item in shipping_inventory_service.get_source_activation_status(source_model)['checks']
if item['code'] == 'configuration_test'
)
# 关键断言:连接配置版本变更使已完成测试失效,不能继续放行正式运行。
assert test_check['status'] == 'stale'
assert test_check['blocking'] is True
def test_configuration_test_rejects_enabled_source_and_stale_confirmation(app, tmp_path):
@@ -184,6 +271,15 @@ def test_configuration_test_rejects_enabled_source_and_stale_confirmation(app, t
inventory_configuration_test.confirm_test_profile(source['id'], completed['run_no'], profile['id'], actor_id=None)
assert stale.value.code == 'SHIPPING.INVENTORY_CONFIGURATION_TEST_STALE'
# 关键断言:旧配置版本的测试只能作为过期证据展示,不能被误判为当前测试通过。
stale_status = shipping_inventory_service.get_source_activation_status(
db.session.get(ShippingInventorySource, source['id']),
)
test_check = next(item for item in stale_status['checks'] if item['code'] == 'configuration_test')
assert test_check['status'] == 'stale'
assert test_check['blocking'] is True
assert test_check['evidence']['run_no'] == completed['run_no']
source_model = db.session.get(ShippingInventorySource, source['id'])
source_model.parsing_profile_id = profile['id']
template = ShippingExcelTemplate(
@@ -221,12 +317,13 @@ def test_failed_callback_and_unrelated_profile_never_bind_source(app, tmp_path):
_publish_profile_from_analysis(completed['analysis_id'], field_id, code='TEST_MATCHED')
unrelated = inventory_parsing_profile.save_draft({
'profile_code': 'TEST_UNRELATED', 'display_name': 'Unrelated', 'file_format': 'xlsx',
'sample_analysis_id': completed['analysis_id'],
'definition': {
'sheet_name': 'Other', 'header_row_index': 1,
'sheet_name': 'Sheet', 'header_row_index': 1,
'field_mapping': {
_inventory_field_id('part_number'): 'Other Part',
_inventory_field_id('reservation_id'): 'Other Reservation',
_inventory_field_id('available_qty'): 'Other Qty',
_inventory_field_id('part_number'): 'Supplier Sku',
_inventory_field_id('reservation_id'): 'Reservation ID',
_inventory_field_id('available_qty'): 'Qty Available',
},
'match_keys': [{'order_field_id': field_id, 'inventory_field_id': _inventory_field_id('part_number')}],
},