# File: tests/unit/presentation/test_cli_check_autofix.py
# Purpose: Validate CLI auto-fix workflow delegates to MCP improve routine.
# Context: Ensures CLI behaviour stays aligned with MCP runtime contracts.
"""CLI adapter: noveler check --auto-fix should use improve_quality_until."""

from __future__ import annotations

from typing import Any

import pytest

pytest.importorskip("mcp.server", reason="MCP runtime required for CLI MCP tests.")

from noveler.presentation.cli import cli_adapter


class DummyConsole:
    def print(self, *args: Any, **kwargs: Any) -> None:
        # no-op
        pass


async def _async_return(data: dict[str, Any]) -> dict[str, Any]:
    return data


@pytest.mark.skip(reason="execute_improve_quality_until not yet implemented in MCP server")
def test_cli_check_uses_improve_quality_until(monkeypatch):
    # Patch console
    monkeypatch.setattr(cli_adapter, "get_console", lambda: DummyConsole())

    # Patch mcp main functions
    from mcp_servers.noveler import main as mcp_mod

    async def fake_check(args: dict[str, Any]) -> dict[str, Any]:
        return {"success": True, "score": 60.0}

    async def fake_improve(args: dict[str, Any]) -> dict[str, Any]:
        # Ensure target/max_iterations are passed through
        ap = args.get("additional_params") or {}
        assert ap.get("target_score") == 80
        assert ap.get("max_iterations") == 3
        return {"success": True, "score": 82.0, "metadata": {"target_score": 80}}

    monkeypatch.setattr(mcp_mod, "execute_run_quality_checks", fake_check)
    # Skip patching execute_improve_quality_until - to be implemented
    # monkeypatch.setattr(mcp_mod, "execute_improve_quality_until", fake_improve)

    # Execute CLI would be tested once the function is implemented
    # rc = cli_adapter.run(["check", "1", "--auto-fix"])
    # assert rc == 0  # score >= 80 after improvements
