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-append.spec.js
136 lines (111 loc) · 4.02 KB
/
log-append.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
'use strict'
const assert = require('assert')
const rmrf = require('rimraf')
const fs = require('fs-extra')
const Log = require('../src/log')
const IdentityProvider = require('orbit-db-identity-provider')
const Keystore = require('orbit-db-keystore')
// Test utils
const {
config,
testAPIs,
startIpfs,
stopIpfs
} = require('orbit-db-test-utils')
let ipfs, testIdentity
Object.keys(testAPIs).forEach((IPFS) => {
describe('Log - Append (' + IPFS + ')', function () {
this.timeout(config.timeout)
const { identityKeyFixtures, signingKeyFixtures, identityKeysPath, signingKeysPath } = config
const ipfsConfig = Object.assign({}, config.defaultIpfsConfig, {
repo: config.defaultIpfsConfig.repo + '-log-append' + new Date().getTime()
})
let keystore, signingKeystore
before(async () => {
rmrf.sync(ipfsConfig.repo)
rmrf.sync(identityKeysPath)
rmrf.sync(signingKeysPath)
await fs.copy(identityKeyFixtures, identityKeysPath)
await fs.copy(signingKeyFixtures, signingKeysPath)
keystore = new Keystore(identityKeysPath)
signingKeystore = new Keystore(signingKeysPath)
testIdentity = await IdentityProvider.createIdentity({ id: 'userA', keystore, signingKeystore })
ipfs = await startIpfs(IPFS, ipfsConfig)
})
after(async () => {
await stopIpfs(ipfs)
rmrf.sync(ipfsConfig.repo)
rmrf.sync(identityKeysPath)
rmrf.sync(signingKeysPath)
await keystore.close()
await signingKeystore.close()
})
describe('append', () => {
describe('append one', async () => {
let log
before(async () => {
log = new Log(ipfs, testIdentity, 'A')
await log.append('hello1')
})
it('added the correct amount of items', () => {
assert.strictEqual(log.length, 1)
})
it('added the correct values', async () => {
log.values.forEach((entry) => {
assert.strictEqual(entry.payload, 'hello1')
})
})
it('added the correct amount of next pointers', async () => {
log.values.forEach((entry) => {
assert.strictEqual(entry.next.length, 0)
})
})
it('has the correct heads', async () => {
log.heads.forEach((head) => {
assert.strictEqual(head.hash, log.values[0].hash)
})
})
it('updated the clocks correctly', async () => {
log.values.forEach((entry) => {
assert.strictEqual(entry.clock.id, testIdentity.publicKey)
assert.strictEqual(entry.clock.time, 1)
})
})
})
describe('append 100 items to a log', async () => {
const amount = 100
const nextPointerAmount = 64
let log
before(async () => {
log = new Log(ipfs, testIdentity, 'A')
for (let i = 0; i < amount; i++) {
await log.append('hello' + i, nextPointerAmount)
// Make sure the log has the right heads after each append
const values = log.values
assert.strictEqual(log.heads.length, 1)
assert.strictEqual(log.heads[0].hash, values[values.length - 1].hash)
}
})
it('added the correct amount of items', () => {
assert.strictEqual(log.length, amount)
})
it('added the correct values', async () => {
log.values.forEach((entry, index) => {
assert.strictEqual(entry.payload, 'hello' + index)
})
})
it('updated the clocks correctly', async () => {
log.values.forEach((entry, index) => {
assert.strictEqual(entry.clock.time, index + 1)
assert.strictEqual(entry.clock.id, testIdentity.publicKey)
})
})
it('added the correct amount of refs pointers', async () => {
log.values.forEach((entry, index) => {
assert.strictEqual(entry.refs.length, index > 0 ? Math.ceil(Math.log2(Math.min(nextPointerAmount, index))) : 0)
})
})
})
})
})
})