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,11 +1,9 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { SignupWithEmailUseCase } from './SignupWithEmailUseCase';
import type { SignupWithEmailInput } from './SignupWithEmailUseCase';
import type { IUserRepository, StoredUser } from '../../domain/repositories/IUserRepository';
import type { AuthSession, IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
type SignupWithEmailOutput = unknown;
import type { IUserRepository } from '../../domain/repositories/IUserRepository';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
describe('SignupWithEmailUseCase', () => {
let userRepository: {
@@ -14,11 +12,8 @@ describe('SignupWithEmailUseCase', () => {
};
let sessionPort: {
createSession: Mock;
getCurrentSession: Mock;
clearSession: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<SignupWithEmailOutput> & { present: Mock };
let useCase: SignupWithEmailUseCase;
beforeEach(() => {
@@ -26,130 +21,106 @@ describe('SignupWithEmailUseCase', () => {
findByEmail: vi.fn(),
create: vi.fn(),
};
sessionPort = {
createSession: vi.fn(),
getCurrentSession: vi.fn(),
clearSession: vi.fn(),
};
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new SignupWithEmailUseCase(
userRepository as unknown as IUserRepository,
sessionPort as unknown as IdentitySessionPort,
logger,
output,
);
});
it('creates a new user and session for valid input', async () => {
const command: SignupWithEmailInput = {
email: 'new@example.com',
password: 'password123',
displayName: 'New User',
};
userRepository.findByEmail.mockResolvedValue(null);
const session: AuthSession = {
userRepository.create.mockResolvedValue(undefined);
sessionPort.createSession.mockResolvedValue({
token: 'session-token',
user: {
id: 'user-1',
email: command.email.toLowerCase(),
displayName: command.displayName,
displayName: 'Test User',
email: 'test@example.com',
},
issuedAt: Date.now(),
expiresAt: Date.now() + 1000,
token: 'session-token',
};
});
sessionPort.createSession.mockResolvedValue(session);
const result = await useCase.execute(command);
expect(userRepository.findByEmail).toHaveBeenCalledWith(command.email);
expect(userRepository.create).toHaveBeenCalled();
expect(sessionPort.createSession).toHaveBeenCalledWith({
id: expect.any(String),
email: command.email.toLowerCase(),
displayName: command.displayName,
const result = await useCase.execute({
email: 'test@example.com',
password: 'Password123',
displayName: 'Test User',
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith({
sessionToken: 'session-token',
userId: 'user-1',
displayName: 'New User',
email: 'new@example.com',
createdAt: expect.any(Date),
isNewUser: true,
});
const signupResult = result.unwrap();
expect(signupResult.sessionToken).toBe('session-token');
expect(signupResult.userId).toBe('user-1');
expect(signupResult.displayName).toBe('Test User');
expect(signupResult.email).toBe('test@example.com');
expect(signupResult.isNewUser).toBe(true);
expect(userRepository.findByEmail).toHaveBeenCalledWith('test@example.com');
expect(userRepository.create).toHaveBeenCalled();
expect(sessionPort.createSession).toHaveBeenCalled();
});
it('returns error when email format is invalid', async () => {
const command: SignupWithEmailInput = {
it('returns error for invalid email format', async () => {
const result = await useCase.execute({
email: 'invalid-email',
password: 'password123',
displayName: 'User',
};
password: 'Password123',
displayName: 'Test User',
});
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('INVALID_EMAIL_FORMAT');
expect(result.unwrapErr().code).toBe('INVALID_EMAIL_FORMAT');
});
it('returns error when password is too short', async () => {
const command: SignupWithEmailInput = {
email: 'valid@example.com',
password: 'short',
displayName: 'User',
};
it('returns error for weak password', async () => {
const result = await useCase.execute({
email: 'test@example.com',
password: 'weak',
displayName: 'Test User',
});
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('WEAK_PASSWORD');
expect(result.unwrapErr().code).toBe('WEAK_PASSWORD');
});
it('returns error when display name is too short', async () => {
const command: SignupWithEmailInput = {
email: 'valid@example.com',
password: 'password123',
displayName: ' ',
};
it('returns error for invalid display name', async () => {
const result = await useCase.execute({
email: 'test@example.com',
password: 'Password123',
displayName: 'A',
});
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('INVALID_DISPLAY_NAME');
expect(result.unwrapErr().code).toBe('INVALID_DISPLAY_NAME');
});
it('returns error when email already exists', async () => {
const command: SignupWithEmailInput = {
email: 'existing@example.com',
password: 'password123',
userRepository.findByEmail.mockResolvedValue({
id: 'existing-user',
email: 'test@example.com',
displayName: 'Existing User',
};
const existingUser: StoredUser = {
id: 'user-1',
email: command.email,
displayName: command.displayName,
passwordHash: 'hash',
createdAt: new Date(),
};
});
userRepository.findByEmail.mockResolvedValue(existingUser);
const result = await useCase.execute({
email: 'test@example.com',
password: 'Password123',
displayName: 'Test User',
});
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('EMAIL_ALREADY_EXISTS');
expect(result.unwrapErr().code).toBe('EMAIL_ALREADY_EXISTS');
});
});
});