# retoor import unittest.mock from devplacepy.services.correction import correct_text def test_correct_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 ("correction result", {"calls": 1}) with unittest.mock.patch( "devplacepy.services.correction.gateway_complete", fake_gateway ): result, _ = correct_text( "test-key", "Fix spelling", "Some text\n```python\nx = 1\n```\nMore text" ) assert result == "correction result" def test_correct_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.correction.gateway_complete", fake_gateway ): result, _ = correct_text("test-key", "Fix spelling", input_text) assert "```python" in result assert "x = 1" in result assert "corrected text" in result def test_correct_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.correction.gateway_complete", fake_gateway ): result, _ = correct_text("test-key", "Fix spelling", input_text) assert "```python" in result assert "x = 1" in result assert "```js" in result assert "y = 2" in result def test_correct_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.correction.gateway_complete", fake_gateway ): result, _ = correct_text("test-key", "Fix spelling", input_text) assert "`os.path.join`" in result def test_correct_text_passes_plain_text_untouched(): captured = {} def fake_gateway(api_key, system, text, timeout, max_growth_factor): captured["text"] = text return ("corrected plain text", {"calls": 1}) with unittest.mock.patch( "devplacepy.services.correction.gateway_complete", fake_gateway ): result, _ = correct_text( "test-key", "Fix spelling", "Just some plain text." ) assert captured["text"] == "Just some plain text." assert result == "corrected plain text" def test_correct_text_sanitized_text_has_no_code_blocks(): captured = {} def fake_gateway(api_key, system, text, timeout, max_growth_factor): captured["text"] = text return ("corrected", {"calls": 1}) with unittest.mock.patch( "devplacepy.services.correction.gateway_complete", fake_gateway ): correct_text( "test-key", "Fix spelling", "Before\n```python\ncode\n```\nAfter\n`inline`", ) assert "```" not in captured["text"] assert "`inline`" not in captured["text"]