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

@@ -1,315 +1,178 @@
/**
* Integration Test: Driver Profile Use Case Orchestration
* Integration Test: GetProfileOverviewUseCase Orchestration
*
* Tests the orchestration logic of driver profile-related Use Cases:
* - GetDriverProfileUseCase: Retrieves driver profile with personal info, statistics, career history, recent results, championship standings, social links, team affiliation
* - Validates that Use Cases correctly interact with their Ports (Repositories, Event Publishers)
* Tests the orchestration logic of GetProfileOverviewUseCase:
* - GetProfileOverviewUseCase: Retrieves driver profile overview with statistics, teams, friends, and extended info
* - 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
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/drivers/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryRaceRepository } from '../../../adapters/races/persistence/inmemory/InMemoryRaceRepository';
import { InMemoryLeagueRepository } from '../../../adapters/leagues/persistence/inmemory/InMemoryLeagueRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { GetDriverProfileUseCase } from '../../../core/drivers/use-cases/GetDriverProfileUseCase';
import { DriverProfileQuery } from '../../../core/drivers/ports/DriverProfileQuery';
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryTeamRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryTeamRepository';
import { InMemoryTeamMembershipRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryTeamMembershipRepository';
import { InMemorySocialGraphRepository } from '../../../adapters/social/persistence/inmemory/InMemorySocialAndFeed';
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 { 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('Driver Profile Use Case Orchestration', () => {
describe('GetProfileOverviewUseCase Orchestration', () => {
let driverRepository: InMemoryDriverRepository;
let raceRepository: InMemoryRaceRepository;
let leagueRepository: InMemoryLeagueRepository;
let eventPublisher: InMemoryEventPublisher;
let getDriverProfileUseCase: GetDriverProfileUseCase;
let teamRepository: InMemoryTeamRepository;
let teamMembershipRepository: InMemoryTeamMembershipRepository;
let socialRepository: InMemorySocialGraphRepository;
let driverExtendedProfileProvider: InMemoryDriverExtendedProfileProvider;
let driverStatsRepository: InMemoryDriverStatsRepository;
let driverStatsUseCase: DriverStatsUseCase;
let rankingUseCase: RankingUseCase;
let getProfileOverviewUseCase: GetProfileOverviewUseCase;
let mockLogger: Logger;
beforeAll(() => {
// TODO: Initialize In-Memory repositories and event publisher
// driverRepository = new InMemoryDriverRepository();
// raceRepository = new InMemoryRaceRepository();
// leagueRepository = new InMemoryLeagueRepository();
// eventPublisher = new InMemoryEventPublisher();
// getDriverProfileUseCase = new GetDriverProfileUseCase({
// driverRepository,
// raceRepository,
// leagueRepository,
// eventPublisher,
// });
mockLogger = {
info: () => {},
debug: () => {},
warn: () => {},
error: () => {},
} as unknown as Logger;
driverRepository = new InMemoryDriverRepository(mockLogger);
teamRepository = new InMemoryTeamRepository(mockLogger);
teamMembershipRepository = new InMemoryTeamMembershipRepository(mockLogger);
socialRepository = new InMemorySocialGraphRepository(mockLogger);
driverExtendedProfileProvider = new InMemoryDriverExtendedProfileProvider(mockLogger);
driverStatsRepository = new InMemoryDriverStatsRepository(mockLogger);
driverStatsUseCase = new DriverStatsUseCase(
{} as any,
{} as any,
driverStatsRepository,
mockLogger
);
rankingUseCase = new RankingUseCase(
{} as any,
{} as any,
driverStatsRepository,
mockLogger
);
getProfileOverviewUseCase = new GetProfileOverviewUseCase(
driverRepository,
teamRepository,
teamMembershipRepository,
socialRepository,
driverExtendedProfileProvider,
driverStatsUseCase,
rankingUseCase
);
});
beforeEach(() => {
// TODO: Clear all In-Memory repositories before each test
// driverRepository.clear();
// raceRepository.clear();
// leagueRepository.clear();
// eventPublisher.clear();
driverRepository.clear();
teamRepository.clear();
teamMembershipRepository.clear();
socialRepository.clear();
driverExtendedProfileProvider.clear();
driverStatsRepository.clear();
});
describe('GetDriverProfileUseCase - Success Path', () => {
it('should retrieve complete driver profile with all data', async () => {
// TODO: Implement test
// Scenario: Driver with complete profile data
// Given: A driver exists with personal information (name, avatar, bio, location)
// And: The driver has statistics (rating, rank, starts, wins, podiums)
// And: The driver has career history (leagues, seasons, teams)
// And: The driver has recent race results
// And: The driver has championship standings
// And: The driver has social links configured
// And: The driver has team affiliation
// When: GetDriverProfileUseCase.execute() is called with driver ID
describe('GetProfileOverviewUseCase - Success Path', () => {
it('should retrieve complete driver profile overview', async () => {
// Scenario: Driver with complete data
// Given: A driver exists
const driverId = 'd1';
const driver = Driver.create({ id: driverId, iracingId: '1', name: 'John Doe', country: 'US' });
await driverRepository.create(driver);
// And: The driver has statistics
await driverStatsRepository.saveDriverStats(driverId, {
rating: 2000,
totalRaces: 10,
wins: 2,
podiums: 5,
overallRank: 1,
safetyRating: 4.5,
sportsmanshipRating: 95,
dnfs: 0,
avgFinish: 3.5,
bestFinish: 1,
worstFinish: 10,
consistency: 85,
experienceLevel: 'pro'
});
// And: The driver is in a team
const team = Team.create({ id: 't1', name: 'Team 1', tag: 'T1', description: 'Desc', ownerId: 'other' });
await teamRepository.create(team);
await teamMembershipRepository.saveMembership({
teamId: 't1',
driverId: driverId,
role: 'driver',
status: 'active',
joinedAt: new Date()
});
// And: The driver has friends
socialRepository.seed({
drivers: [driver, Driver.create({ id: 'f1', iracingId: '2', name: 'Friend 1', country: 'UK' })],
friendships: [{ driverId: driverId, friendId: 'f1' }],
feedEvents: []
});
// When: GetProfileOverviewUseCase.execute() is called
const result = await getProfileOverviewUseCase.execute({ driverId });
// Then: The result should contain all profile sections
// And: Personal information should be correctly populated
// And: Statistics should be correctly calculated
// And: Career history should include all leagues and teams
// And: Recent race results should be sorted by date (newest first)
// And: Championship standings should include league info
// And: Social links should be clickable
// And: Team affiliation should show team name and role
// And: EventPublisher should emit DriverProfileAccessedEvent
expect(result.isOk()).toBe(true);
const overview = result.unwrap();
expect(overview.driverInfo.driver.id).toBe(driverId);
expect(overview.stats?.rating).toBe(2000);
expect(overview.teamMemberships).toHaveLength(1);
expect(overview.teamMemberships[0].team.id).toBe('t1');
expect(overview.socialSummary.friendsCount).toBe(1);
expect(overview.extendedProfile).toBeDefined();
});
it('should retrieve driver profile with minimal data', async () => {
// TODO: Implement test
// Scenario: Driver with minimal profile data
// Given: A driver exists with only basic information (name, avatar)
// And: The driver has no bio or location
// And: The driver has no statistics
// And: The driver has no career history
// And: The driver has no recent race results
// And: The driver has no championship standings
// And: The driver has no social links
// And: The driver has no team affiliation
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain basic driver info
// And: All sections should be empty or show default values
// And: EventPublisher should emit DriverProfileAccessedEvent
});
it('should retrieve driver profile with career history but no recent results', async () => {
// TODO: Implement test
// Scenario: Driver with career history but no recent results
it('should handle driver with minimal data', async () => {
// Scenario: New driver with no history
// Given: A driver exists
// And: The driver has career history (leagues, seasons, teams)
// And: The driver has no recent race results
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain career history
// And: Recent race results section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
const driverId = 'new';
const driver = Driver.create({ id: driverId, iracingId: '9', name: 'New Driver', country: 'DE' });
await driverRepository.create(driver);
it('should retrieve driver profile with recent results but no career history', async () => {
// TODO: Implement test
// Scenario: Driver with recent results but no career history
// Given: A driver exists
// And: The driver has recent race results
// And: The driver has no career history
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain recent race results
// And: Career history section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
// When: GetProfileOverviewUseCase.execute() is called
const result = await getProfileOverviewUseCase.execute({ driverId });
it('should retrieve driver profile with championship standings but no other data', async () => {
// TODO: Implement test
// Scenario: Driver with championship standings but no other data
// Given: A driver exists
// And: The driver has championship standings
// And: The driver has no career history
// And: The driver has no recent race results
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain championship standings
// And: Career history section should be empty
// And: Recent race results section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
it('should retrieve driver profile with social links but no team affiliation', async () => {
// TODO: Implement test
// Scenario: Driver with social links but no team affiliation
// Given: A driver exists
// And: The driver has social links configured
// And: The driver has no team affiliation
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain social links
// And: Team affiliation section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
it('should retrieve driver profile with team affiliation but no social links', async () => {
// TODO: Implement test
// Scenario: Driver with team affiliation but no social links
// Given: A driver exists
// And: The driver has team affiliation
// And: The driver has no social links
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain team affiliation
// And: Social links section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
// Then: The result should contain basic info but null stats
expect(result.isOk()).toBe(true);
const overview = result.unwrap();
expect(overview.driverInfo.driver.id).toBe(driverId);
expect(overview.stats).toBeNull();
expect(overview.teamMemberships).toHaveLength(0);
expect(overview.socialSummary.friendsCount).toBe(0);
});
});
describe('GetDriverProfileUseCase - Edge Cases', () => {
it('should handle driver with no career history', async () => {
// TODO: Implement test
// Scenario: Driver with no career history
// Given: A driver exists
// And: The driver has no career history
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain driver profile
// And: Career history section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
it('should handle driver with no recent race results', async () => {
// TODO: Implement test
// Scenario: Driver with no recent race results
// Given: A driver exists
// And: The driver has no recent race results
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain driver profile
// And: Recent race results section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
it('should handle driver with no championship standings', async () => {
// TODO: Implement test
// Scenario: Driver with no championship standings
// Given: A driver exists
// And: The driver has no championship standings
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain driver profile
// And: Championship standings section should be empty
// And: EventPublisher should emit DriverProfileAccessedEvent
});
it('should handle driver with no data at all', async () => {
// TODO: Implement test
// Scenario: Driver with absolutely no data
// Given: A driver exists
// And: The driver has no statistics
// And: The driver has no career history
// And: The driver has no recent race results
// And: The driver has no championship standings
// And: The driver has no social links
// And: The driver has no team affiliation
// When: GetDriverProfileUseCase.execute() is called with driver ID
// Then: The result should contain basic driver info
// And: All sections should be empty or show default values
// And: EventPublisher should emit DriverProfileAccessedEvent
});
});
describe('GetDriverProfileUseCase - Error Handling', () => {
it('should throw error when driver does not exist', async () => {
// TODO: Implement test
describe('GetProfileOverviewUseCase - Error Handling', () => {
it('should return error when driver does not exist', async () => {
// Scenario: Non-existent driver
// Given: No driver exists with the given ID
// When: GetDriverProfileUseCase.execute() is called with non-existent driver ID
// Then: Should throw DriverNotFoundError
// And: EventPublisher should NOT emit any events
});
// When: GetProfileOverviewUseCase.execute() is called
const result = await getProfileOverviewUseCase.execute({ driverId: 'none' });
it('should throw error when driver ID is invalid', async () => {
// TODO: Implement test
// Scenario: Invalid driver ID
// Given: An invalid driver ID (e.g., empty string, null, undefined)
// When: GetDriverProfileUseCase.execute() is called with invalid driver ID
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit any events
});
it('should handle repository errors gracefully', async () => {
// TODO: Implement test
// Scenario: Repository throws error
// Given: A driver exists
// And: DriverRepository throws an error during query
// When: GetDriverProfileUseCase.execute() is called
// Then: Should propagate the error appropriately
// And: EventPublisher should NOT emit any events
});
});
describe('Driver Profile Data Orchestration', () => {
it('should correctly calculate driver statistics from race results', async () => {
// TODO: Implement test
// Scenario: Driver statistics calculation
// Given: A driver exists
// And: The driver has 10 completed races
// And: The driver has 3 wins
// And: The driver has 5 podiums
// When: GetDriverProfileUseCase.execute() is called
// Then: Driver statistics should show:
// - Starts: 10
// - Wins: 3
// - Podiums: 5
// - Rating: Calculated based on performance
// - Rank: Calculated based on rating
});
it('should correctly format career history with league and team information', async () => {
// TODO: Implement test
// Scenario: Career history formatting
// Given: A driver exists
// And: The driver has participated in 2 leagues
// And: The driver has been on 3 teams across seasons
// When: GetDriverProfileUseCase.execute() is called
// Then: Career history should show:
// - League A: Season 2024, Team X
// - League B: Season 2024, Team Y
// - League A: Season 2023, Team Z
});
it('should correctly format recent race results with proper details', async () => {
// TODO: Implement test
// Scenario: Recent race results formatting
// Given: A driver exists
// And: The driver has 5 recent race results
// When: GetDriverProfileUseCase.execute() is called
// Then: Recent race results should show:
// - Race name
// - Track name
// - Finishing position
// - Points earned
// - Race date (sorted newest first)
});
it('should correctly aggregate championship standings across leagues', async () => {
// TODO: Implement test
// Scenario: Championship standings aggregation
// Given: A driver exists
// And: The driver is in 2 championships
// And: In Championship A: Position 5, 150 points, 20 drivers
// And: In Championship B: Position 12, 85 points, 15 drivers
// When: GetDriverProfileUseCase.execute() is called
// Then: Championship standings should show:
// - League A: Position 5, 150 points, 20 drivers
// - League B: Position 12, 85 points, 15 drivers
});
it('should correctly format social links with proper URLs', async () => {
// TODO: Implement test
// Scenario: Social links formatting
// Given: A driver exists
// And: The driver has social links (Discord, Twitter, iRacing)
// When: GetDriverProfileUseCase.execute() is called
// Then: Social links should show:
// - Discord: https://discord.gg/username
// - Twitter: https://twitter.com/username
// - iRacing: https://members.iracing.com/membersite/member/profile?username=username
});
it('should correctly format team affiliation with role', async () => {
// TODO: Implement test
// Scenario: Team affiliation formatting
// Given: A driver exists
// And: The driver is affiliated with Team XYZ
// And: The driver's role is "Driver"
// When: GetDriverProfileUseCase.execute() is called
// Then: Team affiliation should show:
// - Team name: Team XYZ
// - Team logo: (if available)
// - Driver role: Driver
// Then: Should return DRIVER_NOT_FOUND
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('DRIVER_NOT_FOUND');
});
});
});

View File

@@ -1,281 +1,236 @@
/**
* Integration Test: Drivers List Use Case Orchestration
* Integration Test: GetDriversLeaderboardUseCase Orchestration
*
* Tests the orchestration logic of drivers list-related Use Cases:
* - GetDriversListUseCase: Retrieves list of drivers with search, filter, sort, pagination
* - Validates that Use Cases correctly interact with their Ports (Repositories, Event Publishers)
* Tests the orchestration logic of GetDriversLeaderboardUseCase:
* - GetDriversLeaderboardUseCase: Retrieves list of drivers with rankings and statistics
* - Validates that Use Cases correctly interact with their Ports (Repositories, other Use Cases)
* - 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 { InMemoryDriverRepository } from '../../../adapters/drivers/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { GetDriversListUseCase } from '../../../core/drivers/use-cases/GetDriversListUseCase';
import { DriversListQuery } from '../../../core/drivers/ports/DriversListQuery';
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryDriverStatsRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverStatsRepository';
import { GetDriversLeaderboardUseCase } from '../../../core/racing/application/use-cases/GetDriversLeaderboardUseCase';
import { RankingUseCase } from '../../../core/racing/application/use-cases/RankingUseCase';
import { DriverStatsUseCase } from '../../../core/racing/application/use-cases/DriverStatsUseCase';
import { Driver } from '../../../core/racing/domain/entities/Driver';
import { Logger } from '../../../core/shared/domain/Logger';
describe('Drivers List Use Case Orchestration', () => {
describe('GetDriversLeaderboardUseCase Orchestration', () => {
let driverRepository: InMemoryDriverRepository;
let eventPublisher: InMemoryEventPublisher;
let getDriversListUseCase: GetDriversListUseCase;
let driverStatsRepository: InMemoryDriverStatsRepository;
let rankingUseCase: RankingUseCase;
let driverStatsUseCase: DriverStatsUseCase;
let getDriversLeaderboardUseCase: GetDriversLeaderboardUseCase;
let mockLogger: Logger;
beforeAll(() => {
// TODO: Initialize In-Memory repositories and event publisher
// driverRepository = new InMemoryDriverRepository();
// eventPublisher = new InMemoryEventPublisher();
// getDriversListUseCase = new GetDriversListUseCase({
// driverRepository,
// eventPublisher,
// });
mockLogger = {
info: () => {},
debug: () => {},
warn: () => {},
error: () => {},
} as unknown as Logger;
driverRepository = new InMemoryDriverRepository(mockLogger);
driverStatsRepository = new InMemoryDriverStatsRepository(mockLogger);
// RankingUseCase and DriverStatsUseCase are dependencies of GetDriversLeaderboardUseCase
rankingUseCase = new RankingUseCase(
{} as any, // standingRepository not used in getAllDriverRankings
{} as any, // driverRepository not used in getAllDriverRankings
driverStatsRepository,
mockLogger
);
driverStatsUseCase = new DriverStatsUseCase(
{} as any, // resultRepository not used in getDriverStats
{} as any, // standingRepository not used in getDriverStats
driverStatsRepository,
mockLogger
);
getDriversLeaderboardUseCase = new GetDriversLeaderboardUseCase(
driverRepository,
rankingUseCase,
driverStatsUseCase,
mockLogger
);
});
beforeEach(() => {
// TODO: Clear all In-Memory repositories before each test
// driverRepository.clear();
// eventPublisher.clear();
driverRepository.clear();
driverStatsRepository.clear();
});
describe('GetDriversListUseCase - Success Path', () => {
describe('GetDriversLeaderboardUseCase - Success Path', () => {
it('should retrieve complete list of drivers with all data', async () => {
// TODO: Implement test
// Scenario: System has multiple drivers
// Given: 20 drivers exist with various data
// And: Each driver has name, avatar, rating, and rank
// When: GetDriversListUseCase.execute() is called with default parameters
// Given: 3 drivers exist with various data
const drivers = [
Driver.create({ id: 'd1', iracingId: '1', name: 'Driver 1', country: 'US' }),
Driver.create({ id: 'd2', iracingId: '2', name: 'Driver 2', country: 'UK' }),
Driver.create({ id: 'd3', iracingId: '3', name: 'Driver 3', country: 'DE' }),
];
for (const d of drivers) {
await driverRepository.create(d);
}
// And: Each driver has statistics
await driverStatsRepository.saveDriverStats('d1', {
rating: 2000,
totalRaces: 10,
wins: 2,
podiums: 5,
overallRank: 1,
safetyRating: 4.5,
sportsmanshipRating: 95,
dnfs: 0,
avgFinish: 3.5,
bestFinish: 1,
worstFinish: 10,
consistency: 85,
experienceLevel: 'pro'
});
await driverStatsRepository.saveDriverStats('d2', {
rating: 1800,
totalRaces: 8,
wins: 1,
podiums: 3,
overallRank: 2,
safetyRating: 4.0,
sportsmanshipRating: 90,
dnfs: 1,
avgFinish: 5.2,
bestFinish: 1,
worstFinish: 15,
consistency: 75,
experienceLevel: 'intermediate'
});
await driverStatsRepository.saveDriverStats('d3', {
rating: 1500,
totalRaces: 5,
wins: 0,
podiums: 1,
overallRank: 3,
safetyRating: 3.5,
sportsmanshipRating: 80,
dnfs: 0,
avgFinish: 8.0,
bestFinish: 3,
worstFinish: 12,
consistency: 65,
experienceLevel: 'rookie'
});
// When: GetDriversLeaderboardUseCase.execute() is called
const result = await getDriversLeaderboardUseCase.execute({});
// Then: The result should contain all drivers
// And: Each driver should have name, avatar, rating, and rank
// And: Drivers should be sorted by rating (high to low) by default
// And: EventPublisher should emit DriversListAccessedEvent
expect(result.isOk()).toBe(true);
const leaderboard = result.unwrap();
expect(leaderboard.items).toHaveLength(3);
expect(leaderboard.totalRaces).toBe(23);
expect(leaderboard.totalWins).toBe(3);
expect(leaderboard.activeCount).toBe(3);
// And: Drivers should be sorted by rating (high to low)
expect(leaderboard.items[0].driver.id).toBe('d1');
expect(leaderboard.items[1].driver.id).toBe('d2');
expect(leaderboard.items[2].driver.id).toBe('d3');
expect(leaderboard.items[0].rating).toBe(2000);
expect(leaderboard.items[1].rating).toBe(1800);
expect(leaderboard.items[2].rating).toBe(1500);
});
it('should retrieve drivers list with pagination', async () => {
// TODO: Implement test
// Scenario: System has many drivers requiring pagination
// Given: 50 drivers exist
// When: GetDriversListUseCase.execute() is called with page=1, limit=20
// Then: The result should contain 20 drivers
// And: The result should include pagination info (total, page, limit)
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should retrieve drivers list with search filter', async () => {
// TODO: Implement test
// Scenario: User searches for drivers by name
// Given: 10 drivers exist with names containing "John"
// And: 5 drivers exist with names containing "Jane"
// When: GetDriversListUseCase.execute() is called with search="John"
// Then: The result should contain only drivers with "John" in name
// And: The result should not contain drivers with "Jane" in name
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should retrieve drivers list with rating filter', async () => {
// TODO: Implement test
// Scenario: User filters drivers by rating range
// Given: 15 drivers exist with rating >= 4.0
// And: 10 drivers exist with rating < 4.0
// When: GetDriversListUseCase.execute() is called with minRating=4.0
// Then: The result should contain only drivers with rating >= 4.0
// And: The result should not contain drivers with rating < 4.0
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should retrieve drivers list sorted by rating (high to low)', async () => {
// TODO: Implement test
// Scenario: User sorts drivers by rating
// Given: 10 drivers exist with various ratings
// When: GetDriversListUseCase.execute() is called with sortBy="rating", sortOrder="desc"
// Then: The result should be sorted by rating in descending order
// And: The highest rated driver should be first
// And: The lowest rated driver should be last
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should retrieve drivers list sorted by name (A-Z)', async () => {
// TODO: Implement test
// Scenario: User sorts drivers by name
// Given: 10 drivers exist with various names
// When: GetDriversListUseCase.execute() is called with sortBy="name", sortOrder="asc"
// Then: The result should be sorted by name in alphabetical order
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should retrieve drivers list with combined search and filter', async () => {
// TODO: Implement test
// Scenario: User applies multiple filters
// Given: 5 drivers exist with "John" in name and rating >= 4.0
// And: 3 drivers exist with "John" in name but rating < 4.0
// And: 2 drivers exist with "Jane" in name and rating >= 4.0
// When: GetDriversListUseCase.execute() is called with search="John", minRating=4.0
// Then: The result should contain only the 5 drivers with "John" and rating >= 4.0
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should retrieve drivers list with combined search, filter, and sort', async () => {
// TODO: Implement test
// Scenario: User applies all available filters
// Given: 10 drivers exist with various names and ratings
// When: GetDriversListUseCase.execute() is called with search="D", minRating=3.0, sortBy="rating", sortOrder="desc", page=1, limit=5
// Then: The result should contain only drivers with "D" in name and rating >= 3.0
// And: The result should be sorted by rating (high to low)
// And: The result should contain at most 5 drivers
// And: EventPublisher should emit DriversListAccessedEvent
});
});
describe('GetDriversListUseCase - Edge Cases', () => {
it('should handle empty drivers list', async () => {
// TODO: Implement test
// Scenario: System has no registered drivers
// Given: No drivers exist in the system
// When: GetDriversListUseCase.execute() is called
// When: GetDriversLeaderboardUseCase.execute() is called
const result = await getDriversLeaderboardUseCase.execute({});
// Then: The result should contain an empty array
// And: The result should indicate no drivers found
// And: EventPublisher should emit DriversListAccessedEvent
expect(result.isOk()).toBe(true);
const leaderboard = result.unwrap();
expect(leaderboard.items).toHaveLength(0);
expect(leaderboard.totalRaces).toBe(0);
expect(leaderboard.totalWins).toBe(0);
expect(leaderboard.activeCount).toBe(0);
});
it('should handle search with no matching results', async () => {
// TODO: Implement test
// Scenario: User searches for non-existent driver
// Given: 10 drivers exist
// When: GetDriversListUseCase.execute() is called with search="NonExistentDriver123"
// Then: The result should contain an empty array
// And: The result should indicate no drivers found
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should correctly identify active drivers', async () => {
// Scenario: Some drivers have no races
// Given: 2 drivers exist, one with races, one without
await driverRepository.create(Driver.create({ id: 'active', iracingId: '1', name: 'Active', country: 'US' }));
await driverRepository.create(Driver.create({ id: 'inactive', iracingId: '2', name: 'Inactive', country: 'UK' }));
await driverStatsRepository.saveDriverStats('active', {
rating: 1500,
totalRaces: 1,
wins: 0,
podiums: 0,
overallRank: 1,
safetyRating: 3.0,
sportsmanshipRating: 70,
dnfs: 0,
avgFinish: 10,
bestFinish: 10,
worstFinish: 10,
consistency: 50,
experienceLevel: 'rookie'
});
// No stats for inactive driver or totalRaces = 0
await driverStatsRepository.saveDriverStats('inactive', {
rating: 1000,
totalRaces: 0,
wins: 0,
podiums: 0,
overallRank: null,
safetyRating: 2.5,
sportsmanshipRating: 50,
dnfs: 0,
avgFinish: 0,
bestFinish: 0,
worstFinish: 0,
consistency: 0,
experienceLevel: 'rookie'
});
it('should handle filter with no matching results', async () => {
// TODO: Implement test
// Scenario: User filters with criteria that match no drivers
// Given: All drivers have rating < 5.0
// When: GetDriversListUseCase.execute() is called with minRating=5.0
// Then: The result should contain an empty array
// And: The result should indicate no drivers found
// And: EventPublisher should emit DriversListAccessedEvent
});
// When: GetDriversLeaderboardUseCase.execute() is called
const result = await getDriversLeaderboardUseCase.execute({});
it('should handle pagination beyond available results', async () => {
// TODO: Implement test
// Scenario: User requests page beyond available data
// Given: 15 drivers exist
// When: GetDriversListUseCase.execute() is called with page=10, limit=20
// Then: The result should contain an empty array
// And: The result should indicate no drivers found
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should handle empty search string', async () => {
// TODO: Implement test
// Scenario: User clears search field
// Given: 10 drivers exist
// When: GetDriversListUseCase.execute() is called with search=""
// Then: The result should contain all drivers
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should handle null or undefined filter values', async () => {
// TODO: Implement test
// Scenario: User provides null/undefined filter values
// Given: 10 drivers exist
// When: GetDriversListUseCase.execute() is called with minRating=null
// Then: The result should contain all drivers (filter should be ignored)
// And: EventPublisher should emit DriversListAccessedEvent
// Then: Only one driver should be active
const leaderboard = result.unwrap();
expect(leaderboard.activeCount).toBe(1);
expect(leaderboard.items.find(i => i.driver.id === 'active')?.isActive).toBe(true);
expect(leaderboard.items.find(i => i.driver.id === 'inactive')?.isActive).toBe(false);
});
});
describe('GetDriversListUseCase - Error Handling', () => {
it('should throw error when repository query fails', async () => {
// TODO: Implement test
describe('GetDriversLeaderboardUseCase - Error Handling', () => {
it('should handle repository errors gracefully', async () => {
// Scenario: Repository throws error
// Given: DriverRepository throws an error during query
// When: GetDriversListUseCase.execute() is called
// Then: Should propagate the error appropriately
// And: EventPublisher should NOT emit any events
});
const originalFindAll = driverRepository.findAll.bind(driverRepository);
driverRepository.findAll = async () => {
throw new Error('Repository error');
};
it('should throw error with invalid pagination parameters', async () => {
// TODO: Implement test
// Scenario: Invalid pagination parameters
// Given: Invalid parameters (e.g., negative page, zero limit)
// When: GetDriversListUseCase.execute() is called with invalid parameters
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit any events
});
// When: GetDriversLeaderboardUseCase.execute() is called
const result = await getDriversLeaderboardUseCase.execute({});
it('should throw error with invalid filter parameters', async () => {
// TODO: Implement test
// Scenario: Invalid filter parameters
// Given: Invalid parameters (e.g., negative minRating)
// When: GetDriversListUseCase.execute() is called with invalid parameters
// Then: Should throw ValidationError
// And: EventPublisher should NOT emit any events
});
});
// Then: Should return a repository error
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('REPOSITORY_ERROR');
describe('Drivers List Data Orchestration', () => {
it('should correctly calculate driver count information', async () => {
// TODO: Implement test
// Scenario: Driver count calculation
// Given: 25 drivers exist
// When: GetDriversListUseCase.execute() is called with page=1, limit=20
// Then: The result should show:
// - Total drivers: 25
// - Drivers on current page: 20
// - Total pages: 2
// - Current page: 1
});
it('should correctly format driver cards with consistent information', async () => {
// TODO: Implement test
// Scenario: Driver card formatting
// Given: 10 drivers exist
// When: GetDriversListUseCase.execute() is called
// Then: Each driver card should contain:
// - Driver ID (for navigation)
// - Driver name
// - Driver avatar URL
// - Driver rating (formatted as decimal)
// - Driver rank (formatted as ordinal, e.g., "1st", "2nd", "3rd")
});
it('should correctly handle search case-insensitivity', async () => {
// TODO: Implement test
// Scenario: Search is case-insensitive
// Given: Drivers exist with names "John Doe", "john smith", "JOHNathan"
// When: GetDriversListUseCase.execute() is called with search="john"
// Then: The result should contain all three drivers
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should correctly handle search with partial matches', async () => {
// TODO: Implement test
// Scenario: Search matches partial names
// Given: Drivers exist with names "John Doe", "Jonathan", "Johnson"
// When: GetDriversListUseCase.execute() is called with search="John"
// Then: The result should contain all three drivers
// And: EventPublisher should emit DriversListAccessedEvent
});
it('should correctly handle multiple filter combinations', async () => {
// TODO: Implement test
// Scenario: Multiple filters applied together
// Given: 20 drivers exist with various names and ratings
// When: GetDriversListUseCase.execute() is called with search="D", minRating=3.5, sortBy="name", sortOrder="asc"
// Then: The result should:
// - Only contain drivers with "D" in name
// - Only contain drivers with rating >= 3.5
// - Be sorted alphabetically by name
});
it('should correctly handle pagination with filters', async () => {
// TODO: Implement test
// Scenario: Pagination with active filters
// Given: 30 drivers exist with "A" in name
// When: GetDriversListUseCase.execute() is called with search="A", page=2, limit=10
// Then: The result should contain drivers 11-20 (alphabetically sorted)
// And: The result should show total drivers: 30
// And: The result should show current page: 2
// Restore original method
driverRepository.findAll = originalFindAll;
});
});
});

View File

@@ -0,0 +1,367 @@
/**
* Integration Test: GetDriverUseCase Orchestration
*
* Tests the orchestration logic of GetDriverUseCase:
* - GetDriverUseCase: Retrieves a single driver by ID
* - 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, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { GetDriverUseCase } from '../../../core/racing/application/use-cases/GetDriverUseCase';
import { Driver } from '../../../core/racing/domain/entities/Driver';
import { MediaReference } from '../../../core/domain/media/MediaReference';
import { Logger } from '../../../core/shared/domain/Logger';
describe('GetDriverUseCase Orchestration', () => {
let driverRepository: InMemoryDriverRepository;
let getDriverUseCase: GetDriverUseCase;
let mockLogger: Logger;
beforeAll(() => {
mockLogger = {
info: () => {},
debug: () => {},
warn: () => {},
error: () => {},
} as unknown as Logger;
driverRepository = new InMemoryDriverRepository(mockLogger);
getDriverUseCase = new GetDriverUseCase(driverRepository);
});
beforeEach(() => {
// Clear all In-Memory repositories before each test
driverRepository.clear();
});
describe('GetDriverUseCase - Success Path', () => {
it('should retrieve complete driver with all data', async () => {
// Scenario: Driver with complete profile data
// Given: A driver exists with personal information (name, avatar, bio, country)
const driverId = 'driver-123';
const driver = Driver.create({
id: driverId,
iracingId: '12345',
name: 'John Doe',
country: 'US',
bio: 'A passionate racer with 10 years of experience',
avatarRef: MediaReference.createUploaded('avatar-123'),
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain all driver data
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.iracingId.toString()).toBe('12345');
expect(retrievedDriver.name.toString()).toBe('John Doe');
expect(retrievedDriver.country.toString()).toBe('US');
expect(retrievedDriver.bio?.toString()).toBe('A passionate racer with 10 years of experience');
expect(retrievedDriver.avatarRef).toBeDefined();
});
it('should retrieve driver with minimal data', async () => {
// Scenario: Driver with minimal profile data
// Given: A driver exists with only basic information (name, country)
const driverId = 'driver-456';
const driver = Driver.create({
id: driverId,
iracingId: '67890',
name: 'Jane Smith',
country: 'UK',
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain basic driver info
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.iracingId.toString()).toBe('67890');
expect(retrievedDriver.name.toString()).toBe('Jane Smith');
expect(retrievedDriver.country.toString()).toBe('UK');
expect(retrievedDriver.bio).toBeUndefined();
expect(retrievedDriver.avatarRef).toBeDefined();
});
it('should retrieve driver with bio but no avatar', async () => {
// Scenario: Driver with bio but no avatar
// Given: A driver exists with bio but no avatar
const driverId = 'driver-789';
const driver = Driver.create({
id: driverId,
iracingId: '11111',
name: 'Bob Johnson',
country: 'CA',
bio: 'Canadian racer',
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain driver info with bio
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.bio?.toString()).toBe('Canadian racer');
expect(retrievedDriver.avatarRef).toBeDefined();
});
it('should retrieve driver with avatar but no bio', async () => {
// Scenario: Driver with avatar but no bio
// Given: A driver exists with avatar but no bio
const driverId = 'driver-999';
const driver = Driver.create({
id: driverId,
iracingId: '22222',
name: 'Alice Brown',
country: 'DE',
avatarRef: MediaReference.createUploaded('avatar-999'),
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain driver info with avatar
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.bio).toBeUndefined();
expect(retrievedDriver.avatarRef).toBeDefined();
});
});
describe('GetDriverUseCase - Edge Cases', () => {
it('should handle driver with no bio', async () => {
// Scenario: Driver with no bio
// Given: A driver exists
const driverId = 'driver-no-bio';
const driver = Driver.create({
id: driverId,
iracingId: '33333',
name: 'No Bio Driver',
country: 'FR',
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain driver profile
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.bio).toBeUndefined();
});
it('should handle driver with no avatar', async () => {
// Scenario: Driver with no avatar
// Given: A driver exists
const driverId = 'driver-no-avatar';
const driver = Driver.create({
id: driverId,
iracingId: '44444',
name: 'No Avatar Driver',
country: 'ES',
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain driver profile
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.avatarRef).toBeDefined();
});
it('should handle driver with no data at all', async () => {
// Scenario: Driver with absolutely no data
// Given: A driver exists with only required fields
const driverId = 'driver-minimal';
const driver = Driver.create({
id: driverId,
iracingId: '55555',
name: 'Minimal Driver',
country: 'IT',
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called with driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should contain basic driver info
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver).toBeDefined();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.iracingId.toString()).toBe('55555');
expect(retrievedDriver.name.toString()).toBe('Minimal Driver');
expect(retrievedDriver.country.toString()).toBe('IT');
expect(retrievedDriver.bio).toBeUndefined();
expect(retrievedDriver.avatarRef).toBeDefined();
});
});
describe('GetDriverUseCase - Error Handling', () => {
it('should return null when driver does not exist', async () => {
// Scenario: Non-existent driver
// Given: No driver exists with the given ID
const driverId = 'non-existent-driver';
// When: GetDriverUseCase.execute() is called with non-existent driver ID
const result = await getDriverUseCase.execute({ driverId });
// Then: The result should be null
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeNull();
});
it('should handle repository errors gracefully', async () => {
// Scenario: Repository throws error
// Given: A driver exists
const driverId = 'driver-error';
const driver = Driver.create({
id: driverId,
iracingId: '66666',
name: 'Error Driver',
country: 'US',
});
await driverRepository.create(driver);
// Mock the repository to throw an error
const originalFindById = driverRepository.findById.bind(driverRepository);
driverRepository.findById = async () => {
throw new Error('Repository error');
};
// When: GetDriverUseCase.execute() is called
const result = await getDriverUseCase.execute({ driverId });
// Then: Should propagate the error appropriately
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.message).toBe('Repository error');
// Restore original method
driverRepository.findById = originalFindById;
});
});
describe('GetDriverUseCase - Data Orchestration', () => {
it('should correctly retrieve driver with all fields populated', async () => {
// Scenario: Driver with all fields populated
// Given: A driver exists with all possible fields
const driverId = 'driver-complete';
const driver = Driver.create({
id: driverId,
iracingId: '77777',
name: 'Complete Driver',
country: 'US',
bio: 'Complete driver profile with all fields',
avatarRef: MediaReference.createUploaded('avatar-complete'),
category: 'pro',
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called
const result = await getDriverUseCase.execute({ driverId });
// Then: All fields should be correctly retrieved
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver.id).toBe(driverId);
expect(retrievedDriver.iracingId.toString()).toBe('77777');
expect(retrievedDriver.name.toString()).toBe('Complete Driver');
expect(retrievedDriver.country.toString()).toBe('US');
expect(retrievedDriver.bio?.toString()).toBe('Complete driver profile with all fields');
expect(retrievedDriver.avatarRef).toBeDefined();
expect(retrievedDriver.category).toBe('pro');
});
it('should correctly retrieve driver with system-default avatar', async () => {
// Scenario: Driver with system-default avatar
// Given: A driver exists with system-default avatar
const driverId = 'driver-system-avatar';
const driver = Driver.create({
id: driverId,
iracingId: '88888',
name: 'System Avatar Driver',
country: 'US',
avatarRef: MediaReference.createSystemDefault('avatar'),
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called
const result = await getDriverUseCase.execute({ driverId });
// Then: The avatar reference should be correctly retrieved
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver.avatarRef).toBeDefined();
expect(retrievedDriver.avatarRef.type).toBe('system_default');
});
it('should correctly retrieve driver with generated avatar', async () => {
// Scenario: Driver with generated avatar
// Given: A driver exists with generated avatar
const driverId = 'driver-generated-avatar';
const driver = Driver.create({
id: driverId,
iracingId: '99999',
name: 'Generated Avatar Driver',
country: 'US',
avatarRef: MediaReference.createGenerated('gen-123'),
});
await driverRepository.create(driver);
// When: GetDriverUseCase.execute() is called
const result = await getDriverUseCase.execute({ driverId });
// Then: The avatar reference should be correctly retrieved
expect(result.isOk()).toBe(true);
const retrievedDriver = result.unwrap();
expect(retrievedDriver.avatarRef).toBeDefined();
expect(retrievedDriver.avatarRef.type).toBe('generated');
});
});
});