from __future__ import annotations

from pathlib import Path

import pytest

from blackout_notifier.state import StateError, StateRepository, empty_state


def test_state_round_trip_is_deterministic(tmp_path: Path) -> None:
    repository = StateRepository(tmp_path / "nested" / "outages.json")

    repository.save(empty_state())
    first = repository.path.read_text(encoding="utf-8")
    repository.save(repository.load())

    assert repository.load() == empty_state()
    assert repository.path.read_text(encoding="utf-8") == first


def test_corrupt_state_fails_closed(tmp_path: Path) -> None:
    path = tmp_path / "outages.json"
    path.write_text("not json", encoding="utf-8")

    with pytest.raises(StateError, match="Cannot read state"):
        StateRepository(path).load()

    assert path.read_text(encoding="utf-8") == "not json"
