import importlib.util
import json
from pathlib import Path
import zipfile
import pytest


def test_delivery_rejects_valid_crc_with_wrong_payload(tmp_path):
    script=Path(__file__).resolve().parents[1]/'scripts/finalize_manual_6552.py'
    spec=importlib.util.spec_from_file_location('manual_delivery_finalizer',script)
    module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
    root=tmp_path/'run';workspace=root/'anylabeling';workspace.mkdir(parents=True)
    (workspace/'frame.json').write_text('original-checked-proposal')
    archive=root/'human_correction_remaining_3788_v1.zip'
    with zipfile.ZipFile(archive,'w') as z:
        z.writestr(archive.stem+'/frame.json','different-payload-valid-crc')
    (root/'delivery_manifest.json').write_text(json.dumps({'predicted_frames':3788,'errors':0,
        'test_labels_scored':False,'archive':str(archive),'archive_sha256':module.sha(archive)}))
    (root/'merge_summary.json').write_text('{}')
    with pytest.raises(ValueError,match='payload differs'):
        module.finalize(root)
    assert not (root/'payload_audit.json').exists()
