feat(shipping-notice): 收敛订单处理状态与履约门禁
This commit is contained in:
1 parent
bffcd55b40
commit
70dd0051b8
153 files changed
+10916
-4296
No files matched your search
@@ -2,8 +2,12 @@
|
||||
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 待上传入口面正确。
|
||||
依赖:pytest、ShippingNotice、ui_state、state_machine constants、库存源/发货模板模型。
|
||||
逻辑:build_task_ui_state.summary 含 STP 可解释字段;复核 vs 待上传入口面正确;
|
||||
stage_context.preparation 短缺行投影 release_required_fields;
|
||||
preparation.source_options 下发已启用源供行级覆写下拉;
|
||||
行级 selected_source_code(Candidate)与 source_code(Match)分字段;
|
||||
Notice 详情经 order_task_id→OT 读取 Snapshot/Match。
|
||||
"""
|
||||
|
||||
from app import db
|
||||
@@ -14,9 +18,8 @@ from app.models import (
|
||||
ShippingOrderSnapshot, ShippingOrderSnapshotLine, ShippingOrderTask, User,
|
||||
)
|
||||
from app.services.shipping_notice.constants import (
|
||||
CP_EXPORT_READY,
|
||||
CP_ORDER_FREEZE,
|
||||
CP_PARSE_REVIEW,
|
||||
POSITION_EXPORT_READY,
|
||||
POSITION_PARSE_REVIEW,
|
||||
PH_LOW_CONF,
|
||||
RS_PARSE_REVIEW,
|
||||
)
|
||||
@@ -29,7 +32,7 @@ _REQUIRED_SUMMARY_KEYS = {
|
||||
"message",
|
||||
"issues",
|
||||
"stage",
|
||||
"checkpoint_kind",
|
||||
"user_mode",
|
||||
"current_todo",
|
||||
"pause_reason",
|
||||
"next_auto_step",
|
||||
@@ -145,8 +148,9 @@ def test_build_task_ui_state_summary_has_required_keys(app) -> None:
|
||||
)
|
||||
state = build_task_ui_state(task)
|
||||
assert set(_REQUIRED_SUMMARY_KEYS).issubset(state["summary"].keys())
|
||||
assert state["checkpoint"]["code"] == CP_ORDER_FREEZE
|
||||
assert state["checkpoint_kind"] == "auto_continue"
|
||||
# 无 Gate 时按领域标量回退:parse_review 不得再被 OT 空 stage 盖成 order_freeze。
|
||||
assert state["position"] == POSITION_PARSE_REVIEW
|
||||
assert state["user_mode"] == "must_human"
|
||||
assert "stp_facts" not in state["summary"]
|
||||
|
||||
|
||||
@@ -164,7 +168,7 @@ def test_task_ui_state_stage_context_is_opt_in(app) -> None:
|
||||
|
||||
|
||||
def test_build_task_ui_state_without_formal_gate_is_unclassified(app) -> None:
|
||||
"""旧 checkpoint 不能再推导五阶段或任何可写 UI 动作。"""
|
||||
"""无正式 Gate 时位置不能再推导五阶段或任何可写 UI 动作。"""
|
||||
with app.app_context():
|
||||
task = ShippingOrderTask(
|
||||
task_no="OT-UI-STAGE-ACTIONS", user_id=1,
|
||||
@@ -181,7 +185,7 @@ def test_build_task_ui_state_without_formal_gate_is_unclassified(app) -> None:
|
||||
|
||||
|
||||
def test_build_task_ui_state_without_formal_gate_has_no_lifecycle_entries(app) -> None:
|
||||
"""正式工作台入口必须来自 Gate,不能按旧 checkpoint 生成。"""
|
||||
"""正式工作台入口必须来自 Gate,不能按旧位置码生成。"""
|
||||
with app.app_context():
|
||||
task = ShippingOrderTask(
|
||||
task_no="OT-UI-LIFECYCLE-ENTRIES", user_id=1,
|
||||
@@ -305,29 +309,65 @@ def test_entry_surface_parse_review_is_review_workbench(app) -> None:
|
||||
source_type="direct_pdf",
|
||||
)
|
||||
state = build_task_ui_state(task)
|
||||
assert state["summary"]["entry_surface"] == "task_workbench"
|
||||
assert state["summary"]["entry_surface"] == "review_workbench"
|
||||
|
||||
|
||||
def test_build_task_ui_state_queries_process_gate_once(app, monkeypatch) -> None:
|
||||
"""build_task_ui_state 同一次构建只查 Gate 摘要一次,禁止再经 resolve 二次查库。"""
|
||||
with app.app_context():
|
||||
calls = {'n': 0}
|
||||
|
||||
def _fake_gate(_task):
|
||||
calls['n'] += 1
|
||||
return {
|
||||
'current_gate_code': 'inventory_sufficiency',
|
||||
'status': 'blocked',
|
||||
'lifecycle_stage': 'fulfillment_prep',
|
||||
'current_blocking_gate': {
|
||||
'gate_code': 'inventory_sufficiency',
|
||||
'status': 'blocked_manual',
|
||||
},
|
||||
'primary_action': None,
|
||||
'action_presentation': [],
|
||||
}
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.ui_state._process_gate_user_summary_for_task',
|
||||
_fake_gate,
|
||||
)
|
||||
task = ShippingOrderTask(task_no="OT-UI-GATE-ONCE", user_id=1, source_type="direct_pdf")
|
||||
state = build_task_ui_state(task)
|
||||
assert calls['n'] == 1
|
||||
assert state['lifecycle_stage'] == 'fulfillment_prep'
|
||||
|
||||
|
||||
def test_entry_surface_export_ready_is_task_workbench(app, monkeypatch) -> None:
|
||||
"""Excel Artifact 就绪后仍进入工作台,且未解析的文件不得擅自授权下载。"""
|
||||
"""Gate artifact_generation 投影 export_ready 时进入工作台;未验证 Artifact 不得授权下载。"""
|
||||
with app.app_context():
|
||||
task = ShippingNotice(
|
||||
task_no="SN-UI-EXPORT",
|
||||
source_type="manual",
|
||||
)
|
||||
# Excel 是否就绪只能由 Artifact 事实裁决,不能恢复 Notice 旧文件投影。
|
||||
from types import SimpleNamespace
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.upload_attempt_service.latest_export_artifact',
|
||||
lambda _task: SimpleNamespace(id=0, file_path='/tmp/shipping-export.xlsx', file_name='shipping-export.xlsx'),
|
||||
'app.services.shipping_notice.ui_state._process_gate_user_summary_for_task',
|
||||
lambda _task: {
|
||||
'current_gate_code': 'artifact_generation',
|
||||
'status': 'waiting_human',
|
||||
'lifecycle_stage': 'freight_upload',
|
||||
'current_blocking_gate': {
|
||||
'gate_code': 'artifact_generation',
|
||||
'status': 'blocked_manual',
|
||||
},
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.upload_attempt_service.latest_upload_attempt',
|
||||
'app.services.shipping_notice.ui_state.resolve_task_export_download',
|
||||
lambda _task: None,
|
||||
)
|
||||
state = build_task_ui_state(task)
|
||||
assert state["checkpoint"]["code"] == CP_EXPORT_READY
|
||||
assert state["checkpoint_kind"] == "wait_manual_upload"
|
||||
assert state["position"] == POSITION_EXPORT_READY
|
||||
# Gate freight_upload + waiting_human → wait_manual_upload
|
||||
assert state["user_mode"] == "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 "")
|
||||
@@ -336,7 +376,7 @@ def test_entry_surface_export_ready_is_task_workbench(app, monkeypatch) -> None:
|
||||
for item in [state["actions"].get("primary"), *(state["actions"].get("secondary") or [])]
|
||||
if item
|
||||
]
|
||||
# 关键断言:没有可验证 Artifact 时,遗留 export_file_name 不得生成下载按钮。
|
||||
# 关键断言:没有可验证 Artifact 时不得生成下载按钮。
|
||||
assert "download_excel" not in button_ids
|
||||
assert "mark_manual_uploaded" not in button_ids
|
||||
|
||||
@@ -498,6 +538,7 @@ def test_stp_facts_projects_line_resolutions(app) -> None:
|
||||
assert line["status"] == "inventory_matched"
|
||||
assert line["source_ambiguous"] is False
|
||||
assert line["source_candidates"] == []
|
||||
assert line["can_fulfill_complete_line"] is True
|
||||
# §1.5 明细按钮数据源:matched/ready 无短缺 → 无库存重试动作;行级库存文件号下发供下载按钮;
|
||||
# line_allowed_actions 仍开放 close_unfulfillable(非终态行可关闭)。
|
||||
assert line["inventory_file_no"] == "INV-1"
|
||||
@@ -571,8 +612,15 @@ def test_stage_context_prefers_current_structured_inventory_match(app) -> None:
|
||||
assert "retry_inventory_preparation" in line["inventory_allowed_actions"]
|
||||
|
||||
|
||||
def test_stage_context_preparation_projects_source_ambiguity_and_lock(app) -> None:
|
||||
"""行级 source_conflict 与候选源只读 Match;Excel 生成后锁定。"""
|
||||
def test_stage_context_preparation_projects_source_ambiguity_and_lock(app, monkeypatch) -> None:
|
||||
"""行级 source_conflict 与候选源只读 Match;Excel 未生成时下发 source_options。"""
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.inventory_source_repository.list_database_inventory_sources',
|
||||
lambda enabled_only=True: [
|
||||
{'source_code': 'src_a', 'display_name': 'Source A'},
|
||||
{'source_code': 'SRC_B', 'display_name': ''},
|
||||
],
|
||||
)
|
||||
with app.app_context():
|
||||
user = User(username='ui-source-ambiguity', email='ui-source-ambiguity@example.com')
|
||||
db.session.add(user)
|
||||
@@ -602,8 +650,97 @@ def test_stage_context_preparation_projects_source_ambiguity_and_lock(app) -> No
|
||||
assert preparation['lines'][0]['source_candidates'] == ['SRC_A', 'SRC_B']
|
||||
assert preparation['lines'][0]['source_code'] is None
|
||||
assert preparation['source_locked'] is False
|
||||
# 关键断言:行级覆写下拉依赖 preparation.source_options;编码统一大写。
|
||||
assert preparation['source_options'] == [
|
||||
{'source_code': 'SRC_A', 'display_name': 'Source A'},
|
||||
{'source_code': 'SRC_B', 'display_name': 'SRC_B'},
|
||||
]
|
||||
|
||||
|
||||
def test_stage_context_projects_selected_source_without_backfilling_match_source(app) -> None:
|
||||
"""CandidateSet 单源只填 selected_source_code,禁止回填 Match 的 source_code。"""
|
||||
with app.app_context():
|
||||
task, line = _frozen_task_with_line(
|
||||
task_no='OT-UI-SELECTED-SOURCE',
|
||||
reservation_id='RSV-SEL',
|
||||
part_number='PN-SEL',
|
||||
qty='2',
|
||||
)
|
||||
candidate_set = ShippingInventoryCandidateSet(
|
||||
task_id=task.id, set_version=1, lifecycle_status='frozen', input_fingerprint='b' * 64,
|
||||
)
|
||||
db.session.add(candidate_set)
|
||||
db.session.flush()
|
||||
db.session.add(ShippingInventoryCandidateSource(
|
||||
candidate_set_id=candidate_set.id, source_code='DSV360_AWSSG', candidate_order=1,
|
||||
))
|
||||
db.session.add(ShippingInventoryCandidateLine(
|
||||
candidate_set_id=candidate_set.id, line_no=1, source_codes_json=['DSV360_AWSSG'],
|
||||
))
|
||||
task.active_inventory_candidate_set_id = candidate_set.id
|
||||
db.session.commit()
|
||||
|
||||
preparation = build_task_ui_state(task, include_stage_context=True)['stage_context']['preparation']
|
||||
row = preparation['lines'][0]
|
||||
# 关键断言:取数目标与匹配结论分字段;未匹配时 source_code 必须为空。
|
||||
assert row['snapshot_line_id'] == line.id
|
||||
assert row['selected_source_code'] == 'DSV360_AWSSG'
|
||||
assert row['source_code'] is None
|
||||
assert row['source_ambiguous'] is False
|
||||
|
||||
|
||||
def test_stage_context_notice_resolves_ot_match_and_selected_source(app) -> None:
|
||||
"""Notice 详情入口须经 order_task_id→OT 读 Snapshot/Match/Candidate,禁止用 Notice.id。"""
|
||||
with app.app_context():
|
||||
# 垫偏 OT.id,避免空库 Notice.id==OT.id 碰巧绿。
|
||||
for index in range(3):
|
||||
db.session.add(ShippingOrderTask(
|
||||
task_no=f'OT-UI-SPACER-{index}', user_id=1, source_type='direct_pdf',
|
||||
))
|
||||
db.session.flush()
|
||||
task, line = _frozen_task_with_line(
|
||||
task_no='OT-UI-NOTICE-RESOLVE',
|
||||
reservation_id='RSV-NOTICE',
|
||||
part_number='PN-NOTICE',
|
||||
qty='3',
|
||||
)
|
||||
candidate_set = ShippingInventoryCandidateSet(
|
||||
task_id=task.id, set_version=1, lifecycle_status='frozen', input_fingerprint='c' * 64,
|
||||
)
|
||||
db.session.add(candidate_set)
|
||||
db.session.flush()
|
||||
db.session.add(ShippingInventoryCandidateSource(
|
||||
candidate_set_id=candidate_set.id, source_code='DSV360_AWSSG', candidate_order=1,
|
||||
))
|
||||
db.session.add(ShippingInventoryCandidateLine(
|
||||
candidate_set_id=candidate_set.id, line_no=1, source_codes_json=['DSV360_AWSSG'],
|
||||
))
|
||||
task.active_inventory_candidate_set_id = candidate_set.id
|
||||
db.session.add(ShippingNoticeLineInventoryMatch(
|
||||
task_id=task.id, snapshot_line_id=line.id, line_no=1, line_index=0,
|
||||
match_result='matched', inventory_status='ready', required_qty='3', available_qty='3',
|
||||
matched_qty='3', shortage_qty='0', reservation_id='RSV-NOTICE', part_number='PN-NOTICE',
|
||||
inventory_source_code='DSV360_AWSSG', matched_file_no='INV-NOTICE', is_current=True,
|
||||
))
|
||||
notice = ShippingNotice(
|
||||
task_no='SN-UI-NOTICE-RESOLVE',
|
||||
source_type='manual',
|
||||
user_id=1,
|
||||
order_task_id=task.id,
|
||||
)
|
||||
db.session.add(notice)
|
||||
db.session.commit()
|
||||
|
||||
assert int(notice.id) != int(task.id)
|
||||
preparation = build_task_ui_state(notice, include_stage_context=True)['stage_context']['preparation']
|
||||
row = preparation['lines'][0]
|
||||
# 关键断言:SN 入口仍投影 OT 上的匹配正式源与取数目标。
|
||||
assert preparation['frozen_order']['task_no'] == task.task_no
|
||||
assert preparation['frozen_order']['snapshot_id'] == task.order_snapshot_id
|
||||
assert row['source_code'] == 'DSV360_AWSSG'
|
||||
assert row['selected_source_code'] == 'DSV360_AWSSG'
|
||||
assert preparation['has_match_result'] is True
|
||||
|
||||
def test_stage_context_keeps_frozen_order_lines_when_inventory_match_is_not_ready(app) -> None:
|
||||
"""库存文件尚未取得时,通知准备仍必须返回冻结订单行,不能把订单明细绑到匹配结果。"""
|
||||
with app.app_context():
|
||||
@@ -913,6 +1050,34 @@ def test_preparation_editable_present_for_live_formal_task(monkeypatch):
|
||||
assert _preparation_editable(NoisyStatusTask()) is False
|
||||
|
||||
|
||||
def test_stage_context_line_actions_without_match_rows(app) -> None:
|
||||
"""正式 OT 无匹配行时,preparation.lines 仍须下发明细库存重试动作。
|
||||
|
||||
断言:has_formal_ot 不得用 current_match_rows 判定;无 match 时 fact 兜底 preparing,
|
||||
inventory_allowed_actions 含 retry_inventory_preparation。
|
||||
"""
|
||||
with app.app_context():
|
||||
user = User(username='ui-no-match-actions', email='ui-no-match-actions@example.com')
|
||||
db.session.add(user)
|
||||
db.session.flush()
|
||||
task, _line = _frozen_task_with_line(
|
||||
task_no='OT-UI-NO-MATCH-ACTIONS',
|
||||
user_id=user.id,
|
||||
reservation_id='RSV-NM-1',
|
||||
part_number='PN-NM-1',
|
||||
qty='4',
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
preparation = build_task_ui_state(task, include_stage_context=True)["stage_context"]["preparation"]
|
||||
assert preparation["has_match_result"] is False
|
||||
assert len(preparation["lines"]) == 1
|
||||
line = preparation["lines"][0]
|
||||
# 关键断言:无 Match 行时明细仍可点「重试库存准备」,不得因 has_formal_ot=False 空动作。
|
||||
assert "retry_inventory_preparation" in line["inventory_allowed_actions"]
|
||||
assert "close_unfulfillable" in line["line_allowed_actions"]
|
||||
|
||||
|
||||
def test_stage_context_preparation_projects_editable_and_customer_po_line_no(app) -> None:
|
||||
"""[断言] preparation 携带任务级 editable(active=True / voided=False);
|
||||
行级 customer_po_line_no 直读字段真实值(field_code 兼容键),字段缺失时为 None。"""
|
||||
@@ -936,3 +1101,110 @@ def test_stage_context_preparation_projects_editable_and_customer_po_line_no(app
|
||||
assert preparation["lines"][0]["customer_po_line_no"] is None
|
||||
# 字段缺失/无值的行也必须输出 None,不得用展示序号冒充业务行号。
|
||||
assert preparation["lines"][1]["customer_po_line_no"] is None
|
||||
|
||||
|
||||
def test_stage_context_preparation_projects_shortage_release_template_fields(app) -> None:
|
||||
"""短缺行必须按库存源发货模板投影人工补录字段,禁止 fail-closed 空列表。"""
|
||||
from app.models import (
|
||||
ShippingExcelTemplate,
|
||||
ShippingInventoryField,
|
||||
ShippingInventorySource,
|
||||
ShippingOrderSchema,
|
||||
ShippingOrderSchemaVersion,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
location = ShippingInventoryField.query.filter_by(field_code='location').one()
|
||||
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
template = db.session.get(ShippingExcelTemplate, int(source.excel_template_id))
|
||||
assert template is not None
|
||||
template.definition_json = {
|
||||
'mappings': [
|
||||
{
|
||||
'row_mode': 'line',
|
||||
'source_kind': 'inventory_field',
|
||||
'source_key': str(location.id),
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
user = User(username='ui-release-fields', email='ui-release-fields@example.com')
|
||||
db.session.add(user)
|
||||
db.session.flush()
|
||||
schema = ShippingOrderSchema(schema_code='UI_RELEASE_FIELDS', display_name='UI Release Fields')
|
||||
db.session.add(schema)
|
||||
db.session.flush()
|
||||
schema_version = ShippingOrderSchemaVersion(
|
||||
schema_id=schema.id,
|
||||
version=1,
|
||||
status='published',
|
||||
definition_json={'fields': []},
|
||||
)
|
||||
db.session.add(schema_version)
|
||||
db.session.flush()
|
||||
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-UI-RELEASE-FIELDS',
|
||||
user_id=user.id,
|
||||
stage='inventory_matching',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
snapshot = ShippingOrderSnapshot(
|
||||
task_id=task.id,
|
||||
extraction_id=1,
|
||||
schema_version_id=schema_version.id,
|
||||
order_data_version=1,
|
||||
header_values_json={},
|
||||
)
|
||||
db.session.add(snapshot)
|
||||
db.session.flush()
|
||||
task.order_snapshot_id = snapshot.id
|
||||
snapshot_line = ShippingOrderSnapshotLine(
|
||||
snapshot_id=snapshot.id,
|
||||
position=0,
|
||||
line_no=1,
|
||||
reservation_id='RSV-RELEASE',
|
||||
part_number='PN-RELEASE',
|
||||
qty='10',
|
||||
field_values_json={},
|
||||
)
|
||||
db.session.add(snapshot_line)
|
||||
db.session.flush()
|
||||
# 无库存快照时模板库存字段全部人工补录;源码必须能解析到 published 发货模板。
|
||||
db.session.add(ShippingNoticeLineInventoryMatch(
|
||||
task_id=task.id,
|
||||
snapshot_line_id=snapshot_line.id,
|
||||
line_no=1,
|
||||
line_index=0,
|
||||
match_result='not_found',
|
||||
inventory_status='ready',
|
||||
required_qty='10',
|
||||
available_qty='0',
|
||||
matched_qty='0',
|
||||
shortage_qty='10',
|
||||
reservation_id='RSV-RELEASE',
|
||||
part_number='PN-RELEASE',
|
||||
inventory_source_code='DSV360_AWSDU',
|
||||
inventory_snapshot_id=None,
|
||||
is_current=True,
|
||||
))
|
||||
db.session.commit()
|
||||
|
||||
preparation = build_task_ui_state(task, include_stage_context=True)['stage_context']['preparation']
|
||||
line = preparation['lines'][0]
|
||||
|
||||
assert line['is_shortage'] is True
|
||||
assert line['release_field_resolution_error'] is None
|
||||
assert line['release_required_fields'] == [
|
||||
{'field_id': str(location.id), 'display_name': '库位'},
|
||||
]
|
||||
assert line['release_template_fields'] == [
|
||||
{
|
||||
'field_id': str(location.id),
|
||||
'display_name': '库位',
|
||||
'value': None,
|
||||
'value_source': 'manual_required',
|
||||
'requires_manual': True,
|
||||
},
|
||||
]
|
||||
Reference in new issue
Block a user