import importlib.util
from pathlib import Path
import pytest

spec=importlib.util.spec_from_file_location('remaining_package',Path(__file__).resolve().parents[1]/'scripts/package_mask_remaining_expand.py')
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)


def test_all_historical_and_new_public_namespaces_are_preserved():
    for folder in ('quota100_complete_generation','remaining_classes_generation','remaining_expand_generation','remaining_continue_generation','remaining_round2_generation'):
        assert module.public_file(Path(folder)/'templates/example.png')
    assert module.public_file(Path('images/example.png'))
    assert module.public_file(Path('review_dispositions.jsonl'))
    assert module.public_file(Path('review_completion_statement.json'))


def test_private_sources_are_never_exported():
    for name in ('all_manual_sources.jsonl','manual_source_records.jsonl','gallery_source_records.jsonl',
                 'validated_manual_source_records.jsonl','reviews_snapshot.sqlite3','gallery_reviews_snapshot.sqlite3',
                 'backups/reviews_snapshot.sqlite3','parent_evidence/manifest.json','access_key.txt'):
        assert not module.public_file(Path(name))


@pytest.mark.parametrize('suffix',['.json','.jsonl'])
def test_raw_evidence_hash_is_allowed_but_raw_payload_never_is(suffix):
    import json
    from scripts.package_mask_remaining_expand import private_payload
    assert not private_payload(json.dumps({'human_source':{'current_payload_raw_sha256':'a'*64,'human_event_raw_sha256':'b'*64}}),suffix)
    for key in ('current_payload_raw','human_event_payload_raw','saved_event_payload_raw','current_payload','human_event_payload','access_key'):
        assert private_payload(json.dumps({'nested':{key:'private event bytes'}}),suffix)
    assert private_payload(json.dumps({'current_payload_raw_sha256':'not a hash; leaked event'}),suffix)
