""" tests/test_line_closure_fulfillment.py 用途:验证行结案(close_unfulfillable)在满足性聚合与整单门禁中的豁免语义。 依赖:pytest、fulfillment_workflow、行级履约决定模型、冻结订单夹具。 逻辑:两行订单关闭短缺行后整单应判定满足;全部行结案时整单不通过(无剩余履约内容)。 """ from __future__ import annotations from app import db from app.models import ( ShippingNoticeLineInventoryMatch, ShippingOrderReviewCase, ShippingOrderReviewLineDecision, ) from app.services.shipping_notice.fulfillment_workflow import ( _closed_snapshot_line_ids_for_task, _recompute_task_match_aggregate, match_completed, ) from tests.test_effective_order_view import _seed_frozen_order def _seed_match_facts(fixture): """第 0 行 matched+ready,第 1 行 not_found 短缺(历史阻塞点)。""" for snap_line, result, shortage in ( (fixture.snap_lines[0], 'matched', '0'), (fixture.snap_lines[1], 'not_found', '10'), ): db.session.add(ShippingNoticeLineInventoryMatch( task_id=fixture.order.id, snapshot_line_id=snap_line.id, line_no=snap_line.line_no, line_index=snap_line.position, match_result=result, inventory_status='ready', required_qty='10', available_qty='10' if result == 'matched' else '0', shortage_qty=shortage, reservation_id=snap_line.reservation_id, part_number=snap_line.part_number, is_current=True, )) db.session.flush() def _seed_close_decision(fixture, line_index, *, case_no='SNRC-CLOSE-1', version=1): """给已确认 Case 补一条活动结案决定(FK 不强制,挂哨兵父 id 即可)。""" case = ShippingOrderReviewCase.query.filter_by(case_no=case_no).first() if case is None: case = ShippingOrderReviewCase( case_no=case_no, run_id=901, unit_id=902, document_id=903, owner_user_id=fixture.user.id, status='confirmed', confirmed_task_id=fixture.order.id, ) db.session.add(case) db.session.flush() db.session.add(ShippingOrderReviewLineDecision( review_case_id=case.id, draft_line_id=f'line-{line_index}', line_index=line_index, decision_version=version, decision_code='close_unfulfillable', decision_source='user', line_status='closed_unfulfillable', is_active=True, reason='该行不再发货', actor_user_id=fixture.user.id, snapshot_json={}, )) db.session.flush() def test_closed_line_ids_helper_mirrors_release_query(app): """结案行集合必须与放行同款:活动决定 + line_index 映射快照行。""" with app.app_context(): fixture = _seed_frozen_order() _seed_close_decision(fixture, 1) # 断言:仅第 1 行(line_index=1)进入结案集合。 assert _closed_snapshot_line_ids_for_task(fixture.order) == {int(fixture.snap_lines[1].id)} def test_aggregate_exempts_closed_shortage_line(app): """关闭短缺行后聚合应判满足:closed 行不阻断、不进缺货列表。""" with app.app_context(): fixture = _seed_frozen_order() _seed_match_facts(fixture) _seed_close_decision(fixture, 1) status, shortages = _recompute_task_match_aggregate(fixture.order) payload = fixture.order.step_payload_json or {} line_results = payload['current_match_projection']['line_results'] # 断言:整单满足、缺货列表为空、结案行状态为 closed 而非 shortage。 assert status == 'fulfillable' assert shortages == [] assert line_results[1]['line_status'] == 'closed' def test_match_completed_passes_with_closed_line(app): """整单满足性门禁对结案行豁免:一行 matched + 一行 closed 即通过。""" with app.app_context(): fixture = _seed_frozen_order() _seed_match_facts(fixture) # 断言:结案前整单不通过(历史阻塞行为)。 assert match_completed(fixture.order) is False _seed_close_decision(fixture, 1) # 断言:结案后整单通过(本任务修复目标)。 assert match_completed(fixture.order) is True def test_all_lines_closed_fails_match_completed(app): """全部行结案=无剩余履约内容:整单满足性不通过(此类单应走作废流程)。""" with app.app_context(): fixture = _seed_frozen_order() _seed_match_facts(fixture) _seed_close_decision(fixture, 1, version=1) _seed_close_decision(fixture, 0, version=1) # 断言:无任何待履约行时 match_completed 返回 False,避免自动建空通知。 assert match_completed(fixture.order) is False def test_all_lines_closed_aggregate_is_unfulfillable(app): """全结案聚合口径锁定:结案行不计满足 ⇒ 整单 unfulfillable(与 match_completed 一致)。""" with app.app_context(): fixture = _seed_frozen_order() _seed_match_facts(fixture) _seed_close_decision(fixture, 1, version=1) _seed_close_decision(fixture, 0, version=1) status, shortages = _recompute_task_match_aggregate(fixture.order) # 断言:无 ok 也无 block(无可履约内容)⇒ unfulfillable,且不产生缺货列表。 assert status == 'unfulfillable' assert shortages == [] def test_closed_status_precedence_over_ok(app): """分类优先级锁定:matched+ready 行结案后记 closed(非 ok),且不再贡献整单满足。""" with app.app_context(): fixture = _seed_frozen_order() # 仅第 0 行有匹配事实(matched+ready)且已结案:聚合视角为单行订单。 snap_line = fixture.snap_lines[0] db.session.add(ShippingNoticeLineInventoryMatch( task_id=fixture.order.id, snapshot_line_id=snap_line.id, line_no=snap_line.line_no, line_index=snap_line.position, match_result='matched', inventory_status='ready', required_qty='10', available_qty='10', shortage_qty='0', reservation_id=snap_line.reservation_id, part_number=snap_line.part_number, is_current=True, )) db.session.flush() _seed_close_decision(fixture, 0) status, shortages = _recompute_task_match_aggregate(fixture.order) payload = fixture.order.step_payload_json or {} line_results = payload['current_match_projection']['line_results'] # 断言:结案分类优先于匹配满足;无 ok 无 block ⇒ 整单 unfulfillable。 assert line_results[0]['line_status'] == 'closed' assert status == 'unfulfillable' assert shortages == []