This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathget-value.spec.js
106 lines (81 loc) · 2.8 KB
/
get-value.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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { Buffer } = require('buffer')
const Message = require('../../../src/message')
const handler = require('../../../src/rpc/handlers/get-value')
const utils = require('../../../src/utils')
const T = Message.TYPES.GET_VALUE
const createPeerId = require('../../utils/create-peer-id')
const TestDHT = require('../../utils/test-dht')
describe('rpc - handlers - GetValue', () => {
let peerIds
let tdht
let dht
before(async () => {
peerIds = await createPeerId(2)
})
beforeEach(async () => {
tdht = new TestDHT()
const dhts = await tdht.spawn(1)
dht = dhts[0]
})
afterEach(() => tdht.teardown())
it('errors when missing key', async () => {
const msg = new Message(T, Buffer.alloc(0), 0)
try {
await handler(dht)(peerIds[0], msg)
} catch (err) {
expect(err.code).to.eql('ERR_INVALID_KEY')
return
}
throw new Error('should error when missing key')
})
it('responds with a local value', async () => {
const key = Buffer.from('hello')
const value = Buffer.from('world')
const msg = new Message(T, key, 0)
await dht.put(key, value)
const response = await handler(dht)(peerIds[0], msg)
expect(response.record).to.exist()
expect(response.record.key).to.eql(key)
expect(response.record.value).to.eql(value)
})
it('responds with closerPeers returned from the dht', async () => {
const key = Buffer.from('hello')
const msg = new Message(T, key, 0)
const other = peerIds[1]
await dht._add(other)
const response = await handler(dht)(peerIds[0], msg)
expect(response.closerPeers).to.have.length(1)
expect(response.closerPeers[0].id.toB58String()).to.be.eql(other.toB58String())
})
describe('public key', () => {
it('self', async () => {
const key = utils.keyForPublicKey(dht.peerId)
const msg = new Message(T, key, 0)
const response = await handler(dht)(peerIds[0], msg)
expect(response.record).to.exist()
expect(response.record.value).to.eql(dht.peerId.pubKey.bytes)
})
it('other in peerstore', async () => {
const other = peerIds[1]
const key = utils.keyForPublicKey(other)
const msg = new Message(T, key, 0)
dht.peerStore.addressBook.add(other, [])
await dht._add(other)
const response = await handler(dht)(peerIds[0], msg)
expect(response.record).to.exist()
expect(response.record.value).to.eql(other.pubKey.bytes)
})
it('other unkown', async () => {
const other = peerIds[1]
const key = utils.keyForPublicKey(other)
const msg = new Message(T, key, 0)
const response = await handler(dht)(peerIds[0], msg)
expect(response.record).to.not.exist()
})
})
})