<commit_before>/*jslint unused:vars, node:true */
var mailer   = require('nodemailer');
var ms       = require('../lib/index');
var test     = require('tape');
var os       = require('os');
var async    = require('async');


var PORT = 4025;
var mailServer;
var smtpTransport;
var sender = '<EMAIL>';


function sendmail(to, subject, body, headers, cb) {
  if (!cb && typeof(headers) === 'function') {
    cb = headers;
    headers = {};
  }
  smtpTransport.sendMail({
    from:    sender,
    to:      to,
    subject: subject,
    text:    body,
    headers: headers
  }, cb);
}

test('setup', function(t) {
  smtpTransport = mailer.createTransport({
    host: 'localhost',
    port: PORT,
    secure: false,
    name: os.hostname(),
    tls: {
      rejectUnauthorized: false
    }
  });
  mailServer = ms.init(PORT);
  t.end();
});


test('specific handler', function(t) {
  var recp = '<EMAIL>', subject = 'email test', body = 'This is a test email',
  handler = function(address, id, email) {
    t.equal(address,            recp);
    t.equal(email.headers.To,   recp);
    t.equal(email.headers.From, sender);
    t.equal(email.body,         body);
    mailServer.unbind(recp,handler);
    mailServer.removeAll();
    t.end();
  };

  mailServer.bind(recp, handler);
  sendmail(recp, subject, body, function(err, response) {
    t.error(err);
  });
});

test('handler with two emails', function(t) {
  var recp = '<EMAIL>', subject = 'email test', body = 'This is a test email', mails = [],
  handler = function(address, id, email) {
		// save this email
		mails.push({address:address, id: id, email: email});
  };

  mailServer.bind(recp, handler);

	// send the email twice, each time with a different subject, body
	async.waterfall([
		function (cb) {
			sendmail(recp, subject+"0", body+"0", cb);
		},
		function (res,cb) {
			sendmail(recp, subject+"1", body+"1", cb);
		}
	],function (err) {
		if (err) {
			t.error(err);
		} else {
			// process the emails
			t.equal(mails.length, 2);
			for (var i=0; i<mails.length; i++) {
		    t.equal(mails[i].address,            recp);
		    t.equal(mails[i].email.headers.To,   recp);
		    t.equal(mails[i].email.headers.From, sender);
		    t.equal(mails[i].email.subject,      subject+i);
		    t.equal(mails[i].email.body,         body+i);
			}
	    mailServer.unbind(recp,handler);
	    mailServer.removeAll();
			t.end();
		}
	});
});

test('two emails with different handlers', function(t) {
  var recp = '<EMAIL>', subject = 'email test', body = 'This is a test email';
  var handler0Factory = function (cb) {
  	handler0 = function(address, id, email) {
	    t.equal(address,            recp);
	    t.equal(email.headers.To,   recp);
	    t.equal(email.headers.From, sender);
	    t.equal(email.subject,      subject+"0");
	    t.equal(email.body,         body+"0");
			mailServer.unbind(recp,handler0);
			mailServer.removeAll();
			cb(null);
	  };

    return handler0;
  };
  var handler1Factory = function (cb) {
		handler1 = function(address, id, email) {
	    t.equal(address,            recp);
	    t.equal(email.headers.To,   recp);
	    t.equal(email.headers.From, sender);
	    t.equal(email.subject,      subject+"1");
	    t.equal(email.body,         body+"1");
			mailServer.unbind(recp,handler1);
			mailServer.removeAll();
			cb(null);
	  };

    return handler1;
  };

	// send the email twice, each time with a different subject, body
	async.waterfall([
		function (cb) {
			sendmail(recp, subject+"0", body+"0", cb);
		},
		function (res,cb) {
			mailServer.bind(recp,handler0Factory(cb));
		},
		function (cb) {
			sendmail(recp, subject+"1", body+"1", cb);
		},
		function (res,cb) {
			mailServer.bind(recp,handler1Factory(cb));
		}
	],function (err) {
		if (err) {
			t.error(err);
		} else {
			t.end();
		}
	});
});



test('catch-all handler', function(t) {
  var recp = '<EMAIL>', subject = 'some other test', body = 'This is another test email',
  handler = function(address, id, email) {
    t.equal(address,            null);
    t.equal(email.headers.To,   recp);
    t.equal(email.headers.From,  sender);
    t.equal(email.body,         body);
    mailServer.unbind(handler);
    mailServer.removeAll();
    t.end();
  };

  mailServer.bind(handler);

  sendmail(recp, subject, body, function(err, response) {
    t.error(err);
  });
});


test('folded headers', function(t) {
  var recp = '<EMAIL>', subject = 'email test why not', body = 'Why not a test email?',
  xfolded = 'This is a\r\n folded header', xfoldedtab = 'This is a\r\n\t tab folded header',
  handler = function(address, id, email) {
    t.equal(email.headers.To,                recp);
    t.equal(email.headers.From,              sender);
    t.equal(email.headers.get('xfolded'),    'This is a  folded header');
    t.equal(email.headers.get('xfoldedtab'), 'This is a \t tab folded header');
    mailServer.unbind(handler);
    mailServer.removeAll();
    t.end();
  };

  mailServer.bind(handler);

  var headers = {
    xfolded:    xfolded,
    xfoldedtab: xfoldedtab
  };

  sendmail(recp, subject, body, headers, function(err, response) {
    t.error(err);<commit_msg>Change folding test to test the desired behavior.<commit_after>/*jslint unused:vars, node:true */
var mailer   = require('nodemailer');
var ms       = require('../lib/index');
var test     = require('tape');
var os       = require('os');
var async    = require('async');


var PORT = 4025;
var mailServer;
var smtpTransport;
var sender = '<EMAIL>';


function sendmail(to, subject, body, headers, cb) {
  if (!cb && typeof(headers) === 'function') {
    cb = headers;
    headers = {};
  }
  smtpTransport.sendMail({
    from:    sender,
    to:      to,
    subject: subject,
    text:    body,
    headers: headers
  }, cb);
}

test('setup', function(t) {
  smtpTransport = mailer.createTransport({
    host: 'localhost',
    port: PORT,
    secure: false,
    name: os.hostname(),
    tls: {
      rejectUnauthorized: false
    }
  });
  mailServer = ms.init(PORT);
  t.end();
});


test('specific handler', function(t) {
  var recp = '<EMAIL>', subject = 'email test', body = 'This is a test email',
  handler = function(address, id, email) {
    t.equal(address,            recp);
    t.equal(email.headers.To,   recp);
    t.equal(email.headers.From, sender);
    t.equal(email.body,         body);
    mailServer.unbind(recp,handler);
    mailServer.removeAll();
    t.end();
  };

  mailServer.bind(recp, handler);
  sendmail(recp, subject, body, function(err, response) {
    t.error(err);
  });
});

test('handler with two emails', function(t) {
  var recp = '<EMAIL>', subject = 'email test', body = 'This is a test email', mails = [],
  handler = function(address, id, email) {
		// save this email
		mails.push({address:address, id: id, email: email});
  };

  mailServer.bind(recp, handler);

	// send the email twice, each time with a different subject, body
	async.waterfall([
		function (cb) {
			sendmail(recp, subject+"0", body+"0", cb);
		},
		function (res,cb) {
			sendmail(recp, subject+"1", body+"1", cb);
		}
	],function (err) {
		if (err) {
			t.error(err);
		} else {
			// process the emails
			t.equal(mails.length, 2);
			for (var i=0; i<mails.length; i++) {
		    t.equal(mails[i].address,            recp);
		    t.equal(mails[i].email.headers.To,   recp);
		    t.equal(mails[i].email.headers.From, sender);
		    t.equal(mails[i].email.subject,      subject+i);
		    t.equal(mails[i].email.body,         body+i);
			}
	    mailServer.unbind(recp,handler);
	    mailServer.removeAll();
			t.end();
		}
	});
});

test('two emails with different handlers', function(t) {
  var recp = '<EMAIL>', subject = 'email test', body = 'This is a test email';
  var handler0Factory = function (cb) {
  	handler0 = function(address, id, email) {
	    t.equal(address,            recp);
	    t.equal(email.headers.To,   recp);
	    t.equal(email.headers.From, sender);
	    t.equal(email.subject,      subject+"0");
	    t.equal(email.body,         body+"0");
			mailServer.unbind(recp,handler0);
			mailServer.removeAll();
			cb(null);
	  };

    return handler0;
  };
  var handler1Factory = function (cb) {
		handler1 = function(address, id, email) {
	    t.equal(address,            recp);
	    t.equal(email.headers.To,   recp);
	    t.equal(email.headers.From, sender);
	    t.equal(email.subject,      subject+"1");
	    t.equal(email.body,         body+"1");
			mailServer.unbind(recp,handler1);
			mailServer.removeAll();
			cb(null);
	  };

    return handler1;
  };

	// send the email twice, each time with a different subject, body
	async.waterfall([
		function (cb) {
			sendmail(recp, subject+"0", body+"0", cb);
		},
		function (res,cb) {
			mailServer.bind(recp,handler0Factory(cb));
		},
		function (cb) {
			sendmail(recp, subject+"1", body+"1", cb);
		},
		function (res,cb) {
			mailServer.bind(recp,handler1Factory(cb));
		}
	],function (err) {
		if (err) {
			t.error(err);
		} else {
			t.end();
		}
	});
});



test('catch-all handler', function(t) {
  var recp = '<EMAIL>', subject = 'some other test', body = 'This is another test email',
  handler = function(address, id, email) {
    t.equal(address,            null);
    t.equal(email.headers.To,   recp);
    t.equal(email.headers.From,  sender);
    t.equal(email.body,         body);
    mailServer.unbind(handler);
    mailServer.removeAll();
    t.end();
  };

  mailServer.bind(handler);

  sendmail(recp, subject, body, function(err, response) {
    t.error(err);
  });
});


test('folded headers', function(t) {
  var longHeaderValue = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sagittis purus vitae aliquet euismod.';
  var recp = '<EMAIL>', subject = 'email test why not', body = 'Why not a test email?';
  handler = function(address, id, email) {
    t.equal(email.headers.To,              recp);
    t.equal(email.headers.From,            sender);
    t.equal(email.headers.get('x-folded'), longHeaderValue);
    mailServer.unbind(handler);
    mailServer.removeAll();
    t.end();
  };

  mailServer.bind(handler);

  var headers = {
    'x-folded': longHeaderValue
  };

  sendmail(recp, subject, body, headers, function(err, response) {
    t.error(err);
{'fraction_non_alphanumeric': 0.16881720430107527, 'fraction_numerical': 0.005474095796676442, 'mean_word_length': 2.945622830698033, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3544503', 'n_tokens_mistral': 3747, 'n_tokens_neox': 3523, 'n_words': 799}