Browse Source

add a test for appending to the local log feed

Kieran Gibb 3 years ago
parent
commit
e764c8fb11
No account linked to committer's email address
4 changed files with 46 additions and 2 deletions
  1. 27
    1
      index.js
  2. 3
    1
      package.json
  3. 11
    0
      test/index.test.js
  4. 5
    0
      util.js

+ 27
- 1
index.js View File

@@ -12,7 +12,11 @@ const ram = require('random-access-memory')
12 12
 const raf = require('random-access-file')
13 13
 const level = require('level')
14 14
 const memdb = require('memdb')
15
+const thunky = require('thunky')
15 16
 const { EventEmitter } = require('events')
17
+const assert = require('assert')
18
+
19
+const { isFunction } = require('./util')
16 20
 
17 21
 const crypto = Crypto()
18 22
 
@@ -21,6 +25,8 @@ const indexes = [
21 25
   { key: 'typ', value: [['value', 'type'], ['value', 'timestamp']] },
22 26
 ]
23 27
 
28
+const CALLBACK_REQUIRED = 'cobox-group: callback required'
29
+
24 30
 module.exports = (storage, key, opts) => new CoBoxGroup(storage, key, opts)
25 31
 
26 32
 class CoBoxGroup extends EventEmitter {
@@ -71,8 +77,28 @@ class CoBoxGroup extends EventEmitter {
71 77
     })
72 78
 
73 79
     this.core.use('query', Query(this.logDB, { indexes, validator }))
80
+
81
+    this.log = thunky(this.log.bind(this))
74 82
     this.logs = this.core.api.query
75 83
   }
84
+
85
+  ready (callback) {
86
+    if (this._isReady) return callback()
87
+    else this._ready(callback)
88
+  }
89
+
90
+  // TODO: we should be able to change this so once ready, the log will be the local feed
91
+  // That way we won't need thunky...
92
+  log (callback) {
93
+    assert(isFunction(callback), CALLBACK_REQUIRED)
94
+    this.core.writer('local', callback)
95
+  }
96
+
97
+  // ------------------------------ PRIVATE FUNCTIONS ------------------------------
98
+
99
+  _ready (callback) {
100
+    // make sure the core and everything is ready...
101
+  }
76 102
 }
77 103
 
78 104
 function validator (msg) {
@@ -99,7 +125,7 @@ function storeSecret (storage, pubKey, secretKey) {
99 125
   fs.writeFileSync(path.join(SECRET_KEYS(storage), pubKey.toString('hex')), secretKey)
100 126
 }
101 127
 
102
-// ------------------------------------------------------------------------------------------
128
+// ------------------------ MOVE TO COLLECTION CLASS IN COBOX-CORE ----------------------------
103 129
 
104 130
 // TODO: associate group with the creating identity, take peer pubKey as a param...
105 131
 CoBoxGroup.create = function (name, opts) {

+ 3
- 1
package.json View File

@@ -4,6 +4,7 @@
4 4
   "description": "a p2p private group for files and application-layer data",
5 5
   "main": "index.js",
6 6
   "dependencies": {
7
+    "assert": "^2.0.0",
7 8
     "cobox-config": "^0.0.1",
8 9
     "cobox-crypto": "^1.0.0",
9 10
     "debug": "^4.1.1",
@@ -17,7 +18,8 @@
17 18
     "multifeed": "^4.1.5",
18 19
     "pump": "^3.0.0",
19 20
     "random-access-file": "^2.1.3",
20
-    "random-access-memory": "^3.1.1"
21
+    "random-access-memory": "^3.1.1",
22
+    "thunky": "^1.0.3"
21 23
   },
22 24
   "devDependencies": {
23 25
     "nyc": "^14.1.1",

+ 11
- 0
test/index.test.js View File

@@ -14,4 +14,15 @@ describe('basic', (context) => {
14 14
     assert.ok(group, 'group created')
15 15
     next()
16 16
   })
17
+
18
+  context('append to a group log', (assert, next) => {
19
+    var key = crypto.accessKey()
20
+    var group = Group(ram, key)
21
+    group.log((feed) => {
22
+      feed.append({ type: 'group/about', content: { name: 'CoBox Team' } }, (err, seq) => {
23
+        assert.error(err, 'no error')
24
+        assert.same(seq, 0, 'appended message')
25
+      })
26
+    })
27
+  })
17 28
 })

+ 5
- 0
util.js View File

@@ -0,0 +1,5 @@
1
+function isFunction (variable) {
2
+  return typeof variable === 'function'
3
+}
4
+
5
+module.exports = { isFunction }