from unittest import TestCase

from ha_services import __version__
from ha_services.cli.cli_app import PACKAGE_ROOT
from ha_services.cli_tools.test_utils.assertion import assert_in, assert_startswith
from ha_services.cli_tools.test_utils.rich_test_utils import (
    NoColorEnvRich,
    NoColorEnvRichClick,
    NoColorRichClickCli,
    NoColorTermEnviron,
    assert_no_color_env,
    assert_rich_click_no_color,
    assert_rich_no_color,
    assert_subprocess_rich_diagnose_no_color,
)


class MockRichTestCase(TestCase):
    def test_NoColorTermEnviron(self):
        with NoColorTermEnviron(width=100):
            assert_no_color_env(width=100)
            assert_subprocess_rich_diagnose_no_color(width=100)

    def test_NoColorEnvRich(self):
        with NoColorEnvRich(width=100):
            assert_no_color_env(width=100)
            assert_subprocess_rich_diagnose_no_color(width=100)

            assert_rich_no_color(width=100)

    def test_NoColorEnvRichClick(self):
        with NoColorEnvRichClick(width=100):
            assert_no_color_env(width=100)
            assert_subprocess_rich_diagnose_no_color(width=100)
            assert_rich_no_color(width=100)

            assert_rich_click_no_color(width=100)

    def test_NoColorRichClickCli(self):
        with NoColorRichClickCli() as cm:
            # Without "strip_line_prefix":
            stdout = cm.invoke(cli_bin=PACKAGE_ROOT / 'dev-cli.py', args=('--help',))
            assert_in(
                stdout,
                parts=(
                    '.venv/bin/ha_services_dev --help',
                    f'ha_services v{__version__}',
                    'Usage: ./dev-cli.py',
                    'Show this message and exit.',
                ),
            )
            assert_startswith(stdout, f'\n+ {PACKAGE_ROOT}')

            # Remove prefix lines:
            stdout = cm.invoke(cli_bin=PACKAGE_ROOT / 'dev-cli.py', args=('--help',), strip_line_prefix='Usage: ')
            assert_in(
                stdout,
                parts=(
                    'Usage: ./dev-cli.py',
                    'Show this message and exit.',
                ),
            )
            assert_startswith(stdout, ' Usage: ./dev-cli.py ')
