"""TS-13: property test for the config save/load round-trip.

`config/_loader.py`'s `save_settings` ⇄ `load_settings` is a parse/serialise
round-trip — exactly the shape TS-13 mandates a Hypothesis property test for.
The invariant: the round-trip reaches a fixed point — once a settings object
has been saved + loaded (normalised), saving + loading it AGAIN is the identity.
That catches serialise/parse bugs without depending on the exact normalisation
the preset applies on first load.
"""

from __future__ import annotations

import tempfile
from pathlib import Path

from hypothesis import given
from hypothesis import settings as hyp_settings

from mgf.common.config import load_settings, save_settings
from mgf.common.settings import MgfSettings
from tests.unit.config._strategies import mgf_settings


@hyp_settings(max_examples=25, deadline=None)
@given(s=mgf_settings())
def test_save_load_round_trip_is_idempotent(s: MgfSettings) -> None:
    # The robust round-trip invariant: f = load∘save reaches a fixed point —
    # once normalised, saving + loading AGAIN is the identity. This catches any
    # serialise/parse drift (a field that doesn't round-trip cleanly, a lossy
    # dump) without depending on the exact load-time normalisation the preset
    # applies. The generated rotating-size / backup-count / journald / format
    # fields vary across examples, so this exercises real variation.
    with tempfile.TemporaryDirectory() as d:
        p1 = Path(d) / "c1.yaml"
        p2 = Path(d) / "c2.yaml"
        save_settings(s, p1)
        loaded = load_settings(MgfSettings, path=p1)
        save_settings(loaded, p2)
        reloaded = load_settings(MgfSettings, path=p2)
        assert loaded.model_dump(mode="json") == reloaded.model_dump(mode="json")
