Files
smart-support/backend/tests/unit/test_db_phase4.py
Yaojia Wang af53111928 refactor: fix architectural issues across frontend and backend
Address all architecture review findings:

P0 fixes:
- Add API key authentication for admin endpoints (analytics, replay, openapi)
  and WebSocket connections via ADMIN_API_KEY env var
- Add PostgreSQL-backed PgSessionManager and PgInterruptManager for
  multi-worker production deployments (in-memory defaults preserved)

P1 fixes:
- Implement actual tool generation in OpenAPI approve_job endpoint
  using generate_tool_code() and generate_agent_yaml()
- Add missing clarification, interrupt_expired, and tool_result message
  handlers in frontend ChatPage

P2 fixes:
- Replace monkey-patching on CompiledStateGraph with typed GraphContext
- Replace 9-param dispatch_message with WebSocketContext dataclass
- Extract duplicate _envelope() into shared app/api_utils.py
- Replace mutable module-level counter with crypto.randomUUID()
- Remove hardcoded mock data from ReviewPage, use api.ts wrappers
- Remove `as any` type escape from ReplayPage

All 516 tests passing, 0 TypeScript errors.
2026-04-06 15:59:14 +02:00

56 lines
2.1 KiB
Python

"""Phase 4 DB migration tests -- analytics_events table and conversation columns."""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
pytestmark = pytest.mark.unit
class TestAnalyticsEventsDDL:
def test_analytics_events_ddl_exists(self) -> None:
from app.db import _ANALYTICS_EVENTS_DDL
assert "CREATE TABLE IF NOT EXISTS analytics_events" in _ANALYTICS_EVENTS_DDL
def test_analytics_events_ddl_has_required_columns(self) -> None:
from app.db import _ANALYTICS_EVENTS_DDL
assert "thread_id" in _ANALYTICS_EVENTS_DDL
assert "event_type" in _ANALYTICS_EVENTS_DDL
assert "agent_name" in _ANALYTICS_EVENTS_DDL
assert "tool_name" in _ANALYTICS_EVENTS_DDL
assert "tokens_used" in _ANALYTICS_EVENTS_DDL
assert "cost_usd" in _ANALYTICS_EVENTS_DDL
assert "duration_ms" in _ANALYTICS_EVENTS_DDL
assert "success" in _ANALYTICS_EVENTS_DDL
assert "error_message" in _ANALYTICS_EVENTS_DDL
assert "metadata" in _ANALYTICS_EVENTS_DDL
def test_conversations_migration_ddl_exists(self) -> None:
from app.db import _CONVERSATIONS_MIGRATION_DDL
assert "ALTER TABLE" in _CONVERSATIONS_MIGRATION_DDL
assert "resolution_type" in _CONVERSATIONS_MIGRATION_DDL
assert "agents_used" in _CONVERSATIONS_MIGRATION_DDL
assert "turn_count" in _CONVERSATIONS_MIGRATION_DDL
assert "ended_at" in _CONVERSATIONS_MIGRATION_DDL
assert "IF NOT EXISTS" in _CONVERSATIONS_MIGRATION_DDL
@pytest.mark.asyncio
async def test_setup_app_tables_executes_analytics_ddl(self) -> None:
mock_conn = AsyncMock()
mock_ctx = AsyncMock()
mock_ctx.__aenter__ = AsyncMock(return_value=mock_conn)
mock_ctx.__aexit__ = AsyncMock(return_value=None)
mock_pool = MagicMock()
mock_pool.connection.return_value = mock_ctx
from app.db import setup_app_tables
await setup_app_tables(mock_pool)
# Now expects 5 statements: conversations, interrupts, sessions, analytics_events, migrations
assert mock_conn.execute.await_count == 5