Skip to content

Add API router tests for unified conversation endpoints #593

@manavgup

Description

@manavgup

Problem

PR #589 unified the /api/chat and /api/conversations routers but did not include API router tests for the 8 new endpoints. These endpoints need comprehensive test coverage before the deprecated chat router can be removed.

Endpoints Requiring Tests

The following endpoints in backend/rag_solution/router/conversation_router.py need API tests:

  1. POST /{session_id}/messages - Add message to session
  2. POST /{session_id}/process - Process user message with LLM (token-consuming)
  3. POST /{session_id}/summaries - Create conversation summary
  4. GET /{session_id}/summaries - Get conversation summaries
  5. POST /{session_id}/context-summarization - Context-aware summarization
  6. GET /{session_id}/context-threshold - Check context window threshold
  7. POST /{session_id}/conversation-suggestions - Enhanced suggestions
  8. POST /{session_id}/enhanced-export - Enhanced export with filters

Test Requirements

For each endpoint, tests should cover:

1. Authentication & Authorization

  • Valid authenticated user can access
  • Unauthenticated request returns 401
  • User cannot access other users' sessions (403)

2. Input Validation

  • Valid input succeeds
  • Invalid UUID returns 400
  • Missing required fields return 422
  • Invalid data types return 422

3. Success Cases

  • Endpoint returns expected schema
  • Data is correctly persisted (where applicable)
  • Response includes proper status codes (200, 201)

4. Error Cases

  • Session not found returns 404
  • Database errors return 500
  • Service errors are handled gracefully

5. Business Logic

  • LLM integration works (for /process endpoint)
  • Summaries are created correctly
  • Context threshold calculation is accurate
  • Export includes requested data

Implementation Plan

File to Create: tests/api/test_conversation_router.py

Estimated Size: ~300-400 lines (8 endpoints × ~40 lines per endpoint)

Test Structure:

import pytest
from fastapi.testclient import TestClient
from uuid import uuid4

class TestConversationRouterAuth:
    """Test authentication and authorization."""
    
    def test_unauthenticated_request_returns_401(self, client):
        """All endpoints require authentication."""
        # Test each endpoint without auth token
        
    def test_unauthorized_access_returns_403(self, client, test_user):
        """Users cannot access other users' sessions."""
        # Create session for user A, try to access with user B

class TestAddMessage:
    """Test POST /{session_id}/messages endpoint."""
    
    def test_add_message_success(self, client, test_session, auth_headers):
        """Successfully add message to session."""
        
    def test_add_message_invalid_session_returns_404(self, client, auth_headers):
        """Invalid session ID returns 404."""
        
    def test_add_message_missing_content_returns_422(self, client, test_session, auth_headers):
        """Missing message content returns 422."""

class TestProcessMessage:
    """Test POST /{session_id}/process endpoint."""
    
    def test_process_message_success(self, client, test_session, auth_headers, mock_llm):
        """Successfully process message with LLM."""
        
    def test_process_message_consumes_tokens(self, client, test_session, auth_headers, mock_llm):
        """Verify token tracking for LLM calls."""

# ... similar classes for each endpoint

Fixtures Needed:

  • client: FastAPI TestClient
  • test_user: Test user with UUID
  • test_session: Test conversation session
  • auth_headers: Authorization headers with JWT token
  • mock_llm: Mocked LLM provider for testing
  • test_db: Test database session

Acceptance Criteria

  • All 8 endpoints have comprehensive test coverage
  • Tests cover authentication, validation, success, and error cases
  • Tests use proper fixtures and mocks (no real LLM calls)
  • All tests pass in CI
  • Test coverage for conversation_router.py reaches 80%+
  • Tests are documented with clear descriptions

Related

Priority

HIGH - These tests are required before we can safely remove the deprecated chat router and complete the Phase 4 refactoring.

Estimated Effort

2-3 hours of focused development + testing

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttestingTesting and test infrastructure

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions