# retoor <retoor@molodetz.nl>
import unittest.mock
from devplacepy.services.ai_modifier import modify_text
def test_modify_text_extracts_and_restores_code_blocks():
captured = {}
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
captured["system"] = system
captured["text"] = text
return ("modification result", {"calls": 1})
with unittest.mock.patch(
"devplacepy.services.ai_modifier.gateway_complete", fake_gateway
):
result, _ = modify_text(
"test-key",
"Make it more formal",
"Some text\n```python\nx = 1\n```\nMore text",
)
assert result == "modification result"
def test_modify_text_preserves_fenced_code_blocks_from_ai_corruption():
input_text = "Some text\n```python\nx = 1\n```\nMore text"
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
return ("Some text\n{%CODE_BLOCK_0%}\nMore corrected text", {"calls": 1})
with unittest.mock.patch(
"devplacepy.services.ai_modifier.gateway_complete", fake_gateway
):
result, _ = modify_text(
"test-key", "Make it more formal", input_text
)
assert "```python" in result
assert "x = 1" in result
assert "corrected text" in result
def test_modify_text_preserves_multiple_fenced_code_blocks():
input_text = "A\n```python\nx = 1\n```\nB\n```js\ny = 2\n```\nC"
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
return ("A\n{%CODE_BLOCK_0%}\nX\n{%CODE_BLOCK_1%}\nZ", {"calls": 1})
with unittest.mock.patch(
"devplacepy.services.ai_modifier.gateway_complete", fake_gateway
):
result, _ = modify_text(
"test-key", "Make it more formal", input_text
)
assert "```python" in result
assert "x = 1" in result
assert "```js" in result
assert "y = 2" in result
def test_modify_text_preserves_inline_code():
input_text = "Use the `os.path.join` function for paths."
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
return ("Use {%CODE_BLOCK_0%} always.", {"calls": 1})
with unittest.mock.patch(
"devplacepy.services.ai_modifier.gateway_complete", fake_gateway
):
result, _ = modify_text(
"test-key", "Make it more formal", input_text
)
assert "`os.path.join`" in result
def test_modify_text_passes_plain_text_untouched():
captured = {}
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
captured["text"] = text
return ("modified plain text", {"calls": 1})
with unittest.mock.patch(
"devplacepy.services.ai_modifier.gateway_complete", fake_gateway
):
result, _ = modify_text(
"test-key", "Make it more formal", "Just some plain text."
)
assert captured["text"] == "Just some plain text."
assert result == "modified plain text"
def test_modify_text_sanitized_text_has_no_code_blocks():
captured = {}
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
captured["text"] = text
return ("modified", {"calls": 1})
with unittest.mock.patch(
"devplacepy.services.ai_modifier.gateway_complete", fake_gateway
):
modify_text(
"test-key",
"Make it more formal",
"Before\n```python\ncode\n```\nAfter\n`inline`",
)
assert "```" not in captured["text"]
assert "`inline`" not in captured["text"]