# #region Test.TaskContext [C:3] [TYPE Module] [SEMANTICS test,task,context]
# @BRIEF Tests for core/task_manager/context.py — TaskContext class.
# @RELATION BINDS_TO -> [TaskContextModule]
# @TEST_EDGE: get_param_default -> returns default when key missing
# @TEST_EDGE: create_sub_context_preserves_params

from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))

from unittest.mock import MagicMock, AsyncMock
import pytest


class TestTaskContextInit:
    """Verify TaskContext.__init__."""

    def test_initializes_with_required_params(self):
        from src.core.task_manager.context import TaskContext

        add_log = MagicMock()
        ctx = TaskContext(task_id="task-1", add_log_fn=add_log, params={"key": "val"})
        assert ctx._task_id == "task-1"
        assert ctx._params == {"key": "val"}
        assert ctx._background_tasks is None
        assert ctx._logger is not None
        assert ctx._logger._task_id == "task-1"

    def test_initializes_with_custom_source(self):
        from src.core.task_manager.context import TaskContext

        ctx = TaskContext(task_id="task-1", add_log_fn=MagicMock(), params={}, default_source="custom")
        assert ctx._logger._default_source == "custom"

    def test_initializes_with_background_tasks(self):
        from src.core.task_manager.context import TaskContext

        bt = MagicMock()
        ctx = TaskContext(task_id="task-1", add_log_fn=MagicMock(), params={}, background_tasks=bt)
        assert ctx._background_tasks is bt


class TestTaskContextProperties:
    """Verify property accessors."""

    @pytest.fixture
    def ctx(self):
        from src.core.task_manager.context import TaskContext

        return TaskContext(
            task_id="task-1",
            add_log_fn=MagicMock(),
            params={"key": "val", "num": 42},
            background_tasks="bg",
            default_source="plugin",
        )

    def test_task_id_property(self, ctx):
        assert ctx.task_id == "task-1"

    def test_logger_property(self, ctx):
        from src.core.task_manager.task_logger import TaskLogger

        assert isinstance(ctx.logger, TaskLogger)
        assert ctx.logger._task_id == "task-1"

    def test_params_property(self, ctx):
        assert ctx.params == {"key": "val", "num": 42}

    def test_background_tasks_property(self, ctx):
        assert ctx.background_tasks == "bg"

    def test_background_tasks_none(self):
        from src.core.task_manager.context import TaskContext

        ctx = TaskContext(task_id="t", add_log_fn=MagicMock(), params={})
        assert ctx.background_tasks is None


class TestTaskContextGetParam:
    """Verify get_param method."""

    @pytest.fixture
    def ctx(self):
        from src.core.task_manager.context import TaskContext

        return TaskContext(task_id="task-1", add_log_fn=MagicMock(), params={"key": "val"})

    def test_get_existing_key(self, ctx):
        assert ctx.get_param("key") == "val"

    def test_get_missing_key_returns_none(self, ctx):
        assert ctx.get_param("nonexistent") is None

    def test_get_missing_key_with_default(self, ctx):
        assert ctx.get_param("nonexistent", "default") == "default"

    def test_get_with_none_default(self, ctx):
        assert ctx.get_param("nonexistent", None) is None


class TestTaskContextCreateSubContext:
    """Verify create_sub_context method."""

    def test_creates_sub_context_with_different_source(self):
        from src.core.task_manager.context import TaskContext

        add_log = MagicMock()
        ctx = TaskContext(task_id="task-1", add_log_fn=add_log, params={"key": "val"}, default_source="plugin")
        sub = ctx.create_sub_context("sub-plugin")

        assert isinstance(sub, TaskContext)
        assert sub.task_id == "task-1"
        assert sub.params == {"key": "val"}
        assert sub._logger._default_source == "sub-plugin"
        assert sub._logger._add_log is add_log

    def test_sub_context_preserves_background_tasks(self):
        from src.core.task_manager.context import TaskContext

        bt = MagicMock()
        ctx = TaskContext(task_id="task-1", add_log_fn=MagicMock(), params={}, background_tasks=bt)
        sub = ctx.create_sub_context("sub")
        assert sub.background_tasks is bt

    def test_sub_context_independent_logger(self):
        from src.core.task_manager.context import TaskContext

        ctx = TaskContext(task_id="task-1", add_log_fn=MagicMock(), params={})
        sub = ctx.create_sub_context("sub")

        assert sub.logger is not ctx.logger
        assert sub.logger._default_source == "sub"
        assert ctx.logger._default_source == "plugin"
# #endregion Test.TaskContext
