team rating
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetProfileOverviewUseCase,
|
||||
type GetProfileOverviewInput,
|
||||
type GetProfileOverviewResult,
|
||||
type GetProfileOverviewErrorCode,
|
||||
} from './GetProfileOverviewUseCase';
|
||||
import { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
||||
import { ISocialGraphRepository } from '@core/social/domain/repositories/ISocialGraphRepository';
|
||||
import { Driver } from '../../domain/entities/Driver';
|
||||
import { Team } from '../../domain/entities/Team';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetProfileOverviewUseCase', () => {
|
||||
let useCase: GetProfileOverviewUseCase;
|
||||
@@ -28,8 +24,12 @@ describe('GetProfileOverviewUseCase', () => {
|
||||
let socialRepository: {
|
||||
getFriends: Mock;
|
||||
};
|
||||
let getDriverStats: Mock;
|
||||
let getAllDriverRankings: Mock;
|
||||
let driverStatsUseCase: {
|
||||
getDriverStats: Mock;
|
||||
};
|
||||
let rankingUseCase: {
|
||||
getAllDriverRankings: Mock;
|
||||
};
|
||||
let driverExtendedProfileProvider: {
|
||||
getExtendedProfile: Mock;
|
||||
};
|
||||
@@ -39,20 +39,31 @@ describe('GetProfileOverviewUseCase', () => {
|
||||
driverRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
|
||||
teamRepository = {
|
||||
findAll: vi.fn(),
|
||||
};
|
||||
|
||||
teamMembershipRepository = {
|
||||
getMembership: vi.fn(),
|
||||
};
|
||||
|
||||
socialRepository = {
|
||||
getFriends: vi.fn(),
|
||||
};
|
||||
getDriverStats = vi.fn();
|
||||
getAllDriverRankings = vi.fn();
|
||||
|
||||
driverStatsUseCase = {
|
||||
getDriverStats: vi.fn(),
|
||||
};
|
||||
|
||||
rankingUseCase = {
|
||||
getAllDriverRankings: vi.fn(),
|
||||
};
|
||||
|
||||
driverExtendedProfileProvider = {
|
||||
getExtendedProfile: vi.fn(),
|
||||
};
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetProfileOverviewResult> & { present: Mock };
|
||||
@@ -63,8 +74,8 @@ describe('GetProfileOverviewUseCase', () => {
|
||||
teamMembershipRepository as unknown as ITeamMembershipRepository,
|
||||
socialRepository as unknown as ISocialGraphRepository,
|
||||
driverExtendedProfileProvider,
|
||||
getDriverStats,
|
||||
getAllDriverRankings,
|
||||
driverStatsUseCase as unknown as any,
|
||||
rankingUseCase as unknown as any,
|
||||
output,
|
||||
);
|
||||
});
|
||||
@@ -73,85 +84,40 @@ describe('GetProfileOverviewUseCase', () => {
|
||||
const driverId = 'driver-1';
|
||||
const driver = Driver.create({
|
||||
id: driverId,
|
||||
iracingId: '123',
|
||||
iracingId: '12345',
|
||||
name: 'Test Driver',
|
||||
country: 'US',
|
||||
joinedAt: new Date('2023-01-01'),
|
||||
});
|
||||
const teams = [
|
||||
Team.create({
|
||||
id: 'team-1',
|
||||
name: 'Test Team',
|
||||
tag: 'TT',
|
||||
description: 'Test',
|
||||
ownerId: 'owner-1',
|
||||
leagues: [],
|
||||
}),
|
||||
];
|
||||
const friends = [
|
||||
Driver.create({ id: 'friend-1', iracingId: '456', name: 'Friend', country: 'US' }),
|
||||
];
|
||||
const statsAdapter = {
|
||||
rating: 1500,
|
||||
wins: 5,
|
||||
podiums: 2,
|
||||
dnfs: 1,
|
||||
totalRaces: 10,
|
||||
avgFinish: 3.5,
|
||||
bestFinish: 1,
|
||||
worstFinish: 10,
|
||||
overallRank: 10,
|
||||
consistency: 90,
|
||||
percentile: 75,
|
||||
};
|
||||
const rankings = [{ driverId, rating: 1500, overallRank: 10 }];
|
||||
|
||||
driverRepository.findById.mockResolvedValue(driver);
|
||||
teamRepository.findAll.mockResolvedValue(teams);
|
||||
teamMembershipRepository.getMembership.mockResolvedValue(null);
|
||||
socialRepository.getFriends.mockResolvedValue(friends);
|
||||
getDriverStats.mockReturnValue(statsAdapter);
|
||||
getAllDriverRankings.mockReturnValue(rankings);
|
||||
driverExtendedProfileProvider.getExtendedProfile.mockReturnValue(null);
|
||||
driverStatsUseCase.getDriverStats.mockResolvedValue({
|
||||
rating: 1500,
|
||||
wins: 5,
|
||||
podiums: 10,
|
||||
dnfs: 2,
|
||||
totalRaces: 20,
|
||||
avgFinish: 8.5,
|
||||
bestFinish: 1,
|
||||
worstFinish: 15,
|
||||
overallRank: 50,
|
||||
consistency: 85,
|
||||
});
|
||||
rankingUseCase.getAllDriverRankings.mockResolvedValue([
|
||||
{ driverId: 'driver-1', rating: 1500, wins: 5, totalRaces: 20, overallRank: 50 },
|
||||
{ driverId: 'driver-2', rating: 1400, wins: 3, totalRaces: 18, overallRank: 75 },
|
||||
]);
|
||||
teamRepository.findAll.mockResolvedValue([]);
|
||||
socialRepository.getFriends.mockResolvedValue([]);
|
||||
driverExtendedProfileProvider.getExtendedProfile.mockReturnValue({
|
||||
bio: 'Test bio',
|
||||
location: 'Test location',
|
||||
favoriteTrack: 'Test track',
|
||||
});
|
||||
|
||||
const result = await useCase.execute({ driverId } as GetProfileOverviewInput);
|
||||
const result = await useCase.execute({ driverId });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = (output.present as unknown as Mock).mock.calls[0]?.[0] as GetProfileOverviewResult;
|
||||
expect(presented.driverInfo.driver.id).toBe(driverId);
|
||||
expect(presented.extendedProfile).toBeNull();
|
||||
});
|
||||
|
||||
it('should return error for non-existing driver', async () => {
|
||||
const driverId = 'driver-1';
|
||||
driverRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ driverId } as GetProfileOverviewInput);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetProfileOverviewErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('DRIVER_NOT_FOUND');
|
||||
expect(error.details.message).toBe('Driver not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error on repository failure', async () => {
|
||||
const driverId = 'driver-1';
|
||||
driverRepository.findById.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const result = await useCase.execute({ driverId } as GetProfileOverviewInput);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetProfileOverviewErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(output.present).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user