diff --git a/.nycrc b/.nycrc new file mode 100644 index 0000000..eacd6bd --- /dev/null +++ b/.nycrc @@ -0,0 +1,11 @@ +{ + "include": [ + "src/**/*.js" + ], + "exclude": [ + "src/**/__test__/*.js" + ], + "cache": true, + "all": true +} + diff --git a/README.md b/README.md index d00837c..7cb4331 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ It is a lib to run [Gun](http://gun.js.org) host in Node.js 1. [Install](#install) 2. [Usage](#usage) +3. [Development](#development) # Install ``` @@ -132,3 +133,16 @@ root: { } } ``` +# Development +## Install libs +``` +npm install -g eslint eslint-config-google nyc +``` +## Test +``` +npm run rest +``` +## Run examples +``` +node examples/usage.js +``` diff --git a/package.json b/package.json index 2af42be..2670369 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "start": "node bootstrap.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "nyc ./node_modules/mocha/bin/mocha" }, "repository": { "type": "git", @@ -27,5 +27,10 @@ "hapi": "16.6.2", "lodash": "4.17.5", "pem": "1.12.3" + }, + "devDependencies": { + "expect.js": "0.3.1", + "mocha": "5.0.1", + "nyc": "11.4.1" } } diff --git a/src/__test__/gun-host.js b/src/__test__/gun-host.js new file mode 100644 index 0000000..e62ec88 --- /dev/null +++ b/src/__test__/gun-host.js @@ -0,0 +1,69 @@ +const {describe, it} = require('mocha'); +const expect = require('expect.js'); + +const GunHost = require('../gun-host'); + +describe('Gun', function() { + const cluster = { + enabled: true, + name: '_test_gun', + priority_for_master: 0, + absent_time_for_delete: 86400, + absent_time: 15, + loop_delay: 5, + cert: { + selfsigned: true, + valid: 10, + key: null, + cert: null, + }, + gun: { + port: 9000, + host: '0.0.0.0', + cache: 'data.json', + peers: ['https://localhost:9000/gun'], + }, + host: { + id: '123', + name: 'trex', + priority: 0, + node: 'hosts', + }, + }; + + const node = new GunHost({ + peers: cluster.gun.peers, + rootNodeName: cluster.name, + }); + + it('start server', function() { + return node.start({ + host: cluster.gun.host, + port: cluster.gun.port, + cache: cluster.gun.cache, + cert: cluster.cert, + }).then(function(resp) { + expect(resp).to.eql('gun server started successfully'); + }); + }); + + it('add node', function() { + return node.add(`${cluster.host.node}.${cluster.host.id}`, cluster.host).then(function(resp) { + expect(resp.err).to.be(null); + expect(resp.ok).to.be(1); + }); + }); + + it('get node', function() { + return node.get(`${cluster.host.node}.${cluster.host.id}`).then(function(resp) { + expect(resp.id).to.be(cluster.host.id); + }); + }); + + it('delete node', function() { + return node.delete(`${cluster.host.node}.${cluster.host.id}`).then(function(resp) { + expect(resp.err).to.be(null); + expect(resp.ok).to.be(1); + }); + }); +}); diff --git a/src/__test__/index.js b/src/__test__/index.js new file mode 100644 index 0000000..9f4df69 --- /dev/null +++ b/src/__test__/index.js @@ -0,0 +1 @@ +require('./gun-host'); diff --git a/src/gun-host.js b/src/gun-host.js index c3a2ec7..32f0226 100644 --- a/src/gun-host.js +++ b/src/gun-host.js @@ -32,7 +32,7 @@ class GunHost { * * @param {string} path for node * @param {object} node to add - * @return {object} added node + * @return {object} ack */ add(path, node) { return this.host.put(path, node); diff --git a/src/gun-promise.js b/src/gun-promise.js index c5b4143..e91a1cf 100644 --- a/src/gun-promise.js +++ b/src/gun-promise.js @@ -55,7 +55,7 @@ class GunPromise { * * @param {string} pathway - a.b.c or a * @param {object} value - * @return {object} new Gun node which contains value properties + * @return {object} ack */ put(pathway, value) { return new Promise((resolve, reject) => { @@ -72,14 +72,14 @@ class GunPromise { * Check if value exists * * @param {string} pathway - a.b.c or a - * @return {boolean} + * @return {boolean} exist */ exists(pathway) { return new Promise((resolve, reject) => { this.node.path(pathway).val(function(value) { resolve(value ? true : false); - }); - }); + }); + }); } /** @@ -108,7 +108,7 @@ class GunPromise { // 'null' put here to facilitate filtering deleted values this.node.path(pathway).put(null); // Gun bug, .put(cb) callback is not resolved if value is null: https://github.com/amark/gun/issues/453 - resolve(null); + resolve({err: null, ok: 1}); }); } } diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..6775ce1 --- /dev/null +++ b/test/index.js @@ -0,0 +1 @@ +require('../src/__test__');