fix issues in core

This commit is contained in:
2025-12-23 11:25:08 +01:00
parent 1efd971032
commit 2854ae3c5c
113 changed files with 1142 additions and 458 deletions

View File

@@ -1,8 +1,10 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { SignupWithEmailUseCase, type SignupCommandDTO } from './SignupWithEmailUseCase';
import { SignupWithEmailUseCase } from './SignupWithEmailUseCase';
import type { SignupWithEmailInput } from './SignupWithEmailUseCase';
import type { IUserRepository, StoredUser } from '../../domain/repositories/IUserRepository';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('SignupWithEmailUseCase', () => {
let userRepository: {
@@ -14,6 +16,8 @@ describe('SignupWithEmailUseCase', () => {
getCurrentSession: Mock;
clearSession: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
let useCase: SignupWithEmailUseCase;
beforeEach(() => {
@@ -26,14 +30,25 @@ describe('SignupWithEmailUseCase', () => {
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: SignupCommandDTO = {
const command: SignupWithEmailInput = {
email: 'new@example.com',
password: 'password123',
displayName: 'New User',
@@ -64,42 +79,58 @@ describe('SignupWithEmailUseCase', () => {
displayName: command.displayName,
});
expect(result.session).toEqual(session);
expect(result.isNewUser).toBe(true);
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,
});
});
it('throws when email format is invalid', async () => {
const command: SignupCommandDTO = {
it('returns error when email format is invalid', async () => {
const command: SignupWithEmailInput = {
email: 'invalid-email',
password: 'password123',
displayName: 'User',
};
await expect(useCase.execute(command)).rejects.toThrow('Invalid email format');
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('INVALID_EMAIL_FORMAT');
});
it('throws when password is too short', async () => {
const command: SignupCommandDTO = {
it('returns error when password is too short', async () => {
const command: SignupWithEmailInput = {
email: 'valid@example.com',
password: 'short',
displayName: 'User',
};
await expect(useCase.execute(command)).rejects.toThrow('Password must be at least 8 characters');
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('WEAK_PASSWORD');
});
it('throws when display name is too short', async () => {
const command: SignupCommandDTO = {
it('returns error when display name is too short', async () => {
const command: SignupWithEmailInput = {
email: 'valid@example.com',
password: 'password123',
displayName: ' ',
};
await expect(useCase.execute(command)).rejects.toThrow('Display name must be at least 2 characters');
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('INVALID_DISPLAY_NAME');
});
it('throws when email already exists', async () => {
const command: SignupCommandDTO = {
it('returns error when email already exists', async () => {
const command: SignupWithEmailInput = {
email: 'existing@example.com',
password: 'password123',
displayName: 'Existing User',
@@ -116,6 +147,9 @@ describe('SignupWithEmailUseCase', () => {
userRepository.findByEmail.mockResolvedValue(existingUser);
await expect(useCase.execute(command)).rejects.toThrow('An account with this email already exists');
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.code).toBe('EMAIL_ALREADY_EXISTS');
});
});