from __future__ import annotations

from pathlib import Path

from ablate.config.variant import ConfigVariant


def test_from_path_reads_file(tmp_path: Path):
    p = tmp_path / "CLAUDE.md"
    p.write_text("line one\nline two\n")
    v = ConfigVariant.from_path("A", p)
    assert v.text == "line one\nline two\n"
    assert v.line_count == 2


def test_from_path_dev_null(tmp_path: Path):
    v = ConfigVariant.from_path("B", "/dev/null")
    assert v.text is None
    assert v.line_count == 0
    assert v.source == "/dev/null"


def test_from_path_missing_file_is_none(tmp_path: Path):
    v = ConfigVariant.from_path("B", tmp_path / "does-not-exist.md")
    assert v.text is None


def test_apply_writes_file_into_worktree(tmp_path: Path):
    src = tmp_path / "src.md"
    src.write_text("hello\n")
    v = ConfigVariant.from_path("A", src)

    worktree = tmp_path / "wt"
    worktree.mkdir()
    v.apply(worktree, "CLAUDE.md")

    assert (worktree / "CLAUDE.md").read_text() == "hello\n"


def test_apply_none_removes_existing_file(tmp_path: Path):
    worktree = tmp_path / "wt"
    worktree.mkdir()
    (worktree / "CLAUDE.md").write_text("stale\n")

    v = ConfigVariant.from_path("B", "/dev/null")
    v.apply(worktree, "CLAUDE.md")

    assert not (worktree / "CLAUDE.md").exists()


def test_apply_none_on_absent_file_does_not_raise(tmp_path: Path):
    worktree = tmp_path / "wt"
    worktree.mkdir()
    ConfigVariant.from_path("B", "/dev/null").apply(worktree, "CLAUDE.md")
