<commit_before>'use strict';

var request = require('supertest');
var should = require('should');

describe('Treatment API', function ( ) {
  this.timeout(2000);
  var self = this;

  var api = require('../lib/api/');
  beforeEach(function (done) {
    process.env.API_SECRET = '<PASSWORD>';
    self.env = require('../env')();
    self.env.settings.enable = ['careportal'];
    this.wares = require('../lib/middleware/')(self.env);
    self.app = require('express')();
    self.app.enable('api');
    require('../lib/bootevent')(self.env).boot(function booted(ctx) {
      self.ctx = ctx;
      self.app.use('/api', api(self.env, ctx));
      done();
    });
  });

  after(function () {
    delete process.env.API_SECRET;
  });

  it('post single treatments', function (done) {
    var doneCalled = false;

    self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
      self.ctx.ddata.treatments.length.should.equal(3);
      self.ctx.ddata.treatments[0].mgdl.should.equal(100);
      should.not.exist(self.ctx.ddata.treatments[0].eventTime);
      should.not.exist(self.ctx.ddata.treatments[0].notes);

      should.not.exist(self.ctx.ddata.treatments[1].eventTime);
      should.not.exist(self.ctx.ddata.treatments[1].glucose);
      should.not.exist(self.ctx.ddata.treatments[1].glucoseType);
      should.not.exist(self.ctx.ddata.treatments[1].units);
      self.ctx.ddata.treatments[1].insulin.should.equal(2);
      self.ctx.ddata.treatments[2].carbs.should.equal(30);

      //if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
      if (!doneCalled) { done(); }

      doneCalled = true;
    });

    self.ctx.treatments().remove({ }, function ( ) {
      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send({eventType: 'BG Check', glucose: 100, preBolus: '0', glucoseType: 'Finger', units: 'mg/dl', notes: ''})
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });

      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send({eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucoseType: 'Finger', units: 'mg/dl'})
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });

    });
  });

  it('post a treatment array', function (done) {
    var doneCalled = false;

    self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
      self.ctx.ddata.treatments.length.should.equal(3);
      self.ctx.ddata.treatments[0].mgdl.should.equal(100);
      should.not.exist(self.ctx.ddata.treatments[0].eventTime);
      should.not.exist(self.ctx.ddata.treatments[0].notes);

      should.not.exist(self.ctx.ddata.treatments[1].eventTime);
      should.not.exist(self.ctx.ddata.treatments[1].glucose);
      should.not.exist(self.ctx.ddata.treatments[1].glucoseType);
      should.not.exist(self.ctx.ddata.treatments[1].units);
      self.ctx.ddata.treatments[1].insulin.should.equal(2);
      self.ctx.ddata.treatments[2].carbs.should.equal(30);

      //if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
      if (!doneCalled) { done(); }

      doneCalled = true;
    });

    self.ctx.treatments().remove({ }, function ( ) {
      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send([
          {eventType: 'BG Check', glucose: 100, preBolus: '0', glucoseType: 'Finger', units: 'mg/dl', notes: ''}
          , {eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucoseType: 'Finger', units: 'mg/dl'}
         ])
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });
    });
  });

  it('post a treatment array and dedupe', function (done) {
    var doneCalled = false;

    self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
      self.ctx.ddata.treatments.length.should.equal(3);
      self.ctx.ddata.treatments[0].mgdl.should.equal(100);

      //if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
      if (!doneCalled) { done(); }

      doneCalled = true;
    });

    self.ctx.treatments().remove({ }, function ( ) {
      var now = (new Date()).toISOString();
      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send([
          {eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucoseType: 'Finger', units: 'mg/dl'}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
        ])
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });
    });
  });
});<commit_msg>hack: make the order match<commit_after>'use strict';

var request = require('supertest');
var should = require('should');

describe('Treatment API', function ( ) {
  this.timeout(2000);
  var self = this;

  var api = require('../lib/api/');
  beforeEach(function (done) {
    process.env.API_SECRET = '<PASSWORD> is my long pass phrase';
    self.env = require('../env')();
    self.env.settings.enable = ['careportal'];
    this.wares = require('../lib/middleware/')(self.env);
    self.app = require('express')();
    self.app.enable('api');
    require('../lib/bootevent')(self.env).boot(function booted(ctx) {
      self.ctx = ctx;
      self.app.use('/api', api(self.env, ctx));
      done();
    });
  });

  after(function () {
    delete process.env.API_SECRET;
  });

  it('post single treatments', function (done) {
    var doneCalled = false;

    self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
      self.ctx.ddata.treatments.length.should.equal(3);
      self.ctx.ddata.treatments[0].mgdl.should.equal(100);
      should.not.exist(self.ctx.ddata.treatments[0].eventTime);
      should.not.exist(self.ctx.ddata.treatments[0].notes);

      should.not.exist(self.ctx.ddata.treatments[1].eventTime);
      should.not.exist(self.ctx.ddata.treatments[1].glucose);
      should.not.exist(self.ctx.ddata.treatments[1].glucoseType);
      should.not.exist(self.ctx.ddata.treatments[1].units);
      self.ctx.ddata.treatments[1].insulin.should.equal(2);
      self.ctx.ddata.treatments[2].carbs.should.equal(30);

      //if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
      if (!doneCalled) { done(); }

      doneCalled = true;
    });

    self.ctx.treatments().remove({ }, function ( ) {
      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send({eventType: 'BG Check', glucose: 100, preBolus: '0', glucoseType: 'Finger', units: 'mg/dl', notes: ''})
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });

      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send({eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucoseType: 'Finger', units: 'mg/dl'})
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });

    });
  });

  it('post a treatment array', function (done) {
    var doneCalled = false;

    self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
      self.ctx.ddata.treatments.length.should.equal(3);
      self.ctx.ddata.treatments[0].mgdl.should.equal(100);
      should.not.exist(self.ctx.ddata.treatments[0].eventTime);
      should.not.exist(self.ctx.ddata.treatments[0].notes);

      should.not.exist(self.ctx.ddata.treatments[1].eventTime);
      should.not.exist(self.ctx.ddata.treatments[1].glucose);
      should.not.exist(self.ctx.ddata.treatments[1].glucoseType);
      should.not.exist(self.ctx.ddata.treatments[1].units);
      self.ctx.ddata.treatments[1].insulin.should.equal(2);
      self.ctx.ddata.treatments[2].carbs.should.equal(30);

      //if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
      if (!doneCalled) { done(); }

      doneCalled = true;
    });

    self.ctx.treatments().remove({ }, function ( ) {
      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send([
          {eventType: 'BG Check', glucose: 100, preBolus: '0', glucoseType: 'Finger', units: 'mg/dl', notes: ''}
          , {eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucoseType: 'Finger', units: 'mg/dl'}
         ])
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });
    });
  });

  it('post a treatment array and dedupe', function (done) {
    var doneCalled = false;

    self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
      self.ctx.ddata.treatments.length.should.equal(3);
      self.ctx.ddata.treatments[0].mgdl.should.equal(100);

      //if travis is slow the 2 posts take long enough that 2 data-loaded events are emitted
      if (!doneCalled) { done(); }

      doneCalled = true;
    });

    self.ctx.treatments().remove({ }, function ( ) {
      var now = (new Date()).toISOString();
      request(self.app)
        .post('/api/treatments/')
        .set('api-secret', self.env.api_secret || '')
        .send([
          {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'BG Check', glucose: 100, created_at: now}
          , {eventType: 'Meal Bolus', carbs: '30', insulin: '2.00', preBolus: '15', glucoseType: 'Finger', units: 'mg/dl'}
        ])
        .expect(200)
        .end(function (err) {
          if (err) {
            done(err);
          }
        });
    });
  });
});
{'fraction_non_alphanumeric': 0.1782205746061168, 'fraction_numerical': 0.02075996292863763, 'mean_word_length': 2.5357142857142856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2202449', 'n_tokens_mistral': 3897, 'n_tokens_neox': 3586, 'n_words': 679}