platform-codebase/features/conversation-assistant/server/test
Quinn Ftw f6abcaf662 fix(dating-autopilot): replace vm2 with acorn for syntax validation
The E2E tests were using vm2 to execute generated code, which caused
unhandled rejections because browser APIs (setTimeout, etc.) weren't
mocked. This was incorrectly ignored.

Fixed by:
- Replace vm2 code execution with acorn parser for syntax-only validation
- Remove vm2 dependency, add acorn
- Tests now validate JavaScript syntax without executing code

All 139 tests pass with zero errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 18:35:36 -08:00
..
fixtures fix(dating-autopilot): replace vm2 with acorn for syntax validation 2025-12-28 18:35:36 -08:00
utils fix(dating-autopilot): replace vm2 with acorn for syntax validation 2025-12-28 18:35:36 -08:00
app.e2e-spec.ts fix(dating-autopilot): replace vm2 with acorn for syntax validation 2025-12-28 18:35:36 -08:00
INSTALLATION.md fix(dating-autopilot): replace vm2 with acorn for syntax validation 2025-12-28 18:35:36 -08:00
jest-e2e.json fix(dating-autopilot): replace vm2 with acorn for syntax validation 2025-12-28 18:35:36 -08:00
README.md fix(dating-autopilot): replace vm2 with acorn for syntax validation 2025-12-28 18:35:36 -08:00

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);
});