feat(shipping-notice): 完成货代发货通知全门禁恢复MVP收口

完成全门禁恢复MVP版本交付:调整自动上传与重试配置,重构正式执行链路;修复多处代码bug,新增测试与审计工具;更新全流程文档与契约规范。
This commit is contained in:
hugo committed 2026-08-11 14:04:39 +08:00
1 parent 8e0bbf7a81
commit c94d173f92
181 files changed
+12947 -1724

No files matched your search

@@ -75,3 +75,5 @@ def test_permission_request_approve(app, monkeypatch):
def test_permission_request_not_found(app):
with app.app_context():
with pytest.raises(NotFoundError):
permission_service.approve_permission_request(999999)
@@ -41,6 +41,8 @@ def test_soft_delete_document_cancels_active_jobs(app):
source_file_name='doc.pdf',
source_file_path='data/kb/doc.pdf',
)
db.session.add_all([group, document, version])
db.session.flush()
queued = KbJob(
job_type='clean',
status='queued',
@@ -49,7 +51,7 @@ def test_soft_delete_document_cancels_active_jobs(app):
document_version_id=version.id,
progress=0,
)
db.session.add_all([group, document, version, queued])
db.session.add(queued)
db.session.commit()
assert document_service.soft_delete_document(document.id, user_id=1) is True
@@ -15,6 +15,7 @@ from app.models import (
ShippingInventoryCandidateSource,
ShippingNotice,
ShippingNoticeLineInventoryMatch,
ShippingInventoryFile,
)
from app.services.shipping_notice.inventory_candidate_projection import (
project_inventory_candidate_facts,
@@ -141,3 +142,32 @@ def test_project_inventory_candidate_facts_marks_stale_current_match(app):
assert facts['blocking_reason']['code'] == 'inventory_candidate_version_stale'
assert facts['candidate_set_id'] == new_set.id
assert facts['line_resolutions'][0]['candidate_set_id'] == old_set.id
def test_project_inventory_candidate_facts_reads_file_evidence_from_snapshot(app):
"""行级证据 SHA 必须从正式库存快照读取,不能依赖可选 detail_json。"""
with app.app_context():
task = _task('SN-CAND-EVIDENCE')
candidate_set = _candidate_set(task)
db.session.add(ShippingInventoryCandidateLine(
candidate_set_id=int(candidate_set.id), line_no=1, source_codes_json=['DBS_DUB'],
))
db.session.flush()
snapshot = ShippingInventoryFile(
inventory_file_no='INV-CAND-EVIDENCE', storage_path='memory://evidence.xlsx',
source_code='DBS_DUB', snapshot_status='ready', file_sha256='b' * 64,
)
db.session.add(snapshot)
db.session.flush()
db.session.add(ShippingNoticeLineInventoryMatch(
task_id=int(task.id), line_key='line-1', candidate_set_id=int(candidate_set.id),
candidate_set_version=1, inventory_snapshot_id=int(snapshot.id), match_result='matched',
inventory_status='ready', inventory_source_code='DBS_DUB', matched_file_no=snapshot.inventory_file_no,
matched_row_index=9, is_current=True, detail_json={},
))
db.session.commit()
facts = project_inventory_candidate_facts(task)
assert facts['line_resolutions'][0]['inventory_file_sha256'] == 'b' * 64
assert facts['line_resolutions'][0]['inventory_row_index'] == 9
@@ -202,7 +202,7 @@ def _mock_case_for_p4(case_no='CASE-2608-0042'):
return case
def _install_minimal_mocks(monkeypatch, case=None):
def _install_minimal_mocks(monkeypatch, case=None, app=None):
if case is None:
case = _mock_case_for_p4()
@@ -242,19 +242,20 @@ def _install_minimal_mocks(monkeypatch, case=None):
def _fake_visible(case_no, user_id, is_admin): # noqa: ARG001
return case
monkeypatch.setattr(mod.ShippingOrderReviewCase, 'query', _CaseQuery())
monkeypatch.setattr(mod, 'get_review_case_visible_to_user', _fake_visible)
# SQLAlchemy 的 Model.query 是 Flask app-context scoped descriptor;给测试替换
# 类属性前先进入 fixture 提供的 app context,避免纯合同测试被上下文机制阻断。
monkeypatch.setattr(
mod,
'ShippingOrderReviewCase',
SimpleNamespace(query=_CaseQuery(), id=SimpleNamespace(desc=lambda: None)),
)
monkeypatch.setattr(mod, 'get_review_case_visible_to_user', _fake_visible, raising=False)
# 避免 Job.query / db.session.get / CaseEvent 等深层调用触发
monkeypatch.setattr(mod.db.session, 'get', _fake_get)
monkeypatch.setattr(mod.db, 'session', MagicMock(
commit=lambda: None,
rollback=lambda: None,
flush=lambda: None,
add=lambda _x: None,
))
monkeypatch.setattr(mod.db, 'session', MagicMock())
# L1-L3 对账直接返回 NONE(避免 hit 真 DB)
mod.query_case_task_ownership = lambda *_a, **_kw: { # type: ignore[method-assign]
'match_level': 'NONE',
'match_level': 'none',
'confidence': 'LOW',
'task_no': None,
'candidate_matches': [],
@@ -55,6 +55,8 @@ T1_MATRIX = [
(None, None, False, 'None 码 → fail-closed 不可重试'),
# category_hint 与前缀冲突:前缀优先(空码除外)
('SHIPPING.TRANSIENT_NETWORK', 'worker_timeout', True, '前缀命中即使 hint 不同也可重试'),
('trigger_not_claimed_missing', None, True, '库存 Trigger 缺失 → 技术重试'),
('trigger_not_claimed_timeout', None, True, '库存 Trigger 超时 → 技术重试'),
]
@@ -66,8 +66,11 @@ def _build_accepted_order(*, with_unverified_llm: bool = False, with_extension:
if code == 'packing_reference':
field = extension
else:
field = ShippingOrderField.query.filter_by(field_code=code).one()
scope = 'header' if code in HEADER_FIELD_PROJECTIONS else 'line'
scope = 'header' if code in HEADER_FIELD_PROJECTIONS else 'line'
field = ShippingOrderField.query.filter_by(
field_code=code,
scope=scope,
).one()
value_type = field.value_type
fields[code] = field
definitions.append({'field_id': field.id, 'field_code': code, 'scope': scope, 'value_type': value_type, 'enabled': True})
@@ -287,8 +290,16 @@ def test_auto_freeze_readiness_fails_closed_with_run_policy_blockers(app):
assert blocked_error.value.code == 'SHIPPING.ORDER_FREEZE_BLOCKED'
issues = blocked_error.value.details['issues']
assert {'reason': 'auto_freeze_blocked', 'blocking_reason_code': 'customer_scope_not_configured'} in issues
assert {'reason': 'auto_freeze_blocked', 'blocking_reason_code': 'customer_confirmation_required'} in issues
assert any(
item.get('reason') == 'auto_freeze_blocked'
and item.get('blocking_reason_code') == 'customer_scope_not_configured'
for item in issues
)
assert any(
item.get('reason') == 'auto_freeze_blocked'
and item.get('blocking_reason_code') == 'customer_confirmation_required'
for item in issues
)
assert any(item['reason'] == 'auto_freeze_not_enabled' for item in issues)