Files
gridpilot.gg/tests/integration/drivers/driver-profile-use-cases.integration.test.ts
Marc Mintel 597bb48248
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 4m51s
Contract Testing / contract-snapshot (pull_request) Has been skipped
integration tests
2026-01-22 17:29:06 +01:00

179 lines
7.0 KiB
TypeScript

/**
* Integration Test: GetProfileOverviewUseCase Orchestration
*
* 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, 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('GetProfileOverviewUseCase Orchestration', () => {
let driverRepository: InMemoryDriverRepository;
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(() => {
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(() => {
driverRepository.clear();
teamRepository.clear();
teamMembershipRepository.clear();
socialRepository.clear();
driverExtendedProfileProvider.clear();
driverStatsRepository.clear();
});
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
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 handle driver with minimal data', async () => {
// Scenario: New driver with no history
// Given: A driver exists
const driverId = 'new';
const driver = Driver.create({ id: driverId, iracingId: '9', name: 'New Driver', country: 'DE' });
await driverRepository.create(driver);
// When: GetProfileOverviewUseCase.execute() is called
const result = await getProfileOverviewUseCase.execute({ driverId });
// 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('GetProfileOverviewUseCase - Error Handling', () => {
it('should return error when driver does not exist', async () => {
// Scenario: Non-existent driver
// When: GetProfileOverviewUseCase.execute() is called
const result = await getProfileOverviewUseCase.execute({ driverId: 'none' });
// Then: Should return DRIVER_NOT_FOUND
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.code).toBe('DRIVER_NOT_FOUND');
});
});
});