"""Publication-9 independent provenance, honest dispositions and safe sealing."""
import ast
import copy
import hashlib
import json
import sqlite3

import pytest

from scripts import verify_mask_quota_100_complete as audit


POLYGON = [[1, 1], [20, 1], [20, 10], [15, 10], [15, 5], [5, 5], [5, 10], [1, 10]]
FOUR = [[2, 2], [19, 1], [18, 9], [1, 10]]


def row(key='human', cls=9):
    return dict(target_key=key, target_id=key+'::box:0000', frame_id='frame-'+key,
        final_class_id=cls, split='train', policy_id='camera', viewpoint_candidate='front',
        group_id='group', box_index=0, bbox=dict(x=1, y=1, width=19, height=9),
        size=[24, 16], source_pcd_sha256='pcd')


def store(current, events=()):
    return dict(path=audit.Path('readonly.sqlite3'), metadata={}, current=current,
        events=list(events), history_keys={e[1] for e in events})


def write_json(path, value):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(audit.canonical(value)+b'\n')


def write_jsonl(path, values):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b''.join(audit.canonical(v)+b'\n' for v in values))


@pytest.fixture(params=[FOUR, POLYGON], ids=['four', 'fine'])
def manual_fixture(request):
    r, polygon = row(), copy.deepcopy(request.param)
    human = dict(target_key='human', source=r, polygon=polygon, revision=6,
        state='draft', **dict.fromkeys(audit.FALSE_FLAGS, False))
    current = dict(human, revision=7, event_kind='axes_normalization')
    pose = store({'human': current}, [(10, 'human', 6, json.dumps(human, indent=2)),
        (11, 'human', 7, json.dumps(current))])
    direct = audit.check_manual_sources(pose, {'human': r}, {'human'})
    t = dict(direct['human'], origin=audit.POSE_ORIGIN,
        pose_review=audit.v1.pose_provenance(direct['human'], 'snapshot'))
    req = dict(target_key='human', class_id=9, source=dict(r, formal_manifest_sha256='formal'),
        pose_annotation_url='https://example.test/?target=human')
    four = len(polygon) == 4
    proposal = dict(target_key='human', class_id=9, source=r, polygon=polygon, vertex_count=len(polygon),
        schema='saved_pose_mask_gallery_entry_v1', origin=audit.POSE_ORIGIN,
        human_edited=True, status='human_edited_draft_pending_confirmation',
        source_label_kind='human_mask_source_manual_template_draft', manual_source_kind='completed_manual_request',
        **dict.fromkeys(audit.FALSE_FLAGS, False), pose_snapshot_saved=True, pose_store_written=False,
        gallery_store_written=False, machine_transfer_performed=False, new_human_edit_created=False,
        manual_four_point_source=four, propagation_seed_eligible=not four,
        manual_request_sha256=audit.sha(req), original_pose_url=req['pose_annotation_url'],
        rendering_id=audit.NAMESPACE+(':manual_source:' if four else ':')+'human',
        snapshot_sha256='snapshot', target_expected_revision=7,
        target_snapshot_payload_sha256=audit.sha(current), formal_manifest_sha256='formal', class_assignments_sha256='classes',
        metrics={}, flags=['manual_source_not_machine_transfer'])
    proposal['human_source' if four else 'template'] = audit.previous.template_binding(t)
    return proposal, req, pose, direct, r, 'snapshot', 'formal', 'classes'


def test_actual_human_counts_without_synthesizing_vertices_or_events(manual_fixture):
    before = copy.deepcopy(manual_fixture)
    audit.check_manual(*manual_fixture)
    assert manual_fixture == before
    proposal = manual_fixture[0]
    assert ('human_source' in proposal) == (len(proposal['polygon']) == 4)
    assert ('template' in proposal) == (len(proposal['polygon']) > 4)


@pytest.mark.parametrize('attack', ['vertex', 'vertex_count', 'class', 'request_split', 'request_hash', 'request_url',
    'event_hash', 'current_hash', 'snapshot', 'revision', 'namespace', 'confirmation', 'GT', 'training',
    'robot', 'human', 'transfer', 'new_edit', 'write', 'metrics', 'continuation', 'seed', 'fake_binding'])
def test_new_manual_cannot_be_synthetic_stale_confirmed_or_a_transfer(manual_fixture, attack):
    proposal, req, pose, direct, r, *_ = manual_fixture
    source = proposal.get('human_source') or proposal['template']
    if attack == 'vertex':
        proposal['polygon'] = proposal['polygon']+[[10, 7]]
        proposal['vertex_count'] += 1
    elif attack == 'vertex_count':
        proposal['vertex_count'] += 1
    elif attack == 'class':
        proposal['class_id'] = 10
    elif attack == 'request_split':
        req['source']['split'] = 'test'
    elif attack == 'request_hash':
        proposal['manual_request_sha256'] = 'fake'
    elif attack == 'request_url':
        proposal['original_pose_url'] = 'https://example.test/?target=other'
    elif attack == 'event_hash':
        source['human_event_sha256'] = audit.sha(json.loads(pose['events'][0][3]))
    elif attack == 'current_hash':
        proposal['target_snapshot_payload_sha256'] = 'fake'
    elif attack == 'snapshot':
        proposal['snapshot_sha256'] = 'stale'
    elif attack == 'revision':
        proposal['target_expected_revision'] = 6
    elif attack == 'namespace':
        proposal['rendering_id'] = 'historical:human'
    elif attack in ('confirmation', 'GT', 'training', 'robot'):
        proposal[dict(confirmation='human_mask_confirmed', GT='ground_truth_eligible',
            training='automatic_training_eligible', robot='robot_command_eligible')[attack]] = True
    elif attack == 'human':
        proposal['human_edited'] = False
    elif attack in ('transfer', 'new_edit', 'write'):
        proposal[dict(transfer='machine_transfer_performed', new_edit='new_human_edit_created', write='gallery_store_written')[attack]] = True
    elif attack == 'metrics':
        proposal['metrics'] = {'ecc': 1}
    elif attack == 'continuation':
        proposal['continuation'] = {'batch': 9}
    elif attack == 'seed':
        proposal['propagation_seed_eligible'] = not proposal['propagation_seed_eligible']
    else:
        proposal['template' if 'human_source' in proposal else 'human_source'] = copy.deepcopy(source)
    with pytest.raises(ValueError):
        audit.check_manual(*manual_fixture)


@pytest.mark.parametrize('attack', ['machine', 'deleted', 'deferred', 'rectangle', 'source', 'confirmed'])
def test_direct_source_recovery_never_infers_human_work_from_equal_machine_polygons(attack):
    r = row()
    human = dict(target_key='human', source=r, polygon=FOUR, state='draft', revision=1)
    current = dict(human, revision=2)
    if attack == 'machine':
        current['event_kind'] = 'mask_template_proposal'
    elif attack in ('deleted', 'deferred'):
        current['state'] = attack
    elif attack == 'rectangle':
        human['polygon'] = current['polygon'] = [[1, 1], [20, 1], [20, 10], [1, 10]]
    elif attack == 'source':
        current['source'] = dict(r, split='test')
    else:
        current['human_mask_confirmed'] = True
    pose = store({'human': current}, [(1, 'human', 1, json.dumps(human)), (2, 'human', 2, json.dumps(current))])
    with pytest.raises(ValueError):
        audit.check_manual_sources(pose, {'human': r}, {'human'})


def test_legacy_manual_source_may_omit_new_false_only_gt_flag():
    r = row()
    value = dict(target_key='human', source=r, polygon=POLYGON, state='draft', revision=1)
    pose = store({'human': value}, [(1, 'human', 1, json.dumps(value))])
    assert 'ground_truth_eligible' not in value
    assert audit.check_manual_sources(pose, {'human': r}, {'human'})['human']['polygon'] == POLYGON
    value['ground_truth_eligible'] = True
    with pytest.raises(ValueError):
        audit.check_manual_sources(pose, {'human': r}, {'human'})


def candidate(key, cls=1, polygon=POLYGON):
    return dict(target_key=key, class_id=cls, source=row(key, cls), polygon=polygon)


def quota_fixture():
    old, manual, inherited, gallery = {}, {}, {}, store({})
    gallery_counts = {1: 55, 2: 67, 9: 45, 10: 35, 15: 66}
    manual_counts = {1: 24, 2: 10, 9: 13, 10: 37, 15: 34}
    for cls, total in {1: 100, 2: 100, 9: 90, 10: 85, 15: 100}.items():
        for i in range(total):
            key = f'{cls}-{i}'
            old[key] = candidate(key, cls)
            if i < gallery_counts[cls]:
                gallery['current'][key] = dict(polygon=POLYGON)
            elif i < gallery_counts[cls]+manual_counts[cls]:
                inherited[key] = old[key]
    for cls, total in {9: 10, 10: 15}.items():
        for i in range(total):
            key = f'new-{cls}-{i}'
            manual[key] = candidate(key, cls)
    pending = set(old)-set(inherited)-set(gallery['current'])
    dispositions = dict.fromkeys(sorted(pending)[:45], {})
    return old, inherited, manual, gallery, dispositions


def test_exact500_counts_411_actual_humans_separately_from_45_unchanged_and_44_unsaved():
    old, inherited, manual, gallery, dispositions = quota_fixture()
    counts = audit.check_partition(old, manual, {}, {**old, **manual}, manual)
    assert len(old) == 475 and len(inherited) == 118 and len(manual) == 25
    assert all(c == dict(proposals=100, distinct_frames=100, shortfall=0) for c in counts.values())
    classes = audit.expected_counts(old, inherited, manual, gallery, dispositions)
    assert sum(c['saved_human'] for c in classes.values()) == 411
    assert sum(c['user_reviewed_no_change'] for c in classes.values()) == 45
    assert sum(c['old_pending_machine'] for c in classes.values()) == 89
    assert sum(c['no_new_server_review'] for c in classes.values()) == 44
    assert sum(c['saved_pose_human'] for c in classes.values()) == 143
    assert sum(c['saved_gallery_human'] for c in classes.values()) == 268
    assert sum(c['new_candidates'] for c in classes.values()) == 0


def test_real_appended_gallery_save_updates412_269_88_43_without_changing_quota():
    old, inherited, manual, gallery, dispositions = quota_fixture()
    pending = set(old)-set(inherited)-set(gallery['current'])-set(dispositions)
    key = next(iter(pending))
    gallery['current'][key] = dict(polygon=POLYGON)
    classes = audit.expected_counts(old, inherited, manual, gallery, dispositions)
    assert sum(c['saved_human'] for c in classes.values()) == 412
    assert sum(c['saved_gallery_human'] for c in classes.values()) == 269
    assert sum(c['pending_machine_raw'] for c in classes.values()) == 88
    assert sum(c['no_new_server_review'] for c in classes.values()) == 43
    assert sum(c['user_reviewed_no_change'] for c in classes.values()) == 45
    assert sum(c['total'] for c in classes.values()) == 500


@pytest.mark.parametrize('attack', [None, 'machine', 'no_event', 'parent_hash', 'publication', 'confirmed'])
def test_previously_missing_current_candidate_requires_real_revision1_human_event(attack):
    key = audit.MISSING_CURRENT_KEY
    proposal = candidate(key, 15)
    proposal['source']['frame_id'] = audit.MISSING_CURRENT_FRAME
    value = dict(target_key=key, revision=1, event_kind='human_mask_gallery_revision', human_edited=True,
        proposal_publication=8, base_proposal_id=audit.sha(proposal), proposal_polygon=proposal['polygon'],
        **dict.fromkeys(audit.FALSE_FLAGS, False))
    if attack == 'machine':
        value['event_kind'] = 'mask_template_proposal'
    elif attack == 'parent_hash':
        value['base_proposal_id'] = 'fake'
    elif attack == 'publication':
        value['proposal_publication'] = 7
    elif attack == 'confirmed':
        value['human_mask_confirmed'] = True
    events = [] if attack == 'no_event' else [(285, key, 1, json.dumps(value))]
    gallery = store({key: value}, events)
    if attack:
        with pytest.raises(ValueError):
            audit.check_previously_missing_save(gallery, {key: proposal})
    else:
        assert audit.check_previously_missing_save(gallery, {key: proposal}) is True
        assert audit.check_previously_missing_save(store({}), {key: proposal}) is False


@pytest.mark.parametrize('attack', ['machine', 'missing_manual', 'old_change', 'frame_duplicate', 'wrong_class', 'extra'])
def test_quota_cannot_replace_parent_duplicate_frames_or_add_machine_fill(attack):
    old, _, manual, _, _ = quota_fixture()
    combined, new = {**old, **manual}, {}
    if attack == 'machine':
        new['machine'] = candidate('machine')
    elif attack == 'missing_manual':
        del manual[next(iter(manual))]
    elif attack == 'old_change':
        key = next(iter(old))
        combined[key] = dict(old[key], polygon=FOUR)
    elif attack == 'frame_duplicate':
        first, second = list(manual)[:2]
        manual[first]['source']['frame_id'] = manual[second]['source']['frame_id']
    elif attack == 'wrong_class':
        manual[next(iter(manual))]['class_id'] = 10
    else:
        combined['extra'] = candidate('extra')
    with pytest.raises(ValueError):
        audit.check_partition(old, manual, new, combined, manual)


@pytest.fixture
def dispositions_fixture():
    old84 = {f'old-{i}': candidate(f'old-{i}', 9) for i in range(84)}
    gallery = store({k: {'polygon': POLYGON} for k in list(old84)[45:]})
    gallery['history_keys'] = set(gallery['current'])
    scope, dispositions = {}, {}
    for key, proposal in list(old84.items())[:45]:
        scope[key] = dict(target_key=key, class_id=9, frame_id=proposal['source']['frame_id'], status='no_server_review_saved')
        dispositions[key] = dict(target_key=key, class_id=9, frame_id=proposal['source']['frame_id'],
            disposition='user_reviewed_no_change', origin='explicit_user_statement_in_conversation',
            human_edited=False, **dict.fromkeys(audit.FALSE_FLAGS, False), propagation_seed_eligible=False,
            pose_store_written=False, gallery_store_written=False,
            source_publication=7, source_proposal_id=audit.sha(proposal), source_proposal_sha256=audit.sha(proposal),
            polygon_sha256=audit.sha(proposal['polygon']), source_missing_review_record_sha256=audit.sha(scope[key]))
    return dispositions, scope, old84, copy.deepcopy(old84), gallery


def test_no_change_is_exact45_from_old84_without_fabricated_saves(dispositions_fixture):
    before = copy.deepcopy(dispositions_fixture)
    audit.check_review_dispositions(*dispositions_fixture)
    assert dispositions_fixture == before


@pytest.mark.parametrize('attack', ['expand43', 'current_unsaved', 'omit', 'reviewed', 'saved_later', 'edited',
    'confirmed', 'gt', 'training', 'robot', 'seed', 'write', 'event', 'wrong_origin', 'wrong_disposition',
    'polygon_hash', 'proposal_hash', 'old_canonical', 'class', 'frame'])
def test_user_no_change_statement_cannot_expand_scope_or_become_human_or_seed(dispositions_fixture, attack):
    records, scope, old84, old, gallery = dispositions_fixture
    key = next(iter(records))
    if attack in ('expand43', 'current_unsaved'):
        other = 'old-unreviewed43' if attack == 'expand43' else audit.MISSING_CURRENT_KEY
        records[other] = dict(records[key], target_key=other)
    elif attack == 'omit':
        del records[key]
    elif attack == 'reviewed':
        gallery['current'][key] = {'polygon': POLYGON}
    elif attack == 'saved_later':
        gallery['history_keys'].add(key)
    elif attack in ('edited', 'confirmed', 'gt', 'training', 'robot', 'seed', 'write'):
        records[key][dict(edited='human_edited', confirmed='human_mask_confirmed', gt='ground_truth_eligible',
            training='automatic_training_eligible', robot='robot_command_eligible', seed='propagation_seed_eligible',
            write='gallery_store_written')[attack]] = True
    elif attack == 'event':
        records[key]['human_event_id'] = 1000
    elif attack == 'wrong_origin':
        records[key]['origin'] = 'sqlite_human_save'
    elif attack == 'wrong_disposition':
        records[key]['disposition'] = 'human_mask_confirmed'
    elif attack in ('polygon_hash', 'proposal_hash'):
        records[key]['polygon_sha256' if attack == 'polygon_hash' else 'source_proposal_sha256'] = 'fake'
    elif attack == 'old_canonical':
        old[key]['polygon'] = FOUR
    elif attack == 'class':
        records[key]['class_id'] = 10
    else:
        records[key]['frame_id'] = 'another frame'
    with pytest.raises(ValueError):
        audit.check_review_dispositions(*dispositions_fixture)


@pytest.fixture
def sqlite_fixture(tmp_path, manual_fixture):
    proposal, _, pose, direct, *_ = manual_fixture
    path = tmp_path/'snapshot.sqlite3'
    raw = json.dumps(pose['current']['human'], indent=1)
    event_id, key, revision, event_raw = pose['events'][0]
    with sqlite3.connect(path) as db:
        db.executescript('CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO annotations VALUES (?,?,?)', (key, 7, raw))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (event_id, key, revision, event_raw))
    record = dict(schema='saved_pose_mask_source_freeze_v1', target_key=key, snapshot_sha256='snapshot',
        current_revision=7, current_payload_sha256=audit.sha(json.loads(raw)), current_payload_raw=raw,
        current_payload=json.loads(raw), current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(),
        human_event_id=event_id, human_polygon_revision=revision, human_event_sha256=hashlib.sha256(event_raw.encode()).hexdigest(),
        human_event_payload_raw=event_raw, human_event_payload=json.loads(event_raw), polygon_sha256=audit.sha(proposal['polygon']),
        vertex_count=len(proposal['polygon']), manual_four_point_source=len(proposal['polygon']) == 4,
        propagation_seed_eligible=len(proposal['polygon']) > 4)
    return {key: record}, {key: proposal}, path, 'snapshot', direct


def test_raw_sqlite_current_and_event_bytes_are_checked_read_only(sqlite_fixture):
    path = sqlite_fixture[2]
    before = path.read_bytes()
    assert audit.check_source_records(*sqlite_fixture) == sqlite_fixture[0]
    assert path.read_bytes() == before


@pytest.mark.parametrize('field', ['current_payload_raw', 'human_event_payload_raw', 'current_payload_raw_sha256',
    'human_event_id', 'human_polygon_revision', 'snapshot_sha256', 'polygon_sha256', 'propagation_seed_eligible'])
def test_source_record_hash_event_id_or_raw_whitespace_tampering_is_rejected(sqlite_fixture, field):
    record = sqlite_fixture[0]['human']
    if field in ('human_event_id', 'human_polygon_revision'):
        record[field] += 1
    elif field == 'propagation_seed_eligible':
        record[field] = not record[field]
    else:
        record[field] += ' '
    with pytest.raises(ValueError):
        audit.check_source_records(*sqlite_fixture)


@pytest.fixture
def inherited_fixture(tmp_path):
    proposals, current, direct, audits, records = {}, {}, {}, {}, {}
    for i in range(118):
        key = audit.OLD_FOUR_VERTEX_KEY if i == 0 else f'old-human-{i}'
        polygon = FOUR if i == 0 else POLYGON
        r = row(key)
        value = dict(target_key=key, revision=5, polygon=polygon, source=r, state='draft')
        p = dict(target_key=key, polygon=polygon, snapshot_sha256='original-snapshot', target_expected_revision=4,
            target_snapshot_payload_sha256='historical-current')
        t = dict(row=r, polygon=polygon, revision=5, current_payload_sha256=audit.sha(value),
            human_polygon_revision=5, human_event_sha256='real-event', human_mask_confirmed=False)
        item = dict(target_key=key, original_proposal_sha256=audit.sha(p), historical_revision=4, current_revision=5,
            current_payload_sha256=audit.sha(value), source_changed=True, polygon_changed=False,
            latest_active_direct_human=True, vertex_count=len(polygon), propagation_seed_eligible=len(polygon) > 4,
            historical_assets_preserved=True, canonical_proposal_overwritten=False)
        records[key] = dict(target_key=key, snapshot_sha256='latest-snapshot', inherited_manual_source=True,
            legacy_source_audit=item, latest_human_source=audit.previous.template_binding(dict(t, origin=audit.POSE_ORIGIN,
                pose_review=audit.v1.pose_provenance(t, 'latest-snapshot'))))
        proposals[key], current[key], direct[key], audits[key] = p, value, t, item
    write_jsonl(tmp_path/'inherited_manual_source_audit.jsonl', audits.values())
    return (tmp_path, proposals, store(current), direct, records), audits


def test_all118_inherited_sources_include_original_four_and_preserve_legacy_canonical_objects(inherited_fixture):
    args, _ = inherited_fixture
    before = copy.deepcopy(args[1])
    changed = audit.check_inherited_manual(*args)
    assert len(changed) == 118 and audit.OLD_FOUR_VERTEX_KEY in changed
    assert args[1] == before
    assert all('ground_truth_eligible' not in p for p in args[1].values())


@pytest.mark.parametrize('attack', ['only87', 'omit_four', 'no_overlay', 'rewrite', 'gt', 'fake_template', 'hide_change'])
def test_inherited_source_audit_cannot_omit_old_four_or_rebind_parent(inherited_fixture, attack):
    args, audits = inherited_fixture
    out, proposals, pose, direct, records = args
    key = audit.OLD_FOUR_VERTEX_KEY
    if attack == 'only87':
        for k in list(audits)[87:]:
            del audits[k]
    elif attack == 'omit_four':
        del audits[key]
    elif attack == 'no_overlay':
        records[key]['inherited_manual_source'] = False
    elif attack == 'rewrite':
        proposals[key]['snapshot_sha256'] = 'latest-snapshot'
    elif attack == 'gt':
        proposals[key]['ground_truth_eligible'] = True
    elif attack == 'fake_template':
        records[key]['latest_human_source']['human_event_sha256'] = 'machine'
    else:
        audits[key]['source_changed'] = False
    write_jsonl(out/'inherited_manual_source_audit.jsonl', audits.values())
    with pytest.raises(ValueError):
        audit.check_inherited_manual(*args)


@pytest.mark.parametrize('version', [3, 6, 7, 8])
@pytest.mark.parametrize('attack', [None, 'publication', 'image', 'proposal', 'machine', 'seed', 'source'])
def test_gallery_save_binds_actual_historical_publication_and_original_image(tmp_path, monkeypatch, version, attack):
    from pallet.mask_gallery_review import VERSION as editor_version
    key, r = 'gallery', row('gallery')
    historical, parent = tmp_path/'historical', tmp_path/'parent'
    for root in (historical, parent):
        (root/'images').mkdir(parents=True)
        (root/'images'/f'{key}.png').write_bytes(b'original image bytes')
    (historical/'proposals.jsonl').write_text('frozen historical canonical bytes')
    proposal = dict(source=r, polygon=POLYGON, template={'historical': 'human'})
    source = {f: r[f] for f in ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')}
    source.update(formal_manifest_sha256='formal', image_sha256=audit.digest(historical/'images'/f'{key}.png'))
    value = dict(target_key=key, revision=2, proposal_publication=version, base_proposal_id=audit.sha(proposal),
        proposal_polygon=POLYGON, template_provenance=proposal['template'], source=source, source_binding=audit.sha(source),
        binding=audit.sha(dict(formal='formal', proposals=audit.digest(historical/'proposals.jsonl'), editor_version=editor_version)),
        event_kind='human_mask_gallery_revision', human_edited=True, polygon=POLYGON, vertex_count=8, pose=None,
        **dict.fromkeys(audit.FALSE_FLAGS, False), pose_store_written=False, original_proposal_modified=False)
    monkeypatch.setattr(audit, 'PARENT', parent)
    args = (store({key: value}), store({}), {key: r}, {key: proposal},
        {version: dict(path=historical, proposals={key: proposal})}, 'formal', 'snapshot')
    if attack == 'publication':
        value['binding'] = 'another publication'
    elif attack == 'image':
        (parent/'images'/f'{key}.png').write_bytes(b'changed image')
    elif attack == 'proposal':
        value['base_proposal_id'] = 'fake'
    elif attack == 'machine':
        value['event_kind'] = 'mask_template_proposal'
    elif attack == 'seed':
        value['polygon'], value['vertex_count'] = FOUR, 4
    elif attack == 'source':
        value['source'] = dict(source, split='test')
    if attack:
        with pytest.raises(ValueError):
            audit.independent_gallery(*args)
    else:
        result = audit.independent_gallery(*args)
        assert result[key]['gallery_review']['proposal_publication'] == version


@pytest.mark.parametrize('attack', [None, 'raw', 'event', 'id', 'snapshot'])
def test_gallery_current_and_human_event_freeze_compares_raw_sqlite_bytes(tmp_path, attack):
    snapshot, key = tmp_path/'gallery.sqlite3', 'gallery'
    value = dict(target_key=key, revision=2, polygon=POLYGON)
    raw = json.dumps(value, indent=2)
    with sqlite3.connect(snapshot) as db:
        db.executescript('CREATE TABLE reviews(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO reviews VALUES (?,?,?)', (key, 2, raw))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (10, key, 2, raw))
    record = dict(target_key=key, revision=2, current_payload_raw=raw,
        current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(), current_payload=value,
        human_event_id=10, human_event_payload_raw=raw, human_event_sha256=hashlib.sha256(raw.encode()).hexdigest(),
        snapshot_sha256='snapshot')
    if attack:
        field = dict(raw='current_payload_raw', event='human_event_payload_raw', id='human_event_id', snapshot='snapshot_sha256')[attack]
        record[field] = 11 if attack == 'id' else record[field]+' '
    write_jsonl(tmp_path/'gallery_source_records.jsonl', [record])
    before = snapshot.read_bytes()
    if attack:
        with pytest.raises(ValueError):
            audit.check_gallery_source_records(tmp_path, snapshot, store({key: value}), 'snapshot')
    else:
        audit.check_gallery_source_records(tmp_path, snapshot, store({key: value}), 'snapshot')
    assert before == snapshot.read_bytes()


@pytest.fixture
def statement_fixture(tmp_path, monkeypatch):
    source = tmp_path/'source.jsonl'
    scope = {f'old-{i}': dict(target_key=f'old-{i}') for i in range(45)}
    write_jsonl(source, scope.values())
    monkeypatch.setattr(audit, 'PROJECT', tmp_path)
    monkeypatch.setattr(audit, 'DISPOSITION_INPUT', source)
    monkeypatch.setattr(audit, 'DISPOSITION_INPUT_SHA', audit.digest(source))
    out = tmp_path/'candidate'
    out.mkdir()
    (out/'disposition_scope_source.jsonl').write_bytes(source.read_bytes())
    text = 'Delegated context: the user reviewed the exact old45 from the previous84 and requested no changes.'
    statement = dict(schema='explicit_conversation_review_statement_v1', user_statement=text,
        user_statement_sha256=hashlib.sha256(text.encode()).hexdigest(), user_statement_is_verbatim=False,
        origin='explicit_user_statement_in_conversation', summary_is_not_verbatim_quotation=True,
        scope_count=45, scope_target_keys=sorted(scope), scope_source='source.jsonl', scope_source_sha256=audit.digest(source),
        source_publication=7, source_batch_size=84, no_other_targets_authorized=True,
        creates_gallery_revision=False, human_edited=False, human_mask_confirmed=False, seed_authorization=False,
        production_database_writes=0)
    return out, scope, statement


@pytest.mark.parametrize('attack', [None, 'expand', 'hash', 'seed', 'write', 'edit', 'quotation', 'scope_source'])
def test_conversation_statement_is_honest_bounded_and_not_a_fake_sqlite_save(statement_fixture, attack):
    out, scope, statement = statement_fixture
    if attack == 'expand':
        statement['scope_target_keys'].append('other43')
    elif attack == 'hash':
        statement['user_statement'] += ' and all others'
    elif attack == 'seed':
        statement['seed_authorization'] = True
    elif attack == 'write':
        statement['production_database_writes'] = 1
    elif attack == 'edit':
        statement['human_edited'] = True
    elif attack == 'quotation':
        statement['summary_is_not_verbatim_quotation'] = False
    elif attack == 'scope_source':
        (out/'disposition_scope_source.jsonl').write_text('expanded authorization')
    write_json(out/'review_disposition_statement.json', statement)
    manifest = dict(review_disposition_statement_sha256=audit.digest(out/'review_disposition_statement.json'))
    if attack:
        with pytest.raises(ValueError):
            audit.check_statement(out, scope, manifest)
    else:
        assert audit.check_statement(out, scope, manifest) == statement['user_statement_sha256']


def report_fixture():
    proposal = candidate('manual')
    page = '''<select id="filter"><option value="">全部</option><option>1</option><option>2</option><option>9</option><option>10</option><option>15</option></select>
<input type="checkbox" id="only-new"><article id="target-manual" data-class="1" data-batch="new" data-kind="manual">本轮计入人工</article>
<script>const cls=document.querySelector('#filter'),only=document.querySelector('#only-new');
function filter(){document.querySelectorAll('article').forEach(a=>a.hidden=(!!cls.value&&a.dataset.class!==cls.value)||(only.checked&&a.dataset.batch!=='new'));}cls.onchange=only.onchange=filter;filter();</script>'''
    return page, {'manual': proposal}, {'manual': proposal}, {}, store({}), {}


def test_report_defaults_show_all_even_with_zero_new_machine_proposals():
    parsed = audit.check_report_controls(*report_fixture())
    assert 'checked' not in parsed.inputs['only-new']
    assert all('hidden' not in card for card in parsed.articles)


@pytest.mark.parametrize('attack', ['checked', 'hidden', 'wrong_class', 'wrong_default_class', 'script_overrides',
    'no_label', 'missing_card', 'duplicate_card', 'disabled', 'hardcoded_hide', 'machine_filter', 'no_handlers'])
def test_report_semantics_cannot_hide_all500_or_mislabel25manual(attack):
    args = list(report_fixture())
    page = args[0]
    if attack == 'checked':
        page = page.replace('id="only-new"', 'id="only-new" checked')
    elif attack == 'hidden':
        page = page.replace('<article ', '<article hidden ')
    elif attack == 'wrong_class':
        page = page.replace('data-class="1"', 'data-class="9"')
    elif attack == 'wrong_default_class':
        page = page.replace('<option>9</option>', '<option selected>9</option>')
    elif attack == 'script_overrides':
        page = page.replace('function filter()', 'only.checked=true;function filter()')
    elif attack == 'no_label':
        page = page.replace('本轮计入人工', '本轮机器候选')
    elif attack == 'missing_card':
        page = page.replace('id="target-manual"', 'id="target-other"')
    elif attack == 'duplicate_card':
        page += '<article id="target-manual"></article>'
    elif attack == 'hardcoded_hide':
        page = page.replace('a.hidden=(', 'a.hidden=true||(')
    elif attack == 'machine_filter':
        page = page.replace("a.dataset.batch!=='new'", "a.dataset.kind!=='machine'")
    elif attack == 'no_handlers':
        page = page.replace('cls.onchange=only.onchange=filter;', '')
    else:
        page = page.replace('id="only-new"', 'id="only-new" disabled')
    args[0] = page
    with pytest.raises(ValueError):
        audit.check_report_controls(*args)


def test_live_counters_allow_only_unrelated_append_only_user_work():
    original = dict(target_key='counted', revision=1)
    frozen = {kind: store({'counted': original}, [(1, 'counted', 1, json.dumps(original))]) for kind in ('pose', 'gallery')}
    live = copy.deepcopy(frozen)
    appended = dict(target_key='unrelated', revision=1)
    for kind in live:
        live[kind]['current']['unrelated'] = appended
        live[kind]['events'].append((2, 'unrelated', 1, json.dumps(appended)))
    audit.check_live_stability(frozen, live, {'counted'})
    live['pose']['current']['counted'] = dict(original, revision=2)
    with pytest.raises(ValueError, match='current payload changed'):
        audit.check_live_stability(frozen, live, {'counted'})


def test_live_append_cannot_create_previously_unsaved_counted_gallery_review():
    frozen = {kind: store({}) for kind in ('pose', 'gallery')}
    live = copy.deepcopy(frozen)
    value = dict(target_key='unsaved', revision=1)
    live['gallery']['current']['unsaved'] = value
    live['gallery']['events'] = [(1, 'unsaved', 1, json.dumps(value))]
    with pytest.raises(ValueError, match='dependency changed'):
        audit.check_live_stability(frozen, live, {'unsaved'})


def test_live_history_cannot_be_rewritten_even_on_unrelated_records():
    value = dict(target_key='unrelated', revision=1)
    frozen = {kind: store({'unrelated': value}, [(1, 'unrelated', 1, json.dumps(value))]) for kind in ('pose', 'gallery')}
    live = copy.deepcopy(frozen)
    live['pose']['events'][0] = (1, 'unrelated', 1, json.dumps(dict(value, fake=True)))
    with pytest.raises(ValueError, match='event changed or disappeared'):
        audit.check_live_stability(frozen, live, set())


def test_pinned_inputs_are_real_and_verifier_never_imports_completion_generator():
    for name, expected in audit.PARENT_PINS.items():
        assert audit.digest(audit.PARENT/name) == expected
    for name, expected in audit.REQUEST_PINS.items():
        assert audit.digest(audit.REQUESTS/name) == expected
    assert audit.digest(audit.DISPOSITION_INPUT) == audit.DISPOSITION_INPUT_SHA
    assert audit.digest(audit.PROJECT/audit.GENERATOR) == audit.GENERATOR_SHA
    assert len(audit.REQUEST_KEYS) == 25
    tree = ast.parse(audit.Path(audit.__file__).read_text())
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            assert node.module != 'pallet.mask_quota_100_complete'
        elif isinstance(node, ast.Import):
            assert all(n.name != 'pallet.mask_quota_100_complete' for n in node.names)


def test_failed_audit_writes_failure_report_but_never_success_seal(tmp_path, monkeypatch):
    out = tmp_path/'candidate'
    out.mkdir()
    write_json(out/'manifest.json', {})
    monkeypatch.setattr(audit, 'OUTPUT', out)
    def fail(*_):
        raise ValueError('unverified exact source')
    monkeypatch.setattr(audit, 'audit', fail)
    with pytest.raises(ValueError, match='unverified exact source'):
        audit.main(['--output', str(out)])
    assert not (out/'verification.json').exists()
    assert audit.read_json(out/'validation_report.json')['state'] == 'failed'


def test_success_seal_has_pub9_gallery_contract_and_is_exclusive(tmp_path, monkeypatch):
    out = tmp_path/'candidate'
    out.mkdir()
    write_json(out/'manifest.json', {})
    monkeypatch.setattr(audit, 'OUTPUT', out)
    monkeypatch.setattr(audit, 'audit', lambda *_: dict(state='passed', publication_version=9, proposals_verified=500,
        proposals_sha256='sealed', new_verified=0, manual_verified=25, all_manual_sources_verified=143,
        extra_public_images=['vertex2_before_after.png']))
    audit.main(['--output', str(out)])
    seal = audit.read_json(out/'verification.json')
    assert seal['publication_version'] == 9 and seal['proposals_verified'] == 500
    assert seal['new_verified'] == 0 and seal['manual_verified'] == 25
    assert seal['validation_report_sha256'] == audit.digest(out/'validation_report.json')
    before = (out/'verification.json').read_bytes()
    with pytest.raises(ValueError, match='overwrite a frozen verification'):
        audit.main(['--output', str(out)])
    assert (out/'verification.json').read_bytes() == before


def test_symlink_output_cannot_rewrite_frozen_parent_report(tmp_path, monkeypatch):
    parent, link = tmp_path/'parent', tmp_path/'candidate'
    parent.mkdir()
    write_json(parent/'manifest.json', {})
    (parent/'validation_report.json').write_text('immutable')
    link.symlink_to(parent, target_is_directory=True)
    monkeypatch.setattr(audit, 'OUTPUT', link)
    with pytest.raises(ValueError, match='publication-9'):
        audit.main(['--output', str(link)])
    assert (parent/'validation_report.json').read_text() == 'immutable'


def test_unsafe_report_symlink_cannot_be_written_on_failure(tmp_path, monkeypatch):
    out = tmp_path/'candidate'
    out.mkdir()
    write_json(out/'manifest.json', {})
    protected = tmp_path/'historical.json'
    protected.write_text('immutable')
    (out/'validation_report.json').symlink_to(protected)
    monkeypatch.setattr(audit, 'OUTPUT', out)
    with pytest.raises(ValueError, match='Unsafe validation'):
        audit.main(['--output', str(out)])
    assert protected.read_text() == 'immutable'
