const Connection = require('../../src/connection');
const Request = require('../../src/request');
const fs = require('fs');
const TYPES = require('../../src/data-type').typeByName;
const assert = require('chai').assert;

function getConfig() {
  const config = JSON.parse(
    fs.readFileSync(require('os').homedir() + '/.tedious/test-connection.json', 'utf8')
  ).config;

  config.options.debug = {
    packet: true,
    data: true,
    payload: true,
    token: false,
    log: true,
  };

  config.options.tdsVersion = process.env.TEDIOUS_TDS_VERSION;

  return config;
}

describe('Prepare Execute Statement', function() {
  it('should prepare execute', function(done) {
    const value = 8;

    const config = getConfig();

    const request = new Request('select @param', function(err) {
      assert.ifError(err);
      connection.close();
    });
    request.addParameter('param', TYPES.Int);

    const connection = new Connection(config);

    request.on('prepared', function() {
      assert.ok(request.handle);
      connection.execute(request, { param: value });
    });

    request.on('row', function(columns) {
      assert.strictEqual(columns.length, 1);
      assert.strictEqual(columns[0].value, value);
    });

    connection.connect(function(err) {
      assert.ifError(err);
      connection.prepare(request);
    });

    connection.on('end', function(info) {
      done();
    });

    connection.on('debug', function(text) {
      // console.log(text)
    });
  });

  it('should test unprepare', function(done) {
    const config = getConfig();
    const request = new Request('select 3', function(err) {
      assert.ifError(err);
      connection.close();
    });

    const connection = new Connection(config);

    request.on('prepared', function() {
      assert.ok(request.handle);
      connection.unprepare(request);
    });

    connection.connect(function(err) {
      assert.ifError(err);
      connection.prepare(request);
    });

    connection.on('end', function(info) {
      done();
    });

    connection.on('debug', function(text) {
      // console.log(text)
    });
  });
});

@@ -1488,16 +1488,1111 @@
 %0A  %7D);%0A%0A
+  it('does not leak memory via EventEmitter listeners when reusing a request many times', function(done) %7B%0A    const config = getConfig();%0A%0A    let eventEmitterLeak = false;%0A    const onWarning = (warning) =%3E %7B%0A      if (warning.name === 'MaxListenersExceededWarning') %7B%0A        eventEmitterLeak = true;%0A      %7D%0A    %7D;%0A    process.on('warning', onWarning);%0A%0A    let count = 0;%0A    const request = new Request('select 1', function(err) %7B%0A      assert.ifError(err);%0A%0A      if (count %3C 20) %7B%0A        count += 1;%0A%0A        connection.execute(request);%0A      %7D else %7B%0A        connection.close();%0A      %7D%0A    %7D);%0A%0A    const connection = new Connection(config);%0A%0A    request.on('prepared', function() %7B%0A      connection.execute(request);%0A    %7D);%0A%0A    connection.connect(function(err) %7B%0A      if (err) %7B%0A        return done(err);%0A      %7D%0A%0A      connection.prepare(request);%0A    %7D);%0A%0A    connection.on('end', function(info) %7B%0A      process.removeListener('warning', onWarning);%0A%0A      if (eventEmitterLeak) %7B%0A        assert.fail('EventEmitter memory leak detected');%0A      %7D%0A%0A      done();%0A    %7D);%0A  %7D);%0A%0A
   it('sh

add a test that shows memory is being leaked