integration tests
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 4m51s
Contract Testing / contract-snapshot (pull_request) Has been skipped

This commit is contained in:
2026-01-22 17:29:06 +01:00
parent f61ebda9b7
commit 597bb48248
68 changed files with 11832 additions and 3498 deletions

View File

@@ -2,440 +2,152 @@
* Integration Test: Onboarding Wizard Use Case Orchestration
*
* Tests the orchestration logic of onboarding wizard-related Use Cases:
* - CompleteOnboardingUseCase: Orchestrates the entire onboarding flow
* - ValidatePersonalInfoUseCase: Validates personal information
* - GenerateAvatarUseCase: Generates racing avatar from face photo
* - SubmitOnboardingUseCase: Submits completed onboarding data
* - CompleteDriverOnboardingUseCase: Orchestrates the driver creation flow
*
* Validates that Use Cases correctly interact with their Ports (Repositories, Event Publishers, Services)
* Validates that Use Cases correctly interact with their Ports (Repositories)
* Uses In-Memory adapters for fast, deterministic testing
*
* Focus: Business logic orchestration, NOT UI rendering
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { InMemoryUserRepository } from '../../../adapters/users/persistence/inmemory/InMemoryUserRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { InMemoryAvatarService } from '../../../adapters/media/inmemory/InMemoryAvatarService';
import { CompleteOnboardingUseCase } from '../../../core/onboarding/use-cases/CompleteOnboardingUseCase';
import { ValidatePersonalInfoUseCase } from '../../../core/onboarding/use-cases/ValidatePersonalInfoUseCase';
import { GenerateAvatarUseCase } from '../../../core/onboarding/use-cases/GenerateAvatarUseCase';
import { SubmitOnboardingUseCase } from '../../../core/onboarding/use-cases/SubmitOnboardingUseCase';
import { OnboardingCommand } from '../../../core/onboarding/ports/OnboardingCommand';
import { PersonalInfoCommand } from '../../../core/onboarding/ports/PersonalInfoCommand';
import { AvatarGenerationCommand } from '../../../core/onboarding/ports/AvatarGenerationCommand';
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { CompleteDriverOnboardingUseCase } from '../../../core/racing/application/use-cases/CompleteDriverOnboardingUseCase';
import { Logger } from '../../../core/shared/domain/Logger';
describe('Onboarding Wizard Use Case Orchestration', () => {
let userRepository: InMemoryUserRepository;
let eventPublisher: InMemoryEventPublisher;
let avatarService: InMemoryAvatarService;
let completeOnboardingUseCase: CompleteOnboardingUseCase;
let validatePersonalInfoUseCase: ValidatePersonalInfoUseCase;
let generateAvatarUseCase: GenerateAvatarUseCase;
let submitOnboardingUseCase: SubmitOnboardingUseCase;
let driverRepository: InMemoryDriverRepository;
let completeDriverOnboardingUseCase: CompleteDriverOnboardingUseCase;
let mockLogger: Logger;
beforeAll(() => {
// TODO: Initialize In-Memory repositories, event publisher, and services
// userRepository = new InMemoryUserRepository();
// eventPublisher = new InMemoryEventPublisher();
// avatarService = new InMemoryAvatarService();
// completeOnboardingUseCase = new CompleteOnboardingUseCase({
// userRepository,
// eventPublisher,
// avatarService,
// });
// validatePersonalInfoUseCase = new ValidatePersonalInfoUseCase({
// userRepository,
// eventPublisher,
// });
// generateAvatarUseCase = new GenerateAvatarUseCase({
// avatarService,
// eventPublisher,
// });
// submitOnboardingUseCase = new SubmitOnboardingUseCase({
// userRepository,
// eventPublisher,
// });
mockLogger = {
info: () => {},
debug: () => {},
warn: () => {},
error: () => {},
} as unknown as Logger;
driverRepository = new InMemoryDriverRepository(mockLogger);
completeDriverOnboardingUseCase = new CompleteDriverOnboardingUseCase(
driverRepository,
mockLogger
);
});
beforeEach(() => {
// TODO: Clear all In-Memory repositories before each test
// userRepository.clear();
// eventPublisher.clear();
// avatarService.clear();
beforeEach(async () => {
await driverRepository.clear();
});
describe('CompleteOnboardingUseCase - Success Path', () => {
it('should complete onboarding with valid personal info and avatar', async () => {
// TODO: Implement test
describe('CompleteDriverOnboardingUseCase - Success Path', () => {
it('should complete onboarding with valid personal info', async () => {
// Scenario: Complete onboarding successfully
// Given: A new user exists
// And: User has not completed onboarding
// When: CompleteOnboardingUseCase.execute() is called with valid personal info and avatar
// Then: User should be marked as onboarded
// And: User's personal info should be saved
// And: User's avatar should be saved
// And: EventPublisher should emit OnboardingCompletedEvent
// Given: A new user ID
const userId = 'user-123';
const input = {
userId,
firstName: 'John',
lastName: 'Doe',
displayName: 'RacerJohn',
country: 'US',
bio: 'New racer on the grid',
};
// When: CompleteDriverOnboardingUseCase.execute() is called
const result = await completeDriverOnboardingUseCase.execute(input);
// Then: Driver should be created
expect(result.isOk()).toBe(true);
const { driver } = result.unwrap();
expect(driver.id).toBe(userId);
expect(driver.name.toString()).toBe('RacerJohn');
expect(driver.country.toString()).toBe('US');
expect(driver.bio?.toString()).toBe('New racer on the grid');
// And: Repository should contain the driver
const savedDriver = await driverRepository.findById(userId);
expect(savedDriver).not.toBeNull();
expect(savedDriver?.id).toBe(userId);
});
it('should complete onboarding with minimal required data', async () => {
// TODO: Implement test
// Scenario: Complete onboarding with minimal data
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with minimal valid data
// Then: User should be marked as onboarded
// And: EventPublisher should emit OnboardingCompletedEvent
});
// Given: A new user ID
const userId = 'user-456';
const input = {
userId,
firstName: 'Jane',
lastName: 'Smith',
displayName: 'JaneS',
country: 'UK',
};
it('should complete onboarding with optional fields', async () => {
// TODO: Implement test
// Scenario: Complete onboarding with optional fields
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with optional fields
// Then: User should be marked as onboarded
// And: Optional fields should be saved
// And: EventPublisher should emit OnboardingCompletedEvent
// When: CompleteDriverOnboardingUseCase.execute() is called
const result = await completeDriverOnboardingUseCase.execute(input);
// Then: Driver should be created successfully
expect(result.isOk()).toBe(true);
const { driver } = result.unwrap();
expect(driver.id).toBe(userId);
expect(driver.bio).toBeUndefined();
});
});
describe('CompleteOnboardingUseCase - Validation', () => {
it('should reject onboarding with invalid personal info', async () => {
// TODO: Implement test
// Scenario: Invalid personal info
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with invalid personal info
// Then: Should throw ValidationError
// And: User should not be marked as onboarded
// And: EventPublisher should NOT emit OnboardingCompletedEvent
});
it('should reject onboarding with invalid avatar', async () => {
// TODO: Implement test
// Scenario: Invalid avatar
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with invalid avatar
// Then: Should throw ValidationError
// And: User should not be marked as onboarded
// And: EventPublisher should NOT emit OnboardingCompletedEvent
});
it('should reject onboarding for already onboarded user', async () => {
// TODO: Implement test
describe('CompleteDriverOnboardingUseCase - Validation & Errors', () => {
it('should reject onboarding if driver already exists', async () => {
// Scenario: Already onboarded user
// Given: A user has already completed onboarding
// When: CompleteOnboardingUseCase.execute() is called
// Then: Should throw AlreadyOnboardedError
// And: EventPublisher should NOT emit OnboardingCompletedEvent
});
});
// Given: A driver already exists for the user
const userId = 'existing-user';
const existingInput = {
userId,
firstName: 'Old',
lastName: 'Name',
displayName: 'OldRacer',
country: 'DE',
};
await completeDriverOnboardingUseCase.execute(existingInput);
describe('ValidatePersonalInfoUseCase - Success Path', () => {
it('should validate personal info with all required fields', async () => {
// TODO: Implement test
// Scenario: Valid personal info
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with valid personal info
// Then: Validation should pass
// And: EventPublisher should emit PersonalInfoValidatedEvent
// When: CompleteDriverOnboardingUseCase.execute() is called again for same user
const result = await completeDriverOnboardingUseCase.execute({
userId,
firstName: 'New',
lastName: 'Name',
displayName: 'NewRacer',
country: 'FR',
});
// Then: Should return DRIVER_ALREADY_EXISTS error
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('DRIVER_ALREADY_EXISTS');
});
it('should validate personal info with special characters in display name', async () => {
// TODO: Implement test
// Scenario: Display name with special characters
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with display name containing special characters
// Then: Validation should pass
// And: EventPublisher should emit PersonalInfoValidatedEvent
});
it('should validate personal info with different timezones', async () => {
// TODO: Implement test
// Scenario: Different timezone validation
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with various timezones
// Then: Validation should pass
// And: EventPublisher should emit PersonalInfoValidatedEvent
});
});
describe('ValidatePersonalInfoUseCase - Validation', () => {
it('should reject personal info with empty first name', async () => {
// TODO: Implement test
// Scenario: Empty first name
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with empty first name
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with empty last name', async () => {
// TODO: Implement test
// Scenario: Empty last name
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with empty last name
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with empty display name', async () => {
// TODO: Implement test
// Scenario: Empty display name
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with empty display name
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with display name too short', async () => {
// TODO: Implement test
// Scenario: Display name too short
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with display name less than 3 characters
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with display name too long', async () => {
// TODO: Implement test
// Scenario: Display name too long
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with display name more than 50 characters
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with empty country', async () => {
// TODO: Implement test
// Scenario: Empty country
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with empty country
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with invalid characters in first name', async () => {
// TODO: Implement test
// Scenario: Invalid characters in first name
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with numbers in first name
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with invalid characters in last name', async () => {
// TODO: Implement test
// Scenario: Invalid characters in last name
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with numbers in last name
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with profanity in display name', async () => {
// TODO: Implement test
// Scenario: Profanity in display name
// Given: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with profanity in display name
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
it('should reject personal info with duplicate display name', async () => {
// TODO: Implement test
// Scenario: Duplicate display name
// Given: A user with display name "RacerJohn" already exists
// And: A new user exists
// When: ValidatePersonalInfoUseCase.execute() is called with display name "RacerJohn"
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit PersonalInfoValidatedEvent
});
});
describe('GenerateAvatarUseCase - Success Path', () => {
it('should generate avatar with valid face photo', async () => {
// TODO: Implement test
// Scenario: Generate avatar with valid photo
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called with valid face photo
// Then: Avatar should be generated
// And: EventPublisher should emit AvatarGeneratedEvent
});
it('should generate avatar with different suit colors', async () => {
// TODO: Implement test
// Scenario: Generate avatar with different suit colors
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called with different suit colors
// Then: Avatar should be generated with specified color
// And: EventPublisher should emit AvatarGeneratedEvent
});
it('should generate multiple avatar options', async () => {
// TODO: Implement test
// Scenario: Generate multiple avatar options
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called
// Then: Multiple avatar options should be generated
// And: EventPublisher should emit AvatarGeneratedEvent
});
});
describe('GenerateAvatarUseCase - Validation', () => {
it('should reject avatar generation without face photo', async () => {
// TODO: Implement test
// Scenario: No face photo
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called without face photo
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit AvatarGeneratedEvent
});
it('should reject avatar generation with invalid file format', async () => {
// TODO: Implement test
// Scenario: Invalid file format
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called with invalid file format
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit AvatarGeneratedEvent
});
it('should reject avatar generation with oversized file', async () => {
// TODO: Implement test
// Scenario: Oversized file
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called with oversized file
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit AvatarGeneratedEvent
});
it('should reject avatar generation with invalid dimensions', async () => {
// TODO: Implement test
// Scenario: Invalid dimensions
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called with invalid dimensions
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit AvatarGeneratedEvent
});
it('should reject avatar generation with inappropriate content', async () => {
// TODO: Implement test
// Scenario: Inappropriate content
// Given: A new user exists
// When: GenerateAvatarUseCase.execute() is called with inappropriate content
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit AvatarGeneratedEvent
});
});
describe('SubmitOnboardingUseCase - Success Path', () => {
it('should submit onboarding with valid data', async () => {
// TODO: Implement test
// Scenario: Submit valid onboarding
// Given: A new user exists
// And: User has valid personal info
// And: User has valid avatar
// When: SubmitOnboardingUseCase.execute() is called
// Then: Onboarding should be submitted
// And: User should be marked as onboarded
// And: EventPublisher should emit OnboardingSubmittedEvent
});
it('should submit onboarding with minimal data', async () => {
// TODO: Implement test
// Scenario: Submit minimal onboarding
// Given: A new user exists
// And: User has minimal valid data
// When: SubmitOnboardingUseCase.execute() is called
// Then: Onboarding should be submitted
// And: User should be marked as onboarded
// And: EventPublisher should emit OnboardingSubmittedEvent
});
});
describe('SubmitOnboardingUseCase - Validation', () => {
it('should reject submission without personal info', async () => {
// TODO: Implement test
// Scenario: No personal info
// Given: A new user exists
// When: SubmitOnboardingUseCase.execute() is called without personal info
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit OnboardingSubmittedEvent
});
it('should reject submission without avatar', async () => {
// TODO: Implement test
// Scenario: No avatar
// Given: A new user exists
// When: SubmitOnboardingUseCase.execute() is called without avatar
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit OnboardingSubmittedEvent
});
it('should reject submission for already onboarded user', async () => {
// TODO: Implement test
// Scenario: Already onboarded user
// Given: A user has already completed onboarding
// When: SubmitOnboardingUseCase.execute() is called
// Then: Should throw AlreadyOnboardedError
// And: EventPublisher should NOT emit OnboardingSubmittedEvent
});
});
describe('Onboarding Orchestration - Error Handling', () => {
it('should handle repository errors gracefully', async () => {
// TODO: Implement test
// Scenario: Repository error
// Given: UserRepository throws an error
// When: CompleteOnboardingUseCase.execute() is called
// Then: Should propagate the error appropriately
// And: EventPublisher should NOT emit any events
});
// Given: Repository throws an error
const userId = 'error-user';
const originalCreate = driverRepository.create.bind(driverRepository);
driverRepository.create = async () => {
throw new Error('Database failure');
};
it('should handle avatar service errors gracefully', async () => {
// TODO: Implement test
// Scenario: Avatar service error
// Given: AvatarService throws an error
// When: GenerateAvatarUseCase.execute() is called
// Then: Should propagate the error appropriately
// And: EventPublisher should NOT emit any events
});
// When: CompleteDriverOnboardingUseCase.execute() is called
const result = await completeDriverOnboardingUseCase.execute({
userId,
firstName: 'John',
lastName: 'Doe',
displayName: 'RacerJohn',
country: 'US',
});
it('should handle concurrent onboarding submissions', async () => {
// TODO: Implement test
// Scenario: Concurrent submissions
// Given: A new user exists
// When: SubmitOnboardingUseCase.execute() is called multiple times concurrently
// Then: Only one submission should succeed
// And: Subsequent submissions should fail with appropriate error
});
});
// Then: Should return REPOSITORY_ERROR
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('REPOSITORY_ERROR');
expect(error.details.message).toBe('Database failure');
describe('Onboarding Orchestration - Edge Cases', () => {
it('should handle onboarding with timezone edge cases', async () => {
// TODO: Implement test
// Scenario: Edge case timezones
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with edge case timezones
// Then: Onboarding should complete successfully
// And: Timezone should be saved correctly
});
it('should handle onboarding with country edge cases', async () => {
// TODO: Implement test
// Scenario: Edge case countries
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with edge case countries
// Then: Onboarding should complete successfully
// And: Country should be saved correctly
});
it('should handle onboarding with display name edge cases', async () => {
// TODO: Implement test
// Scenario: Edge case display names
// Given: A new user exists
// When: CompleteOnboardingUseCase.execute() is called with edge case display names
// Then: Onboarding should complete successfully
// And: Display name should be saved correctly
// Restore
driverRepository.create = originalCreate;
});
});
});