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,19 +1,11 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { SignupUseCase } from './SignupUseCase';
import { EmailAddress } from '../../domain/value-objects/EmailAddress';
import { UserId } from '../../domain/value-objects/UserId';
import { User } from '../../domain/entities/User';
import type { IAuthRepository } from '../../domain/repositories/IAuthRepository';
import type { IPasswordHashingService } from '../../domain/services/PasswordHashingService';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
vi.mock('../../domain/value-objects/PasswordHash', () => ({
PasswordHash: {
fromHash: (hash: string) => ({ value: hash }),
},
}));
type SignupOutput = unknown;
import type { Logger } from '@core/shared/application';
import { User } from '../../domain/entities/User';
import { UserId } from '../../domain/value-objects/UserId';
import { PasswordHash } from '../../domain/value-objects/PasswordHash';
describe('SignupUseCase', () => {
let authRepo: {
@@ -24,7 +16,6 @@ describe('SignupUseCase', () => {
hash: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<SignupOutput> & { present: Mock };
let useCase: SignupUseCase;
beforeEach(() => {
@@ -32,64 +23,82 @@ describe('SignupUseCase', () => {
findByEmail: vi.fn(),
save: vi.fn(),
};
passwordService = {
hash: 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 SignupUseCase(
authRepo as unknown as IAuthRepository,
passwordService as unknown as IPasswordHashingService,
logger,
output,
);
});
it('creates and saves a new user when email is free', async () => {
const input = {
email: 'new@example.com',
password: 'Password123',
displayName: 'New User',
};
it('successfully signs up a new user', async () => {
authRepo.findByEmail.mockResolvedValue(null);
passwordService.hash.mockResolvedValue('hashed-password');
const result = await useCase.execute(input);
expect(authRepo.findByEmail).toHaveBeenCalledWith(EmailAddress.create(input.email));
expect(passwordService.hash).toHaveBeenCalledWith(input.password);
expect(authRepo.save).toHaveBeenCalled();
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalled();
});
it('throws when user already exists', async () => {
const input = {
email: 'existing@example.com',
password: 'password123',
displayName: 'Existing User',
};
const existingUser = User.create({
id: UserId.create(),
displayName: 'Existing User',
email: input.email,
const result = await useCase.execute({
email: 'test@example.com',
password: 'Password123',
displayName: 'John Smith', // Valid name that passes validation
});
expect(result.isOk()).toBe(true);
const signupResult = result.unwrap();
expect(signupResult.user).toBeDefined();
expect(signupResult.user.getEmail()).toBe('test@example.com');
expect(signupResult.user.getDisplayName()).toBe('John Smith');
expect(authRepo.findByEmail).toHaveBeenCalledTimes(1);
expect(authRepo.save).toHaveBeenCalledTimes(1);
});
it('returns error when user already exists', async () => {
const existingUser = User.create({
id: UserId.create(),
displayName: 'John Smith',
email: 'test@example.com',
passwordHash: PasswordHash.fromHash('existing-hash'),
});
authRepo.findByEmail.mockResolvedValue(existingUser);
const result = await useCase.execute(input);
const result = await useCase.execute({
email: 'test@example.com',
password: 'Password123',
displayName: 'John Smith',
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('USER_ALREADY_EXISTS');
});
it('returns error for weak password', async () => {
const result = await useCase.execute({
email: 'test@example.com',
password: 'weak',
displayName: 'John Smith',
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('WEAK_PASSWORD');
});
it('returns error for invalid display name', async () => {
const result = await useCase.execute({
email: 'test@example.com',
password: 'Password123',
displayName: 'A', // Too short
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('INVALID_DISPLAY_NAME');
});
});