refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -6,13 +6,12 @@ import {
type GetCurrentUserSocialResult,
} from './GetCurrentUserSocialUseCase';
import type { ISocialGraphRepository } from '../../domain/repositories/ISocialGraphRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Driver } from '@core/racing/domain/entities/Driver';
describe('GetCurrentUserSocialUseCase', () => {
let socialGraphRepository: ISocialGraphRepository & { getFriends: Mock };
let logger: Logger & { debug: Mock; info: Mock; warn: Mock; error: Mock };
let output: UseCaseOutputPort<GetCurrentUserSocialResult> & { present: Mock };
let useCase: GetCurrentUserSocialUseCase;
beforeEach(() => {
@@ -29,14 +28,10 @@ describe('GetCurrentUserSocialUseCase', () => {
error: vi.fn(),
} as unknown as Logger & { debug: Mock; info: Mock; warn: Mock; error: Mock };
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetCurrentUserSocialResult> & { present: Mock };
useCase = new GetCurrentUserSocialUseCase(socialGraphRepository, logger, output);
useCase = new GetCurrentUserSocialUseCase(socialGraphRepository, logger);
});
it('presents current user social with mapped friends', async () => {
it('returns current user social with mapped friends', async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2025-01-01T00:00:00.000Z'));
@@ -55,20 +50,17 @@ describe('GetCurrentUserSocialUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const socialResult = result.unwrap();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = (output.present as Mock).mock.calls[0]![0] as GetCurrentUserSocialResult;
expect(presented.currentUser).toEqual({
expect(socialResult.currentUser).toEqual({
driverId: 'driver-1',
displayName: '',
avatarUrl: '',
countryCode: '',
});
expect(presented.friends).toHaveLength(1);
expect(presented.friends[0]).toEqual({
expect(socialResult.friends).toHaveLength(1);
expect(socialResult.friends[0]).toEqual({
driverId: 'friend-1',
displayName: 'Friend One',
avatarUrl: '',
@@ -82,17 +74,17 @@ describe('GetCurrentUserSocialUseCase', () => {
vi.useRealTimers();
});
it('warns and presents empty friends list when no friends exist', async () => {
it('returns empty friends list when no friends exist', async () => {
socialGraphRepository.getFriends.mockResolvedValue([]);
const input: GetCurrentUserSocialInput = { driverId: 'driver-1' };
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
const socialResult = result.unwrap();
const presented = (output.present as Mock).mock.calls[0]![0] as GetCurrentUserSocialResult;
expect(presented.friends).toEqual([]);
expect(socialResult.friends).toEqual([]);
expect(socialResult.currentUser.driverId).toBe('driver-1');
expect(logger.warn).toHaveBeenCalledTimes(1);
expect((logger.warn as Mock).mock.calls[0]![0]).toBe(
@@ -112,7 +104,6 @@ describe('GetCurrentUserSocialUseCase', () => {
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
expect(logger.error).toHaveBeenCalledTimes(1);
});
});