"""Tests for OptimizationConfig."""

from __future__ import annotations

from pathlib import Path

import pytest

from codeflash_python.api import OptimizationConfig


class TestOptimizationConfig:
    """OptimizationConfig construction and serialization."""

    def test_minimal_construction(self) -> None:
        """Only project_root and module_root are required."""
        cfg = OptimizationConfig(
            project_root=Path(),
            module_root=Path("src"),
        )
        assert cfg.project_root == Path()
        assert cfg.module_root == Path("src")

    def test_defaults(self) -> None:
        """Default values match expectations."""
        cfg = OptimizationConfig(
            project_root=Path(),
            module_root=Path("src"),
        )
        assert cfg.tests_root == Path("tests")
        assert cfg.test_framework == "pytest"
        assert cfg.pytest_cmd == "pytest"
        assert cfg.ignore_paths == ()
        assert cfg.api_key == ""
        assert cfg.n_candidates == 5
        assert 120.0 == cfg.ai_timeout

    def test_path_converter(self) -> None:
        """String arguments are converted to Path objects."""
        cfg = OptimizationConfig(
            project_root="/tmp/proj",  # type: ignore[arg-type]
            module_root="src",  # type: ignore[arg-type]
        )
        assert isinstance(cfg.project_root, Path)
        assert isinstance(cfg.module_root, Path)

    def test_frozen(self) -> None:
        """Instances are immutable."""
        cfg = OptimizationConfig(
            project_root=Path(),
            module_root=Path("src"),
        )
        with pytest.raises(AttributeError):
            cfg.n_candidates = 10  # type: ignore[misc]

    def test_to_dict_from_dict_roundtrip(self) -> None:
        """from_dict(to_dict(cfg)) == cfg."""
        cfg = OptimizationConfig(
            project_root=Path("/tmp/proj"),
            module_root=Path("src"),
            tests_root=Path("test"),
            test_framework="unittest",
            pytest_cmd="python -m pytest",
            ignore_paths=(Path("vendor"), Path(".tox")),
            api_key="sk-test",
            n_candidates=3,
            ai_timeout=60.0,
        )
        restored = OptimizationConfig.from_dict(cfg.to_dict())
        assert restored == cfg

    def test_roundtrip_minimal(self) -> None:
        """Minimal config roundtrips correctly."""
        cfg = OptimizationConfig(
            project_root=Path(),
            module_root=Path("src"),
        )
        assert OptimizationConfig.from_dict(cfg.to_dict()) == cfg
