149 lines
3.7 KiB
TypeScript
149 lines
3.7 KiB
TypeScript
import * as plugins from '../../ts/plugins.js';
|
|
|
|
export interface ITestServerConfig {
|
|
port: number;
|
|
hostname?: string;
|
|
tlsEnabled?: boolean;
|
|
authRequired?: boolean;
|
|
timeout?: number;
|
|
testCertPath?: string;
|
|
testKeyPath?: string;
|
|
maxConnections?: number;
|
|
size?: number;
|
|
maxRecipients?: number;
|
|
}
|
|
|
|
export interface ITestServer {
|
|
server: any;
|
|
smtpServer: any;
|
|
port: number;
|
|
hostname: string;
|
|
config: ITestServerConfig;
|
|
startTime: number;
|
|
}
|
|
|
|
/**
|
|
* Starts a test SMTP server with the given configuration.
|
|
*
|
|
* NOTE: The TS SMTP server implementation was removed in Phase 7B
|
|
* (replaced by the Rust SMTP server). This stub preserves the interface
|
|
* for smtpclient tests that import it, but those tests require `node-forge`
|
|
* which is not installed (pre-existing issue).
|
|
*/
|
|
export async function startTestServer(_config: ITestServerConfig): Promise<ITestServer> {
|
|
throw new Error(
|
|
'startTestServer is no longer available — the TS SMTP server was removed in Phase 7B. ' +
|
|
'Use the Rust SMTP server (via UnifiedEmailServer) for integration testing.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Stops a test SMTP server
|
|
*/
|
|
export async function stopTestServer(testServer: ITestServer): Promise<void> {
|
|
if (!testServer || !testServer.smtpServer) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (testServer.smtpServer.close && typeof testServer.smtpServer.close === 'function') {
|
|
await testServer.smtpServer.close();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error stopping test server:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get an available port for testing
|
|
*/
|
|
export async function getAvailablePort(startPort: number = 25000): Promise<number> {
|
|
for (let port = startPort; port < startPort + 1000; port++) {
|
|
if (await isPortFree(port)) {
|
|
return port;
|
|
}
|
|
}
|
|
throw new Error(`No available ports found starting from ${startPort}`);
|
|
}
|
|
|
|
/**
|
|
* Check if a port is free
|
|
*/
|
|
async function isPortFree(port: number): Promise<boolean> {
|
|
return new Promise((resolve) => {
|
|
const server = plugins.net.createServer();
|
|
|
|
server.listen(port, () => {
|
|
server.close(() => resolve(true));
|
|
});
|
|
|
|
server.on('error', () => resolve(false));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create test email data
|
|
*/
|
|
export function createTestEmail(options: {
|
|
from?: string;
|
|
to?: string | string[];
|
|
subject?: string;
|
|
text?: string;
|
|
html?: string;
|
|
attachments?: any[];
|
|
} = {}): any {
|
|
return {
|
|
from: options.from || 'test@example.com',
|
|
to: options.to || 'recipient@example.com',
|
|
subject: options.subject || 'Test Email',
|
|
text: options.text || 'This is a test email',
|
|
html: options.html || '<p>This is a test email</p>',
|
|
attachments: options.attachments || [],
|
|
date: new Date(),
|
|
messageId: `<${Date.now()}@test.example.com>`
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Simple test server for custom protocol testing
|
|
*/
|
|
export interface ISimpleTestServer {
|
|
server: any;
|
|
hostname: string;
|
|
port: number;
|
|
}
|
|
|
|
export async function createTestServer(options: {
|
|
onConnection?: (socket: any) => void | Promise<void>;
|
|
port?: number;
|
|
hostname?: string;
|
|
}): Promise<ISimpleTestServer> {
|
|
const hostname = options.hostname || 'localhost';
|
|
const port = options.port || await getAvailablePort();
|
|
|
|
const server = plugins.net.createServer((socket) => {
|
|
if (options.onConnection) {
|
|
const result = options.onConnection(socket);
|
|
if (result && typeof result.then === 'function') {
|
|
result.catch(error => {
|
|
console.error('Error in onConnection handler:', error);
|
|
socket.destroy();
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
server.listen(port, hostname, () => {
|
|
resolve({
|
|
server,
|
|
hostname,
|
|
port
|
|
});
|
|
});
|
|
|
|
server.on('error', reject);
|
|
});
|
|
}
|