#!/usr/bin/python3
"""Tests for slpkg.views.package_line.Imprint.package_line() truncation logic."""

import pytest

from slpkg.views.package_line import Imprint, PackageData


def make_imprint(package_alignment: int = 20, version_alignment: int = 10) -> Imprint:
    """Construct Imprint bypassing __init__ (avoids config_load/shutil)."""
    imp: Imprint = object.__new__(Imprint)
    imp.bold = ""
    imp.cyan = ""
    imp.endc = ""
    imp.package_alignment = package_alignment
    imp.version_alignment = version_alignment
    imp.size_alignment = 9
    imp.repo_alignment = 14
    return imp


def make_pkg(package: str = "ffmpeg", version: str = "7.1", size: str = "10 MB", repo: str = "alien") -> PackageData:
    return PackageData(package=package, version=version, size=size, color="", repo=repo)


# ---------------------------------------------------------------------------
# PackageData dataclass
# ---------------------------------------------------------------------------


def test_package_data_stores_fields() -> None:
    pkg = PackageData(package="ffmpeg", version="7.1", size="10 MB", color="", repo="alien")
    assert pkg.package == "ffmpeg"
    assert pkg.version == "7.1"
    assert pkg.size == "10 MB"
    assert pkg.color == ""
    assert pkg.repo == "alien"


# ---------------------------------------------------------------------------
# package_line() — version truncation
# Threshold: len(version) >= version_alignment - 5
# Truncated string: version[:version_alignment - 5] + '...'
# With version_alignment=10: threshold=5, prefix_len=5
# ---------------------------------------------------------------------------


def test_short_version_not_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(version_alignment=10)
    # 'hi' (2 chars) < 5 → not truncated
    imp.package_line(make_pkg(version="hi"))
    assert "hi" in capsys.readouterr().out


def test_version_at_threshold_is_truncated(capsys: pytest.CaptureFixture) -> None:
    # version_alignment=10: threshold=5; 5 chars >= 5 → truncated
    imp = make_imprint(version_alignment=10)
    imp.package_line(make_pkg(version="12345"))
    captured = capsys.readouterr().out
    assert "12345..." in captured


def test_long_version_is_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(version_alignment=10)
    imp.package_line(make_pkg(version="1.2.3.4.5.6"))
    captured = capsys.readouterr().out
    # prefix=5: '1.2.3' + '...'
    assert "1.2.3..." in captured
    assert "1.2.3.4.5.6" not in captured


def test_version_just_below_threshold_not_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(version_alignment=10)
    # 4 chars < 5 → not truncated
    imp.package_line(make_pkg(version="1234"))
    captured = capsys.readouterr().out
    assert "1234" in captured
    assert "..." not in captured


# ---------------------------------------------------------------------------
# package_line() — package name truncation
# Threshold: len(package) >= package_alignment - 4
# Truncated string: package[:package_alignment - 4] + '...'
# With package_alignment=10: threshold=6, prefix_len=6
# ---------------------------------------------------------------------------


def test_short_package_name_not_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(package_alignment=10, version_alignment=40)
    imp.package_line(make_pkg(package="short"))  # 5 < 6
    captured = capsys.readouterr().out
    assert "short" in captured
    assert "..." not in captured


def test_package_name_at_threshold_is_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(package_alignment=10, version_alignment=40)
    # 6 chars >= 6 → truncated; prefix = 6 chars + '...'
    imp.package_line(make_pkg(package="sixchr"))
    captured = capsys.readouterr().out
    assert "sixchr..." in captured


def test_long_package_name_is_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(package_alignment=10, version_alignment=40)
    imp.package_line(make_pkg(package="averylongpackagename"))
    captured = capsys.readouterr().out
    # prefix=6: 'averyl' + '...'
    assert "averyl..." in captured
    assert "averylongpackagename" not in captured


def test_package_name_just_below_threshold_not_truncated(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint(package_alignment=10, version_alignment=40)
    # 5 chars < 6 → not truncated
    imp.package_line(make_pkg(package="vlcpl"))
    captured = capsys.readouterr().out
    assert "vlcpl" in captured
    assert "..." not in captured


# ---------------------------------------------------------------------------
# package_line() — output contains expected fields
# ---------------------------------------------------------------------------


def test_package_line_contains_size(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint()
    imp.package_line(make_pkg(size="5 MB"))
    assert "5 MB" in capsys.readouterr().out


def test_package_line_contains_repo(capsys: pytest.CaptureFixture) -> None:
    imp = make_imprint()
    imp.package_line(make_pkg(repo="alien"))
    assert "alien" in capsys.readouterr().out
