unit tests

This commit is contained in:
jackiettran
2025-12-12 16:27:56 -05:00
parent 25bbf5d20b
commit 3f319bfdd0
24 changed files with 4282 additions and 1806 deletions

View File

@@ -0,0 +1,297 @@
// Mock AWS SDK before requiring modules
jest.mock('@aws-sdk/client-ses', () => ({
SESClient: jest.fn().mockImplementation(() => ({
send: jest.fn(),
})),
SendEmailCommand: jest.fn(),
}));
jest.mock('../../../../config/aws', () => ({
getAWSConfig: jest.fn(() => ({ region: 'us-east-1' })),
}));
jest.mock('../../../../services/email/core/emailUtils', () => ({
htmlToPlainText: jest.fn((html) => html.replace(/<[^>]*>/g, '')),
}));
// Clear singleton between tests
beforeEach(() => {
jest.clearAllMocks();
// Reset the singleton instance
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
});
describe('EmailClient', () => {
const { SESClient, SendEmailCommand } = require('@aws-sdk/client-ses');
const { getAWSConfig } = require('../../../../config/aws');
describe('constructor', () => {
it('should create a new instance', () => {
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
expect(client).toBeDefined();
expect(client.sesClient).toBeNull();
expect(client.initialized).toBe(false);
});
it('should return existing instance (singleton pattern)', () => {
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client1 = new EmailClient();
const client2 = new EmailClient();
expect(client1).toBe(client2);
});
});
describe('initialize', () => {
it('should initialize SES client with AWS config', async () => {
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
await client.initialize();
expect(getAWSConfig).toHaveBeenCalled();
expect(SESClient).toHaveBeenCalledWith({ region: 'us-east-1' });
expect(client.initialized).toBe(true);
});
it('should not re-initialize if already initialized', async () => {
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
await client.initialize();
await client.initialize();
expect(SESClient).toHaveBeenCalledTimes(1);
});
it('should wait for existing initialization if in progress', async () => {
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
// Start two initializations concurrently
const [result1, result2] = await Promise.all([
client.initialize(),
client.initialize(),
]);
expect(SESClient).toHaveBeenCalledTimes(1);
});
it('should throw error if AWS config fails', async () => {
getAWSConfig.mockImplementationOnce(() => {
throw new Error('AWS config error');
});
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
await expect(client.initialize()).rejects.toThrow('AWS config error');
});
});
describe('sendEmail', () => {
const originalEnv = process.env;
beforeEach(() => {
process.env = {
...originalEnv,
EMAIL_ENABLED: 'true',
SES_FROM_EMAIL: 'noreply@rentall.com',
SES_FROM_NAME: 'RentAll',
};
});
afterEach(() => {
process.env = originalEnv;
});
it('should return early if EMAIL_ENABLED is not true', async () => {
process.env.EMAIL_ENABLED = 'false';
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
const result = await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello</p>'
);
expect(result).toEqual({ success: true, messageId: 'disabled' });
});
it('should return early if EMAIL_ENABLED is not set', async () => {
delete process.env.EMAIL_ENABLED;
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
const result = await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello</p>'
);
expect(result).toEqual({ success: true, messageId: 'disabled' });
});
it('should send email with correct parameters', async () => {
const mockSend = jest.fn().mockResolvedValue({ MessageId: 'msg-123' });
SESClient.mockImplementation(() => ({ send: mockSend }));
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
const result = await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello World</p>'
);
expect(SendEmailCommand).toHaveBeenCalledWith({
Source: 'RentAll <noreply@rentall.com>',
Destination: {
ToAddresses: ['test@example.com'],
},
Message: {
Subject: {
Data: 'Test Subject',
Charset: 'UTF-8',
},
Body: {
Html: {
Data: '<p>Hello World</p>',
Charset: 'UTF-8',
},
Text: {
Data: expect.any(String),
Charset: 'UTF-8',
},
},
},
});
expect(result).toEqual({ success: true, messageId: 'msg-123' });
});
it('should send to multiple recipients', async () => {
const mockSend = jest.fn().mockResolvedValue({ MessageId: 'msg-456' });
SESClient.mockImplementation(() => ({ send: mockSend }));
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
await client.sendEmail(
['user1@example.com', 'user2@example.com'],
'Test Subject',
'<p>Hello</p>'
);
expect(SendEmailCommand).toHaveBeenCalledWith(
expect.objectContaining({
Destination: {
ToAddresses: ['user1@example.com', 'user2@example.com'],
},
})
);
});
it('should use provided text content', async () => {
const mockSend = jest.fn().mockResolvedValue({ MessageId: 'msg-789' });
SESClient.mockImplementation(() => ({ send: mockSend }));
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello</p>',
'Custom plain text'
);
expect(SendEmailCommand).toHaveBeenCalledWith(
expect.objectContaining({
Message: expect.objectContaining({
Body: expect.objectContaining({
Text: {
Data: 'Custom plain text',
Charset: 'UTF-8',
},
}),
}),
})
);
});
it('should add reply-to address if configured', async () => {
process.env.SES_REPLY_TO_EMAIL = 'support@rentall.com';
const mockSend = jest.fn().mockResolvedValue({ MessageId: 'msg-000' });
SESClient.mockImplementation(() => ({ send: mockSend }));
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello</p>'
);
expect(SendEmailCommand).toHaveBeenCalledWith(
expect.objectContaining({
ReplyToAddresses: ['support@rentall.com'],
})
);
});
it('should return error if send fails', async () => {
const mockSend = jest.fn().mockRejectedValue(new Error('SES send failed'));
SESClient.mockImplementation(() => ({ send: mockSend }));
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
const result = await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello</p>'
);
expect(result).toEqual({ success: false, error: 'SES send failed' });
});
it('should auto-initialize if not initialized', async () => {
const mockSend = jest.fn().mockResolvedValue({ MessageId: 'msg-auto' });
SESClient.mockImplementation(() => ({ send: mockSend }));
const EmailClient = require('../../../../services/email/core/EmailClient');
EmailClient.instance = null;
const client = new EmailClient();
expect(client.initialized).toBe(false);
await client.sendEmail(
'test@example.com',
'Test Subject',
'<p>Hello</p>'
);
expect(client.initialized).toBe(true);
});
});
});

View File

@@ -0,0 +1,281 @@
// Mock fs before requiring modules
jest.mock('fs', () => ({
promises: {
readFile: jest.fn(),
},
}));
// Clear singleton between tests
beforeEach(() => {
jest.clearAllMocks();
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
});
describe('TemplateManager', () => {
const fs = require('fs').promises;
describe('constructor', () => {
it('should create a new instance', () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
expect(manager).toBeDefined();
expect(manager.templates).toBeInstanceOf(Map);
expect(manager.initialized).toBe(false);
});
it('should return existing instance (singleton pattern)', () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager1 = new TemplateManager();
const manager2 = new TemplateManager();
expect(manager1).toBe(manager2);
});
});
describe('initialize', () => {
it('should load all templates on initialization', async () => {
// Mock fs.readFile to return template content
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
expect(manager.initialized).toBe(true);
expect(fs.readFile).toHaveBeenCalled();
});
it('should not re-initialize if already initialized', async () => {
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
const callCount = fs.readFile.mock.calls.length;
await manager.initialize();
expect(fs.readFile.mock.calls.length).toBe(callCount);
});
it('should wait for existing initialization if in progress', async () => {
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
// Start two initializations concurrently
await Promise.all([manager.initialize(), manager.initialize()]);
// Should only load templates once
const uniquePaths = new Set(fs.readFile.mock.calls.map((call) => call[0]));
expect(uniquePaths.size).toBeLessThanOrEqual(fs.readFile.mock.calls.length);
});
it('should throw error if critical templates fail to load', async () => {
// All template files fail to load
fs.readFile.mockRejectedValue(new Error('File not found'));
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await expect(manager.initialize()).rejects.toThrow('Critical email templates failed to load');
});
it('should succeed if critical templates load but non-critical fail', async () => {
const criticalTemplates = [
'emailVerificationToUser',
'passwordResetToUser',
'passwordChangedToUser',
'personalInfoChangedToUser',
];
fs.readFile.mockImplementation((path) => {
const isCritical = criticalTemplates.some((t) => path.includes(t));
if (isCritical) {
return Promise.resolve('<html>Template content</html>');
}
return Promise.reject(new Error('File not found'));
});
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
// Should not throw since critical templates loaded
await expect(manager.initialize()).resolves.not.toThrow();
expect(manager.initialized).toBe(true);
});
});
describe('renderTemplate', () => {
beforeEach(() => {
fs.readFile.mockResolvedValue('<html>Hello {{name}}, your email is {{email}}</html>');
});
it('should render template with variables', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
// Manually set a template for testing
manager.templates.set('testTemplate', '<html>Hello {{name}}, your email is {{email}}</html>');
const result = await manager.renderTemplate('testTemplate', {
name: 'John',
email: 'john@example.com',
});
expect(result).toBe('<html>Hello John, your email is john@example.com</html>');
});
it('should replace all occurrences of a variable', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
manager.templates.set('testTemplate', '<html>{{name}} {{name}} {{name}}</html>');
const result = await manager.renderTemplate('testTemplate', {
name: 'John',
});
expect(result).toBe('<html>John John John</html>');
});
it('should replace missing variables with empty string', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
manager.templates.set('testTemplate', '<html>Hello {{name}}, {{missing}}</html>');
const result = await manager.renderTemplate('testTemplate', {
name: 'John',
});
expect(result).toBe('<html>Hello John, {{missing}}</html>');
});
it('should use fallback template when template not found', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
const result = await manager.renderTemplate('nonExistentTemplate', {
title: 'Test Title',
message: 'Test Message',
});
// Should return fallback template content
expect(result).toContain('Test Title');
expect(result).toContain('Test Message');
expect(result).toContain('RentAll');
});
it('should auto-initialize if not initialized', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
expect(manager.initialized).toBe(false);
await manager.renderTemplate('someTemplate', {});
expect(manager.initialized).toBe(true);
});
it('should handle empty variables object', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
manager.templates.set('testTemplate', '<html>No variables</html>');
const result = await manager.renderTemplate('testTemplate', {});
expect(result).toBe('<html>No variables</html>');
});
it('should handle null or undefined variable values', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
await manager.initialize();
manager.templates.set('testTemplate', '<html>Hello {{name}}</html>');
const result = await manager.renderTemplate('testTemplate', {
name: null,
});
expect(result).toBe('<html>Hello </html>');
});
});
describe('getFallbackTemplate', () => {
it('should return specific fallback for known templates', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
const fallback = manager.getFallbackTemplate('emailVerificationToUser');
expect(fallback).toContain('Verify Your Email');
expect(fallback).toContain('{{verificationUrl}}');
});
it('should return specific fallback for password reset', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
const fallback = manager.getFallbackTemplate('passwordResetToUser');
expect(fallback).toContain('Reset Your Password');
expect(fallback).toContain('{{resetUrl}}');
});
it('should return specific fallback for rental request', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
const fallback = manager.getFallbackTemplate('rentalRequestToOwner');
expect(fallback).toContain('New Rental Request');
expect(fallback).toContain('{{itemName}}');
});
it('should return generic fallback for unknown templates', async () => {
const TemplateManager = require('../../../../services/email/core/TemplateManager');
TemplateManager.instance = null;
const manager = new TemplateManager();
const fallback = manager.getFallbackTemplate('unknownTemplate');
expect(fallback).toContain('{{title}}');
expect(fallback).toContain('{{message}}');
expect(fallback).toContain('RentAll');
});
});
});

View File

@@ -0,0 +1,152 @@
const {
htmlToPlainText,
formatEmailDate,
formatShortDate,
formatCurrency,
} = require('../../../../services/email/core/emailUtils');
describe('Email Utils', () => {
describe('htmlToPlainText', () => {
it('should remove HTML tags', () => {
const html = '<p>Hello <strong>World</strong></p>';
const result = htmlToPlainText(html);
expect(result).toBe('Hello World');
});
it('should convert br tags to newlines', () => {
const html = 'Line 1<br>Line 2<br/>Line 3';
const result = htmlToPlainText(html);
expect(result).toBe('Line 1\nLine 2\nLine 3');
});
it('should convert p tags to double newlines', () => {
const html = '<p>Paragraph 1</p><p>Paragraph 2</p>';
const result = htmlToPlainText(html);
expect(result).toContain('Paragraph 1');
expect(result).toContain('Paragraph 2');
});
it('should convert li tags to bullet points', () => {
const html = '<ul><li>Item 1</li><li>Item 2</li></ul>';
const result = htmlToPlainText(html);
expect(result).toContain('• Item 1');
expect(result).toContain('• Item 2');
});
it('should remove style tags and their content', () => {
const html = '<style>.class { color: red; }</style><p>Content</p>';
const result = htmlToPlainText(html);
expect(result).toBe('Content');
expect(result).not.toContain('color');
});
it('should remove script tags and their content', () => {
const html = '<script>alert("test")</script><p>Content</p>';
const result = htmlToPlainText(html);
expect(result).toBe('Content');
expect(result).not.toContain('alert');
});
it('should decode HTML entities', () => {
const html = '&amp; &lt; &gt; &quot; &#39; &nbsp;';
const result = htmlToPlainText(html);
expect(result).toContain('&');
expect(result).toContain('<');
expect(result).toContain('>');
expect(result).toContain('"');
expect(result).toContain("'");
});
it('should handle empty string', () => {
expect(htmlToPlainText('')).toBe('');
});
it('should trim whitespace', () => {
const html = ' <p>Content</p> ';
const result = htmlToPlainText(html);
expect(result).toBe('Content');
});
it('should collapse multiple newlines', () => {
const html = '<p>Line 1</p>\n\n\n\n<p>Line 2</p>';
const result = htmlToPlainText(html);
expect(result).not.toMatch(/\n{4,}/);
});
});
describe('formatEmailDate', () => {
it('should format a Date object', () => {
const date = new Date('2024-03-15T14:30:00');
const result = formatEmailDate(date);
expect(result).toContain('March');
expect(result).toContain('15');
expect(result).toContain('2024');
});
it('should format a date string', () => {
const dateStr = '2024-06-20T10:00:00';
const result = formatEmailDate(dateStr);
expect(result).toContain('June');
expect(result).toContain('20');
expect(result).toContain('2024');
});
it('should include day of week', () => {
const date = new Date('2024-03-15T14:30:00'); // Friday
const result = formatEmailDate(date);
expect(result).toContain('Friday');
});
it('should include time', () => {
const date = new Date('2024-03-15T14:30:00');
const result = formatEmailDate(date);
expect(result).toMatch(/\d{1,2}:\d{2}/);
});
});
describe('formatShortDate', () => {
it('should format a Date object without time', () => {
const date = new Date('2024-03-15T14:30:00');
const result = formatShortDate(date);
expect(result).toContain('March');
expect(result).toContain('15');
expect(result).toContain('2024');
expect(result).not.toMatch(/\d{1,2}:\d{2}/); // No time
});
it('should format a date string', () => {
const dateStr = '2024-12-25T00:00:00';
const result = formatShortDate(dateStr);
expect(result).toContain('December');
expect(result).toContain('25');
expect(result).toContain('2024');
});
});
describe('formatCurrency', () => {
it('should format amount in cents to USD', () => {
const result = formatCurrency(1000);
expect(result).toBe('$10.00');
});
it('should handle decimal amounts', () => {
const result = formatCurrency(1050);
expect(result).toBe('$10.50');
});
it('should handle large amounts', () => {
const result = formatCurrency(100000);
expect(result).toBe('$1,000.00');
});
it('should handle zero', () => {
const result = formatCurrency(0);
expect(result).toBe('$0.00');
});
it('should accept currency parameter', () => {
const result = formatCurrency(1000, 'EUR');
expect(result).toContain('€');
});
});
});