"""The independent follow-up verifier cannot substitute an unsealed parent."""
import ast
import json
from pathlib import Path

import pytest

from scripts import verify_mask_remaining_followup_requests as verifier


@pytest.mark.parametrize('attack',['failed','version','proposals','manifest'])
def test_matching_publication_11_parent_seal_is_required_before_source_reads(tmp_path,attack):
    root=tmp_path/'worklist';parent=tmp_path/'parent';root.mkdir();parent.mkdir()
    (root/'manifest.json').write_text('{}');(parent/'manifest.json').write_text('{}')
    (parent/'proposals.jsonl').write_text('{}\n')
    seal=dict(state='passed',publication_version=11,manifest_sha256=verifier.digest(parent/'manifest.json'),
        proposals_sha256=verifier.digest(parent/'proposals.jsonl'))
    if attack=='failed':seal['state']='failed'
    elif attack=='version':seal['publication_version']=10
    elif attack=='proposals':seal['proposals_sha256']='0'*64
    else:seal['manifest_sha256']='0'*64
    (parent/'verification.json').write_text(json.dumps(seal))
    with pytest.raises(ValueError,match='Matching publication-11 seal'):verifier.audit(root,parent)


def test_followup_verifier_does_not_import_the_followup_builder():
    tree=ast.parse(Path(verifier.__file__).read_text())
    imports=[node.module or '' for node in ast.walk(tree) if isinstance(node,ast.ImportFrom)]
    assert not any('build_mask_remaining_followup_requests' in name for name in imports)
    assert verifier.CLASSES==(3,4,5,7,14,22)


def test_raw_payload_hash_not_permitted_as_a_substitute_for_canonical_review_binding():
    value=dict(polygon=[[1.,2.],[3.,4.]],revision=4)
    assert verifier.sha(value)==verifier.sha(dict(reversed(list(value.items()))))
    with pytest.raises(ValueError):verifier.require(False,'missing exact event proof')
