This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
log.spec.js
656 lines (581 loc) · 24.3 KB
/
log.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import { notStrictEqual, deepStrictEqual, strictEqual } from 'assert'
import rimraf from 'rimraf'
import { CID } from 'multiformats/cid'
import { base58btc } from 'multiformats/bases/base58'
import Clock from '../src/lamport-clock.js'
import Entry from '../src/entry.js'
import Log from '../src/log.js'
import IdentityProvider from 'orbit-db-identity-provider'
import Keystore from 'orbit-db-keystore'
import { copy } from 'fs-extra'
import { read, write } from 'orbit-db-io'
// For tiebreaker testing
import LogSorting from '../src/log-sorting.js'
// Test utils
import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils'
const { LastWriteWins } = LogSorting
const FirstWriteWins = (a, b) => LastWriteWins(a, b) * -1
const { sync: rmrf } = rimraf
const { create } = Entry
const { createIdentity } = IdentityProvider
const { fromMultihash, fromEntryHash } = Log
let ipfsd, ipfs, testIdentity, testIdentity2, testIdentity3
Object.keys(testAPIs).forEach((IPFS) => {
describe('Log (' + IPFS + ')', function () {
this.timeout(config.timeout)
const { identityKeyFixtures, signingKeyFixtures, identityKeysPath, signingKeysPath } = config
let keystore, signingKeystore
before(async () => {
await copy(identityKeyFixtures, identityKeysPath)
await copy(signingKeyFixtures, signingKeysPath)
keystore = new Keystore(identityKeysPath)
signingKeystore = new Keystore(signingKeysPath)
testIdentity = await createIdentity({ id: 'userA', keystore, signingKeystore })
testIdentity2 = await createIdentity({ id: 'userB', keystore, signingKeystore })
testIdentity3 = await createIdentity({ id: 'userC', keystore, signingKeystore })
ipfsd = await startIpfs(IPFS, config.defaultIpfsConfig)
ipfs = ipfsd.api
})
after(async () => {
await stopIpfs(ipfsd)
rmrf(signingKeysPath)
rmrf(identityKeysPath)
await keystore.close()
await signingKeystore.close()
})
describe('constructor', async () => {
it('creates an empty log with default params', () => {
const log = new Log(ipfs, testIdentity)
notStrictEqual(log._entryIndex, null)
notStrictEqual(log._headsIndex, null)
notStrictEqual(log._id, null)
notStrictEqual(log.id, null)
notStrictEqual(log.clock, null)
notStrictEqual(log.values, null)
notStrictEqual(log.heads, null)
notStrictEqual(log.tails, null)
notStrictEqual(log.tailCids, null)
deepStrictEqual(log.values, [])
deepStrictEqual(log.heads, [])
deepStrictEqual(log.tails, [])
})
it('throws an error if IPFS instance is not passed as an argument', () => {
let err
try {
const log = new Log() // eslint-disable-line no-unused-vars
} catch (e) {
err = e
}
strictEqual(err.message, 'IPFS instance not defined')
})
it('sets an id', () => {
const log = new Log(ipfs, testIdentity, { logId: 'ABC' })
strictEqual(log.id, 'ABC')
})
it('sets the clock id', () => {
const log = new Log(ipfs, testIdentity, { logId: 'ABC' })
strictEqual(log.id, 'ABC')
strictEqual(log.clock.id, testIdentity.publicKey)
})
it('generates id string if id is not passed as an argument', () => {
const log = new Log(ipfs, testIdentity)
strictEqual(typeof log.id === 'string', true)
})
it('sets items if given as params', async () => {
const one = await create(ipfs, testIdentity, 'A', 'entryA', [], new Clock('A', 0))
const two = await create(ipfs, testIdentity, 'A', 'entryB', [], new Clock('B', 0))
const three = await create(ipfs, testIdentity, 'A', 'entryC', [], new Clock('C', 0))
const log = new Log(ipfs, testIdentity,
{ logId: 'A', entries: [one, two, three] })
strictEqual(log.length, 3)
strictEqual(log.values[0].payload, 'entryA')
strictEqual(log.values[1].payload, 'entryB')
strictEqual(log.values[2].payload, 'entryC')
})
it('sets heads if given as params', async () => {
const one = await create(ipfs, testIdentity, 'A', 'entryA', [])
const two = await create(ipfs, testIdentity, 'A', 'entryB', [])
const three = await create(ipfs, testIdentity, 'A', 'entryC', [])
const log = new Log(ipfs, testIdentity,
{ logId: 'B', entries: [one, two, three], heads: [three] })
strictEqual(log.heads.length, 1)
strictEqual(log.heads[0].hash, three.hash)
})
it('finds heads if heads not given as params', async () => {
const one = await create(ipfs, testIdentity, 'A', 'entryA', [])
const two = await create(ipfs, testIdentity, 'A', 'entryB', [])
const three = await create(ipfs, testIdentity, 'A', 'entryC', [])
const log = new Log(ipfs, testIdentity,
{ logId: 'A', entries: [one, two, three] })
strictEqual(log.heads.length, 3)
strictEqual(log.heads[2].hash, one.hash)
strictEqual(log.heads[1].hash, two.hash)
strictEqual(log.heads[0].hash, three.hash)
})
it('throws an error if entries is not an array', () => {
let err
try {
const log = new Log(ipfs, testIdentity, { logId: 'A', entries: {} }) // eslint-disable-line no-unused-vars
} catch (e) {
err = e
}
notStrictEqual(err, undefined)
strictEqual(err.message, '\'entries\' argument must be an array of Entry instances')
})
it('throws an error if heads is not an array', () => {
let err
try {
const log = new Log(ipfs, testIdentity, { logId: 'A', entries: [], heads: {} }) // eslint-disable-line no-unused-vars
} catch (e) {
err = e
}
notStrictEqual(err, undefined)
strictEqual(err.message, '\'heads\' argument must be an array')
})
it('creates default public AccessController if not defined', async () => {
const log = new Log(ipfs, testIdentity) // eslint-disable-line no-unused-vars
const anyoneCanAppend = await log._access.canAppend('any')
notStrictEqual(log._access, undefined)
strictEqual(anyoneCanAppend, true)
})
it('throws an error if identity is not defined', () => {
let err
try {
const log = new Log(ipfs) // eslint-disable-line no-unused-vars
} catch (e) {
err = e
}
notStrictEqual(err, undefined)
strictEqual(err.message, 'Identity is required')
})
})
describe('toString', async () => {
let log
const expectedData = 'five\n└─four\n └─three\n └─two\n └─one'
beforeEach(async () => {
log = new Log(ipfs, testIdentity, { logId: 'A' })
await log.append('one')
await log.append('two')
await log.append('three')
await log.append('four')
await log.append('five')
})
it('returns a nicely formatted string', () => {
strictEqual(log.toString(), expectedData)
})
})
describe('get', async () => {
let log
beforeEach(async () => {
log = new Log(ipfs, testIdentity, { logId: 'AAA' })
await log.append('one')
})
it('returns an Entry', () => {
const entry = log.get(log.values[0].hash)
deepStrictEqual(entry.hash, 'zdpuAoFzNYcuuQHk1gLcB8fomHGrqT9k1uQeAvewZJ1cSYrms')
})
it('returns undefined when Entry is not in the log', () => {
const entry = log.get('QmFoo')
deepStrictEqual(entry, undefined)
})
})
describe('setIdentity', () => {
let log
beforeEach(async () => {
log = new Log(ipfs, testIdentity, { logId: 'AAA' })
await log.append('one')
})
it('changes identity', async () => {
strictEqual(log.values[0].clock.id, testIdentity.publicKey)
strictEqual(log.values[0].clock.time, 1)
log.setIdentity(testIdentity2)
await log.append('two')
strictEqual(log.values[1].clock.id, testIdentity2.publicKey)
strictEqual(log.values[1].clock.time, 2)
log.setIdentity(testIdentity3)
await log.append('three')
strictEqual(log.values[2].clock.id, testIdentity3.publicKey)
strictEqual(log.values[2].clock.time, 3)
})
})
describe('has', async () => {
let log, expectedData
before(async () => {
expectedData = {
hash: 'zdpuAoFzNYcuuQHk1gLcB8fomHGrqT9k1uQeAvewZJ1cSYrms',
id: 'AAA',
payload: 'one',
next: [],
v: 1,
clock: new Clock(testIdentity.publicKey, 1),
key: testIdentity.toJSON()
}
const sig = await testIdentity.provider.sign(testIdentity, Buffer.from(JSON.stringify(expectedData)))
Object.assign(expectedData, { sig })
})
beforeEach(async () => {
log = new Log(ipfs, testIdentity, { logId: 'AAA' })
await log.append('one')
})
it('returns true if it has an Entry', () => {
strictEqual(log.has(expectedData), true)
})
it('returns true if it has an Entry, hash lookup', () => {
strictEqual(log.has(expectedData.hash), true)
})
it('returns false if it doesn\'t have the Entry', () => {
strictEqual(log.has('zdFoo'), false)
})
})
describe('serialize', async () => {
let log
const expectedData = {
id: 'AAA',
heads: ['zdpuApASvEM59JKWn7Y39JWVSoiQ2CoJWpWseNTzqWvX1dRtC']
}
beforeEach(async () => {
log = new Log(ipfs, testIdentity, { logId: 'AAA' })
await log.append('one')
await log.append('two')
await log.append('three')
})
describe('toJSON', () => {
it('returns the log in JSON format', () => {
strictEqual(JSON.stringify(log.toJSON()), JSON.stringify(expectedData))
})
})
describe('toSnapshot', () => {
const expectedData = {
id: 'AAA',
heads: ['zdpuApASvEM59JKWn7Y39JWVSoiQ2CoJWpWseNTzqWvX1dRtC'],
values: [
'zdpuAoFzNYcuuQHk1gLcB8fomHGrqT9k1uQeAvewZJ1cSYrms',
'zdpuAo5DjP7XfnJqe8v8RTedi44Xg2w49Wb9xwRBdzf3LNJCV',
'zdpuApASvEM59JKWn7Y39JWVSoiQ2CoJWpWseNTzqWvX1dRtC'
]
}
it('returns the log snapshot', () => {
const snapshot = log.toSnapshot()
strictEqual(snapshot.id, expectedData.id)
strictEqual(snapshot.heads.length, expectedData.heads.length)
strictEqual(snapshot.heads[0].hash, expectedData.heads[0])
strictEqual(snapshot.values.length, expectedData.values.length)
strictEqual(snapshot.values[0].hash, expectedData.values[0])
strictEqual(snapshot.values[1].hash, expectedData.values[1])
strictEqual(snapshot.values[2].hash, expectedData.values[2])
})
})
describe('toBuffer', () => {
it('returns the log as a Buffer', () => {
deepStrictEqual(log.toBuffer(), Buffer.from(JSON.stringify(expectedData)))
})
})
describe('toMultihash - cbor', async () => {
it('returns the log as ipfs CID', async () => {
const expectedCid = 'zdpuAwC43AQmYEPAnmidtfuUdBQSWuK95z1446UntBMhrqdto'
const log = new Log(ipfs, testIdentity, { logId: 'A' })
await log.append('one')
const hash = await log.toMultihash()
strictEqual(hash, expectedCid)
})
it('log serialized to ipfs contains the correct data', async () => {
const expectedData = {
id: 'A',
heads: ['zdpuAky58cAEgNyxPGotdCZny1sfk7ima9FJVtPTydDgrCFZw']
}
const expectedCid = 'zdpuAwC43AQmYEPAnmidtfuUdBQSWuK95z1446UntBMhrqdto'
const log = new Log(ipfs, testIdentity, { logId: 'A' })
await log.append('one')
const hash = await log.toMultihash()
strictEqual(hash, expectedCid)
const result = await read(ipfs, hash)
const heads = result.heads.map(head => head.toString(base58btc))
deepStrictEqual(heads, expectedData.heads)
})
it('throws an error if log items is empty', async () => {
const emptyLog = new Log(ipfs, testIdentity)
let err
try {
await emptyLog.toMultihash()
} catch (e) {
err = e
}
notStrictEqual(err, null)
strictEqual(err.message, 'Can\'t serialize an empty log')
})
})
describe('toMultihash - pb', async () => {
it('returns the log as ipfs multihash', async () => {
const expectedMultihash = 'QmSgYrc2cbLghngrBWJtNvmh282BrUgtGxzjYhEuPgC7Sj'
const log = new Log(ipfs, testIdentity, { logId: 'A' })
await log.append('one')
const multihash = await log.toMultihash({ format: 'dag-pb' })
strictEqual(multihash, expectedMultihash)
})
it('log serialized to ipfs contains the correct data', async () => {
const expectedData = {
id: 'A',
heads: ['zdpuAky58cAEgNyxPGotdCZny1sfk7ima9FJVtPTydDgrCFZw']
}
const expectedMultihash = 'QmSgYrc2cbLghngrBWJtNvmh282BrUgtGxzjYhEuPgC7Sj'
const log = new Log(ipfs, testIdentity, { logId: 'A' })
await log.append('one')
const multihash = await log.toMultihash({ format: 'dag-pb' })
strictEqual(multihash, expectedMultihash)
const result = await ipfs.object.get(CID.parse(multihash))
const res = JSON.parse(Buffer.from(result.Data).toString())
deepStrictEqual(res.heads, expectedData.heads)
})
it('throws an error if log items is empty', async () => {
const emptyLog = new Log(ipfs, testIdentity)
let err
try {
await emptyLog.toMultihash()
} catch (e) {
err = e
}
notStrictEqual(err, null)
strictEqual(err.message, 'Can\'t serialize an empty log')
})
})
describe('fromMultihash', async () => {
it('creates a log from ipfs CID - one entry', async () => {
const expectedData = {
id: 'X',
heads: ['zdpuB23XC5xJBJbfk5d9EfpWjX56VqTu3z4CUzVf2mxVURnEy']
}
const log = new Log(ipfs, testIdentity, { logId: 'X' })
await log.append('one')
const hash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash, -1)
strictEqual(JSON.stringify(res.toJSON()), JSON.stringify(expectedData))
strictEqual(res.length, 1)
strictEqual(res.values[0].payload, 'one')
strictEqual(res.values[0].clock.id, testIdentity.publicKey)
strictEqual(res.values[0].clock.time, 1)
})
it('creates a log from ipfs CID - three entries', async () => {
const hash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash, -1)
strictEqual(res.length, 3)
strictEqual(res.values[0].payload, 'one')
strictEqual(res.values[0].clock.time, 1)
strictEqual(res.values[1].payload, 'two')
strictEqual(res.values[1].clock.time, 2)
strictEqual(res.values[2].payload, 'three')
strictEqual(res.values[2].clock.time, 3)
})
it('creates a log from ipfs multihash (backwards compat)', async () => {
const expectedData = {
id: 'X',
heads: ['zdpuB23XC5xJBJbfk5d9EfpWjX56VqTu3z4CUzVf2mxVURnEy']
}
const log = new Log(ipfs, testIdentity, { logId: 'X' })
await log.append('one')
const multihash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, multihash, { length: -1 })
strictEqual(JSON.stringify(res.toJSON()), JSON.stringify(expectedData))
strictEqual(res.length, 1)
strictEqual(res.values[0].payload, 'one')
strictEqual(res.values[0].clock.id, testIdentity.publicKey)
strictEqual(res.values[0].clock.time, 1)
})
it('has the right sequence number after creation and appending', async () => {
const hash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash, { length: -1 })
strictEqual(res.length, 3)
await res.append('four')
strictEqual(res.length, 4)
strictEqual(res.values[3].payload, 'four')
strictEqual(res.values[3].clock.time, 4)
})
it('creates a log from ipfs CID that has three heads', async () => {
const log1 = new Log(ipfs, testIdentity, { logId: 'A' })
const log2 = new Log(ipfs, testIdentity2, { logId: 'A' })
const log3 = new Log(ipfs, testIdentity3, { logId: 'A' })
await log1.append('one') // order is determined by the identity's publicKey
await log2.append('two')
await log3.append('three')
await log1.join(log2)
await log1.join(log3)
const hash = await log1.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash, { length: -1 })
strictEqual(res.length, 3)
strictEqual(res.heads.length, 3)
strictEqual(res.heads[2].payload, 'three')
strictEqual(res.heads[1].payload, 'two') // order is determined by the identity's publicKey
strictEqual(res.heads[0].payload, 'one')
})
it('creates a log from ipfs CID that has three heads w/ custom tiebreaker', async () => {
const log1 = new Log(ipfs, testIdentity, { logId: 'A' })
const log2 = new Log(ipfs, testIdentity2, { logId: 'A' })
const log3 = new Log(ipfs, testIdentity3, { logId: 'A' })
await log1.append('one') // order is determined by the identity's publicKey
await log2.append('two')
await log3.append('three')
await log1.join(log2)
await log1.join(log3)
const hash = await log1.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash,
{ sortFn: FirstWriteWins })
strictEqual(res.length, 3)
strictEqual(res.heads.length, 3)
strictEqual(res.heads[2].payload, 'one')
strictEqual(res.heads[1].payload, 'two') // order is determined by the identity's publicKey
strictEqual(res.heads[0].payload, 'three')
})
it('creates a log from ipfs CID up to a size limit', async () => {
const amount = 100
const size = amount / 2
const log = new Log(ipfs, testIdentity, { logId: 'A' })
for (let i = 0; i < amount; i++) {
await log.append(i.toString())
}
const hash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash, { length: size })
strictEqual(res.length, size)
})
it('creates a log from ipfs CID up without size limit', async () => {
const amount = 100
const log = new Log(ipfs, testIdentity, { logId: 'A' })
for (let i = 0; i < amount; i++) {
await log.append(i.toString())
}
const hash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, hash, { length: -1 })
strictEqual(res.length, amount)
})
it('throws an error if ipfs is not defined', async () => {
let err
try {
await fromMultihash()
} catch (e) {
err = e
}
notStrictEqual(err, null)
strictEqual(err.message, 'IPFS instance not defined')
})
it('throws an error if hash is not defined', async () => {
let err
try {
await fromMultihash(ipfs)
} catch (e) {
err = e
}
notStrictEqual(err, null)
strictEqual(err.message, 'Invalid hash: undefined')
})
it('throws an error if data from hash is not valid JSON', async () => {
const value = 'hello'
const cid = CID.parse(await write(ipfs, 'dag-pb', value))
let err
try {
const hash = cid.toString(base58btc)
await fromMultihash(ipfs, testIdentity, hash)
} catch (e) {
err = e
}
strictEqual(err.message, 'Unexpected token h in JSON at position 0')
})
it('throws an error when data from CID is not instance of Log', async () => {
const hash = await write(ipfs, 'dag-cbor', {})
let err
try {
await fromMultihash(ipfs, testIdentity, hash)
} catch (e) {
err = e
}
strictEqual(err.message, 'Given argument is not an instance of Log')
})
it('onProgress callback is fired for each entry', async () => {
const amount = 100
const log = new Log(ipfs, testIdentity, { logId: 'A' })
for (let i = 0; i < amount; i++) {
await log.append(i.toString())
}
const items = log.values
let i = 0
const loadProgressCallback = (entry) => {
notStrictEqual(entry, null)
strictEqual(entry.hash, items[items.length - i - 1].hash)
strictEqual(entry.payload, items[items.length - i - 1].payload)
i++
}
const hash = await log.toMultihash()
const result = await fromMultihash(ipfs, testIdentity, hash,
{ length: -1, exclude: [], onProgressCallback: loadProgressCallback })
// Make sure the onProgress callback was called for each entry
strictEqual(i, amount)
// Make sure the log entries are correct ones
strictEqual(result.values[0].clock.time, 1)
strictEqual(result.values[0].payload, '0')
strictEqual(result.values[result.length - 1].clock.time, 100)
strictEqual(result.values[result.length - 1].payload, '99')
})
})
describe('fromEntryHash', async () => {
afterEach(() => {
if (fromEntryHash.restore) {
fromEntryHash.restore()
}
})
it('calls fromEntryHash', async () => {
const expectedData = {
id: 'X',
heads: ['zdpuB23XC5xJBJbfk5d9EfpWjX56VqTu3z4CUzVf2mxVURnEy']
}
const log = new Log(ipfs, testIdentity, { logId: 'X' })
await log.append('one')
const res = await fromEntryHash(ipfs, testIdentity, expectedData.heads[0],
{ logId: log.id, length: -1 })
strictEqual(JSON.stringify(res.toJSON()), JSON.stringify(expectedData))
})
})
describe('fromMultihash', async () => {
afterEach(() => {
if (fromMultihash.restore) {
fromMultihash.restore()
}
})
it('calls fromMultihash', async () => {
const expectedData = {
id: 'X',
heads: ['zdpuB23XC5xJBJbfk5d9EfpWjX56VqTu3z4CUzVf2mxVURnEy']
}
const log = new Log(ipfs, testIdentity, { logId: 'X' })
await log.append('one')
const multihash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, multihash, { length: -1 })
strictEqual(JSON.stringify(res.toJSON()), JSON.stringify(expectedData))
})
it('calls fromMultihash with custom tiebreaker', async () => {
const expectedData = {
id: 'X',
heads: ['zdpuB23XC5xJBJbfk5d9EfpWjX56VqTu3z4CUzVf2mxVURnEy']
}
const log = new Log(ipfs, testIdentity, { logId: 'X' })
await log.append('one')
const multihash = await log.toMultihash()
const res = await fromMultihash(ipfs, testIdentity, multihash,
{ length: -1, sortFn: FirstWriteWins })
strictEqual(JSON.stringify(res.toJSON()), JSON.stringify(expectedData))
})
})
})
describe('values', () => {
it('returns all entries in the log', async () => {
const log = new Log(ipfs, testIdentity)
strictEqual(log.values instanceof Array, true)
strictEqual(log.length, 0)
await log.append('hello1')
await log.append('hello2')
await log.append('hello3')
strictEqual(log.values instanceof Array, true)
strictEqual(log.length, 3)
strictEqual(log.values[0].payload, 'hello1')
strictEqual(log.values[1].payload, 'hello2')
strictEqual(log.values[2].payload, 'hello3')
})
})
})
})