TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 5: refactor(gui_2): migrate L1428 request_patch_from_tier4 to Result[T] (Phase 5)

Extract request_patch_from_tier4_result helper from the
ai_client.run_tier4_patch_generation try/except in App.request_patch_from_tier4.
Legacy wrapper drains errors to app._last_request_errors per FR-BC-4
event-handler pattern.

[pre-audit] L1428 INTERNAL_BROAD_CATCH
[post-audit] V count: 10 -> 9 (L1428 removed)
This commit is contained in:
ed
2026-06-19 23:50:33 -04:00
parent 77a48b18bf
commit b20ea145b3
2 changed files with 89 additions and 14 deletions
+43
View File
@@ -975,3 +975,46 @@ def test_phase_5_l1393_open_patch_in_external_editor_result_failure():
err = result.errors[0]
assert err.source == "gui_2._open_patch_in_external_editor_result"
assert "temp file creation blew up" in err.message
def test_phase_5_l1428_request_patch_from_tier4_result_success():
"""
L1428 request_patch_from_tier4_result returns Result.ok=True on success.
The helper wraps the ai_client.run_tier4_patch_generation try/except in
App.request_patch_from_tier4. On success (patch_text has --- and +++),
returns Result(data=True) and sets app._pending_patch_text/files and
app._show_patch_modal=True.
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
patch_text = "--- a/foo.py\n+++ b/foo.py\n@@ -1 +1 @@\n-old\n+new\n"
mock_diff_files = [MagicMock(old_path="/proj/foo/foo.py", new_path="/proj/foo/foo.py")]
with patch.object(gui_2.ai_client, "run_tier4_patch_generation", return_value=patch_text), \
patch("src.diff_viewer.parse_diff", return_value=mock_diff_files):
result = gui_2.request_patch_from_tier4_result(app, "boom", "/proj/foo/foo.py")
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
assert result.data is True
assert app._pending_patch_text is patch_text
assert app._pending_patch_files == ["/proj/foo/foo.py"]
assert app._show_patch_modal is True
def test_phase_5_l1428_request_patch_from_tier4_result_failure():
"""
L1428 request_patch_from_tier4_result returns Result.ok=False with ErrorInfo on failure.
When ai_client.run_tier4_patch_generation raises, the helper converts the
exception to ErrorInfo and returns Result(data=False, errors=[ErrorInfo]).
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
with patch.object(gui_2.ai_client, "run_tier4_patch_generation", side_effect=RuntimeError("tier4 backend down")):
result = gui_2.request_patch_from_tier4_result(app, "boom", "/proj/foo/foo.py")
assert not result.ok, f"Expected ok=False on failure, got data: {result.data}"
assert result.errors, "Expected at least one error on failure"
err = result.errors[0]
assert err.source == "gui_2.request_patch_from_tier4_result"
assert "tier4 backend down" in err.message