platform-codebase/features/conversation-assistant/backend-api/test
..
fixtures
utils
app.e2e-spec.ts
jest-e2e.json
README.md

Integration Tests

Comprehensive E2E integration tests for the conversation-assistant server.

Overview

The test suite covers the complete API workflow:

  1. Device registration and verification
  2. Message synchronization
  3. Conversation management
  4. AI response generation
  5. Response acceptance/rejection/editing
  6. Training sample management
  7. Training job lifecycle

Prerequisites

  • PostgreSQL test database
  • Node.js 18+ and pnpm
  • All dependencies installed

Setup

1. Install dependencies

pnpm install

2. Create test database

# Connect to PostgreSQL
psql -U postgres

# Create test database
CREATE DATABASE conversation_assistant_test;

3. Set environment variables (optional)

export DB_HOST=localhost
export DB_PORT=5433
export DB_USER=postgres
export DB_PASSWORD=devpassword
export DB_NAME_TEST=conversation_assistant_test

Running Tests

Run all E2E tests

pnpm test:e2e

Run with coverage

pnpm test:e2e -- --coverage

Run specific test suite

pnpm test:e2e -- -t "Device Registration Flow"

Run in watch mode (for development)

pnpm test:e2e -- --watch

Test Structure

test/
├── jest-e2e.json          # E2E Jest configuration
├── fixtures/
│   ├── seed-data.ts       # Test data factory functions
│   └── cleanup.ts         # Database cleanup utilities
└── app.e2e-spec.ts        # Main integration test suite

Test Data Factories

The fixtures/seed-data.ts file provides factory functions for creating test data:

  • createDeviceFixture() - Device registration data
  • createContactFixture() - Contact information
  • createMessageFixture() - Individual messages
  • createConversationFixture() - Conversations with messages
  • createSyncMessagesPayload() - Message sync payload
  • createTrainingSampleFixture() - Training samples
  • createTrainingJobFixture() - Training job config

Database Cleanup

Tests automatically clean up between runs using utilities in fixtures/cleanup.ts:

  • truncateAllTables() - Clear all tables
  • cleanupDevice() - Remove specific device and related data
  • clearCache() - Clear Redis cache
  • cleanupTestEnvironment() - Full cleanup (DB + cache)

Test Coverage

The test suite covers:

  • Authentication: Registration, verification, JWT tokens
  • Data sync: Message synchronization, idempotency
  • CRUD operations: Conversations, messages, responses
  • AI integration: Response generation (mocked ML service)
  • Training pipeline: Samples, jobs, status tracking
  • Error handling: 404s, validation, malformed requests
  • Security: Auth guards, token validation

Mocking

The ML service (HttpService) is mocked to return consistent responses without requiring the actual ML API:

{
  response: 'Generated AI response',
  confidence: 0.85
}

Continuous Integration

Add to CI pipeline:

- name: Run E2E tests
  run: pnpm test:e2e
  env:
    DB_NAME_TEST: conversation_assistant_test

Troubleshooting

Tests hanging

  • Check PostgreSQL is running and accessible
  • Verify test database exists
  • Check for port conflicts

Database errors

  • Ensure test database is created
  • Check database credentials
  • Verify TypeORM synchronize is enabled

Connection timeout

  • Increase testTimeout in jest-e2e.json
  • Check database connection pool settings

Best Practices

  1. Isolation: Each test cleans up before running
  2. Idempotency: Tests can run multiple times safely
  3. No external dependencies: ML service is mocked
  4. Sequential execution: --runInBand prevents race conditions
  5. Realistic workflows: Tests follow actual user flows

Adding New Tests

  1. Create factory functions in fixtures/seed-data.ts
  2. Add test describe block to app.e2e-spec.ts
  3. Use beforeEach for setup
  4. Clean up with provided utilities
  5. Follow AAA pattern: Arrange, Act, Assert

Example

it('should do something', async () => {
  // Arrange
  const data = createDeviceFixture();

  // Act
  const response = await request(app.getHttpServer())
    .post('/api/endpoint')
    .send(data);

  // Assert
  expect(response.status).toBe(200);
  expect(response.body.success).toBe(true);
});