import { describe, it, expect, vi, type Mock } from 'vitest'; 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: { clearSession: Mock; getCurrentSession: Mock; createSession: Mock; }; let logger: Logger & { error: Mock }; let output: UseCaseOutputPort & { present: Mock }; let useCase: LogoutUseCase; beforeEach(() => { sessionPort = { clearSession: vi.fn(), getCurrentSession: vi.fn(), createSession: vi.fn(), }; 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 & { present: Mock }; useCase = new LogoutUseCase( sessionPort as unknown as IdentitySessionPort, logger, output, ); }); it('clears the current session and presents success', async () => { const result: Result> = 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> = 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(); }); });