website refactor

This commit is contained in:
2026-01-14 02:02:24 +01:00
parent 8d7c709e0c
commit 4522d41aef
291 changed files with 12763 additions and 9309 deletions

View File

@@ -1,12 +1,6 @@
import { describe, it, expect, vi, Mocked, beforeEach } from 'vitest';
import { LeagueService } from './LeagueService';
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';
import { LeagueStandingsViewModel } from '@/lib/view-models/LeagueStandingsViewModel';
import { LeagueStatsViewModel } from '@/lib/view-models/LeagueStatsViewModel';
import { LeagueScheduleViewModel } from '@/lib/view-models/LeagueScheduleViewModel';
import { LeagueMembershipsViewModel } from '@/lib/view-models/LeagueMembershipsViewModel';
import { RemoveMemberViewModel } from '@/lib/view-models/RemoveMemberViewModel';
import { LeagueMemberViewModel } from '@/lib/view-models/LeagueMemberViewModel';
import type { CreateLeagueInputDTO } from '@/lib/types/generated/CreateLeagueInputDTO';
import type { CreateLeagueOutputDTO } from '@/lib/types/generated/CreateLeagueOutputDTO';
import type { RemoveLeagueMemberOutputDTO } from '@/lib/types/generated/RemoveLeagueMemberOutputDTO';
@@ -24,14 +18,15 @@ describe('LeagueService', () => {
getSchedule: vi.fn(),
getMemberships: vi.fn(),
create: vi.fn(),
removeMember: vi.fn(),
removeRosterMember: vi.fn(),
updateRosterMemberRole: vi.fn(),
} as unknown as Mocked<LeaguesApiClient>;
service = new LeagueService(mockApiClient);
});
describe('getAllLeagues', () => {
it('should call apiClient.getAllWithCapacityAndScoring and return array of LeagueSummaryViewModel', async () => {
it('should call apiClient.getAllWithCapacityAndScoring and return DTO', async () => {
const mockDto = {
totalCount: 2,
leagues: [
@@ -45,8 +40,7 @@ describe('LeagueService', () => {
const result = await service.getAllLeagues();
expect(mockApiClient.getAllWithCapacityAndScoring).toHaveBeenCalled();
expect(result).toHaveLength(2);
expect(result.map((l) => l.id)).toEqual(['league-1', 'league-2']);
expect(result).toEqual(mockDto);
});
it('should handle empty leagues array', async () => {
@@ -56,7 +50,7 @@ describe('LeagueService', () => {
const result = await service.getAllLeagues();
expect(result).toHaveLength(0);
expect(result).toEqual(mockDto);
});
it('should throw error when apiClient.getAllWithCapacityAndScoring fails', async () => {
@@ -68,32 +62,29 @@ describe('LeagueService', () => {
});
describe('getLeagueStandings', () => {
it('should call apiClient.getStandings and return LeagueStandingsViewModel', async () => {
it('should call apiClient.getStandings and return DTO', async () => {
const leagueId = 'league-123';
const currentUserId = 'user-456';
const mockDto = { standings: [] } as any;
mockApiClient.getStandings.mockResolvedValue({ standings: [] } as any);
mockApiClient.getMemberships.mockResolvedValue({ members: [] } as any);
mockApiClient.getStandings.mockResolvedValue(mockDto);
const result = await service.getLeagueStandings(leagueId, currentUserId);
const result = await service.getLeagueStandings(leagueId);
expect(mockApiClient.getStandings).toHaveBeenCalledWith(leagueId);
expect(result).toBeInstanceOf(LeagueStandingsViewModel);
expect(result).toEqual(mockDto);
});
it('should throw error when apiClient.getStandings fails', async () => {
const leagueId = 'league-123';
const currentUserId = 'user-456';
const error = new Error('API call failed');
mockApiClient.getStandings.mockRejectedValue(error);
await expect(service.getLeagueStandings(leagueId, currentUserId)).rejects.toThrow('API call failed');
await expect(service.getLeagueStandings(leagueId)).rejects.toThrow('API call failed');
});
});
describe('getLeagueStats', () => {
it('should call apiClient.getTotal and return LeagueStatsViewModel', async () => {
it('should call apiClient.getTotal and return DTO', async () => {
const mockDto = { totalLeagues: 42 };
mockApiClient.getTotal.mockResolvedValue(mockDto);
@@ -101,8 +92,7 @@ describe('LeagueService', () => {
const result = await service.getLeagueStats();
expect(mockApiClient.getTotal).toHaveBeenCalled();
expect(result).toBeInstanceOf(LeagueStatsViewModel);
expect(result.totalLeagues).toBe(42);
expect(result).toEqual(mockDto);
});
it('should throw error when apiClient.getTotal fails', async () => {
@@ -114,7 +104,7 @@ describe('LeagueService', () => {
});
describe('getLeagueSchedule', () => {
it('should call apiClient.getSchedule and return LeagueScheduleViewModel with Date parsing', async () => {
it('should call apiClient.getSchedule and return DTO', async () => {
const leagueId = 'league-123';
const mockDto = {
races: [
@@ -128,8 +118,7 @@ describe('LeagueService', () => {
const result = await service.getLeagueSchedule(leagueId);
expect(mockApiClient.getSchedule).toHaveBeenCalledWith(leagueId);
expect(result).toBeInstanceOf(LeagueScheduleViewModel);
expect(result.raceCount).toBe(2);
expect(result).toEqual(mockDto);
});
it('should handle empty races array', async () => {
@@ -140,13 +129,11 @@ describe('LeagueService', () => {
const result = await service.getLeagueSchedule(leagueId);
expect(result.races).toEqual([]);
expect(result.hasRaces).toBe(false);
expect(result).toEqual(mockDto);
});
it('should throw error when apiClient.getSchedule fails', async () => {
const leagueId = 'league-123';
const error = new Error('API call failed');
mockApiClient.getSchedule.mockRejectedValue(error);
@@ -155,49 +142,37 @@ describe('LeagueService', () => {
});
describe('getLeagueMemberships', () => {
it('should call apiClient.getMemberships and return LeagueMembershipsViewModel', async () => {
it('should call apiClient.getMemberships and return DTO', async () => {
const leagueId = 'league-123';
const currentUserId = 'user-456';
const mockDto = {
members: [{ driverId: 'driver-1' }, { driverId: 'driver-2' }],
} as any;
mockApiClient.getMemberships.mockResolvedValue(mockDto);
const result = await service.getLeagueMemberships(leagueId, currentUserId);
const result = await service.getLeagueMemberships(leagueId);
expect(mockApiClient.getMemberships).toHaveBeenCalledWith(leagueId);
expect(result).toBeInstanceOf(LeagueMembershipsViewModel);
expect(result.memberships).toHaveLength(2);
const first = result.memberships[0]!;
expect(first).toBeInstanceOf(LeagueMemberViewModel);
expect(first.driverId).toBe('driver-1');
expect(result).toEqual(mockDto);
});
it('should handle empty memberships array', async () => {
const leagueId = 'league-123';
const currentUserId = 'user-456';
const mockDto = { members: [] } as any;
mockApiClient.getMemberships.mockResolvedValue(mockDto);
const result = await service.getLeagueMemberships(leagueId, currentUserId);
const result = await service.getLeagueMemberships(leagueId);
expect(result.memberships).toHaveLength(0);
expect(result.hasMembers).toBe(false);
expect(result).toEqual(mockDto);
});
it('should throw error when apiClient.getMemberships fails', async () => {
const leagueId = 'league-123';
const currentUserId = 'user-456';
const error = new Error('API call failed');
mockApiClient.getMemberships.mockRejectedValue(error);
await expect(service.getLeagueMemberships(leagueId, currentUserId)).rejects.toThrow('API call failed');
await expect(service.getLeagueMemberships(leagueId)).rejects.toThrow('API call failed');
});
});
@@ -238,46 +213,41 @@ describe('LeagueService', () => {
});
describe('removeMember', () => {
it('should call apiClient.removeMember and return RemoveMemberViewModel', async () => {
it('should call apiClient.removeRosterMember and return DTO', async () => {
const leagueId = 'league-123';
const performerDriverId = 'performer-456';
const targetDriverId = 'target-789';
const mockDto: RemoveLeagueMemberOutputDTO = { success: true };
mockApiClient.removeMember.mockResolvedValue(mockDto);
mockApiClient.removeRosterMember.mockResolvedValue(mockDto);
const result = await service.removeMember(leagueId, performerDriverId, targetDriverId);
const result = await service.removeMember(leagueId, targetDriverId);
expect(mockApiClient.removeMember).toHaveBeenCalledWith(leagueId, performerDriverId, targetDriverId);
expect(result).toBeInstanceOf(RemoveMemberViewModel);
expect(result.success).toBe(true);
expect(mockApiClient.removeRosterMember).toHaveBeenCalledWith(leagueId, targetDriverId);
expect(result).toEqual(mockDto);
});
it('should handle unsuccessful removal', async () => {
const leagueId = 'league-123';
const performerDriverId = 'performer-456';
const targetDriverId = 'target-789';
const mockDto: RemoveLeagueMemberOutputDTO = { success: false };
mockApiClient.removeMember.mockResolvedValue(mockDto);
mockApiClient.removeRosterMember.mockResolvedValue(mockDto);
const result = await service.removeMember(leagueId, performerDriverId, targetDriverId);
const result = await service.removeMember(leagueId, targetDriverId);
expect(result.success).toBe(false);
expect(result.successMessage).toBe('Failed to remove member.');
});
it('should throw error when apiClient.removeMember fails', async () => {
it('should throw error when apiClient.removeRosterMember fails', async () => {
const leagueId = 'league-123';
const performerDriverId = 'performer-456';
const targetDriverId = 'target-789';
const error = new Error('API call failed');
mockApiClient.removeMember.mockRejectedValue(error);
mockApiClient.removeRosterMember.mockRejectedValue(error);
await expect(service.removeMember(leagueId, performerDriverId, targetDriverId)).rejects.toThrow('API call failed');
await expect(service.removeMember(leagueId, targetDriverId)).rejects.toThrow('API call failed');
});
});
});
});