from pathlib import Path
import json

from sentra.state import normalize_path, save_json


def test_save_json_is_deterministic(tmp_path: Path) -> None:
    destination = tmp_path / "artifact.json"

    save_json(destination, {"z": 1, "a": {"b": 2}})

    assert json.loads(destination.read_text(encoding="utf-8")) == {"a": {"b": 2}, "z": 1}
    assert destination.read_text(encoding="utf-8").index('"a"') < destination.read_text(encoding="utf-8").index('"z"')


def test_normalize_path_expands_and_returns_absolute_path(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.setenv("NETRA_TMP", str(tmp_path))

    normalized = normalize_path("$NETRA_TMP", field_name="path")

    assert normalized.is_absolute()

