"""SemVer behavior tests for the release tag calculator."""

from __future__ import annotations

import importlib.util
import sys
from pathlib import Path


def _load_module():
    root = Path(__file__).resolve().parents[1]
    script = root / "scripts" / "next_release_tag.py"
    spec = importlib.util.spec_from_file_location("next_release_tag", script)
    assert spec is not None
    assert spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


mod = _load_module()


def test_parse_legacy_alpha_tag() -> None:
    parsed = mod.parse_tag("v0.1.4a2")
    assert parsed is not None
    assert parsed.major == 0
    assert parsed.minor == 1
    assert parsed.patch == 4
    assert parsed.prerelease == ("alpha", 2)


def test_stable_is_newer_than_legacy_alpha_for_same_core() -> None:
    legacy = mod.parse_tag("v0.1.4a2")
    stable = mod.parse_tag("v0.1.4")
    assert legacy is not None
    assert stable is not None
    assert mod.compare_tags(legacy, stable) < 0


def test_patch_from_prerelease_promotes_to_stable_core() -> None:
    current = mod.parse_tag("v0.1.4a2")
    assert current is not None
    assert mod.compute_next_version(current, "patch") == "0.1.4"


def test_feature_bump_in_0x_is_patch() -> None:
    current = mod.parse_tag("v0.1.4")
    assert current is not None
    assert mod.compute_next_version(current, "feature") == "0.1.5"


def test_breaking_bump_in_0x_is_minor() -> None:
    current = mod.parse_tag("v0.1.4")
    assert current is not None
    assert mod.compute_next_version(current, "breaking") == "0.2.0"


def test_feature_bump_from_stable_after_1_is_minor() -> None:
    current = mod.parse_tag("v1.4.2")
    assert current is not None
    assert mod.compute_next_version(current, "feature") == "1.5.0"


def test_classify_release_priority() -> None:
    commits = [
        mod.Commit(subject="fix(core): bugfix", body=""),
        mod.Commit(subject="feat(sim): add feature", body=""),
    ]
    assert mod.classify_release(commits) == "feature"


def test_classify_release_breaking_change_wins() -> None:
    commits = [
        mod.Commit(subject="feat(sim): add feature", body="BREAKING CHANGE: API changed"),
        mod.Commit(subject="fix(core): bugfix", body=""),
    ]
    assert mod.classify_release(commits) == "breaking"
