orbitdb/test/storage.spec.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
4.4 KiB
JavaScript

import * as IPFS from 'ipfs'
import { strictEqual, notStrictEqual } from 'assert'
import rimraf from 'rimraf'
import { Log } from '../src/oplog/index.js'
import { Identities } from '../src/identities/index.js'
import KeyStore from '../src/key-store.js'
import { IPFSBlockStorage, MemoryStorage, LRUStorage, ComposedStorage } from '../src/storage/index.js'
import { copy } from 'fs-extra'
import { config, testAPIs } from 'orbit-db-test-utils'
import testKeysPath from './fixtures/test-keys-path.js '
const { sync: rmrf } = rimraf
Object.keys(testAPIs).forEach((_) => {
describe('Storages (' + _ + ')', function () {
this.timeout(config.timeout)
const { identityKeyFixtures, signingKeyFixtures, identityKeysPath } = config
let ipfs1
let keystore
let testIdentity1
before(async () => {
rmrf(identityKeysPath)
await copy(identityKeyFixtures, identityKeysPath)
await copy(signingKeyFixtures, identityKeysPath)
rmrf('./ipfs1')
await copy('./test/fixtures/ipfs1', './ipfs1')
// Start an IPFS instance
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
keystore = await KeyStore({ path: testKeysPath })
const storage = await MemoryStorage()
const identities = await Identities({ keystore, storage })
testIdentity1 = await identities.createIdentity({ id: 'userA' })
})
after(async () => {
if (ipfs1) {
await ipfs1.stop()
}
if (keystore) {
await keystore.close()
}
rmrf('./ipfs1')
rmrf('./orbitdb')
})
const runTestWithStorage = async (storage) => {
const amount = 100
const log1 = await Log(testIdentity1, { logId: 'A', storage })
const log2 = await Log(testIdentity1, { logId: 'A', storage })
for (let i = 0; i < amount; i++) {
await log1.append('hello' + i)
await log2.append('hello' + i)
}
// await log2.join(log1)
const values = await log1.values()
const heads = await log1.heads()
strictEqual(heads.length, 1)
strictEqual(values.length, amount)
await log1.storage.clear()
await log2.storage.clear()
// const values2 = await log2.values()
// const heads2 = await log2.heads()
// strictEqual(heads2.length, 0)
// strictEqual(values2.length, 0)
await log1.storage.close()
await log2.storage.close()
}
describe('LRUStorage', () => {
it('tests the storage', async () => {
const storage = await LRUStorage()
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
})
describe('MemoryStorage', () => {
it('tests the storage', async () => {
const storage = await MemoryStorage()
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
})
describe('IPFSBlockStorage', () => {
it('tests the storage', async () => {
const storage = await IPFSBlockStorage({ ipfs: ipfs1 })
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
})
describe('Composed Storages', () => {
it('tests Memory + IPFSBlockStorage composition', async () => {
const storage1 = await MemoryStorage()
const storage2 = await IPFSBlockStorage({ ipfs: ipfs1 })
const storage = await ComposedStorage(storage1, storage2)
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
it('tests LRU + IPFSBlockStorage composition', async () => {
const storage1 = await LRUStorage({ size: -1 })
const storage2 = await IPFSBlockStorage({ ipfs: ipfs1 })
const storage = await ComposedStorage(storage1, storage2)
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
it('tests Memory + LRU composition', async () => {
const storage1 = await MemoryStorage()
const storage2 = await LRUStorage({ size: -1 })
const storage = await ComposedStorage(storage1, storage2)
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
it('tests LRU + Memory composition', async () => {
const storage1 = await LRUStorage({ size: -1 })
const storage2 = await MemoryStorage()
const storage = await ComposedStorage(storage1, storage2)
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
})
})
})