# SPDX-FileCopyrightText: 2025 OmniNode.ai Inc.
# SPDX-License-Identifier: MIT

"""
Integration tests for check-kafka-topics skill.

Tests:
- Topic listing
- Topic filtering by pattern
- Topic statistics
- Error handling

Created: 2025-11-20
"""

from pathlib import Path
from unittest.mock import patch

# Import load_skill_module from conftest
conftest_path = Path(__file__).parent / "conftest.py"
import importlib.util

spec = importlib.util.spec_from_file_location("conftest", conftest_path)
conftest = importlib.util.module_from_spec(spec)
spec.loader.exec_module(conftest)
load_skill_module = conftest.load_skill_module


# Import from check-kafka-topics/execute.py

# Load the check-kafka-topics execute module
execute = load_skill_module("check-kafka-topics")
main = execute.main


class TestCheckKafkaTopics:
    """Test check-kafka-topics skill."""

    def test_list_all_topics(self):
        """Test listing all Kafka topics."""
        with (
            patch.object(execute, "list_topics") as mock_topics,
            patch("sys.argv", ["execute.py"]),
        ):
            mock_topics.return_value = {
                "success": True,
                "count": 15,
                "topics": [
                    "agent.routing.requested.v1",
                    "agent.routing.completed.v1",
                    "onex.cmd.omniintelligence.code-analysis.v1",
                ],
            }

            exit_code = main()

            assert exit_code == 0
            mock_topics.assert_called_once()

    def test_filter_topics_by_pattern(self):
        """Test filtering topics by pattern."""
        with (
            patch.object(execute, "list_topics") as mock_topics,
            patch("sys.argv", ["execute.py", "--topics", "agent.routing.*"]),
        ):
            mock_topics.return_value = {
                "success": True,
                "count": 3,
                "topics": [
                    "agent.routing.requested.v1",
                    "agent.routing.completed.v1",
                    "agent.routing.failed.v1",
                ],
            }

            exit_code = main()

            assert exit_code == 0

    def test_kafka_unavailable(self):
        """Test handling when Kafka is unavailable."""
        with (
            patch.object(execute, "list_topics") as mock_topics,
            patch("sys.argv", ["execute.py"]),
        ):
            mock_topics.return_value = {
                "success": False,
                "error": "Connection refused",
                "count": 0,
                "topics": [],
            }

            exit_code = main()

            assert exit_code == 1

    def test_empty_topic_list(self):
        """Test handling of empty topic list."""
        with (
            patch.object(execute, "list_topics") as mock_topics,
            patch("sys.argv", ["execute.py"]),
        ):
            mock_topics.return_value = {
                "success": True,
                "count": 0,
                "topics": [],
            }

            exit_code = main()

            assert exit_code == 0
