refactor dashboard module

This commit is contained in:
2025-12-22 00:10:57 +01:00
parent 44ed8db555
commit b445d6dd37
6 changed files with 46 additions and 86 deletions

View File

@@ -19,10 +19,6 @@ import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPo
describe('DashboardOverviewUseCase', () => {
it('partitions upcoming races into myUpcomingRaces and otherUpcomingRaces and selects nextRace from myUpcomingRaces', async () => {
const output: UseCaseOutputPort<DashboardOverviewResult> = {
present: vi.fn(),
};
const driverId = 'driver-1';
const driver = Driver.create({ id: driverId, iracingId: '12345', name: 'Alice Racer', country: 'US' });
@@ -270,21 +266,17 @@ describe('DashboardOverviewUseCase', () => {
socialRepository,
getDriverAvatar,
getDriverStats,
output,
);
const input: DashboardOverviewInput = { driverId };
const result: UseCaseResult<
void,
DashboardOverviewResult,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
> = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const vm = (output.present as any).mock.calls[0][0] as DashboardOverviewResult;
const vm = result.unwrap();
expect(vm.myUpcomingRaces.map(r => r.race.id)).toEqual(['race-1', 'race-3']);
@@ -295,10 +287,6 @@ describe('DashboardOverviewUseCase', () => {
});
it('builds recentResults sorted by date descending and leagueStandingsSummaries from standings', async () => {
const output: UseCaseOutputPort<DashboardOverviewResult> = {
present: vi.fn(),
};
const driverId = 'driver-2';
const driver = Driver.create({ id: driverId, iracingId: '67890', name: 'Result Driver', country: 'DE' });
@@ -551,21 +539,17 @@ describe('DashboardOverviewUseCase', () => {
socialRepository,
getDriverAvatar,
getDriverStats,
output,
);
const input: DashboardOverviewInput = { driverId };
const result: UseCaseResult<
void,
DashboardOverviewResult,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
> = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const vm = (output.present as any).mock.calls[0][0] as DashboardOverviewResult;
const vm = result.unwrap();
expect(vm.recentResults.length).toBe(2);
expect(vm.recentResults[0]!.race.id).toBe('race-new');
@@ -590,10 +574,6 @@ describe('DashboardOverviewUseCase', () => {
});
it('returns empty collections and safe defaults when driver has no races or standings', async () => {
const output: UseCaseOutputPort<DashboardOverviewResult> = {
present: vi.fn(),
};
const driverId = 'driver-empty';
const driver = Driver.create({ id: driverId, iracingId: '11111', name: 'New Racer', country: 'FR' });
@@ -758,21 +738,17 @@ describe('DashboardOverviewUseCase', () => {
socialRepository,
getDriverAvatar,
getDriverStats,
output,
);
const input: DashboardOverviewInput = { driverId };
const result: UseCaseResult<
void,
DashboardOverviewResult,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
> = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const vm = (output.present as any).mock.calls[0][0] as DashboardOverviewResult;
const vm = result.unwrap();
expect(vm.myUpcomingRaces).toEqual([]);
expect(vm.otherUpcomingRaces).toEqual([]);
@@ -784,11 +760,7 @@ describe('DashboardOverviewUseCase', () => {
expect(vm.feedSummary.items).toEqual([]);
});
it('returns DRIVER_NOT_FOUND error and does not present when driver is missing', async () => {
const output: UseCaseOutputPort<DashboardOverviewResult> = {
present: vi.fn(),
};
it('returns DRIVER_NOT_FOUND error when driver is missing', async () => {
const driverId = 'missing-driver';
const driverRepository = {
@@ -951,13 +923,12 @@ describe('DashboardOverviewUseCase', () => {
socialRepository,
getDriverAvatar,
getDriverStats,
output,
);
const input: DashboardOverviewInput = { driverId };
const result: UseCaseResult<
void,
DashboardOverviewResult,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
> = await useCase.execute(input);
@@ -965,15 +936,9 @@ describe('DashboardOverviewUseCase', () => {
const err = result.unwrapErr();
expect(err.code).toBe('DRIVER_NOT_FOUND');
expect(err.details?.message).toBe('Driver not found');
expect(output.present).not.toHaveBeenCalled();
});
it('returns REPOSITORY_ERROR when an unexpected error occurs and does not present', async () => {
const output: UseCaseOutputPort<DashboardOverviewResult> = {
present: vi.fn(),
};
it('returns REPOSITORY_ERROR when an unexpected error occurs', async () => {
const driverId = 'driver-error';
const driver = Driver.create({ id: driverId, iracingId: '99999', name: 'Error Driver', country: 'GB' });
@@ -1140,13 +1105,12 @@ describe('DashboardOverviewUseCase', () => {
socialRepository,
getDriverAvatar,
getDriverStats,
output,
);
const input: DashboardOverviewInput = { driverId };
const result: UseCaseResult<
void,
DashboardOverviewResult,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
> = await useCase.execute(input);
@@ -1154,7 +1118,5 @@ describe('DashboardOverviewUseCase', () => {
const err = result.unwrapErr();
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details?.message).toBe('DB failure');
expect(output.present).not.toHaveBeenCalled();
});
});

View File

@@ -97,14 +97,13 @@ export class DashboardOverviewUseCase {
private readonly getDriverStats: (
driverId: string,
) => DashboardDriverStatsAdapter | null,
private readonly output: UseCaseOutputPort<DashboardOverviewResult>,
) {}
async execute(
input: DashboardOverviewInput,
): Promise<
Result<
void,
DashboardOverviewResult,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
>
> {
@@ -209,9 +208,7 @@ export class DashboardOverviewUseCase {
friends: friendsSummary,
};
this.output.present(result);
return Result.ok(undefined);
return Result.ok(result);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',