2020-06-19 13:06:31 +02:00
|
|
|
import { assert } from 'chai'
|
2020-06-19 12:49:40 +02:00
|
|
|
import { KeyCache } from '../src/keycache'
|
|
|
|
import { createPeerIds, createPeerIdsFromFixtures } from './fixtures/peer'
|
2020-08-11 15:14:11 +01:00
|
|
|
import uint8ArrayEquals from 'uint8arrays/equals'
|
2020-01-13 15:38:14 +01:00
|
|
|
|
2020-06-19 12:49:40 +02:00
|
|
|
describe('KeyCache', () => {
|
2020-06-19 13:06:31 +02:00
|
|
|
let peerA
|
2020-01-13 15:38:14 +01:00
|
|
|
|
|
|
|
before(async () => {
|
2020-06-19 13:06:31 +02:00
|
|
|
[peerA] = await createPeerIdsFromFixtures(2)
|
2020-06-19 12:49:40 +02:00
|
|
|
})
|
2020-01-13 15:38:14 +01:00
|
|
|
|
2020-06-19 12:49:40 +02:00
|
|
|
it('should store and load same key successfully', async () => {
|
2020-01-13 15:38:14 +01:00
|
|
|
try {
|
2020-06-19 12:49:40 +02:00
|
|
|
const key = Buffer.from('this is id 007')
|
|
|
|
await KeyCache.store(peerA, key)
|
|
|
|
const result = await KeyCache.load(peerA)
|
2020-08-11 15:14:11 +01:00
|
|
|
assert(uint8ArrayEquals(result, key), 'Stored and loaded key are not the same')
|
2020-01-13 15:38:14 +01:00
|
|
|
} catch (e) {
|
|
|
|
assert(false, `Test failed - ${e.message}`)
|
|
|
|
}
|
2020-06-19 12:49:40 +02:00
|
|
|
})
|
2020-01-13 16:18:10 +01:00
|
|
|
|
2020-06-19 12:49:40 +02:00
|
|
|
it('should return undefined if key not found', async () => {
|
2020-01-13 16:18:10 +01:00
|
|
|
try {
|
2020-06-19 12:49:40 +02:00
|
|
|
const [newPeer] = await createPeerIds(1)
|
|
|
|
const result = await KeyCache.load(newPeer)
|
|
|
|
assert(!result)
|
2020-01-13 16:18:10 +01:00
|
|
|
} catch (e) {
|
|
|
|
assert(false, `Test failed - ${e.message}`)
|
|
|
|
}
|
2020-06-19 12:49:40 +02:00
|
|
|
})
|
|
|
|
})
|