var srcPath = process.env.ANGULAR_NODE_COVERAGE?'../src-cov':'../src';

var Store = require(srcPath + '/store.js');
var assert = require("assert");
var Buffer = require('buffer').Buffer;
var SyncObject = require(srcPath + '/sync_object.js');

describe('Buffer', function(){
	describe('#randomize()', function(){
		var buffer = new Buffer(100);
		it('should produce random results', function(){
			buffer.randomize();
			var result = buffer.toString('base64');
			buffer.randomize();
			assert.notEqual(result, buffer.toString('base64'));
		});
	});
});

describe('Store', function(){
	var object = new SyncObject({'this': 'is', 'a': {'big': 'test'}});
	var store = new Store();
	describe('#getUnusedHandlerId()', function(){
		it('should never produce similar results', function(){
			var result = store.getUnusedHandlerId();
			assert.notEqual(result, store.getUnusedHandlerId());
		});
	});
	
	describe('#store()', function(){
		store.store('test', object);
		it('should contain the same sync object', function(){
			assert.deepEqual(store.get('test'), object);
		});
	});
	
	describe('#get()', function(){
		it('should contain the sync object', function(){
			assert.deepEqual(store.get('test'), object);
		});
		var emptyStore = store.get('empty');
		it('should generate an empty store', function(){
			assert.equal(emptyStore.isEmpty(), true);
		});
	});
	
	describe('#serialize()', function(){
		
		var result = store.serialize('test');
		var resultObj = JSON.parse(result);
		it('should contain an handler key', function(){
			assert.notEqual(typeof(resultObj.handlerKey), 'undefined');
			assert.deepEqual(resultObj.a, {'big': 'test'});
		});
		
	});
	
	describe('#connect()', function(){
		var result = store.serialize('test');
		var resultObj = JSON.parse(result);
		it('should throw an exception if the listener does not exist', function(){
			assert.throws(function(){
				store.connect('not existing', function(){})
			}, Error);
		});
		it('should connect to the change listener', function(done){
			store.connect(resultObj.handlerKey, function(data){
				assert.equal(data.should, 'call');
				done();
			});
			object.update({should: 'call'});
		});
	});
	
	describe('#disconnect()', function(){
		var result = store.serialize('test2');
		var resultObj = JSON.parse(result);
		var object = store.get('test2');
		
		it('should disconnect the listener', function(done){
			store.connect(resultObj.handlerKey, function(data){
				assert.fail('The listener has been called!');
				done();
			});
			store.disconnect(resultObj.handlerKey);
			object.update({should: 'call'});
			done();
		});
	});
});

@@ -2151,32 +2151,276 @@
 'call'%7D);%0A%09%09%7D);%0A
+%09%09it('should not connect to the change listener a second time', function()%7B%0A%09%09%09assert.throws(function()%7B%0A%09%09%09%09store.connect(resultObj.handlerKey, function(data)%7B%0A%09%09%09%09%09assert.fail('has been registered!');%0A%09%09%09%09%09done();%0A%09%09%09%09%7D);%0A%09%09%09%7D, Error);%0A%09%09%7D);%0A
 %09%7D);%0A%09%0A%09describe
@@ -2833,25 +2833,251 @@
 %09%09done();%0A%09%09%7D);%0A
+%09%09%0A%09%09it('should throw an exception', function()%7B%0A%09%09%09assert.throws(function()%7B%0A%09%09%09%09store.disconnect(resultObj.handlerKey);%0A%09%09%09%7D, Error);%0A%09%09%09assert.throws(function()%7B%0A%09%09%09%09store.disconnect('does not exist!');%0A%09%09%09%7D, Error);%0A%09%09%7D);%0A
 %09%7D);%0A%7D);%0A

Add tests