var should = require('should');

describe('sandbox', function ( ) {
  var sandbox = require('../lib/sandbox')();

  var now = Date.now();

  it('init on client', function (done) {
    var ctx = {
      settings: {
        units: 'mg/dl'
        , thresholds:{
          bgHigh: 260
          , bgTargetTop: 180
          , bgTargetBottom: 80
          , bgLow: 55
        }
      }
      , pluginBase: {}
    };

    var data = {sgvs: [{mgdl: 100, mills: now}]};

    var sbx = sandbox.clientInit(ctx, Date.now(), data);

    sbx.pluginBase.should.equal(ctx.pluginBase);
    sbx.data.should.equal(data);
    sbx.lastSGVMgdl().should.equal(100);

    done();
  });

  function createServerSandbox() {
    var env = require('../env')();
    var ctx = {};
    ctx.ddata = require('../lib/data/ddata')();
    ctx.notifications = require('../lib/notifications')(env, ctx);

    return sandbox.serverInit(env, ctx);
  }

  it('init on server', function (done) {
    var sbx = createServerSandbox();
    sbx.data.sgvs = [{mgdl: 100, mills: now}];

    should.exist(sbx.notifications.requestNotify);
    should.not.exist(sbx.notifications.process);
    should.not.exist(sbx.notifications.ack);
    sbx.lastSGVMgdl().should.equal(100);

    done();
  });

  it('display 39 as LOW and 401 as HIGH', function () {
    var sbx = createServerSandbox();

    sbx.displayBg({mgdl: 39}).should.equal('LOW');
    sbx.displayBg({mgdl: '39'}).should.equal('LOW');
    sbx.displayBg({mgdl: 401}).should.equal('HIGH');
    sbx.displayBg({mgdl: '401'}).should.equal('HIGH');
  });

  it('build BG Now line using properties', function ( ) {
    var sbx = createServerSandbox();
    sbx.data.sgvs = [{mgdl: 99, mills: now}];
    sbx.properties = { bgnow: { delta: {display: '+5' } }, direction: {value: 'FortyFiveUp', label: '↗', entity: '&#8599;'} };

    sbx.buildBGNowLine().should.equal('BG Now: 99 +5 ↗ mg/dl');

  });

  it('build default message using properties', function ( ) {
    var sbx = createServerSandbox();
    sbx.data.sgvs = [{mgdl: 99, mills: now}];
    sbx.properties = {
      bgnow: { delta: {display: '+5' } }
      , direction: {value: 'FortyFiveUp', label: '↗', entity: '&#8599;'}
      , rawbg: {displayLine: 'Raw BG: 100 mg/dl'}
      , iob: {displayLine: 'IOB: 1.25U'}
      , cob: {displayLine: 'COB: 15g'}
    };

    sbx.buildDefaultMessage().should.equal('BG Now: 99 +5 ↗ mg/dl\nRaw BG: 100 mg/dl\nIOB: 1.25U\nCOB: 15g');

  });

});

@@ -1716,25 +1716,16 @@
 ties = %7B
- bgnow: %7B
  delta: 
@@ -1738,18 +1738,16 @@
 ay: '+5'
- %7D
  %7D, dire
@@ -2060,17 +2060,8 @@
     
- bgnow: %7B
  del
@@ -2080,18 +2080,16 @@
 : '+5' %7D
- %7D
 %0A      ,

fix sandbox delta tests