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,25 +1,19 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { LogoutUseCase, type LogoutResult, type LogoutErrorCode } from './LogoutUseCase';
import { LogoutUseCase } 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 type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
describe('LogoutUseCase', () => {
let sessionPort: {
clearSession: Mock;
getCurrentSession: Mock;
createSession: Mock;
};
let logger: Logger & { error: Mock };
let output: UseCaseOutputPort<LogoutResult> & { present: Mock };
let useCase: LogoutUseCase;
beforeEach(() => {
sessionPort = {
clearSession: vi.fn(),
getCurrentSession: vi.fn(),
createSession: vi.fn(),
};
logger = {
@@ -29,42 +23,30 @@ describe('LogoutUseCase', () => {
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 and presents success', async () => {
const result: Result<void, ApplicationErrorCode<LogoutErrorCode, { message: string }>> =
await useCase.execute();
it('successfully clears session and returns success', async () => {
sessionPort.clearSession.mockResolvedValue(undefined);
const result = await useCase.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const logoutResult = result.unwrap();
expect(logoutResult.success).toBe(true);
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);
it('returns error when session clear fails', async () => {
sessionPort.clearSession.mockRejectedValue(new Error('Session clear failed'));
const result: Result<void, ApplicationErrorCode<LogoutErrorCode, { message: string }>> =
await useCase.execute();
const result = 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(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
expect(logger.error).toHaveBeenCalled();
});
});
});