"""Tests for autobuilder_hook_helpers.ci."""

import sys
from pathlib import Path
from unittest.mock import call, patch

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "hook-helpers"))

from autobuilder_hook_helpers.ci import wait_for_ci


class TestWaitForCi:
    def test_passes_immediately(self, tmp_path):
        run_data = {"id": 100, "status": "completed", "conclusion": "success"}
        with patch(
            "autobuilder_hook_helpers.ci.get_latest_workflow_run",
            return_value=run_data,
        ):
            ok, run_id = wait_for_ci(
                tmp_path, "owner", "repo", "sha123",
                timeout=5, poll_interval=1,
            )
        assert ok is True
        assert run_id == 100

    def test_fails_immediately(self, tmp_path):
        run_data = {"id": 200, "status": "completed", "conclusion": "failure"}
        with patch(
            "autobuilder_hook_helpers.ci.get_latest_workflow_run",
            return_value=run_data,
        ):
            ok, run_id = wait_for_ci(
                tmp_path, "owner", "repo", "sha123",
                timeout=5, poll_interval=1,
            )
        assert ok is False
        assert run_id == 200

    def test_waits_then_passes(self, tmp_path):
        pending = {"id": 300, "status": "in_progress", "conclusion": ""}
        done = {"id": 300, "status": "completed", "conclusion": "success"}
        responses = [pending, done]

        with patch(
            "autobuilder_hook_helpers.ci.get_latest_workflow_run",
            side_effect=responses,
        ), patch("time.sleep"):
            ok, run_id = wait_for_ci(
                tmp_path, "owner", "repo", "sha123",
                timeout=60, poll_interval=1,
            )
        assert ok is True
        assert run_id == 300

    def test_timeout(self, tmp_path):
        pending = {"id": 400, "status": "queued", "conclusion": ""}
        with patch(
            "autobuilder_hook_helpers.ci.get_latest_workflow_run",
            return_value=pending,
        ), patch("time.sleep"), patch("time.time") as mock_time:
            mock_time.side_effect = [0, 0, 100, 200, 700]
            ok, run_id = wait_for_ci(
                tmp_path, "owner", "repo", "sha123",
                timeout=5, poll_interval=1,
            )
        assert ok is False

    def test_no_run_found_timeout(self, tmp_path):
        with patch(
            "autobuilder_hook_helpers.ci.get_latest_workflow_run",
            return_value=None,
        ), patch("time.sleep"), patch("time.time") as mock_time:
            mock_time.side_effect = [0, 0, 100, 700]
            ok, run_id = wait_for_ci(
                tmp_path, "owner", "repo", "sha123",
                timeout=5, poll_interval=1,
            )
        assert ok is False
        assert run_id == 0

    def test_custom_branch(self, tmp_path):
        run_data = {"id": 500, "status": "completed", "conclusion": "success"}
        with patch(
            "autobuilder_hook_helpers.ci.get_latest_workflow_run",
            return_value=run_data,
        ) as mock_get:
            wait_for_ci(
                tmp_path, "owner", "repo", "sha123",
                branch="develop", timeout=5, poll_interval=1,
            )
        mock_get.assert_called_with(
            tmp_path, "owner", "repo", "sha123", branch="develop",
        )
