describe('Gun', function(){ var Gun = require('../gun') , t = {}; describe('Utility', function(){ describe('Type Check', function(){ it('binary', function(){ expect(Gun.bi.is(false)).to.be(true); expect(Gun.bi.is(true)).to.be(true); expect(Gun.bi.is('')).to.be(false); expect(Gun.bi.is('a')).to.be(false); expect(Gun.bi.is(0)).to.be(false); expect(Gun.bi.is(1)).to.be(false); expect(Gun.bi.is([])).to.be(false); expect(Gun.bi.is([1])).to.be(false); expect(Gun.bi.is({})).to.be(false); expect(Gun.bi.is({a:1})).to.be(false); expect(Gun.bi.is(function(){})).to.be(false); }); it('number',function(){ expect(Gun.num.is(0)).to.be(true); expect(Gun.num.is(1)).to.be(true); expect(Gun.num.is(Infinity)).to.be(true); expect(Gun.num.is(NaN)).to.be(false); expect(Gun.num.is('')).to.be(false); expect(Gun.num.is('a')).to.be(false); expect(Gun.num.is([])).to.be(false); expect(Gun.num.is([1])).to.be(false); expect(Gun.num.is({})).to.be(false); expect(Gun.num.is({a:1})).to.be(false); expect(Gun.num.is(false)).to.be(false); expect(Gun.num.is(true)).to.be(false); expect(Gun.num.is(function(){})).to.be(false); }); it('text',function(){ expect(Gun.text.is('')).to.be(true); expect(Gun.text.is('a')).to.be(true); expect(Gun.text.is(false)).to.be(false); expect(Gun.text.is(true)).to.be(false); expect(Gun.text.is(0)).to.be(false); expect(Gun.text.is(1)).to.be(false); expect(Gun.text.is([])).to.be(false); expect(Gun.text.is([1])).to.be(false); expect(Gun.text.is({})).to.be(false); expect(Gun.text.is({a:1})).to.be(false); expect(Gun.text.is(function(){})).to.be(false); }); it('list',function(){ expect(Gun.list.is([])).to.be(true); expect(Gun.list.is([1])).to.be(true); expect(Gun.list.is(0)).to.be(false); expect(Gun.list.is(1)).to.be(false); expect(Gun.list.is('')).to.be(false); expect(Gun.list.is('a')).to.be(false); expect(Gun.list.is({})).to.be(false); expect(Gun.list.is({a:1})).to.be(false); expect(Gun.list.is(false)).to.be(false); expect(Gun.list.is(true)).to.be(false); expect(Gun.list.is(function(){})).to.be(false); }); it('obj',function(){ expect(Gun.obj.is({})).to.be(true); expect(Gun.obj.is({a:1})).to.be(true); expect(Gun.obj.is(0)).to.be(false); expect(Gun.obj.is(1)).to.be(false); expect(Gun.obj.is('')).to.be(false); expect(Gun.obj.is('a')).to.be(false); expect(Gun.obj.is([])).to.be(false); expect(Gun.obj.is([1])).to.be(false); expect(Gun.obj.is(false)).to.be(false); expect(Gun.obj.is(true)).to.be(false); expect(Gun.obj.is(function(){})).to.be(false); }); it('fns',function(){ expect(Gun.fns.is(function(){})).to.be(true); expect(Gun.fns.is('')).to.be(false); expect(Gun.fns.is('a')).to.be(false); expect(Gun.fns.is(0)).to.be(false); expect(Gun.fns.is(1)).to.be(false); expect(Gun.fns.is([])).to.be(false); expect(Gun.fns.is([1])).to.be(false); expect(Gun.fns.is({})).to.be(false); expect(Gun.fns.is({a:1})).to.be(false); expect(Gun.fns.is(false)).to.be(false); expect(Gun.fns.is(true)).to.be(false); }); it('time',function(){ t.ts = Gun.time.is(); expect(13 <= t.ts.toString().length).to.be.ok(); expect(Gun.num.is(t.ts)).to.be.ok(); expect(Gun.time.is(new Date())).to.be.ok(); }); }); describe('Text', function(){ it('ify',function(){ expect(Gun.text.ify(0)).to.be('0'); expect(Gun.text.ify(22)).to.be('22'); expect(Gun.text.ify([true,33,'yay'])).to.be('[true,33,"yay"]'); expect(Gun.text.ify({a:0,b:'1',c:[0,'1'],d:{e:'f'}})).to.be('{"a":0,"b":"1","c":[0,"1"],"d":{"e":"f"}}'); expect(Gun.text.ify(false)).to.be('false'); expect(Gun.text.ify(true)).to.be('true'); }); it('random',function(){ expect(Gun.text.random().length).to.be(24); expect(Gun.text.random(11).length).to.be(11); expect(Gun.text.random(4).length).to.be(4); t.tr = Gun.text.random(2,'as'); expect((t.tr=='as'||t.tr=='aa'||t.tr=='sa'||t.tr=='ss')).to.be.ok(); }); }); describe('List', function(){ it('slit',function(){ (function(){ expect(Gun.list.slit.call(arguments, 0)).to.eql([1,2,3,'a','b','c']); }(1,2,3,'a','b','c')); }); it('sort',function(){ expect([{i:9},{i:4},{i:1},{i:-3},{i:0}].sort(Gun.list.sort('i'))).to.eql([{i:-3},{i:0},{i:1},{i:4},{i:9}]); }); it('map',function(){ expect(Gun.list.map([1,2,3,4,5],function(v,i,t){ t(v+=this.d); this.d=v; },{d:0})).to.eql([1,3,6,10,15]); expect(Gun.list.map([2,3,0,4],function(v,i,t){ if(!v){ return } t(v*=this.d); this.d=v; },{d:1})).to.eql([2,6,24]); expect(Gun.list.map([true,false,NaN,Infinity,'',9],function(v,i,t){ if(i===3){ return 0 }})).to.be(0); }); }); describe('Object', function(){ it('del',function(){ var obj = {a:1,b:2}; Gun.obj.del(obj,'a'); expect(obj).to.eql({b:2}); }); it('has',function(){ var obj = {a:1,b:2}; expect(Gun.obj.has(obj,'a')).to.be.ok(); }); it('copy',function(){ var obj = {"a":false,"b":1,"c":"d","e":[0,1],"f":{"g":"h"}}; var copy = Gun.obj.copy(obj); expect(copy).to.eql(obj); expect(copy).to.not.be(obj); }); it('ify',function(){ expect(Gun.obj.ify('[0,1]')).to.eql([0,1]); expect(Gun.obj.ify('{"a":false,"b":1,"c":"d","e":[0,1],"f":{"g":"h"}}')).to.eql({"a":false,"b":1,"c":"d","e":[0,1],"f":{"g":"h"}}); }); it('map',function(){ expect(Gun.obj.map({a:'z',b:'y',c:'x'},function(v,i,t){ t(v,i) })).to.eql({x:'c',y:'b',z:'a'}); expect(Gun.obj.map({a:'z',b:false,c:'x'},function(v,i,t){ if(!v){ return } t(i,v) })).to.eql({a:'z',c:'x'}); expect(Gun.obj.map({a:'z',b:3,c:'x'},function(v,i,t){ if(v===3){ return 0 }})).to.be(0); }); }); describe('Functions', function(){ it('sum',function(done){ var obj = {a:2, b:2, c:3, d: 9}; Gun.obj.map(obj, function(num, key){ setTimeout(this.add(function(){ this.done(null, num * num); }, key), parseInt((""+Math.random()).substring(2,5))); }, Gun.fns.sum(function(err, val){ expect(val.a).to.eql(4); expect(val.b).to.eql(4); expect(val.c).to.eql(9); expect(val.d).to.eql(81); done(); })); }); }); describe('Gun Safety', function(){ var gun = Gun(); it('is',function(){ expect(Gun.is(gun)).to.be(true); expect(Gun.is(true)).to.be(false); expect(Gun.is(false)).to.be(false); expect(Gun.is(0)).to.be(false); expect(Gun.is(1)).to.be(false); expect(Gun.is('')).to.be(false); expect(Gun.is('a')).to.be(false); expect(Gun.is(Infinity)).to.be(false); expect(Gun.is(NaN)).to.be(false); expect(Gun.is([])).to.be(false); expect(Gun.is([1])).to.be(false); expect(Gun.is({})).to.be(false); expect(Gun.is({a:1})).to.be(false); expect(Gun.is(function(){})).to.be(false); }); it('is value',function(){ expect(Gun.is.value(false)).to.be(true); expect(Gun.is.value(true)).to.be(true); expect(Gun.is.value(0)).to.be(true); expect(Gun.is.value(1)).to.be(true); expect(Gun.is.value('')).to.be(true); expect(Gun.is.value('a')).to.be(true); expect(Gun.is.value({'#':'somesoulidhere'})).to.be('somesoulidhere'); expect(Gun.is.value({'#':'somesoulidhere', and: 'nope'})).to.be(false); expect(Gun.is.value(Infinity)).to.be(false); // boohoo :( expect(Gun.is.value(NaN)).to.be(false); expect(Gun.is.value([])).to.be(false); expect(Gun.is.value([1])).to.be(false); expect(Gun.is.value({})).to.be(false); expect(Gun.is.value({a:1})).to.be(false); expect(Gun.is.value(function(){})).to.be(false); }); it('is soul',function(){ expect(Gun.is.soul({'#':'somesoulidhere'})).to.be('somesoulidhere'); expect(Gun.is.soul({'#':'somethingelsehere'})).to.be('somethingelsehere'); expect(Gun.is.soul({'#':'somesoulidhere', and: 'nope'})).to.be(false); expect(Gun.is.soul({or: 'nope', '#':'somesoulidhere'})).to.be(false); expect(Gun.is.soul(false)).to.be(false); expect(Gun.is.soul(true)).to.be(false); expect(Gun.is.soul('')).to.be(false); expect(Gun.is.soul('a')).to.be(false); expect(Gun.is.soul(0)).to.be(false); expect(Gun.is.soul(1)).to.be(false); expect(Gun.is.soul(Infinity)).to.be(false); // boohoo :( expect(Gun.is.soul(NaN)).to.be(false); expect(Gun.is.soul([])).to.be(false); expect(Gun.is.soul([1])).to.be(false); expect(Gun.is.soul({})).to.be(false); expect(Gun.is.soul({a:1})).to.be(false); expect(Gun.is.soul(function(){})).to.be(false); }); it('is node',function(){ expect(Gun.is.node({_:{'#':'somesoulidhere'}})).to.be(true); expect(Gun.is.node({_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}})).to.be(true); expect(Gun.is.node({_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}, g: Infinity})).to.be(false); expect(Gun.is.node({_:{'#':'somesoulidhere'}, a:0, b: 1, z: NaN, c: '', d: 'e'})).to.be(false); expect(Gun.is.node({_:{'#':'somesoulidhere'}, a:0, b: 1, y: {_: 'cool'}, c: '', d: 'e'})).to.be(false); expect(Gun.is.node({_:{'#':'somesoulidhere'}, a:0, b: 1, x: [], c: '', d: 'e'})).to.be(false); expect(Gun.is.node({})).to.be(false); expect(Gun.is.node({a:1})).to.be(false); expect(Gun.is.node({_:{}})).to.be(false); expect(Gun.is.node({_:{}, a:1})).to.be(false); expect(Gun.is.node({'#':'somesoulidhere'})).to.be(false); }); it('is graph',function(){ expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}}})).to.be(true); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}}, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(true); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}}, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(true); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}}})).to.be(true); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}}, foo: 1, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(false); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}}, foo: {}, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(false); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}}, foo: {_:{'#':'FOO'}}, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(false); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}}, foo: {_:{}}, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(false); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: Infinity, c: '', d: 'e', f: {'#':'somethingelsehere'}}})).to.be(false); expect(Gun.is.graph({'somesoulidhere': {_:{'#':'somesoulidhere'}, a:0, b: Infinity, c: '', d: 'e', f: {'#':'somethingelsehere'}}, 'somethingelsehere': {_:{'#':'somethingelsehere'}}})).to.be(false); expect(Gun.is.graph({_:{'#':'somesoulidhere'}})).to.be(false); expect(Gun.is.graph({_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}})).to.be(false); expect(Gun.is.graph({_:{'#':'somesoulidhere'}, a:0, b: 1, c: '', d: 'e', f: {'#':'somethingelsehere'}, g: Infinity})).to.be(false); expect(Gun.is.graph({_:{'#':'somesoulidhere'}, a:0, b: 1, z: NaN, c: '', d: 'e'})).to.be(false); expect(Gun.is.graph({_:{'#':'somesoulidhere'}, a:0, b: 1, y: {_: 'cool'}, c: '', d: 'e'})).to.be(false); expect(Gun.is.graph({_:{'#':'somesoulidhere'}, a:0, b: 1, x: [], c: '', d: 'e'})).to.be(false); expect(Gun.is.graph({})).to.be(false); // Empty graph is not a graph :( expect(Gun.is.graph({a:1})).to.be(false); expect(Gun.is.graph({_:{}})).to.be(false); expect(Gun.is.graph({_:{}, a:1})).to.be(false); expect(Gun.is.graph({'#':'somesoulidhere'})).to.be(false); }); }); }); it('ify', function(){ var data, test; data = {a: false, b: true, c: 0, d: 1, e: '', f: 'g', h: null}; test = Gun.ify(data); expect(test.err).to.not.be.ok(); data = {}; data.a = {x: 1, y: 2, z: 3} data.b = {m: 'n', o: 'p', q: 'r', s: 't'}; data.a.kid = data.b; data.b.parent = data.a; data.loop = [data.b, data.a.kid, data]; test = Gun.ify(data); expect(test.err).to.not.be.ok(); data = {_: {'#': 'shhh', meta: {yay: 1}}, sneak: true}; test = Gun.ify(data); expect(test.err).to.not.be.ok(); // metadata needs to be stored, but it can't be used for data. data = {}; data.sneak = false; data.both = {inside: 'meta data'}; data._ = {'#': 'shhh', data: {yay: 1}, spin: data.both}; test = Gun.ify(data); expect(test.err.meta).to.be.ok(); // TODO: Fail: this passes, somehow? Fix ify code! data = {one: {two: [9, 8, 7, 6, 5]}}; test = Gun.ify(data); expect(test.err.array).to.be.ok(); data = {z: undefined, x: 'bye'}; test = Gun.ify(data); expect(test.err.invalid).to.be.ok(); data = {a: NaN, b: 2}; test = Gun.ify(data); expect(test.err.invalid).to.be.ok(); data = {a: 1, b: Infinity}; test = Gun.ify(data); expect(test.err.invalid).to.be.ok(); data = {c: function(){}, d: 'hi'}; test = Gun.ify(data); expect(test.err.invalid).to.be.ok(); }); it('union', function(){ var graph, prime; graph = Gun.ify({a: false, b: true, c: 0, d: 1, e: '', f: 'g', h: null}).nodes; prime = Gun.ify({h: 9, i: 'foo', j: 'k', l: 'bar', m: 'Mark', n: 'Nadal'}).nodes; Gun.union(graph, prime); // TODO: BUG! Where is the expect??? }); describe('API', function(){ require('../lib/file'); var gun = Gun({file: 'data.json'}); /* it('set key get', function(done){ gun.set({hello: "world"}).key('hello/world').get(function(val){ expect(val.hello).to.be('world'); done(); }); }); it('load', function(done){ gun.load('hello/world').get(function(val){ expect(val.hello).to.be('world'); done(); }); }); it('load path', function(done){ gun.load('hello/world').path('hello').get(function(val){ expect(val).to.be('world'); done(); }); }); it('load set path', function(done){ gun.load('hello/world').set({hello: 'Mark'}).path('hello').get(function(val){ expect(val).to.be('Mark'); done(); }); }); it('load path set', function(done){ gun.load('hello/world').path('hello').set('World').get(function(val){ expect(val).to.be('World'); done(); }); }); */ it('load blank kick get', function(done){ // it would be cool with GUN console.log("test blank kicking"); gun.load("some/empty/thing").blank(function(){ // that if you call blank first console.log("blank happened"); this.set({now: 'exists'}); // you can set stuff }).get(function(val){ // and THEN still retrieve it. console.log("get happened"); expect(val.now).to.be('exists'); done(); }); }); /* it.skip('var set key path', function(done){ // contexts should be able to be saved to a variable var foo = gun.set({foo: 'bar'}).key('foo/bar'); foo.path('hello.world.nowhere')// this should only change the context temporarily setTimeout(function(){ foo.path('foo').get(function(val){ // and then be reused from the same original context console.log('?', val); expect(val).to.be('bar'); // this should work done(); }); }, 100); }); it.skip('var load path', function(done){ // contexts should be able to be saved to a variable var foo = gun.load('foo/bar'); foo.path('hello.world.nowhere')// this should only change the context temporarily setTimeout(function(){ foo.path('foo').get(function(val){ // and then be reused from the same original context expect(val).to.be('bar'); // this should work done(); }); }, 100); }); it('path', function(done){ console.log("fix path!"); return done(); // TODO: FIX! This is broken because of API changes, fix it! this.timeout(9000); var gun = require('gun')({ s3: require('./shotgun') // replace this with your own keys! }); gun.load('d1ed426098eae2bba8c60605e1e4552f66281770', null, {id: true}) // get Rodney Morris .path('parent.parent.first') // Rodney's parent is Juan Colon, whose parent is Francis Peters .get(function(val){ console.log("val!", val); done(); }); console.log("________________________"); }); */ }); });