refactor use cases
This commit is contained in:
@@ -1,13 +1,7 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
StartAuthUseCase,
|
||||
type StartAuthInput,
|
||||
type StartAuthResult,
|
||||
type StartAuthErrorCode,
|
||||
} from './StartAuthUseCase';
|
||||
import { StartAuthUseCase } from './StartAuthUseCase';
|
||||
import type { IdentityProviderPort } from '../ports/IdentityProviderPort';
|
||||
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('StartAuthUseCase', () => {
|
||||
@@ -15,7 +9,6 @@ describe('StartAuthUseCase', () => {
|
||||
startAuth: Mock;
|
||||
};
|
||||
let logger: Logger & { error: Mock };
|
||||
let output: UseCaseOutputPort<StartAuthResult> & { present: Mock };
|
||||
let useCase: StartAuthUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -30,60 +23,58 @@ describe('StartAuthUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger & { error: Mock };
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<StartAuthResult> & { present: Mock };
|
||||
|
||||
useCase = new StartAuthUseCase(
|
||||
provider as unknown as IdentityProviderPort,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns ok and presents redirect when provider call succeeds', async () => {
|
||||
const input: StartAuthInput = {
|
||||
provider: 'IRACING_DEMO',
|
||||
returnTo: 'https://app/callback',
|
||||
};
|
||||
|
||||
const expected = { redirectUrl: 'https://auth/redirect', state: 'state-123' };
|
||||
|
||||
provider.startAuth.mockResolvedValue(expected);
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<StartAuthErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(provider.startAuth).toHaveBeenCalledWith({
|
||||
provider: input.provider,
|
||||
returnTo: input.returnTo,
|
||||
provider.startAuth.mockResolvedValue({
|
||||
redirectUrl: 'https://auth/redirect',
|
||||
state: 'state-123',
|
||||
});
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as StartAuthResult;
|
||||
expect(presented).toEqual(expected);
|
||||
const result = await useCase.execute({
|
||||
provider: 'iracing',
|
||||
returnTo: '/dashboard',
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const startAuthResult = result.unwrap();
|
||||
expect(startAuthResult.redirectUrl).toBe('https://auth/redirect');
|
||||
expect(startAuthResult.state).toBe('state-123');
|
||||
expect(provider.startAuth).toHaveBeenCalledWith({
|
||||
provider: 'iracing',
|
||||
returnTo: '/dashboard',
|
||||
});
|
||||
});
|
||||
|
||||
it('wraps unexpected errors as REPOSITORY_ERROR and logs them', async () => {
|
||||
const input: StartAuthInput = {
|
||||
provider: 'IRACING_DEMO',
|
||||
returnTo: 'https://app/callback',
|
||||
};
|
||||
it('returns ok without returnTo when not provided', async () => {
|
||||
provider.startAuth.mockResolvedValue({
|
||||
redirectUrl: 'https://auth/redirect',
|
||||
state: 'state-123',
|
||||
});
|
||||
|
||||
provider.startAuth.mockRejectedValue(new Error('Provider failure'));
|
||||
const result = await useCase.execute({
|
||||
provider: 'iracing',
|
||||
});
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<StartAuthErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(provider.startAuth).toHaveBeenCalledWith({
|
||||
provider: 'iracing',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when provider call fails', async () => {
|
||||
provider.startAuth.mockRejectedValue(new Error('Provider error'));
|
||||
|
||||
const result = await useCase.execute({
|
||||
provider: 'iracing',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('Provider failure');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user