integration tests
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped

This commit is contained in:
2026-01-23 00:46:34 +01:00
parent eaf51712a7
commit a0f41f242f
53 changed files with 3214 additions and 8820 deletions

View File

@@ -0,0 +1,57 @@
import { vi } 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 { InMemoryActivityRepository } from '../../../adapters/activity/persistence/inmemory/InMemoryActivityRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { GetDashboardUseCase } from '../../../core/dashboard/application/use-cases/GetDashboardUseCase';
import { DashboardPresenter } from '../../../core/dashboard/application/presenters/DashboardPresenter';
import { DashboardRepository } from '../../../core/dashboard/application/ports/DashboardRepository';
export class DashboardTestContext {
public readonly driverRepository: InMemoryDriverRepository;
public readonly raceRepository: InMemoryRaceRepository;
public readonly leagueRepository: InMemoryLeagueRepository;
public readonly activityRepository: InMemoryActivityRepository;
public readonly eventPublisher: InMemoryEventPublisher;
public readonly getDashboardUseCase: GetDashboardUseCase;
public readonly dashboardPresenter: DashboardPresenter;
public readonly loggerMock: any;
constructor() {
this.driverRepository = new InMemoryDriverRepository();
this.raceRepository = new InMemoryRaceRepository();
this.leagueRepository = new InMemoryLeagueRepository();
this.activityRepository = new InMemoryActivityRepository();
this.eventPublisher = new InMemoryEventPublisher();
this.dashboardPresenter = new DashboardPresenter();
this.loggerMock = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
this.getDashboardUseCase = new GetDashboardUseCase({
driverRepository: this.driverRepository,
raceRepository: this.raceRepository as unknown as DashboardRepository,
leagueRepository: this.leagueRepository as unknown as DashboardRepository,
activityRepository: this.activityRepository as unknown as DashboardRepository,
eventPublisher: this.eventPublisher,
logger: this.loggerMock,
});
}
public clear(): void {
this.driverRepository.clear();
this.raceRepository.clear();
this.leagueRepository.clear();
this.activityRepository.clear();
this.eventPublisher.clear();
vi.clearAllMocks();
}
public static create(): DashboardTestContext {
return new DashboardTestContext();
}
}

View File

@@ -1,674 +0,0 @@
/**
* Integration Test: Dashboard Data Flow
*
* Tests the complete data flow for dashboard functionality:
* 1. Repository queries return correct data
* 2. Use case processes and orchestrates data correctly
* 3. Presenter transforms data to DTOs
* 4. API returns correct response structure
*
* Focus: Data transformation and flow, 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 { InMemoryActivityRepository } from '../../../adapters/activity/persistence/inmemory/InMemoryActivityRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { GetDashboardUseCase } from '../../../core/dashboard/application/use-cases/GetDashboardUseCase';
import { DashboardPresenter } from '../../../core/dashboard/application/presenters/DashboardPresenter';
import { DashboardDTO } from '../../../core/dashboard/application/dto/DashboardDTO';
describe('Dashboard Data Flow Integration', () => {
let driverRepository: InMemoryDriverRepository;
let raceRepository: InMemoryRaceRepository;
let leagueRepository: InMemoryLeagueRepository;
let activityRepository: InMemoryActivityRepository;
let eventPublisher: InMemoryEventPublisher;
let getDashboardUseCase: GetDashboardUseCase;
let dashboardPresenter: DashboardPresenter;
beforeAll(() => {
driverRepository = new InMemoryDriverRepository();
raceRepository = new InMemoryRaceRepository();
leagueRepository = new InMemoryLeagueRepository();
activityRepository = new InMemoryActivityRepository();
eventPublisher = new InMemoryEventPublisher();
getDashboardUseCase = new GetDashboardUseCase({
driverRepository,
raceRepository,
leagueRepository,
activityRepository,
eventPublisher,
});
dashboardPresenter = new DashboardPresenter();
});
beforeEach(() => {
driverRepository.clear();
raceRepository.clear();
leagueRepository.clear();
activityRepository.clear();
eventPublisher.clear();
});
describe('Repository to Use Case Data Flow', () => {
it('should correctly flow driver data from repository to use case', async () => {
// Scenario: Driver data flow
// Given: A driver exists in the repository with specific statistics
const driverId = 'driver-flow';
driverRepository.addDriver({
id: driverId,
name: 'Flow Driver',
rating: 1500,
rank: 123,
starts: 10,
wins: 3,
podiums: 5,
leagues: 1,
});
// And: The driver has rating 1500, rank 123, 10 starts, 3 wins, 5 podiums
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: The use case should retrieve driver data from repository
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('Flow Driver');
// And: The use case should calculate derived statistics
expect(result.statistics.rating).toBe(1500);
expect(result.statistics.rank).toBe(123);
expect(result.statistics.starts).toBe(10);
expect(result.statistics.wins).toBe(3);
expect(result.statistics.podiums).toBe(5);
// And: The result should contain all driver statistics
expect(result.statistics.leagues).toBe(1);
});
it('should correctly flow race data from repository to use case', async () => {
// Scenario: Race data flow
// Given: Multiple races exist in the repository
const driverId = 'driver-race-flow';
driverRepository.addDriver({
id: driverId,
name: 'Race Flow Driver',
rating: 1200,
rank: 500,
starts: 5,
wins: 1,
podiums: 2,
leagues: 1,
});
// And: Some races are scheduled for the future
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Track A',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000),
},
{
id: 'race-2',
trackName: 'Track B',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000),
},
{
id: 'race-3',
trackName: 'Track C',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000),
},
{
id: 'race-4',
trackName: 'Track D',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
},
]);
// And: Some races are completed
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: The use case should retrieve upcoming races from repository
expect(result.upcomingRaces).toBeDefined();
// And: The use case should limit results to 3 races
expect(result.upcomingRaces).toHaveLength(3);
// And: The use case should sort races by scheduled date
expect(result.upcomingRaces[0].trackName).toBe('Track B'); // 1 day
expect(result.upcomingRaces[1].trackName).toBe('Track C'); // 3 days
expect(result.upcomingRaces[2].trackName).toBe('Track A'); // 5 days
});
it('should correctly flow league data from repository to use case', async () => {
// Scenario: League data flow
// Given: Multiple leagues exist in the repository
const driverId = 'driver-league-flow';
driverRepository.addDriver({
id: driverId,
name: 'League Flow Driver',
rating: 1400,
rank: 200,
starts: 12,
wins: 4,
podiums: 7,
leagues: 2,
});
// And: The driver is participating in some leagues
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'League A',
position: 8,
points: 120,
totalDrivers: 25,
},
{
leagueId: 'league-2',
leagueName: 'League B',
position: 3,
points: 180,
totalDrivers: 15,
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: The use case should retrieve league memberships from repository
expect(result.championshipStandings).toBeDefined();
// And: The use case should calculate standings for each league
expect(result.championshipStandings).toHaveLength(2);
// And: The result should contain league name, position, points, and driver count
expect(result.championshipStandings[0].leagueName).toBe('League A');
expect(result.championshipStandings[0].position).toBe(8);
expect(result.championshipStandings[0].points).toBe(120);
expect(result.championshipStandings[0].totalDrivers).toBe(25);
});
it('should correctly flow activity data from repository to use case', async () => {
// Scenario: Activity data flow
// Given: Multiple activities exist in the repository
const driverId = 'driver-activity-flow';
driverRepository.addDriver({
id: driverId,
name: 'Activity Flow Driver',
rating: 1300,
rank: 300,
starts: 8,
wins: 2,
podiums: 4,
leagues: 1,
});
// And: Activities include race results and other events
activityRepository.addRecentActivity(driverId, [
{
id: 'activity-1',
type: 'race_result',
description: 'Race result 1',
timestamp: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
status: 'success',
},
{
id: 'activity-2',
type: 'achievement',
description: 'Achievement 1',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
status: 'success',
},
{
id: 'activity-3',
type: 'league_invitation',
description: 'Invitation',
timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000),
status: 'info',
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: The use case should retrieve recent activities from repository
expect(result.recentActivity).toBeDefined();
// And: The use case should sort activities by timestamp (newest first)
expect(result.recentActivity).toHaveLength(3);
expect(result.recentActivity[0].description).toBe('Achievement 1'); // 1 day ago
expect(result.recentActivity[1].description).toBe('Invitation'); // 2 days ago
expect(result.recentActivity[2].description).toBe('Race result 1'); // 3 days ago
// And: The result should contain activity type, description, and timestamp
expect(result.recentActivity[0].type).toBe('achievement');
expect(result.recentActivity[0].timestamp).toBeDefined();
});
});
describe('Complete Data Flow: Repository -> Use Case -> Presenter', () => {
it('should complete full data flow for driver with all data', async () => {
// Scenario: Complete data flow
// Given: A driver exists with complete data in repositories
const driverId = 'driver-complete-flow';
driverRepository.addDriver({
id: driverId,
name: 'Complete Flow Driver',
avatar: 'https://example.com/avatar.jpg',
rating: 1600,
rank: 85,
starts: 25,
wins: 8,
podiums: 15,
leagues: 2,
});
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Monza',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000),
},
{
id: 'race-2',
trackName: 'Spa',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000),
},
]);
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'Championship A',
position: 5,
points: 200,
totalDrivers: 30,
},
]);
activityRepository.addRecentActivity(driverId, [
{
id: 'activity-1',
type: 'race_result',
description: 'Finished 2nd at Monza',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
status: 'success',
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called with the result
const dto = dashboardPresenter.present(result);
// Then: The final DTO should contain:
expect(dto.driver.id).toBe(driverId);
expect(dto.driver.name).toBe('Complete Flow Driver');
expect(dto.driver.avatar).toBe('https://example.com/avatar.jpg');
// - Driver statistics (rating, rank, starts, wins, podiums, leagues)
expect(dto.statistics.rating).toBe(1600);
expect(dto.statistics.rank).toBe(85);
expect(dto.statistics.starts).toBe(25);
expect(dto.statistics.wins).toBe(8);
expect(dto.statistics.podiums).toBe(15);
expect(dto.statistics.leagues).toBe(2);
// - Upcoming races (up to 3, sorted by date)
expect(dto.upcomingRaces).toHaveLength(2);
expect(dto.upcomingRaces[0].trackName).toBe('Monza');
// - Championship standings (league name, position, points, driver count)
expect(dto.championshipStandings).toHaveLength(1);
expect(dto.championshipStandings[0].leagueName).toBe('Championship A');
expect(dto.championshipStandings[0].position).toBe(5);
expect(dto.championshipStandings[0].points).toBe(200);
expect(dto.championshipStandings[0].totalDrivers).toBe(30);
// - Recent activity (type, description, timestamp, status)
expect(dto.recentActivity).toHaveLength(1);
expect(dto.recentActivity[0].type).toBe('race_result');
expect(dto.recentActivity[0].description).toBe('Finished 2nd at Monza');
expect(dto.recentActivity[0].status).toBe('success');
// And: All data should be correctly transformed and formatted
expect(dto.upcomingRaces[0].scheduledDate).toBeDefined();
expect(dto.recentActivity[0].timestamp).toBeDefined();
});
it('should complete full data flow for new driver with no data', async () => {
// Scenario: Complete data flow for new driver
// Given: A newly registered driver exists with no data
const driverId = 'driver-new-flow';
driverRepository.addDriver({
id: driverId,
name: 'New Flow Driver',
rating: 1000,
rank: 1000,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called with the result
const dto = dashboardPresenter.present(result);
// Then: The final DTO should contain:
expect(dto.driver.id).toBe(driverId);
expect(dto.driver.name).toBe('New Flow Driver');
// - Basic driver statistics (rating, rank, starts, wins, podiums, leagues)
expect(dto.statistics.rating).toBe(1000);
expect(dto.statistics.rank).toBe(1000);
expect(dto.statistics.starts).toBe(0);
expect(dto.statistics.wins).toBe(0);
expect(dto.statistics.podiums).toBe(0);
expect(dto.statistics.leagues).toBe(0);
// - Empty upcoming races array
expect(dto.upcomingRaces).toHaveLength(0);
// - Empty championship standings array
expect(dto.championshipStandings).toHaveLength(0);
// - Empty recent activity array
expect(dto.recentActivity).toHaveLength(0);
// And: All fields should have appropriate default values
// (already verified by the above checks)
});
it('should maintain data consistency across multiple data flows', async () => {
// Scenario: Data consistency
// Given: A driver exists with data
const driverId = 'driver-consistency';
driverRepository.addDriver({
id: driverId,
name: 'Consistency Driver',
rating: 1350,
rank: 250,
starts: 10,
wins: 3,
podiums: 5,
leagues: 1,
});
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Track A',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000),
},
]);
// When: GetDashboardUseCase.execute() is called multiple times
const result1 = await getDashboardUseCase.execute({ driverId });
const result2 = await getDashboardUseCase.execute({ driverId });
const result3 = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called for each result
const dto1 = dashboardPresenter.present(result1);
const dto2 = dashboardPresenter.present(result2);
const dto3 = dashboardPresenter.present(result3);
// Then: All DTOs should be identical
expect(dto1).toEqual(dto2);
expect(dto2).toEqual(dto3);
// And: Data should remain consistent across calls
expect(dto1.driver.name).toBe('Consistency Driver');
expect(dto1.statistics.rating).toBe(1350);
expect(dto1.upcomingRaces).toHaveLength(1);
});
});
describe('Data Transformation Edge Cases', () => {
it('should handle driver with maximum upcoming races', async () => {
// Scenario: Maximum upcoming races
// Given: A driver exists
const driverId = 'driver-max-races';
driverRepository.addDriver({
id: driverId,
name: 'Max Races Driver',
rating: 1200,
rank: 500,
starts: 5,
wins: 1,
podiums: 2,
leagues: 1,
});
// And: The driver has 10 upcoming races scheduled
raceRepository.addUpcomingRaces(driverId, [
{ id: 'race-1', trackName: 'Track A', carType: 'GT3', scheduledDate: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000) },
{ id: 'race-2', trackName: 'Track B', carType: 'GT3', scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000) },
{ id: 'race-3', trackName: 'Track C', carType: 'GT3', scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000) },
{ id: 'race-4', trackName: 'Track D', carType: 'GT3', scheduledDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000) },
{ id: 'race-5', trackName: 'Track E', carType: 'GT3', scheduledDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) },
{ id: 'race-6', trackName: 'Track F', carType: 'GT3', scheduledDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000) },
{ id: 'race-7', trackName: 'Track G', carType: 'GT3', scheduledDate: new Date(Date.now() + 8 * 24 * 60 * 60 * 1000) },
{ id: 'race-8', trackName: 'Track H', carType: 'GT3', scheduledDate: new Date(Date.now() + 4 * 24 * 60 * 60 * 1000) },
{ id: 'race-9', trackName: 'Track I', carType: 'GT3', scheduledDate: new Date(Date.now() + 6 * 24 * 60 * 60 * 1000) },
{ id: 'race-10', trackName: 'Track J', carType: 'GT3', scheduledDate: new Date(Date.now() + 9 * 24 * 60 * 60 * 1000) },
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called
const dto = dashboardPresenter.present(result);
// Then: The DTO should contain exactly 3 upcoming races
expect(dto.upcomingRaces).toHaveLength(3);
// And: The races should be the 3 earliest scheduled races
expect(dto.upcomingRaces[0].trackName).toBe('Track D'); // 1 day
expect(dto.upcomingRaces[1].trackName).toBe('Track B'); // 2 days
expect(dto.upcomingRaces[2].trackName).toBe('Track F'); // 3 days
});
it('should handle driver with many championship standings', async () => {
// Scenario: Many championship standings
// Given: A driver exists
const driverId = 'driver-many-standings';
driverRepository.addDriver({
id: driverId,
name: 'Many Standings Driver',
rating: 1400,
rank: 200,
starts: 12,
wins: 4,
podiums: 7,
leagues: 5,
});
// And: The driver is participating in 5 championships
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'Championship A',
position: 8,
points: 120,
totalDrivers: 25,
},
{
leagueId: 'league-2',
leagueName: 'Championship B',
position: 3,
points: 180,
totalDrivers: 15,
},
{
leagueId: 'league-3',
leagueName: 'Championship C',
position: 12,
points: 95,
totalDrivers: 30,
},
{
leagueId: 'league-4',
leagueName: 'Championship D',
position: 1,
points: 250,
totalDrivers: 20,
},
{
leagueId: 'league-5',
leagueName: 'Championship E',
position: 5,
points: 160,
totalDrivers: 18,
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called
const dto = dashboardPresenter.present(result);
// Then: The DTO should contain standings for all 5 championships
expect(dto.championshipStandings).toHaveLength(5);
// And: Each standing should have correct data
expect(dto.championshipStandings[0].leagueName).toBe('Championship A');
expect(dto.championshipStandings[0].position).toBe(8);
expect(dto.championshipStandings[0].points).toBe(120);
expect(dto.championshipStandings[0].totalDrivers).toBe(25);
expect(dto.championshipStandings[1].leagueName).toBe('Championship B');
expect(dto.championshipStandings[1].position).toBe(3);
expect(dto.championshipStandings[1].points).toBe(180);
expect(dto.championshipStandings[1].totalDrivers).toBe(15);
expect(dto.championshipStandings[2].leagueName).toBe('Championship C');
expect(dto.championshipStandings[2].position).toBe(12);
expect(dto.championshipStandings[2].points).toBe(95);
expect(dto.championshipStandings[2].totalDrivers).toBe(30);
expect(dto.championshipStandings[3].leagueName).toBe('Championship D');
expect(dto.championshipStandings[3].position).toBe(1);
expect(dto.championshipStandings[3].points).toBe(250);
expect(dto.championshipStandings[3].totalDrivers).toBe(20);
expect(dto.championshipStandings[4].leagueName).toBe('Championship E');
expect(dto.championshipStandings[4].position).toBe(5);
expect(dto.championshipStandings[4].points).toBe(160);
expect(dto.championshipStandings[4].totalDrivers).toBe(18);
});
it('should handle driver with many recent activities', async () => {
// Scenario: Many recent activities
// Given: A driver exists
const driverId = 'driver-many-activities';
driverRepository.addDriver({
id: driverId,
name: 'Many Activities Driver',
rating: 1300,
rank: 300,
starts: 8,
wins: 2,
podiums: 4,
leagues: 1,
});
// And: The driver has 20 recent activities
const activities = [];
for (let i = 0; i < 20; i++) {
activities.push({
id: `activity-${i}`,
type: i % 2 === 0 ? 'race_result' : 'achievement',
description: `Activity ${i}`,
timestamp: new Date(Date.now() - i * 60 * 60 * 1000), // each activity 1 hour apart
status: i % 3 === 0 ? 'success' : i % 3 === 1 ? 'info' : 'warning',
});
}
activityRepository.addRecentActivity(driverId, activities);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called
const dto = dashboardPresenter.present(result);
// Then: The DTO should contain all 20 activities
expect(dto.recentActivity).toHaveLength(20);
// And: Activities should be sorted by timestamp (newest first)
for (let i = 0; i < 20; i++) {
expect(dto.recentActivity[i].description).toBe(`Activity ${i}`);
expect(dto.recentActivity[i].timestamp).toBeDefined();
}
});
it('should handle driver with mixed race statuses', async () => {
// Scenario: Mixed race statuses
// Given: A driver exists with statistics reflecting completed races
const driverId = 'driver-mixed-statuses';
driverRepository.addDriver({
id: driverId,
name: 'Mixed Statuses Driver',
rating: 1500,
rank: 100,
starts: 5, // only completed races count
wins: 2,
podiums: 3,
leagues: 1,
});
// And: The driver has scheduled races (upcoming)
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-scheduled-1',
trackName: 'Track A',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000),
},
{
id: 'race-scheduled-2',
trackName: 'Track B',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000),
},
]);
// Note: Cancelled races are not stored in the repository, so they won't appear
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// And: DashboardPresenter.present() is called
const dto = dashboardPresenter.present(result);
// Then: Driver statistics should only count completed races
expect(dto.statistics.starts).toBe(5);
expect(dto.statistics.wins).toBe(2);
expect(dto.statistics.podiums).toBe(3);
// And: Upcoming races should only include scheduled races
expect(dto.upcomingRaces).toHaveLength(2);
expect(dto.upcomingRaces[0].trackName).toBe('Track A');
expect(dto.upcomingRaces[1].trackName).toBe('Track B');
// And: Cancelled races should not appear in any section
// (they are not in upcoming races, and we didn't add them to activities)
expect(dto.upcomingRaces.some(r => r.trackName.includes('Cancelled'))).toBe(false);
});
});
});

View File

@@ -1,870 +0,0 @@
/**
* Integration Test: Dashboard Error Handling
*
* Tests error handling and edge cases at the Use Case level:
* - Repository errors (driver not found, data access errors)
* - Validation errors (invalid driver ID, invalid parameters)
* - Business logic errors (permission denied, data inconsistencies)
*
* Focus: Error orchestration and handling, NOT UI error messages
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } 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 { InMemoryActivityRepository } from '../../../adapters/activity/persistence/inmemory/InMemoryActivityRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { GetDashboardUseCase } from '../../../core/dashboard/application/use-cases/GetDashboardUseCase';
import { DriverNotFoundError } from '../../../core/dashboard/domain/errors/DriverNotFoundError';
import { ValidationError } from '../../../core/shared/errors/ValidationError';
describe('Dashboard Error Handling Integration', () => {
let driverRepository: InMemoryDriverRepository;
let raceRepository: InMemoryRaceRepository;
let leagueRepository: InMemoryLeagueRepository;
let activityRepository: InMemoryActivityRepository;
let eventPublisher: InMemoryEventPublisher;
let getDashboardUseCase: GetDashboardUseCase;
const loggerMock = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
beforeAll(() => {
driverRepository = new InMemoryDriverRepository();
raceRepository = new InMemoryRaceRepository();
leagueRepository = new InMemoryLeagueRepository();
activityRepository = new InMemoryActivityRepository();
eventPublisher = new InMemoryEventPublisher();
getDashboardUseCase = new GetDashboardUseCase({
driverRepository,
raceRepository,
leagueRepository,
activityRepository,
eventPublisher,
logger: loggerMock,
});
});
beforeEach(() => {
driverRepository.clear();
raceRepository.clear();
leagueRepository.clear();
activityRepository.clear();
eventPublisher.clear();
vi.clearAllMocks();
});
describe('Driver Not Found Errors', () => {
it('should throw DriverNotFoundError when driver does not exist', async () => {
// Scenario: Non-existent driver
// Given: No driver exists with ID "non-existent-driver-id"
const driverId = 'non-existent-driver-id';
// When: GetDashboardUseCase.execute() is called with "non-existent-driver-id"
// Then: Should throw DriverNotFoundError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(DriverNotFoundError);
// And: Error message should indicate driver not found
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(`Driver with ID "${driverId}" not found`);
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should throw DriverNotFoundError when driver ID is valid but not found', async () => {
// Scenario: Valid ID but no driver
// Given: A valid UUID format driver ID
const driverId = '550e8400-e29b-41d4-a716-446655440000';
// And: No driver exists with that ID
// When: GetDashboardUseCase.execute() is called with the ID
// Then: Should throw DriverNotFoundError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(DriverNotFoundError);
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should not throw error when driver exists', async () => {
// Scenario: Existing driver
// Given: A driver exists with ID "existing-driver-id"
const driverId = 'existing-driver-id';
driverRepository.addDriver({
id: driverId,
name: 'Existing Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// When: GetDashboardUseCase.execute() is called with "existing-driver-id"
// Then: Should NOT throw DriverNotFoundError
const result = await getDashboardUseCase.execute({ driverId });
// And: Should return dashboard data successfully
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
});
});
describe('Validation Errors', () => {
it('should throw ValidationError when driver ID is empty string', async () => {
// Scenario: Empty driver ID
// Given: An empty string as driver ID
const driverId = '';
// When: GetDashboardUseCase.execute() is called with empty string
// Then: Should throw ValidationError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should indicate invalid driver ID
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver ID cannot be empty');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should throw ValidationError when driver ID is null', async () => {
// Scenario: Null driver ID
// Given: null as driver ID
const driverId = null as any;
// When: GetDashboardUseCase.execute() is called with null
// Then: Should throw ValidationError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should indicate invalid driver ID
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver ID must be a valid string');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should throw ValidationError when driver ID is undefined', async () => {
// Scenario: Undefined driver ID
// Given: undefined as driver ID
const driverId = undefined as any;
// When: GetDashboardUseCase.execute() is called with undefined
// Then: Should throw ValidationError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should indicate invalid driver ID
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver ID must be a valid string');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should throw ValidationError when driver ID is not a string', async () => {
// Scenario: Invalid type driver ID
// Given: A number as driver ID
const driverId = 123 as any;
// When: GetDashboardUseCase.execute() is called with number
// Then: Should throw ValidationError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should indicate invalid driver ID type
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver ID must be a valid string');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should throw ValidationError when driver ID is malformed', async () => {
// Scenario: Malformed driver ID
// Given: A malformed string as driver ID (e.g., " ")
const driverId = ' ';
// When: GetDashboardUseCase.execute() is called with malformed ID
// Then: Should throw ValidationError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should indicate invalid driver ID format
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver ID cannot be empty');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
});
describe('Repository Error Handling', () => {
it('should handle driver repository query error', async () => {
// Scenario: Driver repository error
// Given: A driver exists
const driverId = 'driver-repo-error';
// And: DriverRepository throws an error during query
const spy = vi.spyOn(driverRepository, 'findDriverById').mockRejectedValue(new Error('Driver repo failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should propagate the error appropriately
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver repo failed');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
it('should handle race repository query error', async () => {
// Scenario: Race repository error
// Given: A driver exists
const driverId = 'driver-race-error';
driverRepository.addDriver({
id: driverId,
name: 'Race Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: RaceRepository throws an error during query
const spy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockRejectedValue(new Error('Race repo failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should propagate the error appropriately
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Race repo failed');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
it('should handle league repository query error', async () => {
// Scenario: League repository error
// Given: A driver exists
const driverId = 'driver-league-error';
driverRepository.addDriver({
id: driverId,
name: 'League Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: LeagueRepository throws an error during query
const spy = vi.spyOn(leagueRepository, 'getLeagueStandings').mockRejectedValue(new Error('League repo failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should propagate the error appropriately
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('League repo failed');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
it('should handle activity repository query error', async () => {
// Scenario: Activity repository error
// Given: A driver exists
const driverId = 'driver-activity-error';
driverRepository.addDriver({
id: driverId,
name: 'Activity Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: ActivityRepository throws an error during query
const spy = vi.spyOn(activityRepository, 'getRecentActivity').mockRejectedValue(new Error('Activity repo failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should propagate the error appropriately
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Activity repo failed');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
it('should handle multiple repository errors gracefully', async () => {
// Scenario: Multiple repository errors
// Given: A driver exists
const driverId = 'driver-multi-error';
driverRepository.addDriver({
id: driverId,
name: 'Multi Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: Multiple repositories throw errors
const spy1 = vi.spyOn(raceRepository, 'getUpcomingRaces').mockRejectedValue(new Error('Race repo failed'));
const spy2 = vi.spyOn(leagueRepository, 'getLeagueStandings').mockRejectedValue(new Error('League repo failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should handle errors appropriately (Promise.all will reject with the first error)
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(/repo failed/);
// And: Should not crash the application
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy1.mockRestore();
spy2.mockRestore();
});
});
describe('Event Publisher Error Handling', () => {
it('should handle event publisher error gracefully', async () => {
// Scenario: Event publisher error
// Given: A driver exists with data
const driverId = 'driver-pub-error';
driverRepository.addDriver({
id: driverId,
name: 'Pub Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: EventPublisher throws an error during emit
const spy = vi.spyOn(eventPublisher, 'publishDashboardAccessed').mockRejectedValue(new Error('Publisher failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should complete the use case execution (if we decide to swallow publisher errors)
// Note: Current implementation in GetDashboardUseCase.ts:92-96 does NOT catch publisher errors.
// If it's intended to be critical, it should throw. If not, it should be caught.
// Given the TODO "should handle event publisher error gracefully", it implies it shouldn't fail the whole request.
// For now, let's see if it fails (TDD).
const result = await getDashboardUseCase.execute({ driverId });
// Then: Should complete the use case execution
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
spy.mockRestore();
});
it('should not fail when event publisher is unavailable', async () => {
// Scenario: Event publisher unavailable
// Given: A driver exists with data
const driverId = 'driver-pub-unavail';
driverRepository.addDriver({
id: driverId,
name: 'Pub Unavail Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: EventPublisher is configured to fail
const spy = vi.spyOn(eventPublisher, 'publishDashboardAccessed').mockRejectedValue(new Error('Service Unavailable'));
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: Should complete the use case execution
// And: Dashboard data should still be returned
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
spy.mockRestore();
});
});
describe('Business Logic Error Handling', () => {
it('should handle driver with corrupted data gracefully', async () => {
// Scenario: Corrupted driver data
// Given: A driver exists with corrupted/invalid data
const driverId = 'corrupted-driver';
driverRepository.addDriver({
id: driverId,
name: 'Corrupted Driver',
rating: null as any, // Corrupted: null rating
rank: 0,
starts: -1, // Corrupted: negative starts
wins: 0,
podiums: 0,
leagues: 0,
});
// When: GetDashboardUseCase.execute() is called
// Then: Should handle the corrupted data gracefully
// And: Should not crash the application
// And: Should return valid dashboard data where possible
const result = await getDashboardUseCase.execute({ driverId });
// Should return dashboard with valid data where possible
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('Corrupted Driver');
// Statistics should handle null/invalid values gracefully
expect(result.statistics.rating).toBeNull();
expect(result.statistics.rank).toBe(0);
expect(result.statistics.starts).toBe(-1); // Should preserve the value
});
it('should handle race data inconsistencies', async () => {
// Scenario: Race data inconsistencies
// Given: A driver exists
const driverId = 'driver-with-inconsistent-races';
driverRepository.addDriver({
id: driverId,
name: 'Race Inconsistency Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: Race data has inconsistencies (e.g., scheduled date in past)
const raceRepositorySpy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockResolvedValue([
{
id: 'past-race',
trackName: 'Past Race',
carType: 'Formula 1',
scheduledDate: new Date(Date.now() - 86400000), // Past date
timeUntilRace: 'Race started',
},
{
id: 'future-race',
trackName: 'Future Race',
carType: 'Formula 1',
scheduledDate: new Date(Date.now() + 86400000), // Future date
timeUntilRace: '1 day',
},
]);
// When: GetDashboardUseCase.execute() is called
// Then: Should handle inconsistencies gracefully
// And: Should filter out invalid races
// And: Should return valid dashboard data
const result = await getDashboardUseCase.execute({ driverId });
// Should return dashboard with valid data
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
// Should include the future race
expect(result.upcomingRaces).toHaveLength(1);
expect(result.upcomingRaces[0].trackName).toBe('Future Race');
raceRepositorySpy.mockRestore();
});
it('should handle league data inconsistencies', async () => {
// Scenario: League data inconsistencies
// Given: A driver exists
const driverId = 'driver-with-inconsistent-leagues';
driverRepository.addDriver({
id: driverId,
name: 'League Inconsistency Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: League data has inconsistencies (e.g., missing required fields)
const leagueRepositorySpy = vi.spyOn(leagueRepository, 'getLeagueStandings').mockResolvedValue([
{
leagueId: 'valid-league',
leagueName: 'Valid League',
position: 1,
points: 100,
totalDrivers: 10,
},
{
leagueId: 'invalid-league',
leagueName: 'Invalid League',
position: null as any, // Missing position
points: 50,
totalDrivers: 5,
},
]);
// When: GetDashboardUseCase.execute() is called
// Then: Should handle inconsistencies gracefully
// And: Should filter out invalid leagues
// And: Should return valid dashboard data
const result = await getDashboardUseCase.execute({ driverId });
// Should return dashboard with valid data
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
// Should include the valid league
expect(result.championshipStandings).toHaveLength(1);
expect(result.championshipStandings[0].leagueName).toBe('Valid League');
leagueRepositorySpy.mockRestore();
});
it('should handle activity data inconsistencies', async () => {
// Scenario: Activity data inconsistencies
// Given: A driver exists
const driverId = 'driver-with-inconsistent-activity';
driverRepository.addDriver({
id: driverId,
name: 'Activity Inconsistency Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: Activity data has inconsistencies (e.g., missing timestamp)
const activityRepositorySpy = vi.spyOn(activityRepository, 'getRecentActivity').mockResolvedValue([
{
id: 'valid-activity',
type: 'race_result',
description: 'Valid activity',
timestamp: new Date(),
status: 'success',
},
{
id: 'invalid-activity',
type: 'race_result',
description: 'Invalid activity',
timestamp: null as any, // Missing timestamp
status: 'success',
},
]);
// When: GetDashboardUseCase.execute() is called
// Then: Should handle inconsistencies gracefully
// And: Should filter out invalid activities
// And: Should return valid dashboard data
const result = await getDashboardUseCase.execute({ driverId });
// Should return dashboard with valid data
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
// Should include the valid activity
expect(result.recentActivity).toHaveLength(1);
expect(result.recentActivity[0].description).toBe('Valid activity');
activityRepositorySpy.mockRestore();
});
});
describe('Error Recovery and Fallbacks', () => {
it('should return partial data when one repository fails', async () => {
// Scenario: Partial data recovery
// Given: A driver exists
const driverId = 'driver-partial-data';
driverRepository.addDriver({
id: driverId,
name: 'Partial Data Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: RaceRepository fails but other repositories succeed
const raceRepositorySpy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockRejectedValue(new Error('Race repo failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should propagate the error (not recover partial data)
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Race repo failed');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
raceRepositorySpy.mockRestore();
});
it('should return empty sections when data is unavailable', async () => {
// Scenario: Empty sections fallback
// Given: A driver exists
const driverId = 'driver-empty-sections';
driverRepository.addDriver({
id: driverId,
name: 'Empty Sections Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: All repositories return empty results
const raceRepositorySpy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockResolvedValue([]);
const leagueRepositorySpy = vi.spyOn(leagueRepository, 'getLeagueStandings').mockResolvedValue([]);
const activityRepositorySpy = vi.spyOn(activityRepository, 'getRecentActivity').mockResolvedValue([]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: Should return dashboard with empty sections
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(result.upcomingRaces).toHaveLength(0);
expect(result.championshipStandings).toHaveLength(0);
expect(result.recentActivity).toHaveLength(0);
// And: Should include basic driver statistics
expect(result.statistics.rating).toBe(1000);
expect(result.statistics.rank).toBe(1);
raceRepositorySpy.mockRestore();
leagueRepositorySpy.mockRestore();
activityRepositorySpy.mockRestore();
});
it('should handle timeout scenarios gracefully', async () => {
// Scenario: Timeout handling
// Given: A driver exists
const driverId = 'driver-timeout';
driverRepository.addDriver({
id: driverId,
name: 'Timeout Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: Repository queries take too long
const raceRepositorySpy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockImplementation(() => {
return new Promise((resolve) => {
setTimeout(() => resolve([]), 10000); // 10 second timeout
});
});
// When: GetDashboardUseCase.execute() is called
// Then: Should handle timeout gracefully
// Note: The current implementation doesn't have timeout handling
// This test documents the expected behavior
const result = await getDashboardUseCase.execute({ driverId });
// Should return dashboard data (timeout is handled by the caller)
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
raceRepositorySpy.mockRestore();
});
});
describe('Error Propagation', () => {
it('should propagate DriverNotFoundError to caller', async () => {
// Scenario: Error propagation
// Given: No driver exists
const driverId = 'non-existent-driver-prop';
// When: GetDashboardUseCase.execute() is called
// Then: DriverNotFoundError should be thrown
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(DriverNotFoundError);
// And: Error should be catchable by caller
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(DriverNotFoundError);
// And: Error should have appropriate message
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(`Driver with ID "${driverId}" not found`);
});
it('should propagate ValidationError to caller', async () => {
// Scenario: Validation error propagation
// Given: Invalid driver ID
const driverId = '';
// When: GetDashboardUseCase.execute() is called
// Then: ValidationError should be thrown
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should be catchable by caller
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: Error should have appropriate message
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver ID cannot be empty');
});
it('should propagate repository errors to caller', async () => {
// Scenario: Repository error propagation
// Given: A driver exists
const driverId = 'driver-repo-error-prop';
driverRepository.addDriver({
id: driverId,
name: 'Repo Error Prop Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: Repository throws error
const spy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockRejectedValue(new Error('Repository error'));
// When: GetDashboardUseCase.execute() is called
// Then: Repository error should be propagated
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Repository error');
// And: Error should be catchable by caller
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Repository error');
spy.mockRestore();
});
});
describe('Error Logging and Observability', () => {
it('should log errors appropriately', async () => {
// Scenario: Error logging
// Given: A driver exists
const driverId = 'driver-logging-error';
driverRepository.addDriver({
id: driverId,
name: 'Logging Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: An error occurs during execution
const error = new Error('Logging test error');
const spy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockRejectedValue(error);
// When: GetDashboardUseCase.execute() is called
// Then: Error should be logged appropriately
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Logging test error');
// And: Logger should have been called with the error
expect(loggerMock.error).toHaveBeenCalledWith(
'Failed to fetch dashboard data from repositories',
error,
expect.objectContaining({ driverId })
);
spy.mockRestore();
});
it('should log event publisher errors', async () => {
// Scenario: Event publisher error logging
// Given: A driver exists
const driverId = 'driver-pub-log-error';
driverRepository.addDriver({
id: driverId,
name: 'Pub Log Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: EventPublisher throws an error
const error = new Error('Publisher failed');
const spy = vi.spyOn(eventPublisher, 'publishDashboardAccessed').mockRejectedValue(error);
// When: GetDashboardUseCase.execute() is called
await getDashboardUseCase.execute({ driverId });
// Then: Logger should have been called
expect(loggerMock.error).toHaveBeenCalledWith(
'Failed to publish dashboard accessed event',
error,
expect.objectContaining({ driverId })
);
spy.mockRestore();
});
it('should include context in error messages', async () => {
// Scenario: Error context
// Given: A driver exists
const driverId = 'driver-context-error';
driverRepository.addDriver({
id: driverId,
name: 'Context Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: An error occurs during execution
const spy = vi.spyOn(raceRepository, 'getUpcomingRaces').mockRejectedValue(new Error('Context test error'));
// When: GetDashboardUseCase.execute() is called
// Then: Error message should include driver ID
// Note: The current implementation doesn't include driver ID in error messages
// This test documents the expected behavior
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Context test error');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
});
});

View File

@@ -1,852 +0,0 @@
/**
* Integration Test: Dashboard Use Case Orchestration
*
* Tests the orchestration logic of dashboard-related Use Cases:
* - GetDashboardUseCase: Retrieves driver statistics, upcoming races, standings, and activity
* - Validates that Use Cases correctly interact with their Ports (Repositories, Event Publishers)
* - Uses In-Memory adapters for fast, deterministic testing
*
* Focus: Business logic orchestration, NOT UI rendering
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } 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 { InMemoryActivityRepository } from '../../../adapters/activity/persistence/inmemory/InMemoryActivityRepository';
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
import { GetDashboardUseCase } from '../../../core/dashboard/application/use-cases/GetDashboardUseCase';
import { DashboardQuery } from '../../../core/dashboard/application/ports/DashboardQuery';
import { DriverNotFoundError } from '../../../core/dashboard/domain/errors/DriverNotFoundError';
import { ValidationError } from '../../../core/shared/errors/ValidationError';
describe('Dashboard Use Case Orchestration', () => {
let driverRepository: InMemoryDriverRepository;
let raceRepository: InMemoryRaceRepository;
let leagueRepository: InMemoryLeagueRepository;
let activityRepository: InMemoryActivityRepository;
let eventPublisher: InMemoryEventPublisher;
let getDashboardUseCase: GetDashboardUseCase;
beforeAll(() => {
driverRepository = new InMemoryDriverRepository();
raceRepository = new InMemoryRaceRepository();
leagueRepository = new InMemoryLeagueRepository();
activityRepository = new InMemoryActivityRepository();
eventPublisher = new InMemoryEventPublisher();
getDashboardUseCase = new GetDashboardUseCase({
driverRepository,
raceRepository,
leagueRepository,
activityRepository,
eventPublisher,
});
});
beforeEach(() => {
driverRepository.clear();
raceRepository.clear();
leagueRepository.clear();
activityRepository.clear();
eventPublisher.clear();
});
describe('GetDashboardUseCase - Success Path', () => {
it('should retrieve complete dashboard data for a driver with all data', async () => {
// Scenario: Driver with complete data
// Given: A driver exists with statistics (rating, rank, starts, wins, podiums)
const driverId = 'driver-123';
driverRepository.addDriver({
id: driverId,
name: 'John Doe',
avatar: 'https://example.com/avatar.jpg',
rating: 1500,
rank: 123,
starts: 10,
wins: 3,
podiums: 5,
leagues: 2,
});
// And: The driver has upcoming races scheduled
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Monza',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000), // 2 days from now
},
{
id: 'race-2',
trackName: 'Spa',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000), // 5 days from now
},
{
id: 'race-3',
trackName: 'Nürburgring',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000), // 1 day from now
},
{
id: 'race-4',
trackName: 'Silverstone',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days from now
},
{
id: 'race-5',
trackName: 'Imola',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now
},
]);
// And: The driver is participating in active championships
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'GT3 Championship',
position: 5,
points: 150,
totalDrivers: 20,
},
{
leagueId: 'league-2',
leagueName: 'Endurance Series',
position: 12,
points: 85,
totalDrivers: 15,
},
]);
// And: The driver has recent activity (race results, events)
activityRepository.addRecentActivity(driverId, [
{
id: 'activity-1',
type: 'race_result',
description: 'Finished 3rd at Monza',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), // 1 day ago
status: 'success',
},
{
id: 'activity-2',
type: 'league_invitation',
description: 'Invited to League XYZ',
timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), // 2 days ago
status: 'info',
},
{
id: 'activity-3',
type: 'achievement',
description: 'Reached 1500 rating',
timestamp: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), // 3 days ago
status: 'success',
},
]);
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain all dashboard sections
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('John Doe');
expect(result.driver.avatar).toBe('https://example.com/avatar.jpg');
// And: Driver statistics should be correctly calculated
expect(result.statistics.rating).toBe(1500);
expect(result.statistics.rank).toBe(123);
expect(result.statistics.starts).toBe(10);
expect(result.statistics.wins).toBe(3);
expect(result.statistics.podiums).toBe(5);
expect(result.statistics.leagues).toBe(2);
// And: Upcoming races should be limited to 3
expect(result.upcomingRaces).toHaveLength(3);
// And: The races should be sorted by scheduled date (earliest first)
expect(result.upcomingRaces[0].trackName).toBe('Nürburgring'); // 1 day
expect(result.upcomingRaces[1].trackName).toBe('Monza'); // 2 days
expect(result.upcomingRaces[2].trackName).toBe('Imola'); // 3 days
// And: Championship standings should include league info
expect(result.championshipStandings).toHaveLength(2);
expect(result.championshipStandings[0].leagueName).toBe('GT3 Championship');
expect(result.championshipStandings[0].position).toBe(5);
expect(result.championshipStandings[0].points).toBe(150);
expect(result.championshipStandings[0].totalDrivers).toBe(20);
// And: Recent activity should be sorted by timestamp (newest first)
expect(result.recentActivity).toHaveLength(3);
expect(result.recentActivity[0].description).toBe('Finished 3rd at Monza');
expect(result.recentActivity[0].status).toBe('success');
expect(result.recentActivity[1].description).toBe('Invited to League XYZ');
expect(result.recentActivity[2].description).toBe('Reached 1500 rating');
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should retrieve dashboard data for a new driver with no history', async () => {
// Scenario: New driver with minimal data
// Given: A newly registered driver exists
const driverId = 'new-driver-456';
driverRepository.addDriver({
id: driverId,
name: 'New Driver',
rating: 1000,
rank: 1000,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: The driver has no race history
// And: The driver has no upcoming races
// And: The driver is not in any championships
// And: The driver has no recent activity
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain basic driver statistics
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('New Driver');
expect(result.statistics.rating).toBe(1000);
expect(result.statistics.rank).toBe(1000);
expect(result.statistics.starts).toBe(0);
expect(result.statistics.wins).toBe(0);
expect(result.statistics.podiums).toBe(0);
expect(result.statistics.leagues).toBe(0);
// And: Upcoming races section should be empty
expect(result.upcomingRaces).toHaveLength(0);
// And: Championship standings section should be empty
expect(result.championshipStandings).toHaveLength(0);
// And: Recent activity section should be empty
expect(result.recentActivity).toHaveLength(0);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should retrieve dashboard data with upcoming races limited to 3', async () => {
// Scenario: Driver with many upcoming races
// Given: A driver exists
const driverId = 'driver-789';
driverRepository.addDriver({
id: driverId,
name: 'Race Driver',
rating: 1200,
rank: 500,
starts: 5,
wins: 1,
podiums: 2,
leagues: 1,
});
// And: The driver has 5 upcoming races scheduled
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Track A',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), // 10 days
},
{
id: 'race-2',
trackName: 'Track B',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000), // 2 days
},
{
id: 'race-3',
trackName: 'Track C',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000), // 5 days
},
{
id: 'race-4',
trackName: 'Track D',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000), // 1 day
},
{
id: 'race-5',
trackName: 'Track E',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
},
]);
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain only 3 upcoming races
expect(result.upcomingRaces).toHaveLength(3);
// And: The races should be sorted by scheduled date (earliest first)
expect(result.upcomingRaces[0].trackName).toBe('Track D'); // 1 day
expect(result.upcomingRaces[1].trackName).toBe('Track B'); // 2 days
expect(result.upcomingRaces[2].trackName).toBe('Track C'); // 5 days
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should retrieve dashboard data with championship standings for multiple leagues', async () => {
// Scenario: Driver in multiple championships
// Given: A driver exists
const driverId = 'driver-champ';
driverRepository.addDriver({
id: driverId,
name: 'Champion Driver',
rating: 1800,
rank: 50,
starts: 20,
wins: 8,
podiums: 15,
leagues: 3,
});
// And: The driver is participating in 3 active championships
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'Championship A',
position: 3,
points: 200,
totalDrivers: 25,
},
{
leagueId: 'league-2',
leagueName: 'Championship B',
position: 8,
points: 120,
totalDrivers: 18,
},
{
leagueId: 'league-3',
leagueName: 'Championship C',
position: 15,
points: 60,
totalDrivers: 30,
},
]);
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain standings for all 3 leagues
expect(result.championshipStandings).toHaveLength(3);
// And: Each league should show position, points, and total drivers
expect(result.championshipStandings[0].leagueName).toBe('Championship A');
expect(result.championshipStandings[0].position).toBe(3);
expect(result.championshipStandings[0].points).toBe(200);
expect(result.championshipStandings[0].totalDrivers).toBe(25);
expect(result.championshipStandings[1].leagueName).toBe('Championship B');
expect(result.championshipStandings[1].position).toBe(8);
expect(result.championshipStandings[1].points).toBe(120);
expect(result.championshipStandings[1].totalDrivers).toBe(18);
expect(result.championshipStandings[2].leagueName).toBe('Championship C');
expect(result.championshipStandings[2].position).toBe(15);
expect(result.championshipStandings[2].points).toBe(60);
expect(result.championshipStandings[2].totalDrivers).toBe(30);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should retrieve dashboard data with recent activity sorted by timestamp', async () => {
// Scenario: Driver with multiple recent activities
// Given: A driver exists
const driverId = 'driver-activity';
driverRepository.addDriver({
id: driverId,
name: 'Active Driver',
rating: 1400,
rank: 200,
starts: 15,
wins: 4,
podiums: 8,
leagues: 1,
});
// And: The driver has 5 recent activities (race results, events)
activityRepository.addRecentActivity(driverId, [
{
id: 'activity-1',
type: 'race_result',
description: 'Race 1',
timestamp: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000), // 5 days ago
status: 'success',
},
{
id: 'activity-2',
type: 'race_result',
description: 'Race 2',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), // 1 day ago
status: 'success',
},
{
id: 'activity-3',
type: 'achievement',
description: 'Achievement 1',
timestamp: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), // 3 days ago
status: 'success',
},
{
id: 'activity-4',
type: 'league_invitation',
description: 'Invitation',
timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), // 2 days ago
status: 'info',
},
{
id: 'activity-5',
type: 'other',
description: 'Other event',
timestamp: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000), // 4 days ago
status: 'info',
},
]);
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain all activities
expect(result.recentActivity).toHaveLength(5);
// And: Activities should be sorted by timestamp (newest first)
expect(result.recentActivity[0].description).toBe('Race 2'); // 1 day ago
expect(result.recentActivity[1].description).toBe('Invitation'); // 2 days ago
expect(result.recentActivity[2].description).toBe('Achievement 1'); // 3 days ago
expect(result.recentActivity[3].description).toBe('Other event'); // 4 days ago
expect(result.recentActivity[4].description).toBe('Race 1'); // 5 days ago
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
});
describe('GetDashboardUseCase - Edge Cases', () => {
it('should handle driver with no upcoming races but has completed races', async () => {
// Scenario: Driver with completed races but no upcoming races
// Given: A driver exists
const driverId = 'driver-no-upcoming';
driverRepository.addDriver({
id: driverId,
name: 'Past Driver',
rating: 1300,
rank: 300,
starts: 8,
wins: 2,
podiums: 4,
leagues: 1,
});
// And: The driver has completed races in the past
// And: The driver has no upcoming races scheduled
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain driver statistics from completed races
expect(result.statistics.starts).toBe(8);
expect(result.statistics.wins).toBe(2);
expect(result.statistics.podiums).toBe(4);
// And: Upcoming races section should be empty
expect(result.upcomingRaces).toHaveLength(0);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should handle driver with upcoming races but no completed races', async () => {
// Scenario: Driver with upcoming races but no completed races
// Given: A driver exists
const driverId = 'driver-no-completed';
driverRepository.addDriver({
id: driverId,
name: 'New Racer',
rating: 1100,
rank: 800,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: The driver has upcoming races scheduled
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Track A',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000),
},
]);
// And: The driver has no completed races
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain upcoming races
expect(result.upcomingRaces).toHaveLength(1);
expect(result.upcomingRaces[0].trackName).toBe('Track A');
// And: Driver statistics should show zeros for wins, podiums, etc.
expect(result.statistics.starts).toBe(0);
expect(result.statistics.wins).toBe(0);
expect(result.statistics.podiums).toBe(0);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should handle driver with championship standings but no recent activity', async () => {
// Scenario: Driver in championships but no recent activity
// Given: A driver exists
const driverId = 'driver-champ-only';
driverRepository.addDriver({
id: driverId,
name: 'Champ Only',
rating: 1600,
rank: 100,
starts: 12,
wins: 5,
podiums: 8,
leagues: 2,
});
// And: The driver is participating in active championships
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'Championship A',
position: 10,
points: 100,
totalDrivers: 20,
},
]);
// And: The driver has no recent activity
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain championship standings
expect(result.championshipStandings).toHaveLength(1);
expect(result.championshipStandings[0].leagueName).toBe('Championship A');
// And: Recent activity section should be empty
expect(result.recentActivity).toHaveLength(0);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should handle driver with recent activity but no championship standings', async () => {
// Scenario: Driver with recent activity but not in championships
// Given: A driver exists
const driverId = 'driver-activity-only';
driverRepository.addDriver({
id: driverId,
name: 'Activity Only',
rating: 1250,
rank: 400,
starts: 6,
wins: 1,
podiums: 2,
leagues: 0,
});
// And: The driver has recent activity
activityRepository.addRecentActivity(driverId, [
{
id: 'activity-1',
type: 'race_result',
description: 'Finished 5th',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
status: 'success',
},
]);
// And: The driver is not participating in any championships
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain recent activity
expect(result.recentActivity).toHaveLength(1);
expect(result.recentActivity[0].description).toBe('Finished 5th');
// And: Championship standings section should be empty
expect(result.championshipStandings).toHaveLength(0);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should handle driver with no data at all', async () => {
// Scenario: Driver with absolutely no data
// Given: A driver exists
const driverId = 'driver-no-data';
driverRepository.addDriver({
id: driverId,
name: 'No Data Driver',
rating: 1000,
rank: 1000,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: The driver has no statistics
// And: The driver has no upcoming races
// And: The driver has no championship standings
// And: The driver has no recent activity
// When: GetDashboardUseCase.execute() is called with driver ID
const result = await getDashboardUseCase.execute({ driverId });
// Then: The result should contain basic driver info
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('No Data Driver');
// And: All sections should be empty or show default values
expect(result.upcomingRaces).toHaveLength(0);
expect(result.championshipStandings).toHaveLength(0);
expect(result.recentActivity).toHaveLength(0);
expect(result.statistics.starts).toBe(0);
// And: EventPublisher should emit DashboardAccessedEvent
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
});
describe('GetDashboardUseCase - Error Handling', () => {
it('should throw error when driver does not exist', async () => {
// Scenario: Non-existent driver
// Given: No driver exists with the given ID
const driverId = 'non-existent';
// When: GetDashboardUseCase.execute() is called with non-existent driver ID
// Then: Should throw DriverNotFoundError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(DriverNotFoundError);
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should throw error when driver ID is invalid', async () => {
// Scenario: Invalid driver ID
// Given: An invalid driver ID (e.g., empty string)
const driverId = '';
// When: GetDashboardUseCase.execute() is called with invalid driver ID
// Then: Should throw ValidationError
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
it('should handle repository errors gracefully', async () => {
// Scenario: Repository throws error
// Given: A driver exists
const driverId = 'driver-repo-error';
driverRepository.addDriver({
id: driverId,
name: 'Repo Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: DriverRepository throws an error during query
// (We use a spy to simulate error since InMemory repo doesn't fail by default)
const spy = vi.spyOn(driverRepository, 'findDriverById').mockRejectedValue(new Error('Database connection failed'));
// When: GetDashboardUseCase.execute() is called
// Then: Should propagate the error appropriately
await expect(getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Database connection failed');
// And: EventPublisher should NOT emit any events
expect(eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
});
describe('Dashboard Data Orchestration', () => {
it('should correctly calculate driver statistics from race results', async () => {
// Scenario: Driver statistics calculation
// Given: A driver exists
const driverId = 'driver-stats-calc';
driverRepository.addDriver({
id: driverId,
name: 'Stats Driver',
rating: 1500,
rank: 123,
starts: 10,
wins: 3,
podiums: 5,
leagues: 1,
});
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: Driver statistics should show:
expect(result.statistics.starts).toBe(10);
expect(result.statistics.wins).toBe(3);
expect(result.statistics.podiums).toBe(5);
expect(result.statistics.rating).toBe(1500);
expect(result.statistics.rank).toBe(123);
});
it('should correctly format upcoming race time information', async () => {
// Scenario: Upcoming race time formatting
// Given: A driver exists
const driverId = 'driver-time-format';
driverRepository.addDriver({
id: driverId,
name: 'Time Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: The driver has an upcoming race scheduled in 2 days 4 hours
const scheduledDate = new Date();
scheduledDate.setDate(scheduledDate.getDate() + 2);
scheduledDate.setHours(scheduledDate.getHours() + 4);
raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Monza',
carType: 'GT3',
scheduledDate,
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: The upcoming race should include:
expect(result.upcomingRaces).toHaveLength(1);
expect(result.upcomingRaces[0].trackName).toBe('Monza');
expect(result.upcomingRaces[0].carType).toBe('GT3');
expect(result.upcomingRaces[0].scheduledDate).toBe(scheduledDate.toISOString());
expect(result.upcomingRaces[0].timeUntilRace).toContain('2 days 4 hours');
});
it('should correctly aggregate championship standings across leagues', async () => {
// Scenario: Championship standings aggregation
// Given: A driver exists
const driverId = 'driver-champ-agg';
driverRepository.addDriver({
id: driverId,
name: 'Agg Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 2,
});
// And: The driver is in 2 championships
leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-a',
leagueName: 'Championship A',
position: 5,
points: 150,
totalDrivers: 20,
},
{
leagueId: 'league-b',
leagueName: 'Championship B',
position: 12,
points: 85,
totalDrivers: 15,
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: Championship standings should show:
expect(result.championshipStandings).toHaveLength(2);
expect(result.championshipStandings[0].leagueName).toBe('Championship A');
expect(result.championshipStandings[0].position).toBe(5);
expect(result.championshipStandings[1].leagueName).toBe('Championship B');
expect(result.championshipStandings[1].position).toBe(12);
});
it('should correctly format recent activity with proper status', async () => {
// Scenario: Recent activity formatting
// Given: A driver exists
const driverId = 'driver-activity-format';
driverRepository.addDriver({
id: driverId,
name: 'Activity Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
// And: The driver has a race result (finished 3rd)
// And: The driver has a league invitation event
activityRepository.addRecentActivity(driverId, [
{
id: 'act-1',
type: 'race_result',
description: 'Finished 3rd at Monza',
timestamp: new Date(),
status: 'success',
},
{
id: 'act-2',
type: 'league_invitation',
description: 'Invited to League XYZ',
timestamp: new Date(Date.now() - 1000),
status: 'info',
},
]);
// When: GetDashboardUseCase.execute() is called
const result = await getDashboardUseCase.execute({ driverId });
// Then: Recent activity should show:
expect(result.recentActivity).toHaveLength(2);
expect(result.recentActivity[0].type).toBe('race_result');
expect(result.recentActivity[0].status).toBe('success');
expect(result.recentActivity[0].description).toBe('Finished 3rd at Monza');
expect(result.recentActivity[1].type).toBe('league_invitation');
expect(result.recentActivity[1].status).toBe('info');
expect(result.recentActivity[1].description).toBe('Invited to League XYZ');
});
});
});

View File

@@ -0,0 +1,71 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { DashboardTestContext } from '../DashboardTestContext';
describe('Dashboard Data Flow Integration', () => {
const context = DashboardTestContext.create();
beforeEach(() => {
context.clear();
});
describe('Repository to Use Case Data Flow', () => {
it('should correctly flow driver data from repository to use case', async () => {
const driverId = 'driver-flow';
context.driverRepository.addDriver({
id: driverId,
name: 'Flow Driver',
rating: 1500,
rank: 123,
starts: 10,
wins: 3,
podiums: 5,
leagues: 1,
});
const result = await context.getDashboardUseCase.execute({ driverId });
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('Flow Driver');
expect(result.statistics.rating).toBe(1500);
expect(result.statistics.rank).toBe(123);
expect(result.statistics.starts).toBe(10);
expect(result.statistics.wins).toBe(3);
expect(result.statistics.podiums).toBe(5);
});
});
describe('Complete Data Flow: Repository -> Use Case -> Presenter', () => {
it('should complete full data flow for driver with all data', async () => {
const driverId = 'driver-complete-flow';
context.driverRepository.addDriver({
id: driverId,
name: 'Complete Flow Driver',
avatar: 'https://example.com/avatar.jpg',
rating: 1600,
rank: 85,
starts: 25,
wins: 8,
podiums: 15,
leagues: 2,
});
context.raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Monza',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000),
},
]);
const result = await context.getDashboardUseCase.execute({ driverId });
const dto = context.dashboardPresenter.present(result);
expect(dto.driver.id).toBe(driverId);
expect(dto.driver.name).toBe('Complete Flow Driver');
expect(dto.statistics.rating).toBe(1600);
expect(dto.upcomingRaces).toHaveLength(1);
expect(dto.upcomingRaces[0].trackName).toBe('Monza');
});
});
});

View File

@@ -0,0 +1,77 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { DashboardTestContext } from '../DashboardTestContext';
import { DriverNotFoundError } from '../../../../core/dashboard/domain/errors/DriverNotFoundError';
import { ValidationError } from '../../../../core/shared/errors/ValidationError';
describe('Dashboard Error Handling Integration', () => {
const context = DashboardTestContext.create();
beforeEach(() => {
context.clear();
});
describe('Driver Not Found Errors', () => {
it('should throw DriverNotFoundError when driver does not exist', async () => {
const driverId = 'non-existent-driver-id';
await expect(context.getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(DriverNotFoundError);
expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
});
describe('Validation Errors', () => {
it('should throw ValidationError when driver ID is empty string', async () => {
const driverId = '';
await expect(context.getDashboardUseCase.execute({ driverId }))
.rejects.toThrow(ValidationError);
expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(0);
});
});
describe('Repository Error Handling', () => {
it('should handle driver repository query error', async () => {
const driverId = 'driver-repo-error';
const spy = vi.spyOn(context.driverRepository, 'findDriverById').mockRejectedValue(new Error('Driver repo failed'));
await expect(context.getDashboardUseCase.execute({ driverId }))
.rejects.toThrow('Driver repo failed');
expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(0);
spy.mockRestore();
});
});
describe('Event Publisher Error Handling', () => {
it('should handle event publisher error gracefully', async () => {
const driverId = 'driver-pub-error';
context.driverRepository.addDriver({
id: driverId,
name: 'Pub Error Driver',
rating: 1000,
rank: 1,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
const spy = vi.spyOn(context.eventPublisher, 'publishDashboardAccessed').mockRejectedValue(new Error('Publisher failed'));
const result = await context.getDashboardUseCase.execute({ driverId });
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(context.loggerMock.error).toHaveBeenCalledWith(
'Failed to publish dashboard accessed event',
expect.any(Error),
expect.objectContaining({ driverId })
);
spy.mockRestore();
});
});
});

View File

@@ -0,0 +1,164 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { DashboardTestContext } from '../DashboardTestContext';
describe('GetDashboardUseCase - Success Path', () => {
const context = DashboardTestContext.create();
beforeEach(() => {
context.clear();
});
it('should retrieve complete dashboard data for a driver with all data', async () => {
const driverId = 'driver-123';
context.driverRepository.addDriver({
id: driverId,
name: 'John Doe',
avatar: 'https://example.com/avatar.jpg',
rating: 1500,
rank: 123,
starts: 10,
wins: 3,
podiums: 5,
leagues: 2,
});
context.raceRepository.addUpcomingRaces(driverId, [
{
id: 'race-1',
trackName: 'Monza',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000),
},
{
id: 'race-2',
trackName: 'Spa',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000),
},
{
id: 'race-3',
trackName: 'Nürburgring',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000),
},
{
id: 'race-4',
trackName: 'Silverstone',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
},
{
id: 'race-5',
trackName: 'Imola',
carType: 'GT3',
scheduledDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000),
},
]);
context.leagueRepository.addLeagueStandings(driverId, [
{
leagueId: 'league-1',
leagueName: 'GT3 Championship',
position: 5,
points: 150,
totalDrivers: 20,
},
{
leagueId: 'league-2',
leagueName: 'Endurance Series',
position: 12,
points: 85,
totalDrivers: 15,
},
]);
context.activityRepository.addRecentActivity(driverId, [
{
id: 'activity-1',
type: 'race_result',
description: 'Finished 3rd at Monza',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
status: 'success',
},
{
id: 'activity-2',
type: 'league_invitation',
description: 'Invited to League XYZ',
timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000),
status: 'info',
},
{
id: 'activity-3',
type: 'achievement',
description: 'Reached 1500 rating',
timestamp: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
status: 'success',
},
]);
const result = await context.getDashboardUseCase.execute({ driverId });
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('John Doe');
expect(result.driver.avatar).toBe('https://example.com/avatar.jpg');
expect(result.statistics.rating).toBe(1500);
expect(result.statistics.rank).toBe(123);
expect(result.statistics.starts).toBe(10);
expect(result.statistics.wins).toBe(3);
expect(result.statistics.podiums).toBe(5);
expect(result.statistics.leagues).toBe(2);
expect(result.upcomingRaces).toHaveLength(3);
expect(result.upcomingRaces[0].trackName).toBe('Nürburgring');
expect(result.upcomingRaces[1].trackName).toBe('Monza');
expect(result.upcomingRaces[2].trackName).toBe('Imola');
expect(result.championshipStandings).toHaveLength(2);
expect(result.championshipStandings[0].leagueName).toBe('GT3 Championship');
expect(result.championshipStandings[0].position).toBe(5);
expect(result.championshipStandings[0].points).toBe(150);
expect(result.championshipStandings[0].totalDrivers).toBe(20);
expect(result.recentActivity).toHaveLength(3);
expect(result.recentActivity[0].description).toBe('Finished 3rd at Monza');
expect(result.recentActivity[0].status).toBe('success');
expect(result.recentActivity[1].description).toBe('Invited to League XYZ');
expect(result.recentActivity[2].description).toBe('Reached 1500 rating');
expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
it('should retrieve dashboard data for a new driver with no history', async () => {
const driverId = 'new-driver-456';
context.driverRepository.addDriver({
id: driverId,
name: 'New Driver',
rating: 1000,
rank: 1000,
starts: 0,
wins: 0,
podiums: 0,
leagues: 0,
});
const result = await context.getDashboardUseCase.execute({ driverId });
expect(result).toBeDefined();
expect(result.driver.id).toBe(driverId);
expect(result.driver.name).toBe('New Driver');
expect(result.statistics.rating).toBe(1000);
expect(result.statistics.rank).toBe(1000);
expect(result.statistics.starts).toBe(0);
expect(result.statistics.wins).toBe(0);
expect(result.statistics.podiums).toBe(0);
expect(result.statistics.leagues).toBe(0);
expect(result.upcomingRaces).toHaveLength(0);
expect(result.championshipStandings).toHaveLength(0);
expect(result.recentActivity).toHaveLength(0);
expect(context.eventPublisher.getDashboardAccessedEventCount()).toBe(1);
});
});