const { assert } = require('chai');
const MailosaurClient = require('../lib/mailosaur');

const outputError = (done) => (err) => {
  // eslint-disable-next-line no-console
  console.log(err.errorType, err.httpStatusCode, err.httpResponseBody);
  done(err);
};

describe('devices', () => {
  let client;
  const apiKey = process.env.MAILOSAUR_API_KEY;
  const baseUrl = process.env.MAILOSAUR_BASE_URL || 'https://mailosaur.com/';

  before(() => {
    if (!apiKey) {
      throw new Error('Missing necessary environment variables - refer to README.md');
    }

    client = new MailosaurClient(apiKey, baseUrl);
  });

  describe('CRUD', () => {
    const deviceName = 'My test';
    const sharedSecret = 'ONSWG4TFOQYTEMY=';
    let createdDevice;

    it('should create a new device', (done) => {
      client.devices.create({
        name: deviceName,
        sharedSecret
      }).then((device) => {
        createdDevice = device;
        assert.isNotEmpty(createdDevice.id);
        assert.equal(createdDevice.name, deviceName);
        done();
      }).catch(outputError(done));
    });

    it('retrieve an otp via device ID', (done) => {
      client.devices.otp(createdDevice.id)
        .then((otpResult) => {
          assert.isString(otpResult.code);
          assert.equal(otpResult.code.length, 6);
          done();
        })
        .catch(outputError(done));
    });

    it('should delete an existing device', (done) => {
      client.devices.list().then((result) => {
        assert.equal(result.items.length, 1);
      }).then(() => (
        client.devices.del(createdDevice.id)
      )).then(() => (
        client.devices.list()
      ))
        .then((result) => {
          assert.equal(result.items.length, 0);
          done();
        })
        .catch(outputError(done));
    });

    it('retrieve an otp via shared secret', (done) => {
      client.devices.otp(sharedSecret)
        .then((otpResult) => {
          assert.isString(otpResult.code);
          assert.equal(otpResult.code.length, 6);
          done();
        })
        .catch(outputError(done));
    });
  });
});

@@ -1487,37 +1487,38 @@
 %0A        assert.
-equal
+isTrue
 (result.items.le
@@ -1519,17 +1519,44 @@
 ems.
-length, 1
+some(x =%3E x.id === createdDevice.id)
 );%0A 
@@ -1727,21 +1727,23 @@
  assert.
-equal
+isFalse
 (result.
@@ -1752,17 +1752,44 @@
 ems.
-length, 0
+some(x =%3E x.id === createdDevice.id)
 );%0A 

Fix device tests concurrency issue