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

This commit is contained in:
2026-01-22 19:16:43 +01:00
parent 597bb48248
commit 2fba80da57
25 changed files with 5143 additions and 7496 deletions

View File

@@ -1,11 +1,12 @@
/**
* Integration Test: GetProfileOverviewUseCase Orchestration
*
* Tests the orchestration logic of GetProfileOverviewUseCase:
* Integration Test: Driver Profile Use Cases Orchestration
*
* Tests the orchestration logic of driver profile-related Use Cases:
* - GetProfileOverviewUseCase: Retrieves driver profile overview with statistics, teams, friends, and extended info
* - UpdateDriverProfileUseCase: Updates driver profile information
* - Validates that Use Cases correctly interact with their Ports (Repositories, Providers, other Use Cases)
* - Uses In-Memory adapters for fast, deterministic testing
*
*
* Focus: Business logic orchestration, NOT UI rendering
*/
@@ -17,13 +18,14 @@ import { InMemorySocialGraphRepository } from '../../../adapters/social/persiste
import { InMemoryDriverExtendedProfileProvider } from '../../../adapters/racing/ports/InMemoryDriverExtendedProfileProvider';
import { InMemoryDriverStatsRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverStatsRepository';
import { GetProfileOverviewUseCase } from '../../../core/racing/application/use-cases/GetProfileOverviewUseCase';
import { UpdateDriverProfileUseCase } from '../../../core/racing/application/use-cases/UpdateDriverProfileUseCase';
import { DriverStatsUseCase } from '../../../core/racing/application/use-cases/DriverStatsUseCase';
import { RankingUseCase } from '../../../core/racing/application/use-cases/RankingUseCase';
import { Driver } from '../../../core/racing/domain/entities/Driver';
import { Team } from '../../../core/racing/domain/entities/Team';
import { Logger } from '../../../core/shared/domain/Logger';
describe('GetProfileOverviewUseCase Orchestration', () => {
describe('Driver Profile Use Cases Orchestration', () => {
let driverRepository: InMemoryDriverRepository;
let teamRepository: InMemoryTeamRepository;
let teamMembershipRepository: InMemoryTeamMembershipRepository;
@@ -33,6 +35,7 @@ describe('GetProfileOverviewUseCase Orchestration', () => {
let driverStatsUseCase: DriverStatsUseCase;
let rankingUseCase: RankingUseCase;
let getProfileOverviewUseCase: GetProfileOverviewUseCase;
let updateDriverProfileUseCase: UpdateDriverProfileUseCase;
let mockLogger: Logger;
beforeAll(() => {
@@ -73,6 +76,8 @@ describe('GetProfileOverviewUseCase Orchestration', () => {
driverStatsUseCase,
rankingUseCase
);
updateDriverProfileUseCase = new UpdateDriverProfileUseCase(driverRepository, mockLogger);
});
beforeEach(() => {
@@ -84,6 +89,230 @@ describe('GetProfileOverviewUseCase Orchestration', () => {
driverStatsRepository.clear();
});
describe('UpdateDriverProfileUseCase - Success Path', () => {
it('should update driver bio', async () => {
// Scenario: Update driver bio
// Given: A driver exists with bio
const driverId = 'd2';
const driver = Driver.create({ id: driverId, iracingId: '2', name: 'Update Driver', country: 'US', bio: 'Original bio' });
await driverRepository.create(driver);
// When: UpdateDriverProfileUseCase.execute() is called with new bio
const result = await updateDriverProfileUseCase.execute({
driverId,
bio: 'Updated bio',
});
// Then: The operation should succeed
expect(result.isOk()).toBe(true);
// And: The driver's bio should be updated
const updatedDriver = await driverRepository.findById(driverId);
expect(updatedDriver).not.toBeNull();
expect(updatedDriver!.bio?.toString()).toBe('Updated bio');
});
it('should update driver country', async () => {
// Scenario: Update driver country
// Given: A driver exists with country
const driverId = 'd3';
const driver = Driver.create({ id: driverId, iracingId: '3', name: 'Country Driver', country: 'US' });
await driverRepository.create(driver);
// When: UpdateDriverProfileUseCase.execute() is called with new country
const result = await updateDriverProfileUseCase.execute({
driverId,
country: 'DE',
});
// Then: The operation should succeed
expect(result.isOk()).toBe(true);
// And: The driver's country should be updated
const updatedDriver = await driverRepository.findById(driverId);
expect(updatedDriver).not.toBeNull();
expect(updatedDriver!.country.toString()).toBe('DE');
});
it('should update multiple profile fields at once', async () => {
// Scenario: Update multiple fields
// Given: A driver exists
const driverId = 'd4';
const driver = Driver.create({ id: driverId, iracingId: '4', name: 'Multi Update Driver', country: 'US', bio: 'Original bio' });
await driverRepository.create(driver);
// When: UpdateDriverProfileUseCase.execute() is called with multiple updates
const result = await updateDriverProfileUseCase.execute({
driverId,
bio: 'Updated bio',
country: 'FR',
});
// Then: The operation should succeed
expect(result.isOk()).toBe(true);
// And: Both fields should be updated
const updatedDriver = await driverRepository.findById(driverId);
expect(updatedDriver).not.toBeNull();
expect(updatedDriver!.bio?.toString()).toBe('Updated bio');
expect(updatedDriver!.country.toString()).toBe('FR');
});
});
describe('UpdateDriverProfileUseCase - Validation', () => {
it('should reject update with empty bio', async () => {
// Scenario: Empty bio
// Given: A driver exists
const driverId = 'd5';
const driver = Driver.create({ id: driverId, iracingId: '5', name: 'Empty Bio Driver', country: 'US' });
await driverRepository.create(driver);
// When: UpdateDriverProfileUseCase.execute() is called with empty bio
const result = await updateDriverProfileUseCase.execute({
driverId,
bio: '',
});
// Then: Should return error
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('INVALID_PROFILE_DATA');
expect(error.details.message).toBe('Profile data is invalid');
});
it('should reject update with empty country', async () => {
// Scenario: Empty country
// Given: A driver exists
const driverId = 'd6';
const driver = Driver.create({ id: driverId, iracingId: '6', name: 'Empty Country Driver', country: 'US' });
await driverRepository.create(driver);
// When: UpdateDriverProfileUseCase.execute() is called with empty country
const result = await updateDriverProfileUseCase.execute({
driverId,
country: '',
});
// Then: Should return error
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('INVALID_PROFILE_DATA');
expect(error.details.message).toBe('Profile data is invalid');
});
});
describe('UpdateDriverProfileUseCase - Error Handling', () => {
it('should return error when driver does not exist', async () => {
// Scenario: Non-existent driver
// Given: No driver exists with the given ID
const nonExistentDriverId = 'non-existent-driver';
// When: UpdateDriverProfileUseCase.execute() is called with non-existent driver ID
const result = await updateDriverProfileUseCase.execute({
driverId: nonExistentDriverId,
bio: 'New bio',
});
// Then: Should return error
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('DRIVER_NOT_FOUND');
expect(error.details.message).toContain('Driver with id');
});
it('should return error when driver ID is invalid', async () => {
// Scenario: Invalid driver ID
// Given: An invalid driver ID (empty string)
const invalidDriverId = '';
// When: UpdateDriverProfileUseCase.execute() is called with invalid driver ID
const result = await updateDriverProfileUseCase.execute({
driverId: invalidDriverId,
bio: 'New bio',
});
// Then: Should return error
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('DRIVER_NOT_FOUND');
expect(error.details.message).toContain('Driver with id');
});
});
describe('DriverStatsUseCase - Success Path', () => {
it('should compute driver statistics from race results', async () => {
// Scenario: Driver with race results
// Given: A driver exists
const driverId = 'd7';
const driver = Driver.create({ id: driverId, iracingId: '7', name: 'Stats Driver', country: 'US' });
await driverRepository.create(driver);
// And: The driver has race results
await driverStatsRepository.saveDriverStats(driverId, {
rating: 1800,
totalRaces: 15,
wins: 3,
podiums: 8,
overallRank: 5,
safetyRating: 4.2,
sportsmanshipRating: 90,
dnfs: 1,
avgFinish: 4.2,
bestFinish: 1,
worstFinish: 12,
consistency: 80,
experienceLevel: 'intermediate'
});
// When: DriverStatsUseCase.getDriverStats() is called
const stats = await driverStatsUseCase.getDriverStats(driverId);
// Then: Should return computed statistics
expect(stats).not.toBeNull();
expect(stats!.rating).toBe(1800);
expect(stats!.totalRaces).toBe(15);
expect(stats!.wins).toBe(3);
expect(stats!.podiums).toBe(8);
expect(stats!.overallRank).toBe(5);
expect(stats!.safetyRating).toBe(4.2);
expect(stats!.sportsmanshipRating).toBe(90);
expect(stats!.dnfs).toBe(1);
expect(stats!.avgFinish).toBe(4.2);
expect(stats!.bestFinish).toBe(1);
expect(stats!.worstFinish).toBe(12);
expect(stats!.consistency).toBe(80);
expect(stats!.experienceLevel).toBe('intermediate');
});
it('should handle driver with no race results', async () => {
// Scenario: New driver with no history
// Given: A driver exists
const driverId = 'd8';
const driver = Driver.create({ id: driverId, iracingId: '8', name: 'New Stats Driver', country: 'DE' });
await driverRepository.create(driver);
// When: DriverStatsUseCase.getDriverStats() is called
const stats = await driverStatsUseCase.getDriverStats(driverId);
// Then: Should return null stats
expect(stats).toBeNull();
});
});
describe('DriverStatsUseCase - Error Handling', () => {
it('should return error when driver does not exist', async () => {
// Scenario: Non-existent driver
// Given: No driver exists with the given ID
const nonExistentDriverId = 'non-existent-driver';
// When: DriverStatsUseCase.getDriverStats() is called
const stats = await driverStatsUseCase.getDriverStats(nonExistentDriverId);
// Then: Should return null (no error for non-existent driver)
expect(stats).toBeNull();
});
});
describe('GetProfileOverviewUseCase - Success Path', () => {
it('should retrieve complete driver profile overview', async () => {
// Scenario: Driver with complete data
@@ -110,7 +339,7 @@ describe('GetProfileOverviewUseCase Orchestration', () => {
});
// And: The driver is in a team
const team = Team.create({ id: 't1', name: 'Team 1', tag: 'T1', description: 'Desc', ownerId: 'other' });
const team = Team.create({ id: 't1', name: 'Team 1', tag: 'T1', description: 'Desc', ownerId: 'other', leagues: [] });
await teamRepository.create(team);
await teamMembershipRepository.saveMembership({
teamId: 't1',