import { tap, expect } from '@git.zone/tstest/tapbundle';
import { createTestServer } from '../../helpers/server.loader.js';
import { createTestSmtpClient } from '../../helpers/smtp.client.js';
import { Email } from '../../../ts/mail/core/classes.email.js';

tap.test('CPERF-08: DNS Caching Tests', async () => {
  console.log('\n🌐 Testing SMTP Client DNS Caching');
  console.log('=' .repeat(60));

  const testServer = await createTestServer({});

  try {
    console.log('\nTest: DNS caching with multiple connections');
    
    // Create multiple clients to test DNS caching
    const clients = [];
    
    for (let i = 0; i < 3; i++) {
      const smtpClient = createTestSmtpClient({
        host: testServer.hostname,
        port: testServer.port
      });
      clients.push(smtpClient);
      console.log(`  ✓ Client ${i + 1} created (DNS should be cached)`);
    }

    // Send email with first client
    const email = new Email({
      from: 'sender@example.com',
      to: 'recipient@example.com',
      subject: 'DNS Caching Test',
      text: 'Testing DNS caching efficiency'
    });

    const result = await clients[0].sendMail(email);
    console.log('  ✓ Email sent successfully');
    expect(result).toBeDefined();

    // Clean up all clients
    clients.forEach(client => client.close());
    console.log('  ✓ All clients closed');

    console.log('\n✅ CPERF-08: DNS caching tests completed');

  } finally {
    testServer.server.close();
  }
});

tap.start();