""" tests/test_shipping_wave_rerouting.py 用途:锁定 D8——履约波次(post_ot_fulfillment_wave)不再继承首通知任务级库存源字段, 改为按自身行重跑路由冻结(freeze_wave_notice_routing)。 依赖:post_ot_fulfillment_wave_service / inventory_candidate_freeze_service。 逻辑:波次经 create_shipping_notice_after_inventory_gate 建单,不得携带 inventory_source_* / routing_fingerprint / snapshot_id;freeze_wave_notice_routing 按波次行评估并冻结候选集。 """ from __future__ import annotations import ast import inspect def test_wave_constructor_not_inherit_source_fields_and_reroutes(): """[断言] 波次经 create_shipping_notice_after_inventory_gate 建单不继承首通知源字段,并在建波次后调用 freeze_wave_notice_routing。""" from app.services.shipping_notice import ( inventory_candidate_freeze_service as freeze_svc, post_ot_fulfillment_wave_service as svc, ) fn_node = ast.parse(inspect.getsource(svc.create_post_ot_fulfillment_wave)).body[0] assert isinstance(fn_node, ast.FunctionDef) # [断言 1] create_shipping_notice_after_inventory_gate 调用时无 inventory_source_* / fingerprint / snapshot_id 继承字段。 source_kwargs: set[str] = set() notice_calls = [ node for node in ast.walk(fn_node) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'create_shipping_notice_after_inventory_gate' ] assert notice_calls, 'create_post_ot_fulfillment_wave 必须调用 create_shipping_notice_after_inventory_gate' for call in notice_calls: for kw in call.keywords: if kw.arg is not None: source_kwargs.add(str(kw.arg)) inherited = source_kwargs & { 'inventory_source_code', 'inventory_warehouse_id', 'inventory_source_status', 'inventory_source_method', 'inventory_source_config_version', 'inventory_routing_fingerprint', 'inventory_snapshot_id', } assert not inherited, f"波次不得继承首通知源字段: {sorted(inherited)}" # [断言 2] 建波次后必须调用 freeze_wave_notice_routing(按自身行重跑路由冻结)。 freeze_calls = [ node for node in ast.walk(fn_node) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'freeze_wave_notice_routing' ] assert freeze_calls, 'create_post_ot_fulfillment_wave 必须调用 freeze_wave_notice_routing' assert hasattr(freeze_svc, 'freeze_wave_notice_routing') def test_freeze_wave_notice_routing_routs_by_own_lines(app, monkeypatch): """[断言] freeze_wave_notice_routing 按波次自身行评估候选,并以并集作为任务级候选集冻结。""" from types import SimpleNamespace from app import db from app.models import ShippingNotice, ShippingNoticeLine from app.services.shipping_notice import inventory_candidate_freeze_service as svc src_notice = SimpleNamespace(id=1, task_no='SN-SRC', order_snapshot_id=90, export_file_path=None) notice = ShippingNotice( task_no='SN-WAVE-T6', source_type='post_ot_fulfillment_wave', ) db.session.add(notice) db.session.flush() db.session.add(ShippingNoticeLine( notice_id=int(notice.id), snapshot_line_id=1, position=1, line_no=1, part_number='PN-1', reservation_id='R-1', field_values_json={'f1': 'v1', 'f2': 'v2'}, )) db.session.commit() monkeypatch.setattr('app.services.shipping_notice.export.order_field_id_values', lambda _t: ({'f1': 'v1'}, [])) monkeypatch.setattr( 'app.services.shipping_notice.inventory_routing_engine.evaluate_inventory_source_rules_for_field_values', lambda **_kw: SimpleNamespace( source_codes=['DSV360_AWSBI_BYAWSFRA01'], status='resolved', matched_rules=(), matched_rule_ids=[1], ), ) monkeypatch.setattr( svc, 'get_database_inventory_source', lambda code, **kw: {'source_code': code, 'display_name': code, 'connector_type': 'excel_rpa', 'warehouse_resolution_mode': 'dynamic', 'fixed_warehouse_id': None}, ) captured = {} monkeypatch.setattr( svc, 'freeze_candidates_for_task', lambda task, *, adjudication=None, line_projections=None, **kw: captured.update( {'task': task, 'adjudication': adjudication, 'line_projections': line_projections} ) or svc.CandidateFreezeResult( candidate_set_id=1, created_new=True, source_codes=[c['source_code'] for c in adjudication.candidates], line_count=len(line_projections), manifest_snapshot_sha256='', set_version=1, ), ) svc.freeze_wave_notice_routing(notice, source_notice=src_notice) assert captured['task'] is notice assert captured['line_projections'][0]['candidate_inventory_source_codes'] == ['DSV360_AWSBI_BYAWSFRA01'] assert captured['adjudication'].status == 'candidates_frozen' assert [c['source_code'] for c in captured['adjudication'].candidates] == ['DSV360_AWSBI_BYAWSFRA01'] def test_freeze_wave_notice_routing_falls_back_to_task_candidates(app, monkeypatch): """[断言] 波次行无规则命中时回退到订单级 task 候选集(与首单行为一致)。""" from types import SimpleNamespace from app import db from app.models import ShippingNotice, ShippingNoticeLine from app.services.shipping_notice import inventory_candidate_freeze_service as svc src_notice = SimpleNamespace(id=1, task_no='SN-SRC', order_snapshot_id=90, export_file_path=None) notice = ShippingNotice( task_no='SN-WAVE-FB', source_type='post_ot_fulfillment_wave', ) db.session.add(notice) db.session.flush() db.session.add(ShippingNoticeLine( notice_id=int(notice.id), snapshot_line_id=1, position=1, line_no=1, part_number='PN-1', reservation_id='R-1', field_values_json={'f1': 'v1'}, )) db.session.commit() monkeypatch.setattr('app.services.shipping_notice.export.order_field_id_values', lambda _t: ({'f1': 'v1'}, [])) def _eval(**kw): # task 级命中单源;行级规则不命中(unresolved)→ 应回退到 task 候选集。 if kw.get('scope') == 'task': return SimpleNamespace(status='resolved', source_codes=['DSV360_AWSDU'], matched_rules=(), matched_rule_ids=[1]) return SimpleNamespace(status='unresolved', source_codes=[], matched_rules=(), matched_rule_ids=[]) monkeypatch.setattr('app.services.shipping_notice.inventory_routing_engine.evaluate_inventory_source_rules_for_field_values', _eval) monkeypatch.setattr( svc, 'get_database_inventory_source', lambda code, **kw: {'source_code': code, 'display_name': code, 'connector_type': 'excel_rpa', 'warehouse_resolution_mode': 'dynamic', 'fixed_warehouse_id': None}, ) captured = {} monkeypatch.setattr( svc, 'freeze_candidates_for_task', lambda task, *, adjudication=None, line_projections=None, **kw: captured.update( {'adjudication': adjudication, 'line_projections': line_projections} ) or svc.CandidateFreezeResult( candidate_set_id=1, created_new=True, source_codes=[c['source_code'] for c in adjudication.candidates], line_count=len(line_projections), manifest_snapshot_sha256='', set_version=1, ), ) svc.freeze_wave_notice_routing(notice, source_notice=src_notice) assert captured['line_projections'][0]['candidate_inventory_source_codes'] == ['DSV360_AWSDU'] assert captured['line_projections'][0]['selection_mode'] == 'task_fallback' assert [c['source_code'] for c in captured['adjudication'].candidates] == ['DSV360_AWSDU']