import json

import pytest

from keepedu_performance.core import pipeline_lock as lock_module


def test_pipeline_lock_publishes_status_and_rejects_competitor(tmp_path, monkeypatch):
    status_path = tmp_path / "gold" / "dashboard" / "pipeline_status.json"
    monkeypatch.setattr(lock_module, "LOCK_PATH", tmp_path / "runtime" / "pipeline.lock")
    monkeypatch.setattr(lock_module, "STATUS_PATH", status_path)

    with lock_module.pipeline_lock("atualizar-tudo"):
        active = json.loads(status_path.read_text(encoding="utf-8"))
        assert active["ativo"] is True
        assert active["tipo"] == "global"
        with pytest.raises(lock_module.PipelineBusyError):
            with lock_module.pipeline_lock("atualizar"):
                pass

    inactive = json.loads(status_path.read_text(encoding="utf-8"))
    assert inactive["ativo"] is False
    assert inactive["tipo"] == "global"
