""" tests/test_line_inventory_match_projection.py 用途:锁定库存匹配投影契约——not_found→0、result_missing 不得转 0、动作分域。 依赖:pytest、line_onhand_match_projection。 逻辑:纯单元测试,不启 Flask 全应用。 """ from __future__ import annotations from app.services.shipping_notice.line_onhand_match_projection import ( ACTION_CLOSE, ACTION_RELEASE_SHORTAGE, ACTION_RETRY_MATCHING, ACTION_RETRY_PREPARATION, normalize_match_quantities, project_line_inventory_view, project_user_line_status, resolve_inventory_allowed_actions, resolve_line_allowed_actions, ) def test_ready_not_found_normalizes_to_zero_shortage(): """ready + not_found 必须规范为 available=0 并标短缺。""" qty = normalize_match_quantities( match_result='not_found', inventory_status='ready', required_qty='30', available_qty=None, ) assert qty['available_qty'] == '0' assert qty['shortage_qty'] == '30' assert qty['is_shortage'] is True def test_ready_result_missing_must_not_become_zero(): """ready + result_missing 不得转 0,也不能标短缺。""" qty = normalize_match_quantities( match_result='result_missing', inventory_status='ready', required_qty='30', available_qty=None, ) assert qty['available_qty'] is None assert qty['shortage_qty'] is None assert qty['is_shortage'] is False assert qty['display_available'] == 'pending' def test_preparing_keeps_null_quantities(): """库存处理中:可用数量保持未知。""" qty = normalize_match_quantities( match_result='preparing', inventory_status='preparing', required_qty='30', available_qty=None, ) assert qty['available_qty'] is None assert qty['is_shortage'] is False def test_matched_compares_quantities(): """matched 按实际数量比较短缺。""" ok = normalize_match_quantities( match_result='matched', inventory_status='ready', required_qty='30', available_qty='50', ) assert ok['is_shortage'] is False assert ok['available_qty'] == '50' short = normalize_match_quantities( match_result='matched', inventory_status='ready', required_qty='30', available_qty='10', ) assert short['is_shortage'] is True assert short['shortage_qty'] == '20' def test_preparing_without_rpa_allows_retry_preparation(): """文件未准备(非 RPA 运行中)可重试获取;关闭在 line 域。""" inv = resolve_inventory_allowed_actions( has_formal_ot=True, inventory_status='preparing', match_result='preparing', is_shortage=False, stored_line_status='pending_decision', user_line_status='inventory_file_missing', inventory_rpa_running=False, ) line = resolve_line_allowed_actions( has_formal_ot=True, stored_line_status='pending_decision', user_line_status='inventory_file_missing', ) assert inv == [ACTION_RETRY_PREPARATION] assert ACTION_CLOSE in line assert ACTION_RETRY_MATCHING not in inv def test_shortage_actions_preparation_and_close_only(): """短缺:库存域仅 preparation;行域可关闭;禁止 wait/换源。""" inv = resolve_inventory_allowed_actions( has_formal_ot=True, inventory_status='ready', match_result='not_found', is_shortage=True, stored_line_status='pending_decision', user_line_status='shortage', ) line = resolve_line_allowed_actions( has_formal_ot=True, stored_line_status='pending_decision', user_line_status='shortage', ) assert inv == [ACTION_RETRY_PREPARATION] # 断言意图(既有用例修复):对齐已上线契约(eed1068ae)——短缺行除关闭外 # 同时下发 release_inventory_shortage(填写原因放行),本用例原断言已过期。 assert line == [ACTION_CLOSE, ACTION_RELEASE_SHORTAGE] def test_wave_created_hides_line_actions(): """已入波次不再下发 view_fulfillment_detail。""" line = resolve_line_allowed_actions( has_formal_ot=True, stored_line_status='task_created', user_line_status='wave_created', ) inv = resolve_inventory_allowed_actions( has_formal_ot=True, inventory_status='ready', match_result='matched', is_shortage=False, stored_line_status='task_created', user_line_status='wave_created', ) assert inv == [] assert line == [] def test_result_missing_projects_file_missing_and_retry_prep(): """结果缺失投影为文件未准备,开放重试获取,不当短缺。""" view = project_line_inventory_view( has_formal_ot=True, fact={ 'match_result': 'result_missing', 'inventory_status': 'ready', 'available_qty': None, 'inventory_snapshot_id': 9, 'matched_at': '2026-08-07T00:00:00', }, required_qty='30', stored_line_status='pending_decision', decision_source=None, can_fulfill_complete_line=False, ) assert view['available_qty'] is None assert view['is_shortage'] is False assert view['user_line_status'] == 'inventory_file_missing' assert ACTION_RETRY_PREPARATION in view['inventory_allowed_actions'] assert ACTION_CLOSE in view['line_allowed_actions'] def test_line_inventory_view_preserves_evidence_identity_fields(): """行投影必须保留文件 SHA、文件行号与匹配 Attempt,供证据审计展示。""" view = project_line_inventory_view( has_formal_ot=True, fact={ 'match_result': 'matched', 'inventory_status': 'ready', 'available_qty': '8', 'inventory_snapshot_id': 9, 'matched_at': '2026-08-07T00:00:00', 'inventory_file_no': 'INV-001', 'file_sha256': 'a' * 64, 'matched_row_index': 17, 'match_attempt_id': 301, 'inventory_source_code': 'DBS_DUB', }, required_qty='3', stored_line_status='ready_to_fulfill', decision_source='system', can_fulfill_complete_line=True, ) assert view['inventory_file_no'] == 'INV-001' assert view['inventory_file_sha256'] == 'a' * 64 assert view['inventory_row_index'] == 17 assert view['match_attempt_id'] == 301 def test_user_status_maps_closed_and_system_ready(): """存储态关闭与系统 ready 投影为用户态。""" assert project_user_line_status( inventory_status='ready', match_result='matched', is_shortage=False, stored_line_status='closed_unfulfillable', ) == 'closed' assert project_user_line_status( inventory_status='ready', match_result='matched', is_shortage=False, stored_line_status='ready_to_fulfill', decision_source='system', ) == 'auto_fulfilling' def test_view_exposes_part_available_qty_gated_by_ready(): """料号兜底总量必须随投影透出,且非 ready 一律 None(与 available_qty 同门控)。""" base = { 'match_result': 'not_found', 'inventory_status': 'ready', 'required_qty': '8', 'available_qty': None, 'inventory_snapshot_id': 1, 'matched_at': None, 'inventory_source_code': 'DSV', 'detail_json': {}, } view = project_line_inventory_view( has_formal_ot=True, fact={**base, 'part_available_qty': '9'}, required_qty='8', stored_line_status=None, decision_source=None, can_fulfill_complete_line=False, ) # 断言意图:not_found 行 available 规范为 0,兜底量原样透出(前端新列数据源)。 assert view['available_qty'] == '0' assert view['part_available_qty'] == '9' pending = project_line_inventory_view( has_formal_ot=True, fact={**base, 'inventory_status': 'preparing', 'part_available_qty': '9'}, required_qty='8', stored_line_status=None, decision_source=None, can_fulfill_complete_line=False, ) # 断言意图:非 ready 一律 pending,兜底量同样不下发,避免半就绪状态误导。 assert pending['part_available_qty'] is None