Private
Public Access
0
0
Files
manual_slop/tests/test_mma_agent_focus_phase1.py
T
ed dc397db7ed refactor(src): eliminate 11 T | None legacy wrappers in favor of _result API
TIER-3 READ AGENTS.md + conductor/workflow.md + conductor/code_styleguides/error_handling.md + the 4 source files + 3 test files before this commit.

The code_path_audit_phase_2_20260624 track (Tier 2) shipped 11 audit
fixes (4 NG1 + 7 NG2) but used a heuristic bypass for 4 of the NG2
wrappers: legacy T | None functions that exist only to maintain test
patcher compatibility. Per the review at
docs/reports/REVIEW_TIER2_code_path_audit_phase_2_20260624.md Finding 8,
this track eliminates the legacy wrappers properly.

11 wrappers eliminated (8 main + 3 _legacy_compat inner):
- src/ai_client.py: get_current_tier (1 src + 1 test consumer)
- src/ai_client.py: _gemini_tool_declaration + _legacy_compat (2 test consumers)
- src/ai_client.py: run_tier4_patch_callback + _legacy_compat (was 0 direct callers
  but had 2 callback references in app_controller/multi_agent_conductor;
  callback contract migrated to Callable[[str, str], Result[str]] instead of
  preserving an Optional[str] adapter)
- src/mcp_client.py: _get_symbol_node + _legacy_compat (8 in-file consumers)
- src/mcp_client.py: find_in_scope (nested inside _get_symbol_node_result;
  private impl detail, audit doesn't catch T | None, left as-is)
- src/external_editor.py: launch_diff (1 src + 3 test + 1 live_gui test consumer)
- src/external_editor.py: launch_editor (no consumers; deleted)
- src/session_logger.py: log_tool_output (2 src + 3 test consumers)
- src/project_manager.py: parse_ts (no consumers; deleted)

For each consumer: replace legacy_fn(args) with legacy_fn_result(args).data.
For T | None checks: replace if x is None: with if not result.ok: or
if not result.ok or not isinstance(result.data, ...) (depending on pattern).

For run_tier4_patch_callback specifically: the wrapper was a callback adapter
(not a backward-compat shim) and had 2 callback references as consumers.
Rather than keep the adapter (which would re-introduce the Optional[str]
return that the strict audit catches), the patch_callback contract was migrated
from Callable[[str, str], Optional[str]] to Callable[[str, str], Result[str]]
in shell_runner.py + app_controller.py + 9 _send_<vendor>_result signatures
in ai_client.py. This propagates the Result[str] through the callback and
lets shell_runner unwrap with if r.ok and r.data instead of if patch_text.

Verification:
- audit_optional_in_3_files --strict: 0 return-type Optional[T] (down from 1)
- audit_exception_handling --strict: 0 violations (unchanged)
- audit_legacy_wrappers: 0 legacy wrappers (unchanged)
- 15 affected test files: 168 tests pass
- 8 mcp_client/structural/baseline test files: 55 tests pass
- 3 session/gui test files: 7 tests pass
- 0 return-type Optional[T] in src/ai_client.py (was 1: run_tier4_patch_callback)
2026-06-25 11:18:03 -04:00

85 lines
3.0 KiB
Python

"""
Tests for mma_agent_focus_ux_20260302 — Phase 1: Tier Tagging at Emission.
These tests affirm that ai_client and session_logger correctly preserve 'current_tier'
state when logging comms and tools.
"""
from src import ai_client
def reset_tier():
ai_client.set_current_tier(None)
yield
ai_client.set_current_tier(None)
def test_get_current_tier_exists() -> None:
"""ai_client must expose a get_current_tier_result function."""
assert hasattr(ai_client, "get_current_tier_result")
assert callable(ai_client.get_current_tier_result)
def test_append_comms_has_source_tier_key() -> None:
"""Dict entries in comms log must have a 'source_tier' key."""
ai_client.reset_session()
ai_client.set_provider("gemini", "gemini-2.5-flash-lite")
ai_client._append_comms("OUT", "request", {"msg": "hello"})
log = ai_client.get_comms_log()
assert len(log) > 0
assert "source_tier" in log[-1]
def test_append_comms_source_tier_none_when_unset() -> None:
"""When current_tier is None, source_tier in log must be None."""
ai_client.reset_session()
ai_client.set_current_tier(None)
ai_client._append_comms("OUT", "request", {"msg": "hello"})
log = ai_client.get_comms_log()
assert log[-1]["source_tier"] is None
def test_append_comms_source_tier_set_when_current_tier_set() -> None:
"""When current_tier is 'Tier 1', source_tier in log must be 'Tier 1'."""
ai_client.reset_session()
ai_client.set_current_tier("Tier 1")
ai_client._append_comms("OUT", "request", {"msg": "hello"})
log = ai_client.get_comms_log()
assert log[-1]["source_tier"] == "Tier 1"
ai_client.set_current_tier(None)
def test_append_comms_source_tier_tier2() -> None:
"""When current_tier is 'Tier 2', source_tier in log must be 'Tier 2'."""
ai_client.reset_session()
ai_client.set_current_tier("Tier 2")
ai_client._append_comms("OUT", "request", {"msg": "hello"})
log = ai_client.get_comms_log()
assert log[-1]["source_tier"] == "Tier 2"
ai_client.set_current_tier(None)
def test_append_tool_log_stores_dict(app_instance) -> None:
"""App._append_tool_log must store a dict in self._tool_log."""
app = app_instance
app.controller._append_tool_log("pwd", "/projects")
assert len(app.controller._tool_log) > 0
entry = app.controller._tool_log[-1]
assert isinstance(entry, dict)
def test_append_tool_log_dict_has_source_tier(app_instance) -> None:
"""Dict entry must have 'source_tier' key."""
app = app_instance
app.controller._append_tool_log("pwd", "/projects")
entry = app.controller._tool_log[-1]
assert "source_tier" in entry
def test_append_tool_log_dict_keys(app_instance) -> None:
"""Dict entry must have script, result, ts, source_tier keys."""
app = app_instance
app.controller._append_tool_log("pwd", "/projects")
entry = app.controller._tool_log[-1]
for key in ("script", "result", "ts", "source_tier"):
assert key in entry, f"key '{key}' missing from tool log entry: {entry}"
assert entry["script"] == "pwd"
assert entry["result"] == "/projects"
assert entry["source_tier"] is None