from __future__ import annotations

from pathlib import Path
from unittest import mock

from dspy_file.logging_config import build_log_path


def test_build_log_path_defaults_under_dot_dspyteach(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.chdir(tmp_path)

    result = build_log_path()

    assert result == (tmp_path / ".dspyteach" / "logs" / "run.log").resolve()


def test_build_log_path_resolves_explicit_path(tmp_path: Path) -> None:
    result = build_log_path(str(tmp_path / "custom.log"))

    assert result == (tmp_path / "custom.log").resolve()


def test_main_uses_log_path_from_env(tmp_path: Path, monkeypatch) -> None:
    from dspy_file import analyze_file_cli

    source = tmp_path / "example.md"
    source.write_text("# Title\n\nBody", encoding="utf-8")
    log_path = tmp_path / "custom.log"

    monkeypatch.setenv("DSPYTEACH_LOG_PATH", str(log_path))

    with monkeypatch.context() as m:
        m.chdir(tmp_path)
        with mock.patch.object(analyze_file_cli, "configure_model"), mock.patch.object(
            analyze_file_cli, "analyze_path", return_value=0
        ):
            exit_code = analyze_file_cli.main([str(source)])

    assert exit_code == 0
    assert log_path.exists()
