view data fixes
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped

This commit is contained in:
2026-01-24 23:29:55 +01:00
parent c1750a33dd
commit 1b0a1f4aee
134 changed files with 10380 additions and 415 deletions

View File

@@ -0,0 +1,189 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ForgotPasswordMutation } from './ForgotPasswordMutation';
import { AuthService } from '@/lib/services/auth/AuthService';
import { Result } from '@/lib/contracts/Result';
// Mock dependencies
vi.mock('@/lib/services/auth/AuthService', () => {
return {
AuthService: vi.fn(),
};
});
describe('ForgotPasswordMutation', () => {
let mutation: ForgotPasswordMutation;
let mockServiceInstance: { forgotPassword: ReturnType<typeof vi.fn> };
beforeEach(() => {
vi.clearAllMocks();
mutation = new ForgotPasswordMutation();
mockServiceInstance = {
forgotPassword: vi.fn(),
};
// Use mockImplementation to return the instance
(AuthService as any).mockImplementation(function() {
return mockServiceInstance;
});
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully send forgot password request', async () => {
// Arrange
const input = { email: 'test@example.com' };
const serviceOutput = { message: 'Reset link sent', magicLink: 'https://example.com/reset' };
mockServiceInstance.forgotPassword.mockResolvedValue(Result.ok(serviceOutput));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(serviceOutput);
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledWith(input);
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledTimes(1);
});
it('should handle forgot password request without magicLink', async () => {
// Arrange
const input = { email: 'test@example.com' };
const serviceOutput = { message: 'Reset link sent' };
mockServiceInstance.forgotPassword.mockResolvedValue(Result.ok(serviceOutput));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(serviceOutput);
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledWith(input);
});
});
describe('failure modes', () => {
it('should handle service failure during forgot password request', async () => {
// Arrange
const input = { email: 'test@example.com' };
const serviceError = new Error('Service error');
mockServiceInstance.forgotPassword.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Service error');
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning error result', async () => {
// Arrange
const input = { email: 'test@example.com' };
const domainError = { type: 'serverError', message: 'Email not found' };
mockServiceInstance.forgotPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Email not found');
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning validation error', async () => {
// Arrange
const input = { email: 'invalid-email' };
const domainError = { type: 'validationError', message: 'Invalid email format' };
mockServiceInstance.forgotPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Invalid email format');
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning rate limit error', async () => {
// Arrange
const input = { email: 'test@example.com' };
const domainError = { type: 'rateLimit', message: 'Too many requests' };
mockServiceInstance.forgotPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Too many requests');
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const input = { email: 'test@example.com' };
const testCases = [
{ domainError: { type: 'serverError', message: 'Server error' }, expectedError: 'Server error' },
{ domainError: { type: 'validationError', message: 'Validation error' }, expectedError: 'Validation error' },
{ domainError: { type: 'notFound', message: 'Not found' }, expectedError: 'Not found' },
{ domainError: { type: 'rateLimit', message: 'Rate limit exceeded' }, expectedError: 'Rate limit exceeded' },
];
for (const testCase of testCases) {
mockServiceInstance.forgotPassword.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute(input);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('input validation', () => {
it('should accept valid email input', async () => {
// Arrange
const input = { email: 'test@example.com' };
mockServiceInstance.forgotPassword.mockResolvedValue(
Result.ok({ message: 'Reset link sent' })
);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledTimes(1);
});
it('should handle empty email gracefully', async () => {
// Arrange
const input = { email: '' };
mockServiceInstance.forgotPassword.mockResolvedValue(
Result.ok({ message: 'Reset link sent' })
);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.forgotPassword).toHaveBeenCalledWith(input);
});
});
describe('service instantiation', () => {
it('should create AuthService instance', () => {
// Arrange & Act
const mutation = new ForgotPasswordMutation();
// Assert
expect(mutation).toBeInstanceOf(ForgotPasswordMutation);
});
});
});
});

View File

@@ -0,0 +1,291 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { LoginMutation } from './LoginMutation';
import { AuthService } from '@/lib/services/auth/AuthService';
import { Result } from '@/lib/contracts/Result';
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
// Mock dependencies
vi.mock('@/lib/services/auth/AuthService', () => {
return {
AuthService: vi.fn(),
};
});
describe('LoginMutation', () => {
let mutation: LoginMutation;
let mockServiceInstance: { login: ReturnType<typeof vi.fn> };
beforeEach(() => {
vi.clearAllMocks();
mutation = new LoginMutation();
mockServiceInstance = {
login: vi.fn(),
};
// Use mockImplementation to return the instance
(AuthService as any).mockImplementation(function() {
return mockServiceInstance;
});
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully login with valid credentials', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123' };
const mockUser = {
userId: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeInstanceOf(SessionViewModel);
expect(result.unwrap().userId).toBe('user-123');
expect(result.unwrap().email).toBe('test@example.com');
expect(mockServiceInstance.login).toHaveBeenCalledWith(input);
expect(mockServiceInstance.login).toHaveBeenCalledTimes(1);
});
it('should handle login with rememberMe option', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123', rememberMe: true };
const mockUser = {
userId: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeInstanceOf(SessionViewModel);
expect(mockServiceInstance.login).toHaveBeenCalledWith(input);
});
it('should handle login with optional fields', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123' };
const mockUser = {
userId: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
role: 'user',
primaryDriverId: 'driver-456',
avatarUrl: 'https://example.com/avatar.jpg',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
const session = result.unwrap();
expect(session.userId).toBe('user-123');
expect(session.driverId).toBe('driver-456');
expect(session.avatarUrl).toBe('https://example.com/avatar.jpg');
});
});
describe('failure modes', () => {
it('should handle service failure during login', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'wrongpassword' };
const serviceError = new Error('Invalid credentials');
mockServiceInstance.login.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Invalid credentials');
expect(mockServiceInstance.login).toHaveBeenCalledTimes(1);
});
it('should handle service returning unauthorized error', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'wrongpassword' };
const domainError = { type: 'unauthorized', message: 'Invalid email or password' };
mockServiceInstance.login.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Invalid email or password');
expect(mockServiceInstance.login).toHaveBeenCalledTimes(1);
});
it('should handle service returning validation error', async () => {
// Arrange
const input = { email: 'invalid-email', password: 'password123' };
const domainError = { type: 'validationError', message: 'Invalid email format' };
mockServiceInstance.login.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Invalid email format');
expect(mockServiceInstance.login).toHaveBeenCalledTimes(1);
});
it('should handle service returning accountLocked error', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123' };
const domainError = { type: 'unauthorized', message: 'Account locked due to too many failed attempts' };
mockServiceInstance.login.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Account locked due to too many failed attempts');
expect(mockServiceInstance.login).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123' };
const testCases = [
{ domainError: { type: 'unauthorized', message: 'Invalid credentials' }, expectedError: 'Invalid credentials' },
{ domainError: { type: 'validationError', message: 'Validation failed' }, expectedError: 'Validation failed' },
{ domainError: { type: 'serverError', message: 'Server error' }, expectedError: 'Server error' },
{ domainError: { type: 'notFound', message: 'User not found' }, expectedError: 'User not found' },
];
for (const testCase of testCases) {
mockServiceInstance.login.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute(input);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('input validation', () => {
it('should accept valid email and password input', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123' };
const mockUser = {
userId: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.login).toHaveBeenCalledTimes(1);
});
it('should handle empty email gracefully', async () => {
// Arrange
const input = { email: '', password: 'password123' };
const mockUser = {
userId: 'user-123',
email: '',
displayName: 'Test User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.login).toHaveBeenCalledWith(input);
});
it('should handle empty password gracefully', async () => {
// Arrange
const input = { email: 'test@example.com', password: '' };
const mockUser = {
userId: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.login).toHaveBeenCalledWith(input);
});
});
describe('service instantiation', () => {
it('should create AuthService instance', () => {
// Arrange & Act
const mutation = new LoginMutation();
// Assert
expect(mutation).toBeInstanceOf(LoginMutation);
});
});
describe('result shape', () => {
it('should return SessionViewModel with correct properties', async () => {
// Arrange
const input = { email: 'test@example.com', password: 'password123' };
const mockUser = {
userId: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
role: 'admin',
primaryDriverId: 'driver-456',
avatarUrl: 'https://example.com/avatar.jpg',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.login.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
const session = result.unwrap();
expect(session).toBeInstanceOf(SessionViewModel);
expect(session.userId).toBe('user-123');
expect(session.email).toBe('test@example.com');
expect(session.displayName).toBe('Test User');
expect(session.role).toBe('admin');
expect(session.driverId).toBe('driver-456');
expect(session.avatarUrl).toBe('https://example.com/avatar.jpg');
expect(session.isAuthenticated).toBe(true);
});
});
});
});

View File

@@ -0,0 +1,135 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { LogoutMutation } from './LogoutMutation';
import { AuthService } from '@/lib/services/auth/AuthService';
import { Result } from '@/lib/contracts/Result';
// Mock dependencies
vi.mock('@/lib/services/auth/AuthService', () => {
return {
AuthService: vi.fn(),
};
});
describe('LogoutMutation', () => {
let mutation: LogoutMutation;
let mockServiceInstance: { logout: ReturnType<typeof vi.fn> };
beforeEach(() => {
vi.clearAllMocks();
mutation = new LogoutMutation();
mockServiceInstance = {
logout: vi.fn(),
};
// Use mockImplementation to return the instance
(AuthService as any).mockImplementation(function() {
return mockServiceInstance;
});
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully logout', async () => {
// Arrange
mockServiceInstance.logout.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute();
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
});
});
describe('failure modes', () => {
it('should handle service failure during logout', async () => {
// Arrange
const serviceError = new Error('Session expired');
mockServiceInstance.logout.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute();
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Session expired');
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
});
it('should handle service returning error result', async () => {
// Arrange
const domainError = { type: 'serverError', message: 'Failed to clear session' };
mockServiceInstance.logout.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute();
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Failed to clear session');
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
});
it('should handle service returning unauthorized error', async () => {
// Arrange
const domainError = { type: 'unauthorized', message: 'Not authenticated' };
mockServiceInstance.logout.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute();
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Not authenticated');
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const testCases = [
{ domainError: { type: 'serverError', message: 'Server error' }, expectedError: 'Server error' },
{ domainError: { type: 'unauthorized', message: 'Unauthorized' }, expectedError: 'Unauthorized' },
{ domainError: { type: 'notFound', message: 'Session not found' }, expectedError: 'Session not found' },
{ domainError: { type: 'networkError', message: 'Network error' }, expectedError: 'Network error' },
];
for (const testCase of testCases) {
mockServiceInstance.logout.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute();
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('service instantiation', () => {
it('should create AuthService instance', () => {
// Arrange & Act
const mutation = new LogoutMutation();
// Assert
expect(mutation).toBeInstanceOf(LogoutMutation);
});
});
describe('result shape', () => {
it('should return void result on success', async () => {
// Arrange
mockServiceInstance.logout.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute();
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(typeof result.unwrap()).toBe('undefined');
});
});
});
});

View File

@@ -14,7 +14,10 @@ export class LogoutMutation {
async execute(): Promise<Result<void, string>> {
try {
const authService = new AuthService();
await authService.logout();
const result = await authService.logout();
if (result.isErr()) {
return Result.err(result.getError().message);
}
return Result.ok(undefined);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Logout failed';

View File

@@ -0,0 +1,237 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ResetPasswordMutation } from './ResetPasswordMutation';
import { AuthService } from '@/lib/services/auth/AuthService';
import { Result } from '@/lib/contracts/Result';
// Mock dependencies
vi.mock('@/lib/services/auth/AuthService', () => {
return {
AuthService: vi.fn(),
};
});
describe('ResetPasswordMutation', () => {
let mutation: ResetPasswordMutation;
let mockServiceInstance: { resetPassword: ReturnType<typeof vi.fn> };
beforeEach(() => {
vi.clearAllMocks();
mutation = new ResetPasswordMutation();
mockServiceInstance = {
resetPassword: vi.fn(),
};
// Use mockImplementation to return the instance
(AuthService as any).mockImplementation(function() {
return mockServiceInstance;
});
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully reset password with valid token', async () => {
// Arrange
const input = { token: 'valid-token-123', newPassword: 'newSecurePassword123' };
const serviceOutput = { message: 'Password reset successfully' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.ok(serviceOutput));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(serviceOutput);
expect(mockServiceInstance.resetPassword).toHaveBeenCalledWith(input);
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
it('should handle reset password with complex password', async () => {
// Arrange
const input = { token: 'valid-token-456', newPassword: 'ComplexP@ssw0rd!2024' };
const serviceOutput = { message: 'Password reset successfully' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.ok(serviceOutput));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(serviceOutput);
expect(mockServiceInstance.resetPassword).toHaveBeenCalledWith(input);
});
});
describe('failure modes', () => {
it('should handle service failure during password reset', async () => {
// Arrange
const input = { token: 'expired-token', newPassword: 'newPassword123' };
const serviceError = new Error('Token expired');
mockServiceInstance.resetPassword.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Token expired');
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning error result', async () => {
// Arrange
const input = { token: 'invalid-token', newPassword: 'newPassword123' };
const domainError = { type: 'validationError', message: 'Invalid token' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Invalid token');
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning tokenExpired error', async () => {
// Arrange
const input = { token: 'expired-token', newPassword: 'newPassword123' };
const domainError = { type: 'validationError', message: 'Reset token has expired' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Reset token has expired');
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning weakPassword error', async () => {
// Arrange
const input = { token: 'valid-token', newPassword: 'weak' };
const domainError = { type: 'validationError', message: 'Password does not meet requirements' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Password does not meet requirements');
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
it('should handle service returning serverError', async () => {
// Arrange
const input = { token: 'valid-token', newPassword: 'newPassword123' };
const domainError = { type: 'serverError', message: 'Database connection failed' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Database connection failed');
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const input = { token: 'valid-token', newPassword: 'newPassword123' };
const testCases = [
{ domainError: { type: 'validationError', message: 'Invalid token' }, expectedError: 'Invalid token' },
{ domainError: { type: 'serverError', message: 'Server error' }, expectedError: 'Server error' },
{ domainError: { type: 'notFound', message: 'User not found' }, expectedError: 'User not found' },
{ domainError: { type: 'unauthorized', message: 'Unauthorized' }, expectedError: 'Unauthorized' },
];
for (const testCase of testCases) {
mockServiceInstance.resetPassword.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute(input);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('input validation', () => {
it('should accept valid token and password input', async () => {
// Arrange
const input = { token: 'valid-token-123', newPassword: 'newSecurePassword123' };
mockServiceInstance.resetPassword.mockResolvedValue(
Result.ok({ message: 'Password reset successfully' })
);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.resetPassword).toHaveBeenCalledTimes(1);
});
it('should handle empty token gracefully', async () => {
// Arrange
const input = { token: '', newPassword: 'newPassword123' };
mockServiceInstance.resetPassword.mockResolvedValue(
Result.ok({ message: 'Password reset successfully' })
);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.resetPassword).toHaveBeenCalledWith(input);
});
it('should handle empty password gracefully', async () => {
// Arrange
const input = { token: 'valid-token', newPassword: '' };
mockServiceInstance.resetPassword.mockResolvedValue(
Result.ok({ message: 'Password reset successfully' })
);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.resetPassword).toHaveBeenCalledWith(input);
});
});
describe('service instantiation', () => {
it('should create AuthService instance', () => {
// Arrange & Act
const mutation = new ResetPasswordMutation();
// Assert
expect(mutation).toBeInstanceOf(ResetPasswordMutation);
});
});
describe('result shape', () => {
it('should return message on success', async () => {
// Arrange
const input = { token: 'valid-token', newPassword: 'newPassword123' };
const serviceOutput = { message: 'Password reset successfully' };
mockServiceInstance.resetPassword.mockResolvedValue(Result.ok(serviceOutput));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
const resultData = result.unwrap();
expect(resultData).toEqual(serviceOutput);
expect(resultData.message).toBe('Password reset successfully');
});
});
});
});

View File

@@ -0,0 +1,411 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SignupMutation } from './SignupMutation';
import { AuthService } from '@/lib/services/auth/AuthService';
import { Result } from '@/lib/contracts/Result';
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
// Mock dependencies
vi.mock('@/lib/services/auth/AuthService', () => {
return {
AuthService: vi.fn(),
};
});
describe('SignupMutation', () => {
let mutation: SignupMutation;
let mockServiceInstance: { signup: ReturnType<typeof vi.fn> };
beforeEach(() => {
vi.clearAllMocks();
mutation = new SignupMutation();
mockServiceInstance = {
signup: vi.fn(),
};
// Use mockImplementation to return the instance
(AuthService as any).mockImplementation(function() {
return mockServiceInstance;
});
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully signup with valid credentials', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'SecurePassword123!',
displayName: 'New User',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'New User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeInstanceOf(SessionViewModel);
expect(result.unwrap().userId).toBe('user-789');
expect(result.unwrap().email).toBe('newuser@example.com');
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
it('should handle signup with optional username', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'SecurePassword123!',
displayName: 'New User',
username: 'newuser',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'New User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
});
it('should handle signup with iRacing customer ID', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'SecurePassword123!',
displayName: 'New User',
iracingCustomerId: '123456',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'New User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
});
it('should handle signup with all optional fields', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'SecurePassword123!',
displayName: 'New User',
username: 'newuser',
iracingCustomerId: '123456',
primaryDriverId: 'driver-789',
avatarUrl: 'https://example.com/avatar.jpg',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'New User',
role: 'user',
primaryDriverId: 'driver-789',
avatarUrl: 'https://example.com/avatar.jpg',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
const session = result.unwrap();
expect(session.driverId).toBe('driver-789');
expect(session.avatarUrl).toBe('https://example.com/avatar.jpg');
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
});
});
describe('failure modes', () => {
it('should handle service failure during signup', async () => {
// Arrange
const input = {
email: 'existing@example.com',
password: 'Password123!',
displayName: 'Existing User',
};
const serviceError = new Error('Email already exists');
mockServiceInstance.signup.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Email already exists');
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
it('should handle service returning validation error', async () => {
// Arrange
const input = {
email: 'invalid-email',
password: 'Password123!',
displayName: 'User',
};
const domainError = { type: 'validationError', message: 'Invalid email format' };
mockServiceInstance.signup.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Invalid email format');
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
it('should handle service returning duplicate email error', async () => {
// Arrange
const input = {
email: 'existing@example.com',
password: 'Password123!',
displayName: 'Existing User',
};
const domainError = { type: 'validationError', message: 'Email already registered' };
mockServiceInstance.signup.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Email already registered');
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
it('should handle service returning weak password error', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'weak',
displayName: 'User',
};
const domainError = { type: 'validationError', message: 'Password too weak' };
mockServiceInstance.signup.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Password too weak');
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
it('should handle service returning server error', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'Password123!',
displayName: 'User',
};
const domainError = { type: 'serverError', message: 'Database connection failed' };
mockServiceInstance.signup.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Database connection failed');
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'Password123!',
displayName: 'User',
};
const testCases = [
{ domainError: { type: 'validationError', message: 'Invalid data' }, expectedError: 'Invalid data' },
{ domainError: { type: 'serverError', message: 'Server error' }, expectedError: 'Server error' },
{ domainError: { type: 'notFound', message: 'Resource not found' }, expectedError: 'Resource not found' },
{ domainError: { type: 'unauthorized', message: 'Unauthorized' }, expectedError: 'Unauthorized' },
];
for (const testCase of testCases) {
mockServiceInstance.signup.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute(input);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('input validation', () => {
it('should accept valid email, password, and displayName input', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'Password123!',
displayName: 'New User',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'New User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.signup).toHaveBeenCalledTimes(1);
});
it('should handle empty email gracefully', async () => {
// Arrange
const input = {
email: '',
password: 'Password123!',
displayName: 'User',
};
const mockUser = {
userId: 'user-789',
email: '',
displayName: 'User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
});
it('should handle empty password gracefully', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: '',
displayName: 'User',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'User',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
});
it('should handle empty displayName gracefully', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'Password123!',
displayName: '',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: '',
role: 'user',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.signup).toHaveBeenCalledWith(input);
});
});
describe('service instantiation', () => {
it('should create AuthService instance', () => {
// Arrange & Act
const mutation = new SignupMutation();
// Assert
expect(mutation).toBeInstanceOf(SignupMutation);
});
});
describe('result shape', () => {
it('should return SessionViewModel with correct properties on success', async () => {
// Arrange
const input = {
email: 'newuser@example.com',
password: 'Password123!',
displayName: 'New User',
};
const mockUser = {
userId: 'user-789',
email: 'newuser@example.com',
displayName: 'New User',
role: 'user',
primaryDriverId: 'driver-456',
avatarUrl: 'https://example.com/avatar.jpg',
};
const sessionViewModel = new SessionViewModel(mockUser);
mockServiceInstance.signup.mockResolvedValue(Result.ok(sessionViewModel));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
const session = result.unwrap();
expect(session).toBeInstanceOf(SessionViewModel);
expect(session.userId).toBe('user-789');
expect(session.email).toBe('newuser@example.com');
expect(session.displayName).toBe('New User');
expect(session.role).toBe('user');
expect(session.driverId).toBe('driver-456');
expect(session.avatarUrl).toBe('https://example.com/avatar.jpg');
expect(session.isAuthenticated).toBe(true);
});
});
});
});