"""OCI GenAI configuration validation tests."""

from __future__ import annotations

import os
import unittest

from src.oci_genai_sdk import ALLOWED_OCI_SETTINGS, load_oci_settings


class OCISettingsTest(unittest.TestCase):
    def setUp(self) -> None:
        self._previous = {key: os.environ.get(key) for key in ALLOWED_OCI_SETTINGS}
        os.environ.update(
            {
                "OCI_AUTH_TYPE": "config_file",
                "OCI_CONFIG_FILE": "/home/opc/.oci/config",
                "OCI_PROFILE": "DEFAULT",
            }
        )

    def tearDown(self) -> None:
        for key, value in self._previous.items():
            if value is None:
                os.environ.pop(key, None)
            else:
                os.environ[key] = value

    def test_accepts_a_child_compartment_ocid(self) -> None:
        os.environ["OCI_GENAI_COMPARTMENT_ID"] = "ocid1.compartment.oc1..example"

        settings = load_oci_settings()

        self.assertEqual("ocid1.compartment.oc1..example", settings.compartment_id)

    def test_accepts_a_tenancy_ocid_for_the_root_compartment(self) -> None:
        os.environ["OCI_GENAI_COMPARTMENT_ID"] = "ocid1.tenancy.oc1..example"

        settings = load_oci_settings()

        self.assertEqual("ocid1.tenancy.oc1..example", settings.compartment_id)

    def test_rejects_an_invalid_compartment_identifier(self) -> None:
        os.environ["OCI_GENAI_COMPARTMENT_ID"] = "not-an-ocid"

        with self.assertRaisesRegex(ValueError, "compartment is not configured"):
            load_oci_settings()


if __name__ == "__main__":
    unittest.main()
