from __future__ import annotations

import os
import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

from backend.app.config import get_settings


class ConfigTest(unittest.TestCase):
    def test_default_root_aliases_include_storage1_and_storage2(self) -> None:
        original = os.environ.get("WEBMANAGER_ROOT_ALIASES")
        try:
            os.environ.pop("WEBMANAGER_ROOT_ALIASES", None)
            settings = get_settings()
        finally:
            if original is None:
                os.environ.pop("WEBMANAGER_ROOT_ALIASES", None)
            else:
                os.environ["WEBMANAGER_ROOT_ALIASES"] = original

        self.assertEqual(
            settings.root_aliases,
            {
                "storage1": "/Volumes/8TB",
                "storage2": "/Volumes/8TB_RAID1",
            },
        )

    def test_default_task_db_path_is_backend_data_absolute(self) -> None:
        original = os.environ.get("WEBMANAGER_TASK_DB_PATH")
        try:
            os.environ.pop("WEBMANAGER_TASK_DB_PATH", None)
            settings = get_settings()
        finally:
            if original is None:
                os.environ.pop("WEBMANAGER_TASK_DB_PATH", None)
            else:
                os.environ["WEBMANAGER_TASK_DB_PATH"] = original

        resolved = Path(settings.task_db_path).resolve()
        expected = Path(__file__).resolve().parents[3] / "backend" / "data" / "tasks.db"
        self.assertEqual(resolved, expected.resolve())


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