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

@@ -1,8 +1,8 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { RecordEngagementUseCase, type RecordEngagementInput, type RecordEngagementOutput } from './RecordEngagementUseCase';
import { RecordEngagementUseCase, type RecordEngagementInput } from './RecordEngagementUseCase';
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { EngagementAction, EngagementEntityType } from '../../domain/types/EngagementEvent';
describe('RecordEngagementUseCase', () => {
@@ -10,7 +10,6 @@ describe('RecordEngagementUseCase', () => {
save: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<RecordEngagementOutput> & { present: Mock };
let useCase: RecordEngagementUseCase;
beforeEach(() => {
@@ -25,18 +24,13 @@ describe('RecordEngagementUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new RecordEngagementUseCase(
engagementRepository as unknown as IEngagementRepository,
logger,
output,
);
});
it('creates and saves an EngagementEvent and presents its id and weight', async () => {
it('creates and saves an EngagementEvent and returns its id and weight', async () => {
const input: RecordEngagementInput = {
action: 'view' as EngagementAction,
entityType: 'league' as EngagementEntityType,
@@ -52,6 +46,7 @@ describe('RecordEngagementUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(engagementRepository.save).toHaveBeenCalledTimes(1);
const saved = (engagementRepository.save as unknown as Mock).mock.calls?.[0]?.[0] as EngagementEvent;
@@ -60,14 +55,12 @@ describe('RecordEngagementUseCase', () => {
expect(saved.entityId).toBe(input.entityId);
expect(saved.entityType).toBe(input.entityType);
expect(output.present).toHaveBeenCalledWith({
eventId: saved.id,
engagementWeight: saved.getEngagementWeight(),
});
expect(data.eventId).toBe(saved.id);
expect(data.engagementWeight).toBe(saved.getEngagementWeight());
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
it('logs and presents error when repository save fails', async () => {
it('logs and returns error when repository save fails', async () => {
const input: RecordEngagementInput = {
action: 'view' as EngagementAction,
entityType: 'league' as EngagementEntityType,