services refactor
This commit is contained in:
193
apps/website/lib/services/analytics/AnalyticsService.test.ts
Normal file
193
apps/website/lib/services/analytics/AnalyticsService.test.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { AnalyticsService } from './AnalyticsService';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import type { RecordPageViewInputDto, RecordPageViewOutputDto, RecordEngagementInputDto, RecordEngagementOutputDto } from '../../dtos';
|
||||
|
||||
describe('AnalyticsService', () => {
|
||||
let mockApiClient: AnalyticsApiClient;
|
||||
let service: AnalyticsService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
getDashboardData: vi.fn(),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
|
||||
service = new AnalyticsService(mockApiClient);
|
||||
});
|
||||
|
||||
describe('recordPageView', () => {
|
||||
it('should record page view via API client', async () => {
|
||||
// Arrange
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/dashboard',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
|
||||
const expectedOutput: RecordPageViewOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.recordPageView).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordPageView(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/dashboard',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
|
||||
const error = new Error('API Error: Failed to record page view');
|
||||
vi.mocked(mockApiClient.recordPageView).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.recordPageView(input)).rejects.toThrow(
|
||||
'API Error: Failed to record page view'
|
||||
);
|
||||
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle different input parameters', async () => {
|
||||
// Arrange
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/races',
|
||||
timestamp: '2025-12-18T10:30:00Z',
|
||||
userId: 'user-2',
|
||||
};
|
||||
|
||||
const expectedOutput: RecordPageViewOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.recordPageView).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordPageView(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
});
|
||||
|
||||
describe('recordEngagement', () => {
|
||||
it('should record engagement event via API client', async () => {
|
||||
// Arrange
|
||||
const input: RecordEngagementInputDto = {
|
||||
event: 'button_click',
|
||||
element: 'register-race-btn',
|
||||
page: '/races',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
|
||||
const expectedOutput: RecordEngagementOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.recordEngagement).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordEngagement(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const input: RecordEngagementInputDto = {
|
||||
event: 'form_submit',
|
||||
element: 'contact-form',
|
||||
page: '/contact',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
|
||||
const error = new Error('API Error: Failed to record engagement');
|
||||
vi.mocked(mockApiClient.recordEngagement).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.recordEngagement(input)).rejects.toThrow(
|
||||
'API Error: Failed to record engagement'
|
||||
);
|
||||
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle different engagement types', async () => {
|
||||
// Arrange
|
||||
const input: RecordEngagementInputDto = {
|
||||
event: 'scroll',
|
||||
element: 'race-list',
|
||||
page: '/races',
|
||||
timestamp: '2025-12-18T10:30:00Z',
|
||||
userId: 'user-2',
|
||||
};
|
||||
|
||||
const expectedOutput: RecordEngagementOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.recordEngagement).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordEngagement(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Constructor Dependency Injection', () => {
|
||||
it('should require apiClient', () => {
|
||||
// This test verifies the constructor signature
|
||||
expect(() => {
|
||||
new AnalyticsService(mockApiClient);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should use injected apiClient', async () => {
|
||||
// Arrange
|
||||
const customApiClient = {
|
||||
recordPageView: vi.fn().mockResolvedValue({ success: true }),
|
||||
recordEngagement: vi.fn().mockResolvedValue({ success: true }),
|
||||
getDashboardData: vi.fn(),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
|
||||
const customService = new AnalyticsService(customApiClient);
|
||||
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/test',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
|
||||
// Act
|
||||
await customService.recordPageView(input);
|
||||
|
||||
// Assert
|
||||
expect(customApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,36 @@
|
||||
import { api as api } from '../../api';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import type { RecordPageViewInputDto, RecordPageViewOutputDto, RecordEngagementInputDto, RecordEngagementOutputDto } from '../../dtos';
|
||||
|
||||
export async function recordPageView(input: any): Promise<any> {
|
||||
return await api.analytics.recordPageView(input);
|
||||
}
|
||||
/**
|
||||
* Analytics Service
|
||||
*
|
||||
* Orchestrates analytics operations by coordinating API calls.
|
||||
* All dependencies are injected via constructor.
|
||||
*/
|
||||
export class AnalyticsService {
|
||||
constructor(
|
||||
private readonly apiClient: AnalyticsApiClient
|
||||
) {}
|
||||
|
||||
export async function recordEngagement(input: any): Promise<any> {
|
||||
return await api.analytics.recordEngagement(input);
|
||||
/**
|
||||
* Record a page view
|
||||
*/
|
||||
async recordPageView(input: RecordPageViewInputDto): Promise<RecordPageViewOutputDto> {
|
||||
try {
|
||||
return await this.apiClient.recordPageView(input);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record an engagement event
|
||||
*/
|
||||
async recordEngagement(input: RecordEngagementInputDto): Promise<RecordEngagementOutputDto> {
|
||||
try {
|
||||
return await this.apiClient.recordEngagement(input);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
227
apps/website/lib/services/analytics/DashboardService.test.ts
Normal file
227
apps/website/lib/services/analytics/DashboardService.test.ts
Normal file
@@ -0,0 +1,227 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { DashboardService } from './DashboardService';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import { AnalyticsDashboardPresenter } from '../../presenters/AnalyticsDashboardPresenter';
|
||||
import { AnalyticsMetricsPresenter } from '../../presenters/AnalyticsMetricsPresenter';
|
||||
import type { AnalyticsDashboardDto, AnalyticsMetricsDto } from '../../dtos';
|
||||
import { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';
|
||||
|
||||
describe('DashboardService', () => {
|
||||
let mockApiClient: AnalyticsApiClient;
|
||||
let mockDashboardPresenter: AnalyticsDashboardPresenter;
|
||||
let mockMetricsPresenter: AnalyticsMetricsPresenter;
|
||||
let service: DashboardService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
getDashboardData: vi.fn(),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
|
||||
mockDashboardPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as AnalyticsDashboardPresenter;
|
||||
|
||||
mockMetricsPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as AnalyticsMetricsPresenter;
|
||||
|
||||
service = new DashboardService(mockApiClient, mockDashboardPresenter, mockMetricsPresenter);
|
||||
});
|
||||
|
||||
describe('getDashboardData', () => {
|
||||
it('should fetch dashboard data from API and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsDashboardDto = {
|
||||
totalUsers: 1000,
|
||||
activeUsers: 750,
|
||||
totalRaces: 50,
|
||||
totalLeagues: 25,
|
||||
};
|
||||
|
||||
const mockViewModel: AnalyticsDashboardViewModel = new AnalyticsDashboardViewModel(mockDto);
|
||||
|
||||
vi.mocked(mockApiClient.getDashboardData).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockDashboardPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getDashboardData();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(mockViewModel);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const error = new Error('API Error: Failed to fetch dashboard data');
|
||||
vi.mocked(mockApiClient.getDashboardData).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getDashboardData()).rejects.toThrow(
|
||||
'API Error: Failed to fetch dashboard data'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should propagate presenter errors', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsDashboardDto = {
|
||||
totalUsers: 500,
|
||||
activeUsers: 300,
|
||||
totalRaces: 20,
|
||||
totalLeagues: 10,
|
||||
};
|
||||
|
||||
const error = new Error('Presenter Error: Invalid DTO structure');
|
||||
vi.mocked(mockApiClient.getDashboardData).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockDashboardPresenter.present).mockImplementation(() => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getDashboardData()).rejects.toThrow(
|
||||
'Presenter Error: Invalid DTO structure'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAnalyticsMetrics', () => {
|
||||
it('should fetch analytics metrics from API and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsMetricsDto = {
|
||||
pageViews: 5000,
|
||||
uniqueVisitors: 1200,
|
||||
averageSessionDuration: 180,
|
||||
bounceRate: 35.5,
|
||||
};
|
||||
|
||||
const mockViewModel: AnalyticsMetricsViewModel = new AnalyticsMetricsViewModel(mockDto);
|
||||
|
||||
vi.mocked(mockApiClient.getAnalyticsMetrics).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockMetricsPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getAnalyticsMetrics();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalledTimes(1);
|
||||
expect(mockMetricsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(mockMetricsPresenter.present).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(mockViewModel);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const error = new Error('API Error: Failed to fetch analytics metrics');
|
||||
vi.mocked(mockApiClient.getAnalyticsMetrics).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getAnalyticsMetrics()).rejects.toThrow(
|
||||
'API Error: Failed to fetch analytics metrics'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalledTimes(1);
|
||||
expect(mockMetricsPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should propagate presenter errors', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsMetricsDto = {
|
||||
pageViews: 2500,
|
||||
uniqueVisitors: 600,
|
||||
averageSessionDuration: 120,
|
||||
bounceRate: 45.2,
|
||||
};
|
||||
|
||||
const error = new Error('Presenter Error: Invalid metrics data');
|
||||
vi.mocked(mockApiClient.getAnalyticsMetrics).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockMetricsPresenter.present).mockImplementation(() => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getAnalyticsMetrics()).rejects.toThrow(
|
||||
'Presenter Error: Invalid metrics data'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalledTimes(1);
|
||||
expect(mockMetricsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDashboardOverview', () => {
|
||||
it('should delegate to getDashboardData for backward compatibility', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsDashboardDto = {
|
||||
totalUsers: 800,
|
||||
activeUsers: 600,
|
||||
totalRaces: 40,
|
||||
totalLeagues: 20,
|
||||
};
|
||||
|
||||
const mockViewModel: AnalyticsDashboardViewModel = new AnalyticsDashboardViewModel(mockDto);
|
||||
|
||||
vi.mocked(mockApiClient.getDashboardData).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockDashboardPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getDashboardOverview();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toBe(mockViewModel);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Constructor Dependency Injection', () => {
|
||||
it('should require apiClient, dashboardPresenter, and metricsPresenter', () => {
|
||||
// This test verifies the constructor signature
|
||||
expect(() => {
|
||||
new DashboardService(mockApiClient, mockDashboardPresenter, mockMetricsPresenter);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should use injected dependencies', async () => {
|
||||
// Arrange
|
||||
const customApiClient = {
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
getDashboardData: vi.fn().mockResolvedValue({
|
||||
totalUsers: 100,
|
||||
activeUsers: 80,
|
||||
totalRaces: 5,
|
||||
totalLeagues: 3,
|
||||
}),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
|
||||
const customDashboardPresenter = {
|
||||
present: vi.fn().mockReturnValue({} as AnalyticsDashboardViewModel),
|
||||
} as unknown as AnalyticsDashboardPresenter;
|
||||
|
||||
const customMetricsPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as AnalyticsMetricsPresenter;
|
||||
|
||||
const customService = new DashboardService(customApiClient, customDashboardPresenter, customMetricsPresenter);
|
||||
|
||||
// Act
|
||||
await customService.getDashboardData();
|
||||
|
||||
// Assert
|
||||
expect(customApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(customDashboardPresenter.present).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,50 @@
|
||||
import { api as api } from '../../api';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import { AnalyticsDashboardPresenter } from '../../presenters/AnalyticsDashboardPresenter';
|
||||
import { AnalyticsMetricsPresenter } from '../../presenters/AnalyticsMetricsPresenter';
|
||||
import type { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';
|
||||
|
||||
export async function getDashboardOverview(): Promise<any> {
|
||||
// TODO: aggregate data
|
||||
return {};
|
||||
/**
|
||||
* Dashboard Service
|
||||
*
|
||||
* Orchestrates dashboard operations by coordinating API calls and presentation logic.
|
||||
* All dependencies are injected via constructor.
|
||||
*/
|
||||
export class DashboardService {
|
||||
constructor(
|
||||
private readonly apiClient: AnalyticsApiClient,
|
||||
private readonly analyticsDashboardPresenter: AnalyticsDashboardPresenter,
|
||||
private readonly analyticsMetricsPresenter: AnalyticsMetricsPresenter
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get dashboard data with presentation transformation
|
||||
*/
|
||||
async getDashboardData(): Promise<AnalyticsDashboardViewModel> {
|
||||
try {
|
||||
const dto = await this.apiClient.getDashboardData();
|
||||
return this.analyticsDashboardPresenter.present(dto);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get analytics metrics with presentation transformation
|
||||
*/
|
||||
async getAnalyticsMetrics(): Promise<AnalyticsMetricsViewModel> {
|
||||
try {
|
||||
const dto = await this.apiClient.getAnalyticsMetrics();
|
||||
return this.analyticsMetricsPresenter.present(dto);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dashboard overview (legacy method for backward compatibility)
|
||||
* TODO: Remove when no longer needed
|
||||
*/
|
||||
async getDashboardOverview(): Promise<AnalyticsDashboardViewModel> {
|
||||
return this.getDashboardData();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user