Browse Source

update tests

ameba23 3 years ago
parent
commit
9b4c85bfae
1 changed files with 25 additions and 4 deletions
  1. 25
    4
      test/index.test.js

+ 25
- 4
test/index.test.js View File

@@ -84,7 +84,7 @@ describe('message encoding', (context) => {
84 84
     assert.ok(encrypted instanceof Buffer, 'Returns a buffer')
85 85
 
86 86
     const message = encoder.decode(encrypted)
87
-    assert.same(message, Buffer.from('Hello World'), 'Decrypts the message')
87
+    assert.same(message, 'Hello World', 'Decrypts the message')
88 88
     next()
89 89
   })
90 90
 })
@@ -101,12 +101,33 @@ describe('hypercore', (context) => {
101 101
       assert.error(err, 'no error')
102 102
 
103 103
       var data = fs.readFileSync(path.join(storage.path, 'data'))
104
-      assert.notSame(data.toString(), 'boop', 'log entry is encrypted')
105
-      assert.same(encoder.decode(data), Buffer.from('boop'), 'log entry is encrypted')
104
+      assert.notSame(data, 'boop', 'log entry is encrypted')
105
+      assert.same(encoder.decode(data), 'boop', 'log entry is encrypted')
106 106
 
107 107
       feed.get(0, (err, entry) => {
108 108
         assert.error(err, 'no error')
109
-        assert.same(Buffer.from('boop'), entry, 'hypercore decrypts the message')
109
+        assert.same('boop', entry, 'hypercore decrypts the message')
110
+
111
+        cleanup(storage.root)
112
+        next()
113
+      })
114
+    })
115
+  })
116
+  context('encrypted the log, with a json object', (assert, next) => {
117
+    const storage = tmp()
118
+
119
+    const key = crypto.symmetricKey()
120
+    const encoder = crypto.encoder(key, { valueEncoding: 'json' })
121
+    const feed = hypercore(storage.path, { valueEncoding: encoder })
122
+
123
+    const message = { boop: 'beep' }
124
+
125
+    feed.append(message, (err) => {
126
+      assert.error(err, 'no error')
127
+
128
+      feed.get(0, (err, entry) => {
129
+        assert.error(err, 'no error')
130
+        assert.same(message, entry, 'hypercore decrypts the message')
110 131
 
111 132
         cleanup(storage.root)
112 133
         next()