"""Unit tests for the NOVA-only env helper (core/env.py, D-108, REQ-164).

P5 (REQ-164) removed the ACDL_* dual-read fallback. `get_env` now reads
`NOVA_*` only. These tests verify:
  - only NOVA_* set → returned
  - neither set → default returned
  - ACDL_* set but NOVA_* unset → default returned (fallback REMOVED)
  - blank NOVA_* → default returned (not the ACDL_* value)
"""

from __future__ import annotations

import pytest

from core import env


@pytest.fixture(autouse=True)
def _isolate_env(monkeypatch):
    """Ensure no ACDL_*/NOVA_* leakage between tests."""
    for key in list(__import__("os").environ):
        if key.startswith(("ACDL_", "NOVA_")):
            monkeypatch.delenv(key, raising=False)
    yield


def test_only_nova_set(monkeypatch):
    monkeypatch.setenv("NOVA_AWS_ACCOUNT_ID", "nova-value")
    assert env.get_env("AWS_ACCOUNT_ID") == "nova-value"


def test_neither_set_returns_default():
    assert env.get_env("AWS_ACCOUNT_ID") is None
    assert env.get_env("AWS_ACCOUNT_ID", default="581513795199") == "581513795199"


def test_only_acdl_set_no_fallback(monkeypatch):
    # P5 (REQ-164): ACDL_* fallback removed — ACDL_* alone returns default.
    monkeypatch.setenv("ACDL_AWS_ACCOUNT_ID", "acdl-value")
    assert env.get_env("AWS_ACCOUNT_ID") is None
    assert env.get_env("AWS_ACCOUNT_ID", default="fallback") == "fallback"


def test_both_set_nova_wins(monkeypatch):
    # NOVA_* takes precedence; ACDL_* is ignored.
    monkeypatch.setenv("NOVA_AWS_ACCOUNT_ID", "nova-value")
    monkeypatch.setenv("ACDL_AWS_ACCOUNT_ID", "acdl-value")
    assert env.get_env("AWS_ACCOUNT_ID") == "nova-value"


def test_blank_nova_returns_default_not_acdl(monkeypatch):
    # An explicitly-empty NOVA key returns default (not an ACDL_* value).
    monkeypatch.setenv("NOVA_AWS_ACCOUNT_ID", "")
    monkeypatch.setenv("ACDL_AWS_ACCOUNT_ID", "acdl-value")
    assert env.get_env("AWS_ACCOUNT_ID") is None
    assert env.get_env("AWS_ACCOUNT_ID", default="d") == "d"