from __future__ import annotations

from pathlib import Path

import yaml


ROOT = Path(__file__).resolve().parents[3]
EXPECTED_ORDER = {
    "OLLAMA_URL": "http://192.168.0.110:11435",
    "OLLAMA_SECONDARY_URL": "http://192.168.0.110:11436",
    "OLLAMA_FALLBACK_URL": "http://192.168.0.110:11437",
}


def _load_single_doc(path: Path) -> dict:
    docs = [doc for doc in yaml.safe_load_all(path.read_text()) if doc]
    assert docs, f"no YAML documents in {path}"
    return docs[0]


def test_prod_configmap_preserves_ollama_policy_order() -> None:
    configmap = _load_single_doc(ROOT / "k8s/awoooi-prod/04-configmap.yaml")

    assert configmap["data"] | EXPECTED_ORDER == configmap["data"]
    assert {configmap["data"][key] for key in EXPECTED_ORDER} == set(EXPECTED_ORDER.values())


def test_prod_deployment_preserves_ollama_policy_order() -> None:
    deployment = _load_single_doc(ROOT / "k8s/awoooi-prod/06-deployment-api.yaml")
    env = {
        item["name"]: item.get("value")
        for item in deployment["spec"]["template"]["spec"]["containers"][0]["env"]
    }

    assert {key: env[key] for key in EXPECTED_ORDER} == EXPECTED_ORDER
    assert {env[key] for key in EXPECTED_ORDER} == set(EXPECTED_ORDER.values())
