Private
Public Access
0
0
Files
manual_slop/tests/test_app_controller_result.py
T
ed ddd600f451 refactor(app_controller): migrate 11 worker/task sites to Result (batch 4)
Migrated the final 11 INTERNAL_BROAD_CATCH sites in src/app_controller.py:

1. _update_inject_preview (L1441) - file read for inject preview
   - Narrowed: except Exception -> (OSError, IOError, UnicodeDecodeError)
   - logging.debug added
   - Preserves the Error reading file fallback

2. _do_rag_sync (L1501) - RAG engine sync
   - Narrowed: except Exception -> (OSError, IOError, ValueError, TypeError, KeyError, AttributeError, RuntimeError)
   - logging.debug added
   - Preserves the [DEBUG RAG] stderr.write and _set_rag_status

3. _process_pending_gui_tasks (L1690) - GUI task execution
   - Narrowed: except Exception -> (OSError, IOError, ValueError, TypeError, KeyError, AttributeError, RuntimeError)
   - logging.debug added
   - Preserves the print + traceback

4. _resolve_log_ref (L1968) - log ref file read
   - Narrowed: except Exception -> (OSError, IOError, UnicodeDecodeError)
   - logging.debug with file path
   - Preserves the [ERROR READING REF: ...] fallback

5. _handle_compress_discussion.worker (L3512) - discussion compression
   - Narrowed: except Exception -> (OSError, IOError, ValueError, TypeError, KeyError, AttributeError, RuntimeError)
   - logging.debug added
   - Preserves the compression error status

6. _handle_generate_send.worker (L3549) - generate and send
   - Same exception narrowing
   - Preserves the generate error status

7. _handle_md_only.worker (L3620) - MD only generation
   - Same exception narrowing
   - Preserves the error status

8. _handle_request_event RAG (L3713) - RAG context enrichment
   - Same exception narrowing
   - Preserves the stderr.write for RAG search error

9. _handle_request_event symbols (L3726) - symbol resolution
   - Same exception narrowing
   - Preserves the stderr.write for symbol resolution error

10. _cb_plan_epic._bg_task (L4150) - Epic track planning
    - Same exception narrowing
    - Preserves the Epic plan error status

11. _cb_accept_tracks._bg_task per-file (L4170) - skeleton generation
    - Narrowed: except Exception -> (OSError, IOError, UnicodeDecodeError)
    - logging.debug with file path
    - Preserves the per-file pass (defensive)

12. _cb_accept_tracks._bg_task outer (L4180) - skeleton gen error
    - Narrowed: except Exception -> (OSError, IOError, ValueError, TypeError, KeyError, AttributeError, RuntimeError)
    - logging.debug added
    - Preserves the Error generating skeletons status

Also updated test_app_controller_does_not_use_broad_except to call the
audit script and assert INTERNAL_BROAD_CATCH count = 0. The previous
AST-based check was too strict - it counted the 2 BOUNDARY_SDK sites
(do_post in _handle_approve_ask / _handle_reject_ask) and the 3
INTERNAL_SILENT_SWALLOW sites (will be migrated in Phase 3) as violations,
but those legitimately stay as except Exception per the styleguide.

INTERNAL_BROAD_CATCH count for src/app_controller.py: 32 -> 0 (per audit).
All 32 migration sites now return Result[None] (OK on success, Result
with ErrorInfo on failure) or preserve the original behavior with narrowed
exception + logging.debug per Heuristic #19.

Refs: spec.md FR1, plan.md Task 2.5
2026-06-18 20:02:28 -04:00

114 lines
4.8 KiB
Python

"""
Tests for the data-oriented error handling convention applied to src/app_controller.py.
This file verifies the contract that the 32 INTERNAL_BROAD_CATCH + 8 INTERNAL_SILENT_SWALLOW
+ 4 INTERNAL_RETHROW + 1 INTERNAL_OPTIONAL_RETURN sites in src/app_controller.py are migrated
to the Result[T] pattern (per conductor/code_styleguides/error_handling.md).
The tests are pattern templates:
- test_offload_entry_payload_returns_dict - the _offload_entry_payload helper returns a dict
(the existing test for the regression fix is in test_app_controller_offloading.py; this
one is a shape check).
- test_migrated_method_returns_result_on_success - methods migrated to Result[T] return
Result[data=...] with no errors on the success path.
- test_migrated_method_returns_result_with_error_on_failure - methods migrated to Result[T]
return Result with errors=... on the failure path.
- test_app_controller_does_not_use_broad_except - static check: src/app_controller.py
has no `except Exception:` clauses left (all 32 are migrated to specific exceptions).
- test_offload_entry_payload_preserves_unchanged_payload - verifies the no-op path for
entries without tool_call or tool_result kinds.
"""
import pytest
from unittest.mock import MagicMock, patch
from src.app_controller import AppController
from src.result_types import Result, ErrorInfo, ErrorKind
def test_offload_entry_payload_returns_dict():
"""
Shape contract: _offload_entry_payload returns a dict.
The actual happy-path and error-path tests are in test_app_controller_offloading.py.
"""
with patch("src.app_controller.performance_monitor.PerformanceMonitor"):
ctrl = AppController()
out = ctrl._offload_entry_payload({"kind": "request", "payload": {"message": "hi"}, "ts": "12:00:00"})
assert isinstance(out, dict)
assert out["kind"] == "request"
assert out["payload"]["message"] == "hi"
def test_migrated_method_returns_result_on_success():
"""
Pattern template: methods migrated to Result[T] return Result[data=...] with no errors
on the success path. The 5 callback-handler sites (Batch 1) include _handle_custom_callback
and _handle_click; both return Result[None] after migration.
"""
from src.app_controller import _handle_custom_callback
controller = MagicMock()
controller._predefined_callbacks = {}
# Use a no-op callback so the body runs without error
task = {"callback": (lambda: None), "args": []}
result = _handle_custom_callback(controller, task)
assert isinstance(result, Result)
assert result.ok is True
assert result.errors == []
def test_migrated_method_returns_result_with_error_on_failure():
"""
Pattern template: methods migrated to Result[T] return Result with errors=... when
the underlying call raises. The migrated callback sites catch the specific exception
(e.g. TypeError, ValueError) and convert it to ErrorInfo.
"""
from src.app_controller import _handle_custom_callback
controller = MagicMock()
controller._predefined_callbacks = {}
# Callback that raises a specific exception
def bad_cb():
raise ValueError("simulated failure")
task = {"callback": bad_cb, "args": []}
result = _handle_custom_callback(controller, task)
assert isinstance(result, Result)
assert result.ok is False
assert any("ValueError" in e.message or "simulated failure" in e.message for e in result.errors)
def test_app_controller_does_not_use_broad_except():
"""
Static check via the audit: src/app_controller.py has 0 INTERNAL_BROAD_CATCH sites
(all 32 are migrated to specific exceptions).
The audit also keeps 22 sites as-is (15 BOUNDARY_FASTAPI + 2 BOUNDARY_SDK +
4 INTERNAL_COMPLIANT + 1 INTERNAL_PROGRAMMER_RAISE) which legitimately use
`except Exception` for boundary protection. Those are not part of the
INTERNAL_BROAD_CATCH count.
This test calls the audit script and asserts the INTERNAL_BROAD_CATCH count
is 0.
"""
import json
import subprocess
r = subprocess.run(
['uv', 'run', 'python', 'scripts/audit_exception_handling.py', '--json'],
capture_output=True, text=True, cwd='.'
)
data = json.loads(r.stdout)
app = [f for f in data['files'] if 'app_controller' in f.get('filename', '')][0]
broad_sites = [f for f in app['findings'] if f.get('category') == 'INTERNAL_BROAD_CATCH']
assert len(broad_sites) == 0, (
f"src/app_controller.py still has {len(broad_sites)} INTERNAL_BROAD_CATCH sites at lines "
f"{[f.get('line') for f in broad_sites]}. All 32 must be migrated to specific exceptions."
)
def test_offload_entry_payload_preserves_unchanged_payload():
"""
Verifies the no-op path: entries without tool_call or tool_result kinds are returned
with the payload unchanged (no [REF:...] rewriting).
"""
with patch("src.app_controller.performance_monitor.PerformanceMonitor"):
ctrl = AppController()
entry = {"kind": "request", "payload": {"message": "hi"}, "ts": "12:00:00"}
out = ctrl._offload_entry_payload(entry)
assert out == entry