Files
gridpilot.gg/core/racing/application/use-cases/GetProfileOverviewUseCase.test.ts
2025-12-21 00:43:42 +01:00

168 lines
5.4 KiB
TypeScript

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 type { IImageServicePort } from '../ports/IImageServicePort';
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;
let driverRepository: {
findById: Mock;
};
let teamRepository: {
findAll: Mock;
};
let teamMembershipRepository: {
getMembership: Mock;
};
let socialRepository: {
getFriends: Mock;
};
let imageService: {
getDriverAvatar: Mock;
};
let getDriverStats: Mock;
let getAllDriverRankings: Mock;
let driverExtendedProfileProvider: {
getExtendedProfile: Mock;
};
let output: UseCaseOutputPort<GetProfileOverviewResult> & { present: Mock };
beforeEach(() => {
driverRepository = {
findById: vi.fn(),
};
teamRepository = {
findAll: vi.fn(),
};
teamMembershipRepository = {
getMembership: vi.fn(),
};
socialRepository = {
getFriends: vi.fn(),
};
imageService = {
getDriverAvatar: vi.fn(),
};
getDriverStats = vi.fn();
getAllDriverRankings = vi.fn();
driverExtendedProfileProvider = {
getExtendedProfile: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetProfileOverviewResult> & { present: Mock };
useCase = new GetProfileOverviewUseCase(
driverRepository as unknown as IDriverRepository,
teamRepository as unknown as ITeamRepository,
teamMembershipRepository as unknown as ITeamMembershipRepository,
socialRepository as unknown as ISocialGraphRepository,
imageService as unknown as IImageServicePort,
driverExtendedProfileProvider,
getDriverStats,
getAllDriverRankings,
output,
);
});
it('should return profile overview for existing driver', async () => {
const driverId = 'driver-1';
const driver = Driver.create({
id: driverId,
iracingId: '123',
name: 'Test Driver',
country: 'US',
});
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);
imageService.getDriverAvatar.mockReturnValue('avatar-url');
getDriverStats.mockReturnValue(statsAdapter);
getAllDriverRankings.mockReturnValue(rankings);
driverExtendedProfileProvider.getExtendedProfile.mockReturnValue(null);
const result = await useCase.execute({ driverId } as GetProfileOverviewInput);
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();
});
});