"""Public verification compares exact bytes, never a size/count-only shortcut."""
import hashlib
import json
import sqlite3
from types import SimpleNamespace

import pytest

from scripts.check_mask_public_pool import checked_bytes,stored


def test_static_public_asset_requires_exact_sha_without_accepting_similar_size():
    calls=[]
    class Client:
        def get(self,path):calls.append(path);return SimpleNamespace(content=b'changed')
    with pytest.raises(ValueError,match='Public bytes differ'):
        checked_bytes(Client(),'/masks/example.png',hashlib.sha256(b'correct').hexdigest())
    assert calls==['/masks/example.png']


def test_exact_static_public_asset_passes():
    client=SimpleNamespace(get=lambda _:SimpleNamespace(content=b'actual public bytes'))
    assert checked_bytes(client,'/images/example.png',hashlib.sha256(b'actual public bytes').hexdigest())=='/images/example.png'


def test_read_only_store_snapshot_retains_exact_event_bytes(tmp_path):
    path=tmp_path/'synthetic.sqlite3';raw='{"revision": 1, "note": "fixture"}'
    with sqlite3.connect(path) as db:
        db.executescript('CREATE TABLE reviews(target_key TEXT,payload TEXT);CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO reviews VALUES(?,?)',('fixture',raw))
        db.execute('INSERT INTO events VALUES(?,?,?,?)',(1,'fixture',1,raw))
    before=path.read_bytes();values,events=stored(path,'reviews',immutable=True)
    assert values=={'fixture':json.loads(raw)} and events==[(1,'fixture',1,raw)] and path.read_bytes()==before
