"""Integration tests for AgentRunner environment propagation via fixture interface.

These tests intentionally use only the ``env_test_runner`` fixture contract from
test-scaffolding and assert behavior through returned snapshots.
"""

from pathlib import Path

from tests.integration_helpers import env_test_runner  # noqa: F401 - pytest fixture re-export


def test_anima_state_set_when_missing(monkeypatch, request) -> None:
    """(T1) ANIMA_STATE is set when absent before setup."""
    monkeypatch.delenv("ANIMA_STATE", raising=False)

    result = request.getfixturevalue("env_test_runner")
    env_snapshot = result["env_snapshot"]

    assert env_snapshot["ANIMA_STATE"], "Expected ANIMA_STATE to be set when missing"


def test_existing_anima_state_preserved(monkeypatch, request, tmp_path: Path) -> None:
    """(T2) Existing ANIMA_STATE value is preserved by setup."""
    preconfigured_state = tmp_path / "preconfigured-anima-state"
    preconfigured_state.mkdir(parents=True, exist_ok=True)
    monkeypatch.setenv("ANIMA_STATE", str(preconfigured_state))

    result = request.getfixturevalue("env_test_runner")
    env_snapshot = result["env_snapshot"]

    assert env_snapshot["ANIMA_STATE"] == str(preconfigured_state), (
        "Expected existing ANIMA_STATE to be preserved; "
        f"got {env_snapshot['ANIMA_STATE']!r}"
    )


def test_agent_and_trace_ids_set_from_state(env_test_runner) -> None:  # noqa: F811
    """(T3) ANIMA_AGENT_ID and ANIMA_TRACE_ID mirror state identifiers."""
    env_snapshot = env_test_runner["env_snapshot"]
    state = env_test_runner["state"]

    assert env_snapshot["ANIMA_AGENT_ID"] == state.instance_id, (
        f"Expected ANIMA_AGENT_ID={state.instance_id!r}, "
        f"got {env_snapshot['ANIMA_AGENT_ID']!r}"
    )
    assert env_snapshot["ANIMA_TRACE_ID"] == state.trace_id, (
        f"Expected ANIMA_TRACE_ID={state.trace_id!r}, "
        f"got {env_snapshot['ANIMA_TRACE_ID']!r}"
    )
