refactor use cases
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { StartAuthUseCase } from './StartAuthUseCase';
|
||||
import {
|
||||
StartAuthUseCase,
|
||||
type StartAuthInput,
|
||||
type StartAuthResult,
|
||||
type StartAuthErrorCode,
|
||||
} from './StartAuthUseCase';
|
||||
import type { IdentityProviderPort } from '../ports/IdentityProviderPort';
|
||||
import type { StartAuthCommandDTO } from '../dto/StartAuthCommandDTO';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('StartAuthUseCase', () => {
|
||||
let provider: {
|
||||
startAuth: Mock;
|
||||
};
|
||||
let logger: Logger & { error: Mock };
|
||||
let output: UseCaseOutputPort<StartAuthResult> & { present: Mock };
|
||||
let useCase: StartAuthUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -14,22 +23,67 @@ describe('StartAuthUseCase', () => {
|
||||
startAuth: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new StartAuthUseCase(provider as unknown as IdentityProviderPort);
|
||||
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<StartAuthResult> & { present: Mock };
|
||||
|
||||
useCase = new StartAuthUseCase(
|
||||
provider as unknown as IdentityProviderPort,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('delegates to the identity provider to start auth', async () => {
|
||||
const command: StartAuthCommandDTO = {
|
||||
redirectUri: 'https://app/callback',
|
||||
provider: 'demo',
|
||||
it('returns ok and presents redirect when provider call succeeds', async () => {
|
||||
const input: StartAuthInput = {
|
||||
provider: 'IRACING_DEMO' as any,
|
||||
returnTo: 'https://app/callback',
|
||||
};
|
||||
|
||||
const expected = { redirectUrl: 'https://auth/redirect', state: 'state-123' };
|
||||
|
||||
provider.startAuth.mockResolvedValue(expected);
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
const result: Result<void, ApplicationErrorCode<StartAuthErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(provider.startAuth).toHaveBeenCalledWith(command);
|
||||
expect(result).toEqual(expected);
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(provider.startAuth).toHaveBeenCalledWith({
|
||||
provider: input.provider,
|
||||
returnTo: input.returnTo,
|
||||
});
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as StartAuthResult;
|
||||
expect(presented).toEqual(expected);
|
||||
});
|
||||
|
||||
it('wraps unexpected errors as REPOSITORY_ERROR and logs them', async () => {
|
||||
const input: StartAuthInput = {
|
||||
provider: 'IRACING_DEMO' as any,
|
||||
returnTo: 'https://app/callback',
|
||||
};
|
||||
|
||||
provider.startAuth.mockRejectedValue(new Error('Provider failure'));
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<StartAuthErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
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(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user