Browse Source

complete repository pattern in group-store

Kieran Gibb 2 years ago
parent
commit
2176e3ea27
No known key found for this signature in database
2 changed files with 50 additions and 25 deletions
  1. 8
    13
      index.js
  2. 42
    12
      test/index.test.js

+ 8
- 13
index.js View File

@@ -20,9 +20,9 @@ class GroupStore {
20 20
     this.createGroup = createGroup
21 21
 
22 22
     this.groups = {}
23
-    this.config.groups.list().forEach((groupOpts) => (
23
+    this.config.groups.list().forEach((groupOpts, i) => {
24 24
       this._cacheGroup(this._buildGroup(groupOpts))
25
-    ))
25
+    })
26 26
 
27 27
     this._readyCallback = thunky(this._ready.bind(this))
28 28
   }
@@ -54,10 +54,9 @@ class GroupStore {
54 54
 
55 55
       for (var i = 0; i < keys.length; ++i) {
56 56
         var key = keys[i]
57
-        response = response.filter((entry) => {
58
-          var found = entry[key] === params[key]
59
-          return found
60
-        })
57
+        response = response.filter((entry) => (
58
+          entry[key].toString('hex') === params[key].toString('hex')
59
+        ))
61 60
       }
62 61
 
63 62
       return response
@@ -83,7 +82,7 @@ class GroupStore {
83 82
       if (group) throw new Error('group already exists')
84 83
 
85 84
       var { name, address, encryptionKey } = params
86
-      var group = this.buildGroup(params)
85
+      var group = this._buildGroup(params)
87 86
 
88 87
       await group.ready()
89 88
 
@@ -132,17 +131,13 @@ class GroupStore {
132 131
   _buildGroup (params = {}) {
133 132
     return this.createGroup(
134 133
       this.storage,
135
-      Buffer.from(params.address),
136
-      Object.assign(this.opts, {
137
-        encryptionKey: Buffer.from(params.encryptionKey),
138
-        name: params.name
139
-      })
134
+      params.address,
135
+      Object.assign(this.opts, params)
140 136
     )
141 137
   }
142 138
 
143 139
   _cacheGroup (group) {
144 140
     this.groups[group.address.toString('hex')] = group
145
-    this.groups[group.address] = group
146 141
     if (group.name) this.groups[group.name] = group
147 142
     return true
148 143
   }

+ 42
- 12
test/index.test.js View File

@@ -18,34 +18,64 @@ describe('group store: basic', (context) => {
18 18
 
19 19
     await store.ready()
20 20
 
21
-    assert.ok(Object.values(store.groups).length, 'builds then caches groups from the config')
22
-    assert.ok(store.groups[params.address], 'accessible using buffer')
23
-    assert.ok(store.groups[params.address.toString('hex')], 'accessible using hex')
21
+    assert.same(Object.values(store.groups).length, 1, 'builds then caches groups from the config')
22
+    assert.ok(store.groups[params.address.toString('hex')], 'accessible using address hex string')
24 23
 
25 24
     cleanup(storage, next)
26 25
   })
27 26
 
28
-  context('where()', async function (assert, next) {
27
+  context('all()', async function (assert, next) {
29 28
     let storage = tmp(),
30 29
       config = Config(storage),
31 30
       params = crypto.keySet()
32 31
 
33
-    Object.keys(params).forEach((key) => {
34
-      if (Buffer.isBuffer(params[key])) params[key] = Buffer.from(params[key])
35
-    })
32
+    config.groups.set(params.address, params)
33
+
34
+    const store = Store(Decrypted, storage, { config })
35
+
36
+    await store.ready()
37
+
38
+    var response = await store.all(params)
39
+    assert.ok(Array.isArray(response), 'returns an array')
40
+    assert.same(response.length, 1, 'returns the correct number')
41
+    cleanup(storage, next)
42
+  })
43
+
44
+  context('where()', async function (assert, next) {
45
+    let storage = tmp(),
46
+      config = Config(storage),
47
+      params = crypto.keySet()
36 48
 
37 49
     config.groups.set(params.address, params)
38
-    config.save()
39
-    config.load()
40 50
 
41 51
     const store = Store(Decrypted, storage, { config })
42 52
 
43 53
     await store.ready()
44 54
 
55
+    delete params.encryptionKey
45 56
     var response = await store.where(params)
57
+    assert.ok(Array.isArray(response), 'returns an array')
58
+    var group = response[0]
59
+    assert.ok(group, 'finds a group')
60
+    assert.same(group && group.address, params.address, 'finds the correct group')
61
+    cleanup(storage, next)
62
+  })
63
+
64
+  context('findBy()', async function (assert, next) {
65
+    let storage = tmp(),
66
+      config = Config(storage),
67
+      params = crypto.keySet()
68
+
69
+    config.groups.set(params.address, params)
46 70
 
47
-    assert.same(response.address, params.address, 'finds a group')
71
+    const store = Store(Decrypted, storage, { config })
72
+
73
+    await store.ready()
48 74
 
75
+    delete params.encryptionKey
76
+    var response = await store.findBy(params)
77
+    assert.ok(response, 'finds a group')
78
+    assert.same(response && response.address, params.address, 'finds the correct group')
49 79
     cleanup(storage, next)
50 80
   })
51 81
 
@@ -55,9 +85,9 @@ describe('group store: basic', (context) => {
55 85
       params = Object.assign(crypto.keySet(), { name: 'magma' })
56 86
 
57 87
     const store = Store(Decrypted, storage, { config })
58
-    var a = await store.create(params)
88
+    var group = await store.create(params)
59 89
 
60
-    assert.ok(a, 'creates a new group')
90
+    assert.ok(group && group.address, 'creates a new group')
61 91
     cleanup(storage, next)
62 92
   })
63 93
 })