""" tests/test_shipping_line_source_ambiguity.py 用途:锁定多候选库存源「数据自证 + 歧义兜底」契约(设计 D4/D5,2026-08-29)。 依赖:stp_line_inventory_match.match_order_lines_across_candidates。 逻辑:同一行命中多源必须 multi_file_conflict 阻断并携带候选源列表(禁止静默选源); 唯一源命中按行反写 源/可用量/缺口。既有能力以测试固化,防止后续回归。 """ from __future__ import annotations from app.services.shipping_notice.stp_line_inventory_match import ( match_order_lines_across_candidates, ) def test_same_reservation_across_two_sources_marks_conflict_with_candidates(): """[断言] 同一预约单在两个源都有库存:该行 multi_file_conflict 阻断并带候选源列表, resolved source 必须为空(选源交由人工 override_line_inventory_source)。""" result = match_order_lines_across_candidates( order_lines=[{'line_key': 'L1', 'part_number': 'PN-1', 'reservation_id': '30001', 'quantity': '5'}], inventory_lines=[ {'reservation_id': '30001', 'part_number': 'PN-1', 'qty': '5', '__source_code': 'SRC_A'}, {'reservation_id': '30001', 'part_number': 'PN-1', 'qty': '9', '__source_code': 'SRC_B'}, ], ) line = result['line_results'][0] assert line['disposition'] == 'multi_file_conflict' assert set(line['candidate_hit_source_codes']) == {'SRC_A', 'SRC_B'} assert line['resolved_inventory_source_code'] is None # 歧义行必须阻断整单可履行状态,防止带着未定源进入导出。 assert result['match_status'] == 'unfulfillable' assert result['resolutions'][0]['candidate_hit_source_codes'] == line['candidate_hit_source_codes'] def test_unique_source_hit_writes_back_source_qty_and_shortage(): """[断言] 唯一源命中:足量反写 ok+源+零缺口;不足反写 shortage+缺口数量。""" base_lines = [ {'line_key': 'L1', 'part_number': 'PN-1', 'reservation_id': '30001', 'quantity': '5'}, ] inventory_full = [ {'reservation_id': '30001', 'part_number': 'PN-1', 'qty': '5', '__source_code': 'SRC_A'}, ] result_full = match_order_lines_across_candidates( order_lines=base_lines, inventory_lines=inventory_full, ) line_full = result_full['line_results'][0] assert line_full['line_status'] == 'ok' assert line_full['resolved_inventory_source_code'] == 'SRC_A' assert line_full['available_qty'] == 5.0 assert line_full['shortage_qty'] == 0.0 inventory_short = [ {'reservation_id': '30001', 'part_number': 'PN-1', 'qty': '2', '__source_code': 'SRC_A'}, ] result_short = match_order_lines_across_candidates( order_lines=base_lines, inventory_lines=inventory_short, ) line_short = result_short['line_results'][0] assert line_short['line_status'] == 'partial' # 有货但不足 → partial(disposition=shortage) assert line_short['disposition'] == 'shortage' assert line_short['resolved_inventory_source_code'] == 'SRC_A' assert line_short['available_qty'] == 2.0 assert line_short['shortage_qty'] == 3.0 def test_disambiguation_via_pinned_allowed_sources_resolves_conflict(): """[断言] 人工选源后(行 allowed_source_codes 钉住单一源):原冲突行在钉住源内正常匹配, 这是 override_line_inventory_source 行级重匹配的数据前提。""" result = match_order_lines_across_candidates( order_lines=[{ 'line_key': 'L1', 'part_number': 'PN-1', 'reservation_id': '30001', 'quantity': '5', 'allowed_source_codes': ['SRC_B'], }], inventory_lines=[ {'reservation_id': '30001', 'part_number': 'PN-1', 'qty': '5', '__source_code': 'SRC_A'}, {'reservation_id': '30001', 'part_number': 'PN-1', 'qty': '9', '__source_code': 'SRC_B'}, ], ) line = result['line_results'][0] assert line['disposition'] == 'ok' assert line['resolved_inventory_source_code'] == 'SRC_B' assert line['available_qty'] == 9.0