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