import { describe, expect, it } from 'vitest';
import { buildOrderStatusEmail, ORDER_STATE_LABELS } from '../infrastructure/order-status-mailer.js';

const BASE = {
  to: 'customer@example.com',
  orderId: 'abcdef12-3456-7890-abcd-ef1234567890',
};

describe('buildOrderStatusEmail', () => {
  it('includes tracking number and courier in the SHIPPED email', () => {
    const email = buildOrderStatusEmail({
      ...BASE,
      state: 'SHIPPED',
      trackingNumber: 'TRK-998877',
      courier: 'SEUR',
    });
    expect(email.subject).toContain(ORDER_STATE_LABELS.SHIPPED);
    expect(email.text).toContain('Número de seguimiento: TRK-998877');
    expect(email.text).toContain('Transportista: SEUR');
    expect(email.html).toContain('Número de seguimiento: <strong>TRK-998877</strong>');
    expect(email.html).toContain('Transportista: <strong>SEUR</strong>');
  });

  it('does not mention tracking or courier in the PROCESSING email', () => {
    const email = buildOrderStatusEmail({ ...BASE, state: 'PROCESSING' });
    expect(email.subject).toContain(ORDER_STATE_LABELS.PROCESSING);
    expect(email.text).not.toContain('Número de seguimiento');
    expect(email.text).not.toContain('Transportista');
    expect(email.html).not.toContain('Transportista');
  });

  it('omits the courier line when the courier is blank', () => {
    const email = buildOrderStatusEmail({
      ...BASE,
      state: 'SHIPPED',
      trackingNumber: 'TRK-1',
      courier: '   ',
    });
    expect(email.text).not.toContain('Transportista');
    expect(email.html).not.toContain('Transportista');
  });

  it('escapes HTML in courier and tracking values', () => {
    const email = buildOrderStatusEmail({
      ...BASE,
      state: 'SHIPPED',
      trackingNumber: '<script>alert(1)</script>',
      courier: 'ACME & Co <b>',
    });
    expect(email.html).not.toContain('<script>');
    expect(email.html).not.toContain('<b>');
    expect(email.html).toContain('ACME &amp; Co &lt;b&gt;');
    expect(email.html).toContain('&lt;script&gt;alert(1)&lt;/script&gt;');
  });
});
