"""Tests for CLI error handling when not in an experiment directory."""

import os

from click.testing import CliRunner

from abreka.cli import main


class TestConfigNotFoundHandling:
    """Commands that require abreka/config.toml should show a friendly message."""

    def test_run_outside_project(self, tmp_path):
        runner = CliRunner()
        prev = os.getcwd()
        try:
            os.chdir(tmp_path)
            result = runner.invoke(main, ["run", "--step"], catch_exceptions=False)
        finally:
            os.chdir(prev)
        assert result.exit_code != 0
        assert "not inside an abreka project" in result.output.lower()
        assert "cd into your project directory" in result.output
        assert "abreka init" in result.output
        assert "Traceback" not in result.output

    def test_status_outside_project(self, tmp_path):
        runner = CliRunner()
        prev = os.getcwd()
        try:
            os.chdir(tmp_path)
            result = runner.invoke(main, ["status"], catch_exceptions=False)
        finally:
            os.chdir(prev)
        assert result.exit_code != 0
        assert "not inside an abreka project" in result.output.lower()
        assert "cd into your project directory" in result.output
        assert "abreka init" in result.output
        assert "Traceback" not in result.output

    def test_goal_outside_project(self, tmp_path):
        runner = CliRunner()
        prev = os.getcwd()
        try:
            os.chdir(tmp_path)
            result = runner.invoke(main, ["goal"], catch_exceptions=False)
        finally:
            os.chdir(prev)
        assert result.exit_code != 0
        assert "not inside an abreka project" in result.output.lower()
        assert "cd into your project directory" in result.output
        assert "abreka init" in result.output
        assert "Traceback" not in result.output
