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,6 +1,8 @@
import type { AuthenticatedUserDTO } from '../dto/AuthenticatedUserDTO';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
// TODO not so sure if this here is proper clean architecture
export interface IdentitySessionPort {
getCurrentSession(): Promise<AuthSessionDTO | null>;
createSession(user: AuthenticatedUserDTO): Promise<AuthSessionDTO>;

View File

@@ -2,6 +2,7 @@ import { vi, type Mock } from 'vitest';
import { GetCurrentSessionUseCase } from './GetCurrentSessionUseCase';
import { User } from '../../domain/entities/User';
import { IUserRepository, StoredUser } from '../../domain/repositories/IUserRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('GetCurrentSessionUseCase', () => {
let useCase: GetCurrentSessionUseCase;
@@ -12,6 +13,8 @@ describe('GetCurrentSessionUseCase', () => {
update: Mock;
emailExists: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
beforeEach(() => {
mockUserRepo = {
@@ -21,7 +24,20 @@ describe('GetCurrentSessionUseCase', () => {
update: vi.fn(),
emailExists: vi.fn(),
};
useCase = new GetCurrentSessionUseCase(mockUserRepo as IUserRepository);
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new GetCurrentSessionUseCase(
mockUserRepo as IUserRepository,
logger,
output,
);
});
it('should return User when user exists', async () => {
@@ -37,21 +53,24 @@ describe('GetCurrentSessionUseCase', () => {
};
mockUserRepo.findById.mockResolvedValue(storedUser);
const result = await useCase.execute(userId);
const result = await useCase.execute({ userId });
expect(mockUserRepo.findById).toHaveBeenCalledWith(userId);
expect(result).toBeInstanceOf(User);
expect(result?.getId().value).toBe(userId);
expect(result?.getDisplayName()).toBe('Test User');
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalled();
const callArgs = output.present.mock.calls?.[0]?.[0];
expect(callArgs?.user).toBeInstanceOf(User);
expect(callArgs?.user.getId().value).toBe(userId);
expect(callArgs?.user.getDisplayName()).toBe('Test User');
});
it('should return null when user does not exist', async () => {
it('should return error when user does not exist', async () => {
const userId = 'user-123';
mockUserRepo.findById.mockResolvedValue(null);
const result = await useCase.execute(userId);
const result = await useCase.execute({ userId });
expect(mockUserRepo.findById).toHaveBeenCalledWith(userId);
expect(result).toBeNull();
expect(result.isErr()).toBe(true);
});
});

View File

@@ -2,6 +2,7 @@ import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetCurrentUserSessionUseCase } from './GetCurrentUserSessionUseCase';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('GetCurrentUserSessionUseCase', () => {
let sessionPort: {
@@ -9,7 +10,8 @@ describe('GetCurrentUserSessionUseCase', () => {
createSession: Mock;
clearSession: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
let useCase: GetCurrentUserSessionUseCase;
beforeEach(() => {
@@ -19,7 +21,22 @@ describe('GetCurrentUserSessionUseCase', () => {
clearSession: vi.fn(),
};
useCase = new GetCurrentUserSessionUseCase(sessionPort as unknown as IdentitySessionPort);
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new GetCurrentUserSessionUseCase(
sessionPort as unknown as IdentitySessionPort,
logger,
output,
);
});
it('returns the current auth session when one exists', async () => {
@@ -40,7 +57,8 @@ describe('GetCurrentUserSessionUseCase', () => {
const result = await useCase.execute();
expect(sessionPort.getCurrentSession).toHaveBeenCalledTimes(1);
expect(result).toEqual(session);
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith(session);
});
it('returns null when there is no active session', async () => {
@@ -49,6 +67,7 @@ describe('GetCurrentUserSessionUseCase', () => {
const result = await useCase.execute();
expect(sessionPort.getCurrentSession).toHaveBeenCalledTimes(1);
expect(result).toBeNull();
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith(null);
});
});
});

View File

@@ -2,12 +2,15 @@ import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetUserUseCase } from './GetUserUseCase';
import { User } from '../../domain/entities/User';
import type { IUserRepository, StoredUser } from '../../domain/repositories/IUserRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Result } from '@core/shared/application/Result';
describe('GetUserUseCase', () => {
let userRepository: {
findById: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
let useCase: GetUserUseCase;
beforeEach(() => {
@@ -15,7 +18,22 @@ describe('GetUserUseCase', () => {
findById: vi.fn(),
};
useCase = new GetUserUseCase(userRepository as unknown as IUserRepository);
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new GetUserUseCase(
userRepository as unknown as IUserRepository,
logger,
output,
);
});
it('returns a User when the user exists', async () => {
@@ -31,18 +49,24 @@ describe('GetUserUseCase', () => {
userRepository.findById.mockResolvedValue(storedUser);
const result = await useCase.execute('user-1');
const result = await useCase.execute({ userId: 'user-1' });
expect(userRepository.findById).toHaveBeenCalledWith('user-1');
expect(result).toBeInstanceOf(User);
expect(result.getId().value).toBe('user-1');
expect(result.getDisplayName()).toBe('Test User');
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalled();
const callArgs = output.present.mock.calls?.[0]?.[0] as Result<any, any>;
const user = callArgs.unwrap().user;
expect(user).toBeInstanceOf(User);
expect(user.getId().value).toBe('user-1');
expect(user.getDisplayName()).toBe('Test User');
});
it('throws when the user does not exist', async () => {
it('returns error when the user does not exist', async () => {
userRepository.findById.mockResolvedValue(null);
await expect(useCase.execute('missing-user')).rejects.toThrow('User not found');
const result = await useCase.execute({ userId: 'missing-user' });
expect(userRepository.findById).toHaveBeenCalledWith('missing-user');
expect(result.isErr()).toBe(true);
});
});
});

View File

@@ -5,6 +5,7 @@ import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { AuthCallbackCommandDTO } from '../dto/AuthCallbackCommandDTO';
import type { AuthenticatedUserDTO } from '../dto/AuthenticatedUserDTO';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('HandleAuthCallbackUseCase', () => {
let provider: {
@@ -15,6 +16,8 @@ describe('HandleAuthCallbackUseCase', () => {
getCurrentSession: Mock;
clearSession: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
let useCase: HandleAuthCallbackUseCase;
beforeEach(() => {
@@ -26,18 +29,30 @@ describe('HandleAuthCallbackUseCase', () => {
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 HandleAuthCallbackUseCase(
provider as unknown as IdentityProviderPort,
sessionPort as unknown as IdentitySessionPort,
logger,
output,
);
});
it('completes auth and creates a session', async () => {
const command: AuthCallbackCommandDTO = {
provider: 'IRACING_DEMO',
code: 'auth-code',
state: 'state-123',
redirectUri: 'https://app/callback',
returnTo: 'https://app/callback',
};
const user: AuthenticatedUserDTO = {
@@ -60,6 +75,7 @@ describe('HandleAuthCallbackUseCase', () => {
expect(provider.completeAuth).toHaveBeenCalledWith(command);
expect(sessionPort.createSession).toHaveBeenCalledWith(user);
expect(result).toEqual(session);
expect(output.present).toHaveBeenCalledWith(session);
expect(result.isOk()).toBe(true);
});
});

View File

@@ -48,7 +48,7 @@ export class LoginUseCase implements UseCase<LoginInput, void, LoginErrorCode> {
const isValid = await this.passwordService.verify(input.password, passwordHash.value);
if (!isValid) {
return Result.err<LoginApplicationError>({
return Result.err<void, LoginApplicationError>({
code: 'INVALID_CREDENTIALS',
details: { message: 'Invalid credentials' },
});
@@ -66,7 +66,7 @@ export class LoginUseCase implements UseCase<LoginInput, void, LoginErrorCode> {
input,
});
return Result.err<LoginApplicationError>({
return Result.err<void, LoginApplicationError>({
code: 'REPOSITORY_ERROR',
details: { message },
});

View File

@@ -24,7 +24,7 @@ export class LogoutUseCase implements UseCase<LogoutInput, void, LogoutErrorCode
this.sessionPort = sessionPort;
}
async execute(input: LogoutInput): Promise<Result<void, LogoutApplicationError>> {
async execute(): Promise<Result<void, LogoutApplicationError>> {
try {
await this.sessionPort.clearSession();

View File

@@ -5,6 +5,7 @@ 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: {
@@ -20,6 +21,8 @@ describe('SignupUseCase', () => {
let passwordService: {
hash: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
let useCase: SignupUseCase;
beforeEach(() => {
@@ -30,42 +33,61 @@ describe('SignupUseCase', () => {
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 email = 'new@example.com';
const password = 'password123';
const displayName = 'New User';
const input = {
email: 'new@example.com',
password: 'password123',
displayName: 'New User',
};
authRepo.findByEmail.mockResolvedValue(null);
passwordService.hash.mockResolvedValue('hashed-password');
const result = await useCase.execute(email, password, displayName);
const result = await useCase.execute(input);
expect(authRepo.findByEmail).toHaveBeenCalledWith(EmailAddress.create(email));
expect(passwordService.hash).toHaveBeenCalledWith(password);
expect(authRepo.findByEmail).toHaveBeenCalledWith(EmailAddress.create(input.email));
expect(passwordService.hash).toHaveBeenCalledWith(input.password);
expect(authRepo.save).toHaveBeenCalled();
expect(result).toBeInstanceOf(User);
expect(result.getDisplayName()).toBe(displayName);
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalled();
});
it('throws when user already exists', async () => {
const email = 'existing@example.com';
const input = {
email: 'existing@example.com',
password: 'password123',
displayName: 'Existing User',
};
const existingUser = User.create({
id: UserId.create(),
displayName: 'Existing User',
email,
email: input.email,
});
authRepo.findByEmail.mockResolvedValue(existingUser);
await expect(useCase.execute(email, 'password', 'Existing User')).rejects.toThrow('User already exists');
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
});
});
});

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');
});
});

View File

@@ -1,12 +1,15 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { CreateAchievementUseCase, type IAchievementRepository } from './CreateAchievementUseCase';
import { Achievement } from '@core/identity/domain/entities/Achievement';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('CreateAchievementUseCase', () => {
let achievementRepository: {
save: Mock;
findById: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<any> & { present: Mock };
let useCase: CreateAchievementUseCase;
beforeEach(() => {
@@ -15,7 +18,22 @@ describe('CreateAchievementUseCase', () => {
findById: vi.fn(),
};
useCase = new CreateAchievementUseCase(achievementRepository as unknown as IAchievementRepository);
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new CreateAchievementUseCase(
achievementRepository as unknown as IAchievementRepository,
logger,
output,
);
});
it('creates an achievement and persists it', async () => {
@@ -29,9 +47,9 @@ describe('CreateAchievementUseCase', () => {
points: 50,
requirements: [
{
type: 'wins',
type: 'wins' as const,
value: 1,
operator: '>=',
operator: '>=' as const,
},
],
isSecret: false,
@@ -41,13 +59,12 @@ describe('CreateAchievementUseCase', () => {
const result = await useCase.execute(props);
expect(result).toBeInstanceOf(Achievement);
expect(result.id).toBe(props.id);
expect(result.name).toBe(props.name);
expect(result.description).toBe(props.description);
expect(result.category).toBe(props.category);
expect(result.points).toBe(props.points);
expect(result.requirements).toHaveLength(1);
expect(achievementRepository.save).toHaveBeenCalledWith(result);
expect(result.isOk()).toBe(true);
expect(achievementRepository.save).toHaveBeenCalledTimes(1);
const savedAchievement = achievementRepository.save.mock.calls?.[0]?.[0];
expect(savedAchievement).toBeInstanceOf(Achievement);
expect(savedAchievement.id).toBe(props.id);
expect(savedAchievement.name).toBe(props.name);
expect(output.present).toHaveBeenCalledWith({ achievement: savedAchievement });
});
});
});