from __future__ import annotations

from unittest.mock import MagicMock

import pytest

from pygittools.tui import tabs


def test_init_tab_colors_without_colors(monkeypatch: pytest.MonkeyPatch) -> None:
	monkeypatch.setattr(tabs, "has_colors", lambda: False)
	tabs.init_tab_colors()
	assert tabs._active_tab_attr == tabs.A_BOLD


def test_init_tab_colors_with_colors(monkeypatch: pytest.MonkeyPatch) -> None:
	monkeypatch.setattr(tabs, "has_colors", lambda: True)
	monkeypatch.setattr(tabs, "init_pair", MagicMock())
	tabs.init_tab_colors()
	assert tabs._active_tab_attr == tabs.COLOR_PAIR(tabs._TAB_ACTIVE_PAIR)


def test_draw_tab_row_zero_width() -> None:
	tabs.draw_tab_row(0, 0, "prefix", ("A",), 0)


def test_draw_tab_row_renders_tabs() -> None:
	tabs.draw_tab_row(0, 80, "project", ("Status", "Log"), 1, prefix_attr=1)


def test_tabs_reserved_width_empty() -> None:
	assert tabs._tabs_reserved_width(()) == 0


def test_clip_prefix_truncates_with_ellipsis() -> None:
	clipped = tabs._clip_prefix("very-long-project-name", 20, ("Status", "Log"))
	assert clipped.endswith("…")
	assert len(clipped) <= 20 - tabs._tabs_reserved_width(("Status", "Log"))


def test_clip_prefix_short_max() -> None:
	clipped = tabs._clip_prefix("hello", 10, ("Status",))
	assert len(clipped) <= 10
	assert clipped.startswith("h")