"""Tests for the V4A diff helper.""" from __future__ import annotations import pytest from agents import apply_diff def test_apply_diff_with_floating_hunk_adds_lines() -> None: diff = "\n".join(["@@", "+hello", "+world"]) # no trailing newline assert apply_diff("", diff) == "hello\nworld\n" def test_apply_diff_with_empty_input_and_crlf_diff_preserves_crlf() -> None: diff = "\r\n".join(["@@", "+hello", "+world"]) assert apply_diff("", diff) == "hello\r\nworld\r\n" def test_apply_diff_create_mode_requires_plus_prefix() -> None: diff = "plain line" with pytest.raises(ValueError): apply_diff("", diff, mode="create") def test_apply_diff_create_mode_preserves_trailing_newline() -> None: diff = "\n".join(["+hello", "+world", "+"]) assert apply_diff("", diff, mode="create") == "hello\nworld\n" def test_apply_diff_applies_contextual_replacement() -> None: input_text = "line1\nline2\nline3\n" diff = "\n".join(["@@ line1", "-line2", "+updated", " line3"]) assert apply_diff(input_text, diff) == "line1\nupdated\nline3\n" def test_apply_diff_raises_on_context_mismatch() -> None: input_text = "one\ntwo\n" diff = "\n".join(["@@ -1,2 +1,2 @@", " x", "-two", "+2"]) with pytest.raises(ValueError): apply_diff(input_text, diff) def test_apply_diff_with_crlf_input_and_lf_diff_preserves_crlf() -> None: input_text = "line1\r\nline2\r\nline3\r\n" diff = "\n".join(["@@ line1", "-line2", "+updated", " line3"]) assert apply_diff(input_text, diff) == "line1\r\nupdated\r\nline3\r\n" def test_apply_diff_with_lf_input_and_crlf_diff_preserves_lf() -> None: input_text = "line1\nline2\nline3\n" diff = "\r\n".join(["@@ line1", "-line2", "+updated", " line3"]) assert apply_diff(input_text, diff) == "line1\nupdated\nline3\n" def test_apply_diff_with_crlf_input_and_crlf_diff_preserves_crlf() -> None: input_text = "line1\r\nline2\r\nline3\r\n" diff = "\r\n".join(["@@ line1", "-line2", "+updated", " line3"]) assert apply_diff(input_text, diff) == "line1\r\nupdated\r\nline3\r\n" def test_apply_diff_create_mode_preserves_crlf_newlines() -> None: diff = "\r\n".join(["+hello", "+world", "+"]) assert apply_diff("", diff, mode="create") == "hello\r\nworld\r\n"