"""Tests for the ``lsp_diagnostics`` field on WriteResult / PatchResult.

The field exists so the agent can read syntax errors (``lint``) and
semantic errors (``lsp_diagnostics``) as separate signals rather than
having LSP output prepended to the lint string.
"""
from __future__ import annotations

from unittest.mock import MagicMock, patch


from tools.environments.local import LocalEnvironment
from tools.file_operations import (
    PatchResult,
    ShellFileOperations,
    WriteResult,
)


# ---------------------------------------------------------------------------
# Dataclass shape
# ---------------------------------------------------------------------------










def test_patchresult_to_dict_omits_field_when_none():
    r = PatchResult(success=True)
    assert "lsp_diagnostics" not in r.to_dict()




# ---------------------------------------------------------------------------
# Channel separation: lint and lsp_diagnostics stay independent
# ---------------------------------------------------------------------------


def test_lint_and_lsp_diagnostics_are_separate_channels():
    """A WriteResult can carry BOTH a syntax-error lint AND an LSP
    diagnostic block.  They belong in separate fields."""
    r = WriteResult(
        bytes_written=42,
        lint={"status": "error", "output": "SyntaxError: ..."},
        lsp_diagnostics="<diagnostics>ERROR [1:5] type mismatch</diagnostics>",
    )
    d = r.to_dict()
    assert "lint" in d
    assert "lsp_diagnostics" in d
    assert d["lint"]["output"] == "SyntaxError: ..."
    assert "type mismatch" in d["lsp_diagnostics"]


# ---------------------------------------------------------------------------
# write_file populates the field via _maybe_lsp_diagnostics
# ---------------------------------------------------------------------------






def test_write_file_skips_lsp_when_syntax_failed(tmp_path):
    """If the syntax check finds errors, the LSP layer should not be
    consulted (a file that won't parse won't yield meaningful semantic
    diagnostics)."""
    fops = ShellFileOperations(LocalEnvironment(cwd=str(tmp_path)))
    target = tmp_path / "broken.py"

    with patch.object(fops, "_maybe_lsp_diagnostics") as mock_lsp:
        res = fops.write_file(str(target), "def x(:\n")  # syntax error
    assert mock_lsp.call_count == 0
    assert res.lsp_diagnostics is None
    assert res.lint["status"] == "error"


def test_maybe_lsp_diagnostics_swallows_enabled_for_failure(tmp_path):
    """``_maybe_lsp_diagnostics`` gates through the guarded ``_lsp_will_handle``
    helper, so a workspace-resolution failure (e.g. the process cwd was removed
    under a running worker) degrades to "no LSP for this write" instead of
    surfacing as an error from a write that already landed on disk."""
    fops = ShellFileOperations(LocalEnvironment(cwd=str(tmp_path)))

    with patch.object(fops, "_lsp_service") as mock_service:
        mock_service.return_value.enabled_for = MagicMock(
            side_effect=FileNotFoundError(2, "No such file or directory")
        )
        result = fops._maybe_lsp_diagnostics(str(tmp_path / "x.py"))

    assert result == ""


# ---------------------------------------------------------------------------
# patch_replace propagates the field from the inner write_file
# ---------------------------------------------------------------------------


def test_patch_replace_propagates_lsp_diagnostics(tmp_path):
    """patch_replace's internal write_file populates lsp_diagnostics —
    the outer PatchResult must carry it forward."""
    fops = ShellFileOperations(LocalEnvironment(cwd=str(tmp_path)))
    target = tmp_path / "x.py"
    target.write_text("x = 1\n")

    block = "<diagnostics>ERROR [1:5] semantic issue</diagnostics>"

    with patch.object(fops, "_maybe_lsp_diagnostics", return_value=block):
        res = fops.patch_replace(str(target), "x = 1", "x = 2")

    assert res.success is True
    assert res.lsp_diagnostics == block
