import pytest

from gui_v6.single_instance import (
    APP_MUTEX_NAME,
    AlreadyRunningError,
    SingleInstance,
)


def test_mutex_name_is_stable():
    # Nom destiné à l'installeur (Inno AppMutex, Plan 3). Sentinelle anti-renommage accidentel.
    assert APP_MUTEX_NAME == "AivanonymAnonymisationV6"


def test_second_instance_is_rejected(tmp_path, monkeypatch):
    monkeypatch.setenv("LOCALAPPDATA", str(tmp_path))
    first = SingleInstance()
    first.acquire()
    try:
        with pytest.raises(AlreadyRunningError):
            SingleInstance().acquire()
    finally:
        first.release()


def test_release_allows_reacquire(tmp_path, monkeypatch):
    monkeypatch.setenv("LOCALAPPDATA", str(tmp_path))
    a = SingleInstance()
    a.acquire()
    a.release()
    b = SingleInstance()
    b.acquire()  # ne lève pas
    b.release()
