"""Tests for the experiments dashboard route."""

from unittest.mock import patch


class TestExperimentsRoute:
    """Tests for /experiments endpoints."""

    def test_experiments_page_returns_200(self, client):
        response = client.get("/experiments")
        assert response.status_code == 200
        assert "Autoresearch" in response.text

    @patch("dashboard.routes.experiments.settings")
    def test_experiments_page_shows_disabled_when_off(self, mock_settings, client):
        mock_settings.autoresearch_enabled = False
        mock_settings.autoresearch_metric = "perplexity"
        mock_settings.autoresearch_time_budget = 300
        mock_settings.autoresearch_max_iterations = 10
        mock_settings.repo_root = "/tmp"
        mock_settings.autoresearch_workspace = "test-experiments"
        response = client.get("/experiments")
        assert response.status_code == 200
        assert "disabled" in response.text.lower() or "Disabled" in response.text

    @patch("dashboard.routes.experiments.settings")
    def test_start_experiment_when_disabled(self, mock_settings, client):
        mock_settings.autoresearch_enabled = False
        response = client.post("/experiments/start")
        assert response.status_code == 403

    def test_experiment_detail_not_found(self, client):
        response = client.get("/experiments/nonexistent-id")
        assert response.status_code == 404

    @patch("dashboard.routes.experiments.settings")
    @patch("timmy.autoresearch.subprocess.run")
    def test_start_experiment_when_enabled(self, mock_run, mock_settings, client):
        mock_settings.autoresearch_enabled = True
        mock_settings.repo_root = "/tmp"
        mock_settings.autoresearch_workspace = "test-experiments"
        mock_run.return_value = type("R", (), {"returncode": 0, "stdout": "", "stderr": ""})()
        response = client.post("/experiments/start")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "started"
