resolve todos in website

This commit is contained in:
2025-12-20 12:55:07 +01:00
parent 20588e1c0b
commit 92be9d2e1b
56 changed files with 2476 additions and 78 deletions

View File

@@ -0,0 +1,64 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetUnreadNotificationsUseCase } from './GetUnreadNotificationsUseCase';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import type { Logger } from '@core/shared/application';
import { Notification } from '../../domain/entities/Notification';
interface NotificationRepositoryMock {
findUnreadByRecipientId: Mock;
}
describe('GetUnreadNotificationsUseCase', () => {
let notificationRepository: NotificationRepositoryMock;
let logger: Logger;
let useCase: GetUnreadNotificationsUseCase;
beforeEach(() => {
notificationRepository = {
findUnreadByRecipientId: vi.fn(),
} as unknown as INotificationRepository as any;
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
useCase = new GetUnreadNotificationsUseCase(
notificationRepository as unknown as INotificationRepository,
logger,
);
});
it('returns unread notifications and total count', async () => {
const recipientId = 'driver-1';
const notifications: Notification[] = [
Notification.create({
id: 'n1',
recipientId,
type: 'info',
title: 'Test',
body: 'Body',
channel: 'in_app',
}),
];
notificationRepository.findUnreadByRecipientId.mockResolvedValue(notifications);
const result = await useCase.execute(recipientId);
expect(notificationRepository.findUnreadByRecipientId).toHaveBeenCalledWith(recipientId);
expect(result.notifications).toEqual(notifications);
expect(result.totalCount).toBe(1);
});
it('handles repository errors by logging and rethrowing', async () => {
const recipientId = 'driver-1';
const error = new Error('DB error');
notificationRepository.findUnreadByRecipientId.mockRejectedValue(error);
await expect(useCase.execute(recipientId)).rejects.toThrow('DB error');
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
});

View File

@@ -4,8 +4,7 @@
* Retrieves unread notifications for a recipient.
*/
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { AsyncUseCase , Logger } from '@core/shared/application';
import type { Notification } from '../../domain/entities/Notification';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';

View File

@@ -0,0 +1,83 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { MarkNotificationReadUseCase } from './MarkNotificationReadUseCase';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import type { Logger } from '@core/shared/application';
import { Notification } from '../../domain/entities/Notification';
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
interface NotificationRepositoryMock {
findById: Mock;
update: Mock;
markAllAsReadByRecipientId: Mock;
}
describe('MarkNotificationReadUseCase', () => {
let notificationRepository: NotificationRepositoryMock;
let logger: Logger;
let useCase: MarkNotificationReadUseCase;
beforeEach(() => {
notificationRepository = {
findById: vi.fn(),
update: vi.fn(),
markAllAsReadByRecipientId: vi.fn(),
} as unknown as INotificationRepository as any;
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
useCase = new MarkNotificationReadUseCase(
notificationRepository as unknown as INotificationRepository,
logger,
);
});
it('throws when notification is not found', async () => {
notificationRepository.findById.mockResolvedValue(null);
await expect(
useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' }),
).rejects.toThrow(NotificationDomainError);
expect((logger.warn as unknown as Mock)).toHaveBeenCalled();
});
it('throws when recipientId does not match', async () => {
const notification = Notification.create({
id: 'n1',
recipientId: 'driver-2',
type: 'info',
title: 'Test',
body: 'Body',
channel: 'in_app',
});
notificationRepository.findById.mockResolvedValue(notification);
await expect(
useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' }),
).rejects.toThrow(NotificationDomainError);
});
it('marks notification as read when unread', async () => {
const notification = Notification.create({
id: 'n1',
recipientId: 'driver-1',
type: 'info',
title: 'Test',
body: 'Body',
channel: 'in_app',
});
notificationRepository.findById.mockResolvedValue(notification);
await useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' });
expect(notificationRepository.update).toHaveBeenCalled();
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
});

View File

@@ -4,10 +4,9 @@
* Marks a notification as read.
*/
import type { AsyncUseCase } from '@core/shared/application';
import type { AsyncUseCase , Logger } from '@core/shared/application';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
import type { Logger } from '@core/shared/application';
export interface MarkNotificationReadCommand {
notificationId: string;

View File

@@ -0,0 +1,165 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import {
GetNotificationPreferencesQuery,
UpdateChannelPreferenceUseCase,
UpdateTypePreferenceUseCase,
UpdateQuietHoursUseCase,
SetDigestModeUseCase,
} from './NotificationPreferencesUseCases';
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
import type { NotificationPreference , ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
import type { Logger } from '@core/shared/application';
import type { NotificationChannel, NotificationType } from '../../domain/types/NotificationTypes';
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
describe('NotificationPreferencesUseCases', () => {
let preferenceRepository: {
getOrCreateDefault: Mock;
save: Mock;
};
let logger: Logger;
beforeEach(() => {
preferenceRepository = {
getOrCreateDefault: vi.fn(),
save: vi.fn(),
} as unknown as INotificationPreferenceRepository as any;
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
});
it('GetNotificationPreferencesQuery returns preferences from repository', async () => {
const preference = {
id: 'pref-1',
} as unknown as NotificationPreference;
preferenceRepository.getOrCreateDefault.mockResolvedValue(preference);
const useCase = new GetNotificationPreferencesQuery(
preferenceRepository as unknown as INotificationPreferenceRepository,
logger,
);
const result = await useCase.execute('driver-1');
expect(preferenceRepository.getOrCreateDefault).toHaveBeenCalledWith('driver-1');
expect(result).toBe(preference);
});
it('UpdateChannelPreferenceUseCase updates channel preference', async () => {
const preference = {
updateChannel: vi.fn().mockReturnThis(),
} as unknown as NotificationPreference;
preferenceRepository.getOrCreateDefault.mockResolvedValue(preference);
const useCase = new UpdateChannelPreferenceUseCase(
preferenceRepository as unknown as INotificationPreferenceRepository,
logger,
);
await useCase.execute({
driverId: 'driver-1',
channel: 'email' as NotificationChannel,
preference: 'enabled' as ChannelPreference,
});
expect(preference.updateChannel).toHaveBeenCalled();
expect(preferenceRepository.save).toHaveBeenCalledWith(preference);
});
it('UpdateTypePreferenceUseCase updates type preference', async () => {
const preference = {
updateTypePreference: vi.fn().mockReturnThis(),
} as unknown as NotificationPreference;
preferenceRepository.getOrCreateDefault.mockResolvedValue(preference);
const useCase = new UpdateTypePreferenceUseCase(
preferenceRepository as unknown as INotificationPreferenceRepository,
logger,
);
await useCase.execute({
driverId: 'driver-1',
type: 'info' as NotificationType,
preference: 'enabled' as TypePreference,
});
expect(preference.updateTypePreference).toHaveBeenCalled();
expect(preferenceRepository.save).toHaveBeenCalledWith(preference);
});
it('UpdateQuietHoursUseCase validates hours and updates preferences', async () => {
const preference = {
updateQuietHours: vi.fn().mockReturnThis(),
} as unknown as NotificationPreference;
preferenceRepository.getOrCreateDefault.mockResolvedValue(preference);
const useCase = new UpdateQuietHoursUseCase(
preferenceRepository as unknown as INotificationPreferenceRepository,
logger,
);
await useCase.execute({
driverId: 'driver-1',
startHour: 22,
endHour: 7,
});
expect(preference.updateQuietHours).toHaveBeenCalledWith(22, 7);
expect(preferenceRepository.save).toHaveBeenCalledWith(preference);
});
it('UpdateQuietHoursUseCase throws on invalid hours', async () => {
const useCase = new UpdateQuietHoursUseCase(
preferenceRepository as unknown as INotificationPreferenceRepository,
logger,
);
await expect(
useCase.execute({ driverId: 'd1', startHour: -1, endHour: 10 }),
).rejects.toThrow(NotificationDomainError);
await expect(
useCase.execute({ driverId: 'd1', startHour: 10, endHour: 24 }),
).rejects.toThrow(NotificationDomainError);
});
it('SetDigestModeUseCase sets digest mode with valid frequency', async () => {
const preference = {
setDigestMode: vi.fn().mockReturnThis(),
} as unknown as NotificationPreference;
preferenceRepository.getOrCreateDefault.mockResolvedValue(preference);
const useCase = new SetDigestModeUseCase(
preferenceRepository as unknown as INotificationPreferenceRepository,
);
await useCase.execute({
driverId: 'driver-1',
enabled: true,
frequencyHours: 4,
});
expect(preference.setDigestMode).toHaveBeenCalledWith(true, 4);
expect(preferenceRepository.save).toHaveBeenCalledWith(preference);
});
it('SetDigestModeUseCase throws on invalid frequency', async () => {
const useCase = new SetDigestModeUseCase(
preferenceRepository as unknown as INotificationPreferenceRepository,
);
await expect(
useCase.execute({ driverId: 'driver-1', enabled: true, frequencyHours: 0 }),
).rejects.toThrow(NotificationDomainError);
});
});

View File

@@ -4,8 +4,7 @@
* Manages user notification preferences.
*/
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { AsyncUseCase , Logger } from '@core/shared/application';
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
import type { ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';