from pprint import pp

import pytest
from typer.testing import CliRunner

from uv_go import cli


class TestCli(object):
    @pytest.fixture()
    def runner(self):
        return CliRunner()

    def test_root(self, runner: CliRunner):
        result = runner.invoke(cli)
        assert result.exit_code == 2  # Typer exits with 2 when no command is provided

    def test_version(self, runner: CliRunner):
        result = runner.invoke(cli, ['--version'])
        pp(result.output)
        assert result.exit_code == 0

    def test_script_execution(self, runner: CliRunner):
        result = runner.invoke(cli, ['example.calls:call'])
        assert result.exit_code == 0
        assert 'call works' in result.output

    def test_script_execution_verbose(self, runner: CliRunner):
        result = runner.invoke(cli, ['--verbose', 'example.calls:call'])
        assert result.exit_code == 0
        assert 'call works' in result.output
        assert 'Running script:' in result.output
        assert 'Importing module:' in result.output
        assert 'call works' in result.output

    def test_script_execution_class_method(self, runner: CliRunner):
        result = runner.invoke(cli, ['example.calls:Calls.call'])
        assert result.exit_code == 0
        assert 'Calls.call works' in result.output

    def test_script_execution_async(self, runner: CliRunner):
        result = runner.invoke(cli, ['example.calls:async_call'])
        assert result.exit_code == 0
        assert 'async_call works' in result.output

    def test_script_execution_invalid_script(self, runner: CliRunner):
        result = runner.invoke(cli, ['invalid.script'])
        assert result.exit_code != 0
