""" tests/test_shipping_notice_ui_state.py 用途:校验发货通知 UI State 摘要字段与 entry_surface 映射。 依赖:pytest、ShippingNotice、ui_state、state_machine constants。 逻辑:build_task_ui_state.summary 含 STP 可解释字段;复核 vs 待上传入口面正确。 """ from app.models import ShippingNotice from app.services.shipping_notice.constants import ( CP_EXPORT_READY, CP_PARSE_REVIEW, PH_LOW_CONF, RS_PARSE_REVIEW, ) from app.services.shipping_notice.ui_state import build_task_ui_state _REQUIRED_SUMMARY_KEYS = { "title", "message", "issues", "stage", "checkpoint_kind", "current_todo", "pause_reason", "next_auto_step", "event_at", "entry_surface", "allowed_actions", } def test_build_task_ui_state_summary_has_required_keys(app) -> None: """summary 必须包含 STP 可解释字段全集。""" with app.app_context(): task = ShippingNotice( task_no="SN-UI-SUMMARY", process_status="pending_review", review_status=RS_PARSE_REVIEW, pending_human_reason=PH_LOW_CONF, source_type="manual", ) state = build_task_ui_state(task) assert set(_REQUIRED_SUMMARY_KEYS).issubset(state["summary"].keys()) assert state["checkpoint"] == CP_PARSE_REVIEW assert state["checkpoint_kind"] == "must_human" assert "stp_facts" not in state["summary"] def test_entry_surface_parse_review_is_review_workbench(app) -> None: """解析复核应进入人工复核工作台。""" with app.app_context(): task = ShippingNotice( task_no="SN-UI-REVIEW", process_status="pending_review", review_status=RS_PARSE_REVIEW, pending_human_reason=PH_LOW_CONF, source_type="manual", ) state = build_task_ui_state(task) assert state["summary"]["entry_surface"] == "review_workbench" def test_entry_surface_export_ready_is_task_workbench(app) -> None: """Excel 待人工上传登记应进入任务处理工作台(下载 + 登记)。""" with app.app_context(): task = ShippingNotice( task_no="SN-UI-EXPORT", process_status="matched", source_type="manual", export_file_path="/tmp/shipping-export.xlsx", export_file_name="shipping-export.xlsx", ) state = build_task_ui_state(task) assert state["checkpoint"] == CP_EXPORT_READY assert state["checkpoint_kind"] == "wait_manual_upload" assert state["summary"]["entry_surface"] == "task_workbench" assert state["summary"].get("pause_reason_code") == "wait_manual_upload" assert "登记凭据" in str(state["summary"].get("pause_reason") or "") button_ids = [b.get("id") for b in (state.get("action_buttons") or [])] assert "download_excel" in button_ids assert "mark_manual_uploaded" in button_ids def test_stp_facts_candidates_frozen_projected(app) -> None: """多候选冻结任务须投影 stp_facts.frozen_candidate_codes。""" with app.app_context(): task = ShippingNotice( task_no="SN-UI-STP-FROZEN", process_status="waiting_inventory", source_type="manual", inventory_source_status="candidates_frozen", inventory_source_method="stp_build_freeze", step_payload_json={ "line_inventory_candidates": { "source_codes": ["DBS_DUB", "DBS_RTM"], "frozen_at": "build", }, }, ) state = build_task_ui_state(task) facts = state["summary"]["stp_facts"] assert facts["inventory_source_status"] == "candidates_frozen" assert facts["frozen_candidate_codes"] == ["DBS_DUB", "DBS_RTM"] assert facts["multi_source_excel_block"] is False def test_stp_facts_projects_line_resolutions(app) -> None: """stp_facts 须包含逐行 Reservation/料号/实际库存源/文件证据。""" with app.app_context(): task = ShippingNotice( task_no="SN-UI-STP-LINES", process_status="matched", source_type="manual", match_status="fulfillable", inventory_source_status="resolved", inventory_source_code="DBS_DUB", inventory_source_method="stp_line_match", step_payload_json={ "line_inventory_resolutions": [ { "line_index": 0, "disposition": "ok", "effective_reservation_id": "3000092917", "part_number": "PN-1", "resolved_inventory_source_code": "DBS_DUB", "inventory_file_no": "INV-1", "inventory_row_ref": { "reservation_id": "3000092917", "part_number": "PN-1", "source_code": "DBS_DUB", }, }, ], }, ) state = build_task_ui_state(task) rows = state["summary"]["stp_facts"]["line_resolutions"] assert rows[0]["effective_reservation_id"] == "3000092917" assert rows[0]["part_number"] == "PN-1" assert rows[0]["resolved_inventory_source_code"] == "DBS_DUB" assert rows[0]["inventory_file_no"] == "INV-1" def test_stp_facts_multi_source_excel_block_legacy_payload(app) -> None: """历史 multi_source_excel_block 载荷仍覆盖 pause_reason(兼容旧任务)。""" from app.services.shipping_notice.constants import RS_SHORTAGE_REVIEW with app.app_context(): task = ShippingNotice( task_no="SN-UI-STP-MULTI", process_status="matched", review_status=RS_SHORTAGE_REVIEW, pending_human_reason="shortage", source_type="manual", match_status="fulfillable", inventory_source_status="candidates_frozen", inventory_source_method="stp_line_match", step_payload_json={ "multi_source_excel_block": True, "resolved_ok_source_codes": ["DBS_DUB", "DBS_RTM"], "line_inventory_resolutions": [ {"line_index": 0, "disposition": "ok", "resolved_inventory_source_code": "DBS_DUB"}, {"line_index": 1, "disposition": "ok", "resolved_inventory_source_code": "DBS_RTM"}, ], }, ) state = build_task_ui_state(task) summary = state["summary"] assert summary["pause_reason_code"] == "multi_source_excel_block" assert summary["stp_facts"]["multi_source_excel_block"] is True assert summary["entry_surface"] == "task_workbench"