import pytest
from scripts import publish_mask_remaining_round3 as release


def test_unresolved_atomic_file_is_not_overwritten(tmp_path):
    path=tmp_path/'active.json';path.write_text('old user configuration')
    temporary=path.with_suffix('.json.round3.tmp');temporary.write_text('unresolved previous work')
    with pytest.raises(ValueError,match='Unresolved'):release.write(path,dict(version=13))
    assert path.read_text()=='old user configuration' and temporary.read_text()=='unresolved previous work'


@pytest.mark.parametrize('stage,filename',[(release.activate,'activation.json'),(release.finalize,'final_acceptance.json')])
def test_completed_release_evidence_cannot_be_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_any_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()
