git ssb

4+

Dominic / scuttlebot



Tree: c1997c8a400cf294934a5e97a93d35b3705dec55

Files: c1997c8a400cf294934a5e97a93d35b3705dec55 / test / plugins.js

4718 bytesRaw
1var ssbKeys = require('ssb-keys')
2var tape = require('tape')
3var u = require('./util')
4var pull = require('pull-stream')
5var osenv = require('osenv')
6var path = require('path')
7var fs = require('fs')
8var createSbot = require('../')
9
10var initialPlugins = createSbot.plugins.slice()
11function resetSbot () {
12 createSbot.plugins = initialPlugins.slice()
13 createSbot.use(require('../plugins/plugins'))
14}
15
16tape('install and load plugins', function (t) {
17
18 var aliceKeys = ssbKeys.generate()
19 var datadirPath = path.join(osenv.tmpdir(), 'test-plugins1')
20
21 t.test('install plugin', function (t) {
22
23 resetSbot()
24 var sbot = createSbot({
25 path: datadirPath,
26 port: 45451, host: 'localhost',
27 keys: aliceKeys
28 })
29
30 console.log('installing plugin...')
31 pull(
32 sbot.plugins.install('test-plugin', { from: __dirname + '/test-plugin' }),
33 pull.collect(function (err, out) {
34 if (err) throw err
35 console.log(out.map(function (b) { return b.toString('utf-8') }).join(''))
36
37 t.ok(fs.statSync(path.join(datadirPath, 'node_modules/test-plugin')))
38
39 sbot.close(function () {
40 t.end()
41 })
42 })
43 )
44 })
45
46 t.test('installed and enabled plugin is loaded', function (t) {
47
48 var config = {
49 path: datadirPath,
50 port: 45451, host: 'localhost',
51 keys: aliceKeys,
52 plugins: {
53 'test-plugin': true
54 }
55 }
56 resetSbot()
57 require('../plugins/plugins').loadUserPlugins(createSbot, config)
58 var sbot = createSbot(config)
59
60 t.ok(sbot.test)
61 t.ok(sbot.test.ping)
62
63 sbot.test.ping('ping', function (err, res) {
64 if (err) throw err
65 t.equal(res, 'ping pong')
66
67 sbot.close(function () {
68 t.end()
69 })
70 })
71 })
72
73 t.test('installed and disabled plugin is not loaded', function (t) {
74
75 var config = {
76 path: datadirPath,
77 port: 45451, host: 'localhost',
78 keys: aliceKeys,
79 plugins: {
80 'test-plugin': false
81 }
82 }
83 resetSbot()
84 require('../plugins/plugins').loadUserPlugins(createSbot, config)
85 var sbot = createSbot(config)
86
87 t.equal(sbot.test, undefined)
88
89 sbot.close(function () {
90 t.end()
91 })
92 })
93
94 t.test('uninstall plugin', function (t) {
95
96 resetSbot()
97 var sbot = createSbot({
98 path: datadirPath,
99 port: 45451, host: 'localhost',
100 keys: aliceKeys
101 })
102
103 console.log('uninstalling plugin...')
104 pull(
105 sbot.plugins.uninstall('test-plugin'),
106 pull.collect(function (err, out) {
107 if (err) throw err
108 console.log(out.map(function (b) { return b.toString('utf-8') }).join(''))
109
110 t.throws(function () { fs.statSync(path.join(datadirPath, 'node_modules/test-plugin')) })
111
112 sbot.close(function () {
113 t.end()
114 })
115 })
116 )
117 })
118
119 t.test('install plugin under custom name', function (t) {
120
121 resetSbot()
122 var sbot = createSbot({
123 path: datadirPath,
124 port: 45451, host: 'localhost',
125 keys: aliceKeys
126 })
127
128 console.log('installing plugin...')
129 pull(
130 sbot.plugins.install('my-test-plugin', { from: __dirname + '/test-plugin' }),
131 pull.collect(function (err, out) {
132 if (err) throw err
133 console.log(out.map(function (b) { return b.toString('utf-8') }).join(''))
134
135 t.ok(fs.statSync(path.join(datadirPath, 'node_modules/my-test-plugin')))
136
137 sbot.close(function () {
138 t.end()
139 })
140 })
141 )
142 })
143
144 t.test('installed and enabled plugin is loaded, under custom name', function (t) {
145
146 var config = {
147 path: datadirPath,
148 port: 45451, host: 'localhost',
149 keys: aliceKeys,
150 plugins: {
151 'my-test-plugin': true
152 }
153 }
154 resetSbot()
155 require('../plugins/plugins').loadUserPlugins(createSbot, config)
156 var sbot = createSbot(config)
157
158 t.ok(sbot.test)
159 t.ok(sbot.test.ping)
160
161 sbot.test.ping('ping', function (err, res) {
162 if (err) throw err
163 t.equal(res, 'ping pong')
164
165 sbot.close(function () {
166 t.end()
167 })
168 })
169 })
170
171 t.test('uninstall plugin under custom name', function (t) {
172
173 resetSbot()
174 var sbot = createSbot({
175 path: datadirPath,
176 port: 45451, host: 'localhost',
177 keys: aliceKeys
178 })
179
180 console.log('uninstalling plugin...')
181 pull(
182 sbot.plugins.uninstall('my-test-plugin'),
183 pull.collect(function (err, out) {
184 if (err) throw err
185 console.log(out.map(function (b) { return b.toString('utf-8') }).join(''))
186
187 t.throws(function () { fs.statSync(path.join(datadirPath, 'node_modules/my-test-plugin')) })
188
189 sbot.close(function () {
190 t.end()
191 })
192 })
193 )
194 })
195})

Built with git-ssb-web