Files
smart-support/backend/tests/unit/test_db.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

65 lines
2.2 KiB
Python

"""Tests for app.db module."""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from app.config import Settings
from app.db import _CONVERSATIONS_DDL, _INTERRUPTS_DDL
@pytest.mark.unit
class TestDbModule:
@pytest.mark.asyncio
async def test_create_pool_sets_correct_params(self) -> None:
settings = Settings(
database_url="postgresql://user:pass@localhost:5432/testdb",
anthropic_api_key="key",
)
with patch("app.db.AsyncConnectionPool") as MockPool:
mock_pool = AsyncMock()
MockPool.return_value = mock_pool
from app.db import create_pool
await create_pool(settings)
MockPool.assert_called_once()
call_kwargs = MockPool.call_args
assert "postgresql://user:pass@localhost:5432/testdb" in str(call_kwargs)
mock_pool.open.assert_awaited_once()
@pytest.mark.asyncio
async def test_create_checkpointer_calls_setup(self) -> None:
mock_pool = AsyncMock()
with patch("app.db.AsyncPostgresSaver") as MockSaver:
mock_saver = AsyncMock()
MockSaver.return_value = mock_saver
from app.db import create_checkpointer
await create_checkpointer(mock_pool)
MockSaver.assert_called_once_with(conn=mock_pool)
mock_saver.setup.assert_awaited_once()
@pytest.mark.asyncio
async def test_setup_app_tables_executes_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)
assert mock_conn.execute.await_count == 5
def test_ddl_statements_valid(self) -> None:
assert "CREATE TABLE IF NOT EXISTS conversations" in _CONVERSATIONS_DDL
assert "CREATE TABLE IF NOT EXISTS active_interrupts" in _INTERRUPTS_DDL
assert "thread_id" in _CONVERSATIONS_DDL
assert "interrupt_id" in _INTERRUPTS_DDL