feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约

将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-17 23:18:48 +08:00
1 parent bf3154033b
commit ffc0d4a430
147 files changed
+4221 -2678

No files matched your search

@@ -11,7 +11,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
"""开始导出标记 processing;每笔完成后 batch-progress 抬升排程计数。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_receipt_rpa_finalize import (
from app.services.schedule.freight_inbound_rpa_finalize import (
mark_receipt_unit_processing,
report_receipt_batch_progress,
)
@@ -20,7 +20,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(
schedule_no='SCH-RECEIPT-LIVE', name='Receipt live progress', schedule_type='platform_job',
platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *',
platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *',
)
db.session.add(task)
db.session.flush()
@@ -40,7 +40,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
db.session.commit()
marked = mark_receipt_unit_processing({
'schema_version': 'shipping_receipt_rpa.v1',
'schema_version': 'freight_rpa.inbound.v1',
'callback_id': unit.callback_id,
'status': 'processing',
})
@@ -49,7 +49,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
assert db.session.get(ShippingReceiptRpaUnitRun, unit.id).status == 'processing'
progressed = report_receipt_batch_progress({
'schema_version': 'shipping_receipt_rpa.v1',
'schema_version': 'freight_rpa.inbound.v1',
'schedule_run_id': run.id,
'batch_run_no': run.run_no,
'success_count': 1,
@@ -73,7 +73,7 @@ def test_receipt_callback_success_refreshes_schedule_progress(app, monkeypatch):
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(
schedule_no='SCH-RECEIPT-PROG', name='Receipt progress', schedule_type='platform_job',
platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *',
platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *',
)
db.session.add(task)
db.session.flush()
@@ -92,15 +92,15 @@ def test_receipt_callback_success_refreshes_schedule_progress(app, monkeypatch):
db.session.add(unit)
db.session.commit()
monkeypatch.setattr(
'app.services.shipping_notice.receipt_report_storage._read_receipt_callback_file',
'app.services.freight_rpa.inbound.report_archive._read_inbound_callback_file',
lambda _path: b'receipt-bytes',
)
monkeypatch.setattr(
'app.services.shipping_notice.receipt_report_storage.file_asset_storage.upload_bytes',
'app.services.freight_rpa.inbound.report_archive.file_asset_storage.upload_bytes',
lambda **_kwargs: None,
)
payload = {
'schema_version': 'shipping_receipt_rpa.v1',
'schema_version': 'freight_rpa.inbound.v1',
'callback_id': unit.callback_id,
'status': 'success',
'file_path': 'Z:/receipt.xlsx',
@@ -122,15 +122,15 @@ def test_receipt_callback_archives_once_without_inventory_file(app, monkeypatch)
with app.app_context():
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-CB', name='Receipt callback', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
task = ScheduleTask(schedule_no='SCH-RECEIPT-CB', name='Receipt callback', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-CB', schedule_task_id=task.id, status='running')
db.session.add(run); db.session.flush()
unit = ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='AWSDU', callback_id='receipt-callback-archive', status='awaiting_rpa', config_snapshot_json={})
db.session.add(unit); db.session.commit()
monkeypatch.setattr('app.services.shipping_notice.receipt_report_storage._read_receipt_callback_file', lambda _path: b'receipt-bytes')
monkeypatch.setattr('app.services.shipping_notice.receipt_report_storage.file_asset_storage.upload_bytes', lambda **_kwargs: None)
payload = {'schema_version': 'shipping_receipt_rpa.v1', 'callback_id': unit.callback_id, 'status': 'success', 'file_path': 'Z:/receipt.xlsx', 'original_filename': 'receipt.xlsx', 'sha256': hashlib.sha256(b'receipt-bytes').hexdigest()}
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive._read_inbound_callback_file', lambda _path: b'receipt-bytes')
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive.file_asset_storage.upload_bytes', lambda **_kwargs: None)
payload = {'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.callback_id, 'status': 'success', 'file_path': 'Z:/receipt.xlsx', 'original_filename': 'receipt.xlsx', 'sha256': hashlib.sha256(b'receipt-bytes').hexdigest()}
first = ingest_receipt_callback(payload)
second = ingest_receipt_callback(payload)
assert first['file_id'] == second['file_id']
@@ -145,7 +145,7 @@ def test_ingest_success_advances_transfer_waiting_job(app, monkeypatch):
with app.app_context():
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-CB2', name='Receipt callback 2', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
task = ScheduleTask(schedule_no='SCH-RECEIPT-CB2', name='Receipt callback 2', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-CB2', schedule_task_id=task.id, status='running')
db.session.add(run); db.session.flush()
@@ -155,9 +155,9 @@ def test_ingest_success_advances_transfer_waiting_job(app, monkeypatch):
db.session.add(transfer_run); db.session.flush()
job = TransferSourceJob(run_id=transfer_run.id, source_code=source.source_code, status='waiting_rpa_callback')
db.session.add(job); db.session.commit()
monkeypatch.setattr('app.services.shipping_notice.receipt_report_storage._read_receipt_callback_file', lambda _path: b'receipt-bytes')
monkeypatch.setattr('app.services.shipping_notice.receipt_report_storage.file_asset_storage.upload_bytes', lambda **_kwargs: None)
payload = {'schema_version': 'shipping_receipt_rpa.v1', 'callback_id': unit.callback_id, 'status': 'success', 'file_path': 'Z:/receipt.xlsx', 'original_filename': 'receipt.xlsx', 'sha256': hashlib.sha256(b'receipt-bytes').hexdigest()}
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive._read_inbound_callback_file', lambda _path: b'receipt-bytes')
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive.file_asset_storage.upload_bytes', lambda **_kwargs: None)
payload = {'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.callback_id, 'status': 'success', 'file_path': 'Z:/receipt.xlsx', 'original_filename': 'receipt.xlsx', 'sha256': hashlib.sha256(b'receipt-bytes').hexdigest()}
result = ingest_receipt_callback(payload)
# [断言意图] 桥接发生:收料归档后调拨任务状态推进 file_ready,file_no 为归档文件 id
@@ -175,7 +175,7 @@ def test_ingest_failed_advance_is_durable(app):
with app.app_context():
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-CB3', name='Receipt callback 3', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
task = ScheduleTask(schedule_no='SCH-RECEIPT-CB3', name='Receipt callback 3', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-CB3', schedule_task_id=task.id, status='running')
db.session.add(run); db.session.flush()
@@ -185,7 +185,7 @@ def test_ingest_failed_advance_is_durable(app):
db.session.add(transfer_run); db.session.flush()
job = TransferSourceJob(run_id=transfer_run.id, source_code=source.source_code, status='waiting_rpa_callback')
db.session.add(job); db.session.commit()
payload = {'schema_version': 'shipping_receipt_rpa.v1', 'callback_id': unit.callback_id, 'status': 'failed', 'error_code': 'RPA_TEST_FAIL', 'error_message': 'boom'}
payload = {'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.callback_id, 'status': 'failed', 'error_code': 'RPA_TEST_FAIL', 'error_message': 'boom'}
result = ingest_receipt_callback(payload)
assert result['status'] == 'failed'
@@ -201,17 +201,17 @@ def test_ingest_failed_advance_is_durable(app):
def test_receipt_callback_rejects_file_outside_receipt_drop_root(app, tmp_path):
"""Callback cannot read arbitrary local xlsx files outside the receipt-only drop root."""
from app.core.errors import ValidationError
from app.services.shipping_notice.receipt_report_storage import _read_receipt_callback_file
from app.services.freight_rpa.inbound.report_archive import _read_inbound_callback_file
with app.app_context():
allowed_root = tmp_path / 'receipt-drop'
allowed_root.mkdir()
app.config['FREIGHT_RPA_SHARED_RECEIPT_DROP_DIR'] = str(allowed_root)
app.config['FREIGHT_RPA_SHARED_INBOUND_DROP_DIR'] = str(allowed_root)
outside = tmp_path / 'outside.xlsx'
outside.write_bytes(b'not-a-real-xlsx')
with pytest.raises(ValidationError, match='receipt callback file'):
_read_receipt_callback_file(str(outside))
_read_inbound_callback_file(str(outside))
def test_ingest_empty_persists_no_receipt_data(app):
@@ -230,7 +230,7 @@ def test_ingest_empty_persists_no_receipt_data(app):
schedule_no='SCH-RECEIPT-EMPTY',
name='Receipt empty',
schedule_type='platform_job',
platform_api_path='freight-receipt-rpa',
platform_api_path='freight-rpa-inbound',
owner_user_id=1,
cron_expr='0 8 * * *',
)
@@ -253,7 +253,7 @@ def test_ingest_empty_persists_no_receipt_data(app):
db.session.commit()
result = ingest_receipt_callback({
'schema_version': 'shipping_receipt_rpa.v1',
'schema_version': 'freight_rpa.inbound.v1',
'callback_id': unit.callback_id,
'status': 'empty',
'error_code': 'no_receipt_data',
@@ -269,19 +269,21 @@ def test_ingest_empty_persists_no_receipt_data(app):
def test_receipt_progress_routes_are_registered(app):
"""回归锁:receipt-unit-progress / receipt-batch-progress 必须注册在 Tool 蓝图上。
"""回归锁:inbound-unit-progress / inbound-batch-progress 必须注册在 freight-rpa 蓝图上。
2026-10-08 事故:重构分叉合并丢失两路由,Worker 上报全程 404、活动锚失效、
在飞批次被超时对账误杀——本用例保证路由缺失在任何部署前即红。
(批次 R:路由自旧 shipping-notice rpa 回调前缀迁至 /api/freight-rpa/inbound-*,
处理函数不变,本回归锁同步改指新 URL。)
"""
app.config['SHIPPING_NOTICE_TOOL_API_KEY'] = 'test-tool-key'
client = app.test_client()
headers = {'X-Integration-Key': 'test-tool-key'}
for path, payload in (
('/api/shipping-notice/rpa/receipt-unit-progress',
{'schema_version': 'shipping_receipt_rpa.v1', 'callback_id': 'no-such-callback'}),
('/api/shipping-notice/rpa/receipt-batch-progress',
{'schema_version': 'shipping_receipt_rpa.v1', 'schedule_run_id': 999999999}),
('/api/freight-rpa/inbound-unit-progress',
{'schema_version': 'freight_rpa.inbound.v1', 'callback_id': 'no-such-callback'}),
('/api/freight-rpa/inbound-batch-progress',
{'schema_version': 'freight_rpa.inbound.v1', 'schedule_run_id': 999999999}),
):
# [断言意图] 路由必须存在且鉴权通过(非 401/403/404);无匹配数据时服务层
# 返回 200 + updated=False(注册性探针,不依赖任何夹具数据)。
@@ -293,22 +295,22 @@ def test_receipt_progress_routes_are_registered(app):
def test_read_receipt_callback_file_resolves_drop_root_filename(app, tmp_path):
"""跨机路径契约:Worker 只上报文件名(或历史 Windows 绝对路径),后端按自身 drop 根定位。"""
from app.core.errors import ValidationError
from app.services.shipping_notice.receipt_report_storage import _read_receipt_callback_file
from app.services.freight_rpa.inbound.report_archive import _read_inbound_callback_file
with app.app_context():
drop_root = tmp_path / 'receipt-drop'
drop_root.mkdir()
app.config['FREIGHT_RPA_SHARED_RECEIPT_DROP_DIR'] = str(drop_root)
app.config['FREIGHT_RPA_SHARED_INBOUND_DROP_DIR'] = str(drop_root)
target = drop_root / 'receipt_RUN_X_SITE_NA_2026.xlsx'
target.write_bytes(b'receipt-bytes')
# [断言意图] 裸文件名(新 Worker 契约)与 Windows 本机绝对路径(历史载荷)都
# 必须解析到后端 drop 根内同一文件;根外文件依旧拒绝(任意本机文件读取护栏不变)。
assert _read_receipt_callback_file('receipt_RUN_X_SITE_NA_2026.xlsx') == b'receipt-bytes'
assert _read_receipt_callback_file(
assert _read_inbound_callback_file('receipt_RUN_X_SITE_NA_2026.xlsx') == b'receipt-bytes'
assert _read_inbound_callback_file(
r'C:\HxopsRpaTest\data\freight_rpa\receipt\drop\receipt_RUN_X_SITE_NA_2026.xlsx'
) == b'receipt-bytes'
outside = tmp_path / 'outside.xlsx'
outside.write_bytes(b'x')
with pytest.raises(ValidationError, match='receipt callback file'):
_read_receipt_callback_file(str(outside))
_read_inbound_callback_file(str(outside))