import { expect, tap } 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('CRFC-04: SMTP Response Code Handling', async () => {
  console.log('\n📧 Testing SMTP Client Response Code Handling');
  console.log('=' .repeat(60));

  const testServer = await createTestServer({});

  try {
    const smtpClient = createTestSmtpClient({
      host: testServer.hostname,
      port: testServer.port
    });

    console.log('\nTest 1: Successful email (2xx responses)');
    const email1 = new Email({
      from: 'sender@example.com',
      to: ['recipient@example.com'],
      subject: 'Success test',
      text: 'Testing successful response codes'
    });

    const result1 = await smtpClient.sendMail(email1);
    console.log('  ✓ 2xx response codes handled correctly');
    expect(result1).toBeDefined();

    console.log('\nTest 2: Verify connection');
    const verified = await smtpClient.verify();
    console.log('  ✓ Connection verification successful');
    expect(verified).toBeDefined();

    console.log('\nTest 3: Multiple recipients (multiple 250 responses)');
    const email2 = new Email({
      from: 'sender@example.com',
      to: ['user1@example.com', 'user2@example.com', 'user3@example.com'],
      subject: 'Multiple recipients',
      text: 'Testing multiple positive responses'
    });

    const result2 = await smtpClient.sendMail(email2);
    console.log('  ✓ Multiple positive responses handled');
    expect(result2).toBeDefined();

    console.log('\n✅ CRFC-04: Response code handling tests completed');

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

tap.start();