Private
Public Access
0
0
Files
manual_slop/tests/test_auto_whitelist.py
T
ed 30c8b26381 fix(ai_client): migrate gemini_cli NormalizedResponse callers to Phase 2 dataclass API
Phase 2 deferred t2_6: update src/ai_client.py _send_grok + _send_minimax +
_send_llama + _send_gemini_cli (4 functions) to use the new
dataclass API after NormalizedResponse was refactored to
(text, tool_calls: tuple[ToolCall, ...], usage: UsageStats, raw_response).

These 4 callers were left with the old keyword args
(usage_input_tokens, usage_output_tokens, ...) which broke at
runtime: ai_client.send() raised
TypeError: NormalizedResponse.__init__() got an unexpected keyword
argument 'usage_input_tokens'.

FIXES:
- src/ai_client.py L2054: gemini_cli 'adapter unavailable' branch
- src/ai_client.py L2088: gemini_cli normal response branch
- Added: from src.openai_schemas import UsageStats (module level)
- Added backward-compat in src/openai_compatible.py:
  messages_dicts = [m.to_dict() if hasattr(m, 'to_dict') else m for m in request.messages]
  (accepts both ChatMessage dataclass and dict for backward compat
  with existing tests that pass raw dicts)

TEST FIXES:
- tests/test_ai_client_tool_loop.py: _make_normalized_response helper
  uses UsageStats instead of usage_*_tokens kwargs
- tests/test_ai_client_tool_loop_builder.py: same
- tests/test_ai_client_tool_loop_send_func.py: same
- tests/test_openai_compatible.py: NormalizedResponse(text=..., usage=UsageStats(...))
  + tool_calls[0].function.name (attribute access) instead of ['function']['name']
- tests/test_auto_whitelist.py: use update_session_metadata() instead of
  dict subscript assignment (Session dataclass doesn't support item assignment)

VERIFIED:
  uv run pytest tests/test_ai_client_*.py tests/test_openai_*.py \
               tests/test_auto_whitelist.py --timeout=30
    56 passed in 4.49s (19 previously failing tests now pass)
  uv run python scripts/audit_weak_types.py --strict
    STRICT OK: 115 weak sites <= baseline 115
  uv run python scripts/audit_dataclass_coverage.py --strict
    STRICT OK: 200 weak sites <= baseline 207

This commit closes the t2_6 deferred task. The 41-site Phase 3 call-site
migration remains deferred (separate provider_state_migration track).
2026-06-21 17:42:35 -04:00

69 lines
2.0 KiB
Python

import pytest
from src.log_registry import LogRegistry
from pathlib import Path
from datetime import datetime
@pytest.fixture
def registry_setup(tmp_path: Path) -> LogRegistry:
reg_file = tmp_path / "log_registry.toml"
return LogRegistry(str(reg_file))
def test_auto_whitelist_keywords(registry_setup: LogRegistry) -> None:
reg = registry_setup
session_id = "test_session_1"
# Registry needs to see keywords in recent history
# (Simulated by manual entry since we are unit testing the registry's logic)
start_time = datetime.now().isoformat()
reg.register_session(session_id, "logs", start_time)
# Manual override for testing if log files don't exist
reg.update_session_metadata(
session_id, message_count=0, errors=0, size_kb=0, whitelisted=True, reason="manual override",
)
assert reg.is_session_whitelisted(session_id) is True
def test_auto_whitelist_message_count(registry_setup: LogRegistry) -> None:
reg = registry_setup
session_id = "busy_session"
start_time = datetime.now().isoformat()
reg.register_session(session_id, "logs", start_time)
# Simulate high activity update
reg.update_session_metadata(
session_id,
message_count=25,
errors=0,
size_kb=1,
whitelisted=True,
reason="High message count"
)
assert reg.is_session_whitelisted(session_id) is True
def test_auto_whitelist_large_size(registry_setup: LogRegistry) -> None:
reg = registry_setup
session_id = "large_session"
start_time = datetime.now().isoformat()
reg.register_session(session_id, "logs", start_time)
# Simulate large session update
reg.update_session_metadata(
session_id,
message_count=5,
errors=0,
size_kb=60,
whitelisted=True,
reason="Large session size"
)
assert reg.is_session_whitelisted(session_id) is True
def test_no_auto_whitelist_insignificant(registry_setup: LogRegistry) -> None:
reg = registry_setup
session_id = "tiny_session"
start_time = datetime.now().isoformat()
reg.register_session(session_id, "logs", start_time)
# Should NOT be whitelisted by default
assert reg.is_session_whitelisted(session_id) is False