import json
import pytest

from pallet.cluster_targets import digest
from pallet.mask_gallery_view import online_page


def test_new_machine_inventory_drives_online_filter_without_changing_frozen_report(tmp_path):
    a,b='a'*24,'b'*24
    page=f'<input id="only-supplement" checked>只看本轮复用六份<article data-batch="previous" id="target-{a}"></article><article data-batch="supplement" id="target-{b}"></article><script>const only=document.querySelector("#only-supplement");function filter(){{a.hidden=only.checked&&a.dataset.batch!==\'supplement\';}}</script>'
    (tmp_path/'new_proposals.jsonl').write_text(json.dumps(dict(target_key=a))+'\n')
    (tmp_path/'manifest.json').write_text(json.dumps(dict(new_proposals_sha256=digest(tmp_path/'new_proposals.jsonl'))))
    (tmp_path/'report_zh.html').write_text(page)
    view=online_page(page,tmp_path,{a,b})
    assert 'id="only-new"' in view and '只看本轮新增候选' in view
    assert f'data-batch="new" id="target-{a}"' in view
    assert f'data-batch="previous" id="target-{b}"' in view
    assert "a.dataset.batch!=='new'" in view
    assert (tmp_path/'report_zh.html').read_text()==page
    with pytest.raises(ValueError):online_page(page,tmp_path,{b})


def test_existing_standard_report_remains_byte_identical(tmp_path):
    page='<input id="only-new">'
    assert online_page(page,tmp_path,set())==page


def test_standard_report_pending_heading_changes_only_with_matching_publication_seal(tmp_path):
    page='<h1>第七版 · 每类一百个不同帧（待独立核验）</h1><input id="only-new">'
    assert online_page(page,tmp_path,set())==page
    (tmp_path/'proposals.jsonl').write_text('')
    seal=tmp_path/'verification.json'
    seal.write_text(json.dumps(dict(state='passed',proposals_sha256='wrong')))
    assert online_page(page,tmp_path,set())==page
    seal.write_text(json.dumps(dict(state='passed',proposals_sha256=digest(tmp_path/'proposals.jsonl'))))
    assert online_page(page,tmp_path,set())==page.replace('待独立核验','已独立核验')


def test_verified_multiclass_page_clips_only_outer_document_without_rewriting_asset(tmp_path):
    page='<style>.table-wrap{overflow-x:auto}</style><h1>第十版（待独立核验）</h1><input id="only-new">'
    (tmp_path/'proposals.jsonl').write_text('')
    (tmp_path/'report_zh.html').write_text(page)
    (tmp_path/'verification.json').write_text(json.dumps(dict(state='passed',publication_version=10,
        proposals_sha256=digest(tmp_path/'proposals.jsonl'))))
    normalized=online_page(page,tmp_path,set())
    assert 'html{overflow-x:clip}body{overflow-x:clip}' in normalized
    assert '.table-wrap{overflow-x:auto}' in normalized and '已独立核验' in normalized
    assert (tmp_path/'report_zh.html').read_text()==page


def test_multiclass_pending_publication_heading_normalizes_after_matching_seal(tmp_path):
    page='<style></style><h1>第十版（待独立核验，尚未发布）</h1>'
    (tmp_path/'proposals.jsonl').write_text('')
    (tmp_path/'verification.json').write_text(json.dumps(dict(state='passed',publication_version=10,
        proposals_sha256=digest(tmp_path/'proposals.jsonl'))))
    normalized=online_page(page,tmp_path,set())
    assert '（已独立核验）' in normalized and '尚未发布' not in normalized
    assert 'overflow-x:clip' in normalized


def test_round14_bare_draft_heading_needs_both_manifest_and_proposal_seal(tmp_path):
    page='<style></style><h1>第十四版草稿 · 待独立核验，尚未发布</h1>'
    (tmp_path/'proposals.jsonl').write_text('');(tmp_path/'manifest.json').write_text('{}')
    (tmp_path/'report_zh.html').write_text(page)
    proof=dict(state='passed',publication_version=14,proposals_sha256=digest(tmp_path/'proposals.jsonl'),manifest_sha256='wrong')
    (tmp_path/'verification.json').write_text(json.dumps(proof))
    assert '草稿 · 待独立核验' in online_page(page,tmp_path,set())
    proof['manifest_sha256']=digest(tmp_path/'manifest.json');(tmp_path/'verification.json').write_text(json.dumps(proof))
    normalized=online_page(page,tmp_path,set())
    assert '<h1>第十四版 · 已独立核验</h1>' in normalized and '尚未发布' not in normalized
    assert (tmp_path/'report_zh.html').read_text()==page
    proof['proposals_sha256']='wrong';(tmp_path/'verification.json').write_text(json.dumps(proof))
    assert online_page(page,tmp_path,set())==page


def test_later_bare_heading_uses_matching_seal_without_hardcoded_round_number(tmp_path):
    page='<style></style><h1>第十五版草稿 · 待独立核验，尚未发布</h1>'
    (tmp_path/'proposals.jsonl').write_text('');(tmp_path/'manifest.json').write_text('{}')
    (tmp_path/'verification.json').write_text(json.dumps(dict(state='passed',publication_version=15,
        proposals_sha256=digest(tmp_path/'proposals.jsonl'),manifest_sha256=digest(tmp_path/'manifest.json'))))
    assert '<h1>第十五版 · 已独立核验</h1>' in online_page(page,tmp_path,set())
