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('does not leak memory via EventEmitter listeners when reusing a request many times', function(done) {
    const config = getConfig();

    let eventEmitterLeak = false;
    const onWarning = (warning) => {
      if (warning.name === 'MaxListenersExceededWarning') {
        eventEmitterLeak = true;
      }
    };
    process.on('warning', onWarning);

    let count = 0;
    const request = new Request('select 1', function(err) {
      assert.ifError(err);

      if (count < 20) {
        count += 1;

        connection.execute(request);
      } else {
        connection.close();
      }
    });

    const connection = new Connection(config);

    request.on('prepared', function() {
      connection.execute(request);
    });

    connection.connect(function(err) {
      if (err) {
        return done(err);
      }

      connection.prepare(request);
    });

    connection.on('end', function(info) {
      process.removeListener('warning', onWarning);

      if (eventEmitterLeak) {
        assert.fail('EventEmitter memory leak detected');
      }

      done();
    });
  });

  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)
    });
  });
});

@@ -1,101 +1,17 @@
-const Connection = require('../../src/connection');%0Aconst Request = require('../../src/request');
+// @ts-check%0A
 %0Acon
@@ -130,16 +130,105 @@
 ssert;%0A%0A
+import Connection from '../../src/connection';%0Aimport Request from '../../src/request';%0A%0A
 function
@@ -1368,36 +1368,32 @@
 'end', function(
-info
 ) %7B%0A      done()
@@ -1662,16 +1662,71 @@
 = false;
+%0A%0A    /**%0A     * @type %7BNodeJS.WarningListener%7D%0A     */
 %0A    con
@@ -2444,36 +2444,32 @@
 'end', function(
-info
 ) %7B%0A      proces
@@ -3105,32 +3105,32 @@
 uest);%0A    %7D);%0A%0A
+
     connection.o
@@ -3151,12 +3151,8 @@
 ion(
-info
 ) %7B%0A

enable type checking for `test/integration/prepare-execute-statements-test.js`