"""Tests for Sprint 06 tool-surface expansion.""" from __future__ import annotations import json import subprocess from pathlib import Path import pytest from loader.tools.base import create_default_registry from loader.tools.file_tools import PatchTool from loader.tools.git_tools import GitTool from loader.tools.workflow_tools import AskUserQuestionTool @pytest.mark.asyncio async def test_patch_tool_applies_structured_hunks(temp_dir: Path) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\n") tool = PatchTool(workspace_root=temp_dir) result = await tool.execute( file_path=str(target), hunks=[ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 1, "lines": ["-beta", "+beta updated"], } ], ) assert result.is_error is False assert target.read_text() == "alpha\nbeta updated\ngamma\n" assert result.metadata["structured_patch"] @pytest.mark.asyncio async def test_patch_tool_accepts_json_encoded_structured_hunks( temp_dir: Path, ) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\n") tool = PatchTool(workspace_root=temp_dir) result = await tool.execute( file_path=str(target), hunks=json.dumps( [ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 1, "lines": ["-beta", "+beta from json string"], } ] ), ) assert result.is_error is False assert target.read_text() == "alpha\nbeta from json string\ngamma\n" @pytest.mark.asyncio async def test_patch_tool_accepts_json_hunks_missing_outer_close( temp_dir: Path, ) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\n") tool = PatchTool(workspace_root=temp_dir) hunk_payload = json.dumps( [ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 1, "lines": ["-beta", "+beta from repaired json string"], } ] )[:-1] result = await tool.execute( file_path=str(target), hunks=hunk_payload, ) assert result.is_error is False assert target.read_text() == "alpha\nbeta from repaired json string\ngamma\n" @pytest.mark.asyncio async def test_patch_tool_accepts_python_literal_structured_hunks( temp_dir: Path, ) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\n") tool = PatchTool(workspace_root=temp_dir) result = await tool.execute( file_path=str(target), hunks=repr( [ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 1, "lines": ["-beta", "+beta from literal string"], } ] ), ) assert result.is_error is False assert target.read_text() == "alpha\nbeta from literal string\ngamma\n" @pytest.mark.asyncio async def test_patch_tool_accepts_python_literal_hunks_missing_outer_close( temp_dir: Path, ) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\n") tool = PatchTool(workspace_root=temp_dir) hunk_payload = repr( [ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 1, "lines": ["-beta", "+beta from repaired literal string"], } ] )[:-1] result = await tool.execute( file_path=str(target), hunks=hunk_payload, ) assert result.is_error is False assert target.read_text() == "alpha\nbeta from repaired literal string\ngamma\n" @pytest.mark.asyncio async def test_patch_tool_rejects_context_mismatch(temp_dir: Path) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\n") tool = PatchTool(workspace_root=temp_dir) result = await tool.execute( file_path=str(target), hunks=[ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 1, "lines": ["-wrong line", "+beta updated"], } ], ) assert result.is_error is True assert "context mismatch" in result.output @pytest.mark.asyncio async def test_patch_tool_accepts_replacement_block_hunks(temp_dir: Path) -> None: target = temp_dir / "sample.txt" target.write_text("alpha\nbeta\ngamma\ndelta\n") tool = PatchTool(workspace_root=temp_dir) result = await tool.execute( file_path=str(target), hunks=[ { "old_start": 2, "old_end": 3, "new_lines": [ "beta updated", "gamma updated", "inserted line", ], } ], ) assert result.is_error is False assert target.read_text() == "alpha\nbeta updated\ngamma updated\ninserted line\ndelta\n" assert result.metadata["structured_patch"] @pytest.mark.asyncio async def test_patch_tool_accepts_raw_lines_replacement_hunks( temp_dir: Path, ) -> None: target = temp_dir / "sample.html" target.write_text("
Short.
\n\n") tool = PatchTool(workspace_root=temp_dir) result = await tool.execute( file_path=str(target), hunks=[ { "old_start": 2, "old_lines": 1, "new_start": 2, "new_lines": 4, "lines": [ "Expanded body copy.
", "", "Expanded body copy.
\n" "\n" "