import pytest
from scripts import publish_mask_remaining_round4 as release


def test_unresolved_atomic_file_is_preserved(tmp_path):
    path=tmp_path/'active.json';path.write_text('original user config');temporary=path.with_suffix('.json.round4.tmp');temporary.write_text('unresolved work')
    with pytest.raises(ValueError,match='Unresolved'):release.write(path,dict(version=14))
    assert path.read_text()=='original user config' and temporary.read_text()=='unresolved work'


@pytest.mark.parametrize('stage,filename',[(release.activate,'activation.json'),(release.finalize,'final_acceptance.json')])
def test_completed_release_evidence_not_replaced(tmp_path,monkeypatch,stage,filename):
    monkeypatch.setattr(release,'OUT',tmp_path);(tmp_path/filename).write_text('immutable evidence')
    with pytest.raises(ValueError,match='Preserve'):stage()
    assert (tmp_path/filename).read_text()=='immutable evidence'


def test_changed_user_configuration_blocks_before_write(tmp_path,monkeypatch):
    monkeypatch.setattr(release,'OUT',tmp_path);monkeypatch.setattr(release,'seals',lambda: ({},{},{},{},{}))
    monkeypatch.setattr(release,'read',lambda _: {'active_mask_gallery.json':'expected'})
    monkeypatch.setattr(release,'file_inventory',lambda _: {'active_mask_gallery.json':'user change'})
    with pytest.raises(ValueError,match='do not overwrite'):release.activate()
    assert not (tmp_path/'activation.json').exists()
