"""阶段七:重发版本替代与上传后撤销门禁。""" from __future__ import annotations import pytest from app import db from app.core.errors import ConflictError from app.models import ShippingFreightCancellation, ShippingNotice, User from app.services.shipping_notice.freight_cancellation_service import complete_freight_cancellation from app.services.shipping_notice.task_supersession_service import assert_task_not_supersession_blocked, establish_task_supersession def _tasks(*, uploaded: bool) -> tuple[ShippingNotice, ShippingNotice, User]: user = User(username=f'supersession-{uploaded}', email=f'supersession-{uploaded}@example.test', password_hash='x') old = ShippingNotice(task_no=f'SN-SUP-OLD-{uploaded}', user_id=None, upload_status='uploaded' if uploaded else None, freight_ref_no='F-1' if uploaded else None) db.session.add_all([user, old]); db.session.flush() old.user_id = user.id new = ShippingNotice(task_no=f'SN-SUP-NEW-{uploaded}', user_id=user.id, supersedes_task_id=old.id) db.session.add(new); db.session.commit() return old, new, user def test_unuploaded_resend_voids_old_task_and_keeps_new_active(app): """关键断言:未上传重发立即使旧版失效,库存入口不能继续消费旧版本。""" with app.app_context(): old, new, user = _tasks(uploaded=False) establish_task_supersession(old_task=old, new_task=new, actor_id=user.id, reason='customer resend') assert old.lifecycle_status == 'voided' assert new.lifecycle_status == 'active' def test_uploaded_resend_requires_cancellation_receipt_before_new_can_continue(app): """关键断言:已上传旧版必须先有撤销回执,后继版才恢复活动状态。""" with app.app_context(): old, new, user = _tasks(uploaded=True) establish_task_supersession(old_task=old, new_task=new, actor_id=user.id, reason='customer resend') row = ShippingFreightCancellation.query.one() assert new.lifecycle_status == 'blocked' with pytest.raises(ConflictError): assert_task_not_supersession_blocked(new) complete_freight_cancellation(cancellation_id=row.id, actor_id=user.id, freight_receipt_no='CANCEL-1', receipt_asset_id=1) assert row.status == 'cancelled' assert old.lifecycle_status == 'voided' assert new.lifecycle_status == 'active'