""" 用途:验证库存快照复用策略和多源组合指纹的 fail-closed 合同。 依赖:纯函数服务。 逻辑:覆盖身份、状态、过期与指纹变化;不触发外部 RPA。 """ from datetime import datetime, timedelta from types import SimpleNamespace from app.services.shipping_notice.onhand_combination_fingerprint import build_inventory_combination_fingerprint from app.services.shipping_notice.onhand_snapshot_reuse_policy import check_inventory_snapshot_reusable def test_snapshot_reuse_requires_ready_identity_and_versions(): snap = SimpleNamespace( snapshot_status='ready', expires_at=datetime.utcnow() + timedelta(hours=1), source_code='A', warehouse_id='W1', source_config_version='v1', input_fingerprint='cand-1', file_sha256='sha-1', parser_version='p1', mapping_version='m1', invalidated_at=None, ) ok, reasons = check_inventory_snapshot_reusable( snapshot=snap, source_code='A', warehouse_id='W1', source_config_version='v1', candidate_input_fingerprint='cand-1', parser_version='p1', mapping_version='m1', expected_file_sha256='sha-1', ) assert ok is True and reasons == [] ok, reasons = check_inventory_snapshot_reusable(snapshot=snap, source_code='B', warehouse_id='W1') assert ok is False and 'source_code_mismatch' in reasons ok, reasons = check_inventory_snapshot_reusable(snapshot=snap, source_code='A', parser_version='p2') assert ok is False and 'parser_version_mismatch' in reasons def test_combination_fingerprint_is_order_independent_but_input_sensitive(): a = build_inventory_combination_fingerprint( task_snapshot_fingerprint='task', candidate_freeze_fingerprint='cand', sources=[{'source_code': 'B', 'snapshot_id': 2}, {'source_code': 'A', 'snapshot_id': 1}], reservation_version='r1', matching_rule_version='m1', ) b = build_inventory_combination_fingerprint( task_snapshot_fingerprint='task', candidate_freeze_fingerprint='cand', sources=[{'source_code': 'A', 'snapshot_id': 1}, {'source_code': 'B', 'snapshot_id': 2}], reservation_version='r1', matching_rule_version='m1', ) c = build_inventory_combination_fingerprint( task_snapshot_fingerprint='task', candidate_freeze_fingerprint='cand', sources=[{'source_code': 'A', 'snapshot_id': 1}, {'source_code': 'B', 'snapshot_id': 3}], reservation_version='r1', matching_rule_version='m1', ) assert a == b assert a != c