<commit_before>// var sdebug = require('slf-debug').default;
// require('slf').LoggerFactory.setFactory(sdebug);
// require('debug').enable('*');

var should = require('should');
var Promise = require('bluebird');

var Repository = require('..').Repository;

var Location = require('./aggregates/location');


describe('Repository', function () {
  var Partition = function () {
    this.openStream = function (streamId) {
      var Stream = function () {
        this.getCommittedEvents = function () {
          return [];
        };
        this.getVersion = function () {
          return -1;
        }
        this.append = function () { };
        this.commit = function () { return Promise.resolve(null); };
      }
      return Promise.resolve(new Stream(streamId));
    }
  }

  var SnapshotPartition = function (snapshot, events) {
    this._snapshot = snapshot;
    this.loadSnapshot = function () {
      return Promise.resolve(this._snapshot);
    }
    this.openStream = function (streamId) {
      var Stream = function () {
        this.getCommittedEvents = function () {
          return [];
        };
        this.getVersion = function () {
          return -1;
        }
        this.append = function () { };
        this.commit = function () { return Promise.resolve(null); };
      }
      return Promise.resolve(new Stream(streamId));
    }
    this.storeSnapshot = function (id, snapshot, version) {
      this._snapshot = { id: id, snapshot: snapshot, version: version };
      return Promise.resolve(this._snapshot);
    }
    this.removeSnapshot = function (id) {
      this._snapshot = { id: id, version: -1 };
      return Promise.resolve(this._snapshot);
    }
    this.queryStream = function (id, fromEventSequence, callback) {
      var result = [{ events: events }];
      if (fromEventSequence > 0) {
        var startCommitId = 0;
        var foundEvents = 0;
        for (var i = 0; i < result.length; i++) {
          foundEvents += result[0].events.length;
          startCommitId++;
          if (foundEvents >= fromEventSequence) {
            break;
          }
        }
        var tooMany = foundEvents - fromEventSequence;

        result = result.slice(startCommitId - (tooMany > 0 ? 1 : 0));
        if (tooMany > 0) {
          result[0].events = result[0].events.slice(result[0].events.length - tooMany);
        }
      }
      return Promise.resolve(result).nodeify(callback);
    }
  }


  describe('#findById', function () {

    it('returns aggregate with version = -1 if new stream', function (done) {
      var repo = new Repository(new Partition(), 'test_aggregate', undefined, undefined, {resetSnapshotOnFail: false});
      repo.findById('ID_THAT_DO_NOT_EXIST').then(function (aggregate) {
        aggregate.getVersion().should.equal(-1);
        done();
      }).catch(function (err) {
        done(err);
      });;
    });

    it('creates aggregates with custom factory', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new Partition(), 'location', factory);
      repo.findById('ID_THAT_DO_NOT_EXIST').then(function (aggregate) {
        if (aggregate instanceof Location) {
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });;
    });


    it('hydrates aggregates with snapshot', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { name: 'hello' } }, [{ id: 1, type: 'location.registered_name.event', payload: 'Hello' }]), 'location', factory);
      repo.findById('1').then(function (aggregate) {
        if (aggregate instanceof Location) {
          aggregate._getSnapshot().name.should.equal('hello');
          aggregate.getVersion().should.equal(1);
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });
    });
    it('hydrates aggregates with snapshot and events', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { name: 'hello' } }, [{ id: 1, aggregateId: '1', type: 'location.registered_name.event', payload: 'Hello' }, { id: 2, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello, world' }]), 'location', factory);
      repo.findById('1').then(function (aggregate) {
        if (aggregate instanceof Location) {
          aggregate._getSnapshot().name.should.equal('Hello, world');
          aggregate.getVersion().should.equal(2);
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });
    });
    it('hydrates aggregates without snapshot', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new SnapshotPartition(undefined, [{ id: 1, aggregateId: '1', type: 'location.registered_name.event', payload: 'Hello' }]), 'location', factory);
      repo.findById('1').then(function (aggregate) {
        if (aggregate instanceof Location) {
          aggregate._getSnapshot().name.should.equal('Hello');
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });
    });
    it('stores snapshot for aggregate on save', function (done) {
      var factory = function () { return new Location(); };
      var part = new SnapshotPartition({ id: '1', version: 1, snapshot: { name: 'hello' } }, [{ id: 1, type: 'location.registered_name.event', payload: 'Hello' }]);
      var repo = new Repository(part, 'location', factory);
      repo.findById('1').then(function (aggregate) {
        aggregate.changeName('Hello, World!');
        repo.save(aggregate).then(function () {
          part.loadSnapshot('1').then(function (snapshot) {
            snapshot.snapshot.name.should.equal('Hello, World!');
            done();
          })
        });
      }).catch(function (err) {
        done(err);
      });;
    });
  });
  describe('#save', function () {
    it('save should clear uncommitted events ', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new Partition(), 'location', factory);
      repo.findById('ID_THAT_DO_NOT_EXIST').then(function (location) {
        location.registerName('<NAME>');
        repo.save(location).then(function (x) {
          x.getUncommittedEvents().length.should.equal(0);
          done();
        });
      }).catch(function (err) {
        done(err);
      });
    });
  });
  it('removes and retries snapshot but does not end up in loop if not working', function (done) {
    var factory = function () { return new Location(); };
    var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { no_name: 'hello' } }, [{ id: 1, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello' }, { id: 2, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello, world' }]), 'location', factory);
    repo.findById('1').then(function(aggregate) {
      console.log('helvete', aggregate);
      
    }).catch(function () {
      done();
    });
  });
  it('removes and retries snapshot create when snapshot is broken', function (done) {
    var factory = function () { return new Location(); };
    var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { no_name: 'hello' } }, [{ id: 1, aggregateId: '1', type: 'location.registered_name.event', payload: 'Hello' }, { id: 1, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello' }, { id: 2, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello, world' }]), 'location', factory);
    repo.findById('1').then(function (aggregate) {
      if (aggregate instanceof Location) {
        aggregate._getSnapshot().name.should.equal('Hello, world');<commit_msg>remove console log<commit_after>// var sdebug = require('slf-debug').default;
// require('slf').LoggerFactory.setFactory(sdebug);
// require('debug').enable('*');

var should = require('should');
var Promise = require('bluebird');

var Repository = require('..').Repository;

var Location = require('./aggregates/location');


describe('Repository', function () {
  var Partition = function () {
    this.openStream = function (streamId) {
      var Stream = function () {
        this.getCommittedEvents = function () {
          return [];
        };
        this.getVersion = function () {
          return -1;
        }
        this.append = function () { };
        this.commit = function () { return Promise.resolve(null); };
      }
      return Promise.resolve(new Stream(streamId));
    }
  }

  var SnapshotPartition = function (snapshot, events) {
    this._snapshot = snapshot;
    this.loadSnapshot = function () {
      return Promise.resolve(this._snapshot);
    }
    this.openStream = function (streamId) {
      var Stream = function () {
        this.getCommittedEvents = function () {
          return [];
        };
        this.getVersion = function () {
          return -1;
        }
        this.append = function () { };
        this.commit = function () { return Promise.resolve(null); };
      }
      return Promise.resolve(new Stream(streamId));
    }
    this.storeSnapshot = function (id, snapshot, version) {
      this._snapshot = { id: id, snapshot: snapshot, version: version };
      return Promise.resolve(this._snapshot);
    }
    this.removeSnapshot = function (id) {
      this._snapshot = { id: id, version: -1 };
      return Promise.resolve(this._snapshot);
    }
    this.queryStream = function (id, fromEventSequence, callback) {
      var result = [{ events: events }];
      if (fromEventSequence > 0) {
        var startCommitId = 0;
        var foundEvents = 0;
        for (var i = 0; i < result.length; i++) {
          foundEvents += result[0].events.length;
          startCommitId++;
          if (foundEvents >= fromEventSequence) {
            break;
          }
        }
        var tooMany = foundEvents - fromEventSequence;

        result = result.slice(startCommitId - (tooMany > 0 ? 1 : 0));
        if (tooMany > 0) {
          result[0].events = result[0].events.slice(result[0].events.length - tooMany);
        }
      }
      return Promise.resolve(result).nodeify(callback);
    }
  }


  describe('#findById', function () {

    it('returns aggregate with version = -1 if new stream', function (done) {
      var repo = new Repository(new Partition(), 'test_aggregate', undefined, undefined, {resetSnapshotOnFail: false});
      repo.findById('ID_THAT_DO_NOT_EXIST').then(function (aggregate) {
        aggregate.getVersion().should.equal(-1);
        done();
      }).catch(function (err) {
        done(err);
      });;
    });

    it('creates aggregates with custom factory', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new Partition(), 'location', factory);
      repo.findById('ID_THAT_DO_NOT_EXIST').then(function (aggregate) {
        if (aggregate instanceof Location) {
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });;
    });


    it('hydrates aggregates with snapshot', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { name: 'hello' } }, [{ id: 1, type: 'location.registered_name.event', payload: 'Hello' }]), 'location', factory);
      repo.findById('1').then(function (aggregate) {
        if (aggregate instanceof Location) {
          aggregate._getSnapshot().name.should.equal('hello');
          aggregate.getVersion().should.equal(1);
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });
    });
    it('hydrates aggregates with snapshot and events', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { name: 'hello' } }, [{ id: 1, aggregateId: '1', type: 'location.registered_name.event', payload: 'Hello' }, { id: 2, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello, world' }]), 'location', factory);
      repo.findById('1').then(function (aggregate) {
        if (aggregate instanceof Location) {
          aggregate._getSnapshot().name.should.equal('Hello, world');
          aggregate.getVersion().should.equal(2);
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });
    });
    it('hydrates aggregates without snapshot', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new SnapshotPartition(undefined, [{ id: 1, aggregateId: '1', type: 'location.registered_name.event', payload: 'Hello' }]), 'location', factory);
      repo.findById('1').then(function (aggregate) {
        if (aggregate instanceof Location) {
          aggregate._getSnapshot().name.should.equal('Hello');
          done();
        } else {
          done('Wrong type created');
        }
      }).catch(function (err) {
        done(err);
      });
    });
    it('stores snapshot for aggregate on save', function (done) {
      var factory = function () { return new Location(); };
      var part = new SnapshotPartition({ id: '1', version: 1, snapshot: { name: 'hello' } }, [{ id: 1, type: 'location.registered_name.event', payload: 'Hello' }]);
      var repo = new Repository(part, 'location', factory);
      repo.findById('1').then(function (aggregate) {
        aggregate.changeName('Hello, World!');
        repo.save(aggregate).then(function () {
          part.loadSnapshot('1').then(function (snapshot) {
            snapshot.snapshot.name.should.equal('Hello, World!');
            done();
          })
        });
      }).catch(function (err) {
        done(err);
      });;
    });
  });
  describe('#save', function () {
    it('save should clear uncommitted events ', function (done) {
      var factory = function () { return new Location(); };
      var repo = new Repository(new Partition(), 'location', factory);
      repo.findById('ID_THAT_DO_NOT_EXIST').then(function (location) {
        location.registerName('<NAME>');
        repo.save(location).then(function (x) {
          x.getUncommittedEvents().length.should.equal(0);
          done();
        });
      }).catch(function (err) {
        done(err);
      });
    });
  });
  it('removes and retries snapshot but does not end up in loop if not working', function (done) {
    var factory = function () { return new Location(); };
    var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { no_name: 'hello' } }, [{ id: 1, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello' }, { id: 2, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello, world' }]), 'location', factory);
    repo.findById('1').then(function() {
      done(new Error('should not fulfill'));
    }).catch(function () {
      done();
    });
  });
  it('removes and retries snapshot create when snapshot is broken', function (done) {
    var factory = function () { return new Location(); };
    var repo = new Repository(new SnapshotPartition({ id: '1', version: 1, snapshot: { no_name: 'hello' } }, [{ id: 1, aggregateId: '1', type: 'location.registered_name.event', payload: 'Hello' }, { id: 1, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello' }, { id: 2, aggregateId: '1', type: 'location.changed_name.event', payload: 'Hello, world' }]), 'location', factory);
    repo.findById('1').then(function (aggregate) {
      if (aggregate instanceof Location) {
        aggregate._getSnapshot().name.should.equal('Hello, world');
{'fraction_non_alphanumeric': 0.15949632738719832, 'fraction_numerical': 0.0067897043392383186, 'mean_word_length': 2.7443956551883524, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3697928', 'n_tokens_mistral': 4648, 'n_tokens_neox': 4562, 'n_words': 1145}