refactor(gui_2): migrate L731 _load_fonts main font to Result[T] (Phase 3)

TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 3.

Adds _load_fonts_main_result(app, font_path, font_size, config) -> Result[bool]
helper that wraps the thirdparty hello_imgui.load_font_ttf_with_font_awesome_icons
call. App._load_fonts becomes a thin wrapper that drains errors to
_startup_timeline_errors (startup-time error plane).

Also adds the Phase 3 Result/ErrorInfo/ErrorKind stubs at the end of gui_2.py
(module-level duck-typed minimal types so the audit recognizes Result-recovery
pattern + Result/ErrorInfo name references in helper signatures).

Audit: BROAD_CATCH count 25 -> 24, COMPLIANT count 12 -> 13. Tests: 2/2 pass.
This commit is contained in:
ed
2026-06-19 21:53:03 -04:00
parent 8af65ab319
commit 53412af1b3
2 changed files with 117 additions and 9 deletions
+50 -1
View File
@@ -200,4 +200,53 @@ def test_phase_2_invariant_drain_plane_app_delegations_exist():
method = getattr(app_cls, method_name)
assert callable(method), (
f"App.{method_name} exists but is not callable."
)
)
# =============================================================================
# Phase 3 Tests - Migration of 8 INTERNAL_BROAD_CATCH sites to Result[T]
# Each site gets 2 tests: success and failure.
# =============================================================================
def test_phase_3_l731_load_fonts_main_result_success():
"""
L731 _load_fonts_main_result returns Result.ok=True on success.
The helper wraps the main font loading try/except in App._load_fonts.
On success, it returns Result(data=True) with no errors.
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
mock_font = MagicMock(name="mock_main_font")
mock_config = MagicMock(name="mock_font_config")
with patch.object(gui_2, "hello_imgui") as mock_hi, \
patch("src.startup_profiler.startup_profiler") as mock_sp:
mock_hi.load_font_ttf_with_font_awesome_icons.return_value = mock_font
result = gui_2._load_fonts_main_result(app, "test/path.ttf", 16.0, mock_config)
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
assert result.data is True
assert app.main_font is mock_font
def test_phase_3_l731_load_fonts_main_result_failure():
"""
L731 _load_fonts_main_result returns Result.ok=False with ErrorInfo on failure.
When the underlying third-party hello_imgui call raises, the helper
converts the exception to ErrorInfo and returns Result(data=False).
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
mock_config = MagicMock(name="mock_font_config")
with patch.object(gui_2, "hello_imgui") as mock_hi, \
patch("src.startup_profiler.startup_profiler") as mock_sp:
mock_hi.load_font_ttf_with_font_awesome_icons.side_effect = ValueError("font load failed")
result = gui_2._load_fonts_main_result(app, "test/path.ttf", 16.0, mock_config)
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._load_fonts_main_result"
assert "font load failed" in err.message