from __future__ import annotations

import pytest


def test_v2_config_from_root_dict_new_format_defaults():
    from argus.service.config import V2Config

    cfg = V2Config.from_root_dict(
        {
            "ray": {"shared_root": "/private"},
            "service": {
                "api": {"host": "127.0.0.1", "port": 9999},
                "auth": {"token_env": "X"},
                "sqlite": {"db_path": "/tmp/x.sqlite3"},
                "scheduler": {"tick_s": 1, "retry_interval_s": 2, "max_running_tasks": 3},
            },
        }
    )
    assert cfg.api.host == "127.0.0.1"
    assert cfg.api.port == 9999
    assert cfg.auth.token_env == "X"
    assert cfg.sqlite.db_path.endswith(".sqlite3")
    assert cfg.scheduler.max_running_tasks == 3


def test_v2_config_backward_compat_v2_section_and_default_db_path():
    from argus.service.config import V2Config

    cfg = V2Config.from_root_dict({"shared_root": "/private", "v2": {"sqlite": {}}})
    assert cfg.sqlite.db_path == "/private/common/db/mvp.sqlite3"


def test_v2_config_requires_mappings():
    from argus.service.config import V2Config

    with pytest.raises(ValueError, match="config\\.service must be a mapping"):
        V2Config.from_root_dict({"service": ["nope"]})
    with pytest.raises(ValueError, match="config\\.service\\.\\{api,auth,sqlite,scheduler\\} must be mappings"):
        V2Config.from_root_dict({"service": {"api": [1], "auth": {}, "sqlite": {}, "scheduler": {}}})
