refactor use cases

This commit is contained in:
2025-12-21 01:20:27 +01:00
parent c12656d671
commit 8ecd638396
39 changed files with 2523 additions and 686 deletions

View File

@@ -1,6 +1,9 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { LogoutUseCase } from './LogoutUseCase';
import { LogoutUseCase, type LogoutResult, type LogoutErrorCode } from './LogoutUseCase';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Result } from '@core/shared/application/Result';
describe('LogoutUseCase', () => {
let sessionPort: {
@@ -8,6 +11,8 @@ describe('LogoutUseCase', () => {
getCurrentSession: Mock;
createSession: Mock;
};
let logger: Logger & { error: Mock };
let output: UseCaseOutputPort<LogoutResult> & { present: Mock };
let useCase: LogoutUseCase;
beforeEach(() => {
@@ -17,12 +22,49 @@ describe('LogoutUseCase', () => {
createSession: vi.fn(),
};
useCase = new LogoutUseCase(sessionPort as unknown as IdentitySessionPort);
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger & { error: Mock };
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<LogoutResult> & { present: Mock };
useCase = new LogoutUseCase(
sessionPort as unknown as IdentitySessionPort,
logger,
output,
);
});
it('clears the current session', async () => {
await useCase.execute();
it('clears the current session and presents success', async () => {
const result: Result<void, ApplicationErrorCode<LogoutErrorCode, { message: string }>> =
await useCase.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(sessionPort.clearSession).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ success: true });
});
it('wraps unexpected errors as REPOSITORY_ERROR and logs them', async () => {
const error = new Error('Session clear failed');
sessionPort.clearSession.mockRejectedValue(error);
const result: Result<void, ApplicationErrorCode<LogoutErrorCode, { message: string }>> =
await useCase.execute();
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('Session clear failed');
expect(output.present).not.toHaveBeenCalled();
expect(logger.error).toHaveBeenCalled();
});
});