orbitdb/test/oplog/append.test.js
haad a063b3fb4a Refactor OrbitDB
Fix sync
Fix linter
Fix tests
Clean up
Set default references count to 0
Fix sync
Use address instead of databaseId
Sync protocol
Keep references to open databases in OrbitDB
Fix append benchmark
Initial version of heads exchange
Remove Feed
Fix KeyValuePersisted iterator
Refactor OrbitDBAddress a bit more
Add rest of the database types
Refactor OrbitDB addresses
Initial version for the full circle
Initial structure and tests for new OrbitDB
Make sure KeyStore is open when a Database is created
Re-organize OrbitDB
Use new databases and Log
More clean up
Add 'drop' event to Database
Clean up OrbitDB
Remove id from OrbitDB
Use new KeyStore and Identities
Remove storage from OrbitDB
Remove migrations from OrbitDB
Remove caches from OrbitDB
Remove pubsub from OrbitDB
2023-03-01 16:21:07 +02:00

135 lines
3.9 KiB
JavaScript

import { strictEqual, deepStrictEqual } from 'assert'
import rimraf from 'rimraf'
import { copy } from 'fs-extra'
import { Log } from '../../src/oplog/index.js'
import MemoryStorage from '../../src/storage/memory.js'
import LevelStorage from '../../src/storage/level.js'
import { Identities } from '../../src/identities/index.js'
import KeyStore from '../../src/key-store.js'
// Test utils
import { config, testAPIs } from 'orbit-db-test-utils'
const { sync: rmrf } = rimraf
let testIdentity
Object.keys(testAPIs).forEach((IPFS) => {
describe('Log - Append (' + IPFS + ')', function () {
this.timeout(config.timeout)
const { identityKeyFixtures, signingKeyFixtures, identityKeysPath } = config
let keystore
let identities
before(async () => {
rmrf(identityKeysPath)
await copy(identityKeyFixtures, identityKeysPath)
await copy(signingKeyFixtures, identityKeysPath)
keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) })
const storage = await MemoryStorage()
identities = await Identities({ keystore, storage })
testIdentity = await identities.createIdentity({ id: 'userA' })
})
after(async () => {
await keystore.close()
rmrf(identityKeysPath)
})
describe('append', async () => {
describe('append one', async () => {
let log
let values = []
let heads = []
before(async () => {
log = await Log(testIdentity, { logId: 'A' })
await log.append('hello1')
values = await log.values()
heads = await log.heads()
})
it('added the correct amount of items', () => {
strictEqual(values.length, 1)
})
it('added the correct values', async () => {
values.forEach((entry) => {
strictEqual(entry.payload, 'hello1')
})
})
it('added the correct amount of next pointers', async () => {
values.forEach((entry) => {
strictEqual(entry.next.length, 0)
})
})
it('has the correct heads', async () => {
heads.forEach((head) => {
strictEqual(head.hash, values[0].hash)
})
})
it('updated the clocks correctly', async () => {
values.forEach((entry) => {
strictEqual(entry.clock.id, testIdentity.publicKey)
strictEqual(entry.clock.time, 1)
})
})
})
describe('append 100 items to a log', async () => {
const amount = 100
const nextPointerAmount = 64
let log
let values = []
let heads = []
before(async () => {
log = await Log(testIdentity, { logId: 'A' })
for (let i = 0; i < amount; i++) {
await log.append('hello' + i, { pointerCount: nextPointerAmount })
}
values = await log.values()
heads = await log.heads()
})
it('set the correct heads', () => {
strictEqual(heads.length, 1)
deepStrictEqual(heads[0], values[values.length - 1])
})
it('added the correct amount of items', () => {
strictEqual(values.length, amount)
})
it('added the correct values', async () => {
values.forEach((entry, index) => {
strictEqual(entry.payload, 'hello' + index)
})
})
it('updated the clocks correctly', async () => {
values.forEach((entry, index) => {
strictEqual(entry.clock.time, index + 1)
strictEqual(entry.clock.id, testIdentity.publicKey)
})
})
it('added the correct amount of refs pointers', async () => {
values.forEach((entry, index) => {
strictEqual(entry.refs.length, index > 0 ? Math.floor(Math.log2(Math.min(nextPointerAmount, index))) : 0)
})
})
})
})
})
})