var assert = require('chai').assert;
var sinon = require('sinon');

let PromiseStore = require('../../build').PromiseStore;

describe('PromiseStore', function () {
  describe('.make()', function () {
    it("should track a promise's callbacks", function () {
      var promises = new PromiseStore();
      // There should be no tracked promises.
      assert.lengthOf(promises, 0);

      // Add one promise.
      new Promise(function (resolve, reject) {
        promises.make(randomToken(), resolve, reject);
      });
      assert.lengthOf(promises, 1);
    });

    // TODO: test existing token
  });

  describe('.keep()', function () {
    it('should call the resolve() function', function () {
      var promises = new PromiseStore();
      var token = randomToken();
      var promise = new Promise(function (resolve, reject) {
        promises.make(token, resolve, reject);
      });

      // Resolve the promise asynchronously.
      var randomPayload = randomToken();
      doLater(function () {
        assert.lengthOf(promises, 1);
        // TODO: this API probably should not return a resolve function;
        // it should call it immediately.
        promises.keep(token)(randomPayload);
      });

      return promise.then(function (actual) {
        assert.strictEqual(actual, randomPayload);
        assert.lengthOf(promises, 0);
      });
    });
  });

  describe('.break()', function () {
    it('should call the reject() function', function () {
      var promises = new PromiseStore();
      var token = randomToken();
      var promise = new Promise(function (resolve, reject) {
        promises.make(token, resolve, reject);
      });

      // Resolve the promise asynchronously.
      var error = new Error();
      doLater(function () {
        assert.lengthOf(promises, 1);
        promises.break(token, error);
      });

      return promise.catch(function (actualError) {
        assert.strictEqual(actualError, error);
        assert.lengthOf(promises, 0);
      });
    });
  });

  function randomToken() {
    var range =  Number.MAX_SAFE_INTEGER - Number.MIN_SAFE_INTEGER;
    return Math.random() * range + Number.MIN_SAFE_INTEGER;
  }

  // Do something asynchronously.
  function doLater(callback) {
    return setTimeout(callback, 0);
  }
});

@@ -1859,29 +1859,229 @@
     
-return promise.catch(
+// inspired by:%0A      // https://gist.github.com/haroldtreen/5f1055eee5fcf01da3e0e15b8ec86bf6#gistcomment-2623170%0A      return promise.then(%0A        function () %7B return Promise.reject('should not be called'); %7D,%0A        
 func
@@ -2101,32 +2101,34 @@
 rror) %7B%0A        
+  
 assert.strictEqu
@@ -2151,32 +2151,34 @@
 error);%0A        
+  
 assert.lengthOf(
@@ -2189,33 +2189,42 @@
 ises, 0);%0A      
-%7D
+  %7D%0A      
 );%0A    %7D);%0A  %7D);

Use more robust rejection assertion.