refactor(files): simplify patch staging and bounded diff rendering

This commit is contained in:
Xubin Ren committed 2026-11-14 22:09:21 +08:00
1 parent f24383771d
commit 150f0dfced
4 files changed
+119 -229

No files matched your search

+18
View File
@@ -141,6 +141,24 @@ def test_apply_patch_edits_add_to_existing_crlf_file_without_final_newline(tmp_p
assert target.read_bytes() == b"alpha\r\nbravo\r\ncharlie\r\n"
@pytest.mark.parametrize("existing", [False, True], ids=["new-file", "existing-crlf"])
def test_apply_patch_replaces_text_from_an_earlier_edit_in_the_batch(tmp_path, existing):
target = tmp_path / "notes.txt"
if existing:
target.write_bytes(b"alpha\r\nbravo")
tool = ApplyPatchTool(workspace=tmp_path)
result = asyncio.run(tool.execute(edits=[
{"path": "notes.txt", "action": "add", "new_text": "charlie"},
{"path": "./notes.txt", "action": "replace", "old_text": "charlie", "new_text": "delta"},
]))
assert result.startswith("Patch applied:\n")
assert result.count("notes.txt") == 1
assert f"{'update' if existing else 'add'} notes.txt" in result
assert target.read_bytes() == (b"alpha\r\nbravo\r\ndelta\r\n" if existing else b"delta\n")
def test_apply_patch_rejects_delete_action(tmp_path):
target = tmp_path / "utils.py"
target.write_text("def unused():\n pass\ndef used():\n return 1\n")
+12
View File
@@ -210,6 +210,18 @@ def test_unified_diff_payload_truncates_large_diffs() -> None:
assert len(body_lines) == 5
def test_unified_diff_clips_long_content_without_changing_header_counts() -> None:
payload = build_unified_diff_payload(
"old line\nkeep this\n", "new line\nkeep this\n", max_line_chars=3,
)
assert payload == {
"format": "unified",
"context": 3,
"truncated": True,
"text": "--- before\n+++ after\n@@ -1,2 +1,2 @@\n+new\n-old\n kee",
}
@pytest.mark.parametrize(("max_lines", "expected_tail", "truncated"), [
(2, "", True),
(3, "\n@@ -3,0 +3 @@\n+new last", True),