update
This commit is contained in:
154
test/test.smtp.client.compatibility.ts
Normal file
154
test/test.smtp.client.compatibility.ts
Normal file
@ -0,0 +1,154 @@
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import { SmtpClient } from '../ts/mail/delivery/classes.smtp.client.js';
|
||||
import type { ISmtpClientOptions } from '../ts/mail/delivery/classes.smtp.client.js';
|
||||
import { Email } from '../ts/mail/core/classes.email.js';
|
||||
|
||||
/**
|
||||
* Compatibility tests for the legacy SMTP client facade
|
||||
*/
|
||||
|
||||
tap.test('verify backward compatibility - client creation', async () => {
|
||||
// Create test configuration
|
||||
const options: ISmtpClientOptions = {
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
connectionTimeout: 10000,
|
||||
domain: 'test.example.com'
|
||||
};
|
||||
|
||||
// Create SMTP client instance using legacy constructor
|
||||
const smtpClient = new SmtpClient(options);
|
||||
|
||||
// Verify instance was created correctly
|
||||
expect(smtpClient).toBeTruthy();
|
||||
expect(smtpClient.isConnected()).toBeFalsy(); // Should start disconnected
|
||||
});
|
||||
|
||||
tap.test('verify backward compatibility - methods exist', async () => {
|
||||
const options: ISmtpClientOptions = {
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false
|
||||
};
|
||||
|
||||
const smtpClient = new SmtpClient(options);
|
||||
|
||||
// Verify all expected methods exist
|
||||
expect(typeof smtpClient.sendMail === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.verify === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.isConnected === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.getPoolStatus === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.updateOptions === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.close === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.on === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.off === 'function').toBeTruthy();
|
||||
expect(typeof smtpClient.emit === 'function').toBeTruthy();
|
||||
});
|
||||
|
||||
tap.test('verify backward compatibility - options update', async () => {
|
||||
const options: ISmtpClientOptions = {
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false
|
||||
};
|
||||
|
||||
const smtpClient = new SmtpClient(options);
|
||||
|
||||
// Test option updates don't throw
|
||||
expect(() => smtpClient.updateOptions({
|
||||
host: 'new-smtp.example.com',
|
||||
port: 465,
|
||||
secure: true
|
||||
})).not.toThrow();
|
||||
|
||||
expect(() => smtpClient.updateOptions({
|
||||
debug: true,
|
||||
connectionTimeout: 5000
|
||||
})).not.toThrow();
|
||||
});
|
||||
|
||||
tap.test('verify backward compatibility - connection failure handling', async () => {
|
||||
const options: ISmtpClientOptions = {
|
||||
host: 'nonexistent.invalid.domain',
|
||||
port: 587,
|
||||
secure: false,
|
||||
connectionTimeout: 1000 // Short timeout for faster test
|
||||
};
|
||||
|
||||
const smtpClient = new SmtpClient(options);
|
||||
|
||||
// verify() should return false for invalid hosts
|
||||
const isValid = await smtpClient.verify();
|
||||
expect(isValid).toBeFalsy();
|
||||
|
||||
// sendMail should fail gracefully for invalid hosts
|
||||
const email = new Email({
|
||||
from: 'test@example.com',
|
||||
to: 'recipient@example.com',
|
||||
subject: 'Test Email',
|
||||
text: 'This is a test email'
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await smtpClient.sendMail(email);
|
||||
expect(result.success).toBeFalsy();
|
||||
expect(result.error).toBeTruthy();
|
||||
} catch (error) {
|
||||
// Connection errors are expected for invalid domains
|
||||
expect(error).toBeTruthy();
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('verify backward compatibility - pool status', async () => {
|
||||
const options: ISmtpClientOptions = {
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
pool: true,
|
||||
maxConnections: 5
|
||||
};
|
||||
|
||||
const smtpClient = new SmtpClient(options);
|
||||
|
||||
// Get pool status
|
||||
const status = smtpClient.getPoolStatus();
|
||||
expect(status).toBeTruthy();
|
||||
expect(typeof status.total === 'number').toBeTruthy();
|
||||
expect(typeof status.active === 'number').toBeTruthy();
|
||||
expect(typeof status.idle === 'number').toBeTruthy();
|
||||
expect(typeof status.pending === 'number').toBeTruthy();
|
||||
|
||||
// Initially should have no connections
|
||||
expect(status.total).toEqual(0);
|
||||
expect(status.active).toEqual(0);
|
||||
expect(status.idle).toEqual(0);
|
||||
expect(status.pending).toEqual(0);
|
||||
});
|
||||
|
||||
tap.test('verify backward compatibility - event handling', async () => {
|
||||
const options: ISmtpClientOptions = {
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false
|
||||
};
|
||||
|
||||
const smtpClient = new SmtpClient(options);
|
||||
|
||||
// Test event listener methods don't throw
|
||||
const testListener = () => {};
|
||||
|
||||
expect(() => smtpClient.on('test', testListener)).not.toThrow();
|
||||
expect(() => smtpClient.off('test', testListener)).not.toThrow();
|
||||
expect(() => smtpClient.emit('test')).not.toThrow();
|
||||
});
|
||||
|
||||
tap.test('clean up after compatibility tests', async () => {
|
||||
// No-op - just to make sure everything is cleaned up properly
|
||||
});
|
||||
|
||||
tap.test('stop', async () => {
|
||||
await tap.stopForcefully();
|
||||
});
|
||||
|
||||
export default tap.start();
|
@ -1,7 +1,7 @@
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import * as plugins from '../ts/plugins.js';
|
||||
import * as paths from '../ts/paths.js';
|
||||
import { SMTPServer } from '../ts/mail/delivery/classes.smtpserver.js';
|
||||
import { createSmtpServer } from '../ts/mail/delivery/smtpserver/index.js';
|
||||
import { UnifiedEmailServer } from '../ts/mail/routing/classes.unified.email.server.js';
|
||||
import { Email } from '../ts/mail/core/classes.email.js';
|
||||
import type { ISmtpServerOptions } from '../ts/mail/delivery/interfaces.js';
|
||||
@ -29,7 +29,7 @@ tap.test('verify SMTP server initialization', async () => {
|
||||
};
|
||||
|
||||
// Create SMTP server instance
|
||||
const smtpServer = new SMTPServer(mockEmailServer, options);
|
||||
const smtpServer = createSmtpServer(mockEmailServer, options);
|
||||
|
||||
// Verify instance was created correctly
|
||||
expect(smtpServer).toBeTruthy();
|
||||
@ -62,7 +62,7 @@ tap.test('verify SMTP server listen method', async () => {
|
||||
};
|
||||
|
||||
// Create SMTP server instance
|
||||
const smtpServer = new SMTPServer(mockEmailServer, options);
|
||||
const smtpServer = createSmtpServer(mockEmailServer, options);
|
||||
|
||||
// Mock net.Server.listen and net.Server.close to avoid actual networking
|
||||
const originalListen = smtpServer.server.listen;
|
||||
@ -118,7 +118,7 @@ tap.test('verify SMTP server error handling', async () => {
|
||||
};
|
||||
|
||||
// Create SMTP server instance
|
||||
const smtpServer = new SMTPServer(mockEmailServer, options);
|
||||
const smtpServer = createSmtpServer(mockEmailServer, options);
|
||||
|
||||
// Mock server.listen to simulate an error
|
||||
const originalListen = smtpServer.server.listen;
|
||||
|
Reference in New Issue
Block a user